You are here

function location_save_locations in Location 5.3

Same name and namespace in other branches
  1. 6.3 location.module \location_save_locations()
  2. 7.5 location.module \location_save_locations()
  3. 7.3 location.module \location_save_locations()
  4. 7.4 location.module \location_save_locations()

Save associated locations.

Parameters

$locations The associated locations.: You can pass an empty array to remove all location references associated with the given criteria. This is useful if you are about to delete an object, and need Location to clean up any locations that are no longer referenced.

$criteria An array of instance criteria to save as.: Example: array('genid' => 'my_custom_1111')

3 calls to location_save_locations()
location_cck_field in contrib/location_cck/location_cck.module
Implementation of hook_field().
location_node_nodeapi in ./location_node.module
Implementation of hook_nodeapi().
location_user_user in ./location_user.module
Implementation of hook_user().

File

./location.module, line 748
Location module main routines. An implementation of a universal API for location manipulation. Provides functions for postal_code proximity searching, deep-linking into online mapping services. Currently, some options are configured through an…

Code

function location_save_locations(&$locations, $criteria) {
  if (isset($locations) && is_array($locations) && !empty($criteria) && is_array($criteria)) {
    foreach (array_keys($locations) as $key) {
      location_save($locations[$key], TRUE, $criteria);
    }
    $columns = array();
    $placeholders = array();
    $qfrags = array();
    $args = array();
    foreach (array(
      'nid' => '%d',
      'vid' => '%d',
      'uid' => '%d',
      'genid' => "'%s'",
    ) as $key => $placeholder) {
      if (isset($criteria[$key])) {
        $columns[] = $key;
        $placeholders[] = $placeholder;
        $args[] = $criteria[$key];
        $qfrags[] = "{$key} = {$placeholder}";
      }
    }
    $querybase = 'FROM {location_instance} WHERE ' . implode(' AND ', $qfrags);
    $oldlids = array();
    $newlids = array();

    // Find affected lids.
    $query = "SELECT lid {$querybase}";
    $result = db_query($query, $args);
    while ($t = db_fetch_object($result)) {
      $oldlids[] = $t->lid;
    }
    $query = "DELETE {$querybase}";
    db_query($query, $args);

    // Tack on the lid.
    $columns[] = 'lid';
    $placeholders[] = '%d';
    foreach ($locations as $location) {

      // Don't save "empty" locations.
      // location_save() explicitly returns FALSE for empty locations,
      // so it should be ok to rely on the data type.
      if ($location['lid'] !== FALSE) {
        $args[] = $location['lid'];
        $newlids[] = $location['lid'];
        db_query('INSERT INTO {location_instance} (' . implode(', ', $columns) . ') VALUES (' . implode(', ', $placeholders) . ')', $args);
        array_pop($args);
      }
    }

    // Check anything that dropped a reference during this operation.
    foreach (array_diff($oldlids, $newlids) as $check) {

      // An instance may have been deleted. Check reference count.
      $count = db_result(db_query('SELECT COUNT(*) FROM {location_instance} WHERE lid = %d', $check));
      if ($count !== FALSE && $count == 0) {
        watchdog('location', t('Deleting unreferenced location with LID %lid.', array(
          '%lid' => $check,
        )));
        $location = array(
          'lid' => $check,
        );
        location_invoke_locationapi($location, 'delete');
        db_query('DELETE FROM {location} WHERE lid = %d', $location['lid']);
      }
    }
  }
}