You are here

function location_generate_form_submit in Location 5.3

File

contrib/location_generate/location_generate.module, line 52
Generate random locations.

Code

function location_generate_form_submit($form_id, $form_values) {
  if ($form_values['nodes']) {
    $maxnid = db_result(db_query('SELECT MAX(nid) FROM {node}'));
    $sources = array(
      LOCATION_LATLON_USER_SUBMITTED,
      LOCATION_LATLON_GEOCODED_APPROX,
      LOCATION_LATLON_GEOCODED_EXACT,
    );
    for ($nid = 1; $nid <= $maxnid; $nid++) {
      if ($node = node_load($nid, NULL, TRUE)) {
        $numlocs = rand(0, (int) variable_get('location_maxnum_' . $node->type, 0));
        $node->locations = array();
        for ($i = 0; $i < $numlocs; $i++) {
          $node->locations[] = array(
            //'name' => '',

            //'street' => '',

            //'additional' => '',

            //'city' => '',

            //'province' => '',

            //'postal_code' => '',

            //'country' => '',
            'latitude' => rand(-60, 60),
            'longitude' => rand(-180, 180),
            'source' => $sources[rand(0, 2)],
          );
        }
        if ($numlocs) {
          $node = node_submit($node);
          node_save($node);
        }
      }
    }
    drupal_set_message(t('Geodata was set for all nodes'));
  }
  if ($form_values['users']) {
    $maxuid = db_result(db_query('SELECT MAX(uid) FROM {users}'));
    $sources = array(
      LOCATION_LATLON_USER_SUBMITTED,
      LOCATION_LATLON_GEOCODED_APPROX,
      LOCATION_LATLON_GEOCODED_EXACT,
    );
    for ($uid = 1; $uid <= $maxuid; $uid++) {
      if (db_result(db_query('SELECT COUNT(*) FROM {users} WHERE uid = %d', $uid))) {
        db_query('DELETE FROM {location_instance} WHERE uid = %d', $uid);
        $location = array(
          //'name' => '',

          //'street' => '',

          //'additional' => '',

          //'city' => '',

          //'province' => '',

          //'postal_code' => '',

          //'country' => '',
          'latitude' => rand(-60, 60),
          'longitude' => rand(-180, 180),
          'source' => $sources[rand(0, 2)],
        );
        $lid = location_save($location, TRUE);
        if ($lid) {
          db_query('INSERT INTO {location_instance} (uid, lid) VALUES (%d, %d)', $uid, $lid);
        }
      }
    }
    drupal_set_message(t('Geodata was set for all users'));
  }
}