You are here

function location_save_locations in Location 7.4

Same name and namespace in other branches
  1. 5.3 location.module \location_save_locations()
  2. 6.3 location.module \location_save_locations()
  3. 7.5 location.module \location_save_locations()
  4. 7.3 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')

8 calls to location_save_locations()
location_addanother_form_submit in contrib/location_addanother/location_addanother.module
Submission function for "add another location" form.
location_cck_field in contrib/location_cck/location_cck.module
Implementation of hook_field(). @@@ GONE IN D7, move to hooks above...
location_node_node_delete in ./location_node.module
Implementation of hook_node_delete().
location_node_node_delete_revision in ./location_node.module
Implementation of hook_node_delete_revision().
location_node_node_insert in ./location_node.module
Implementation of hook_node_insert().

... See full list

File

./location.module, line 883
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);
    }

    // Find affected lids.
    $query = db_select('location_instance', 'l');
    $lid_field = $query
      ->addField('l', 'lid');
    foreach ($criteria as $key => $value) {
      $query
        ->condition($key, $value);
    }
    $oldlids = $query
      ->execute()
      ->fetchCol();

    // Delete current set of instances.
    $query = db_delete('location_instance');
    foreach ($criteria as $key => $value) {
      $query
        ->condition($key, $value);
    }
    $query
      ->execute();
    $newlids = array();
    $query = db_insert('location_instance')
      ->fields(array(
      'nid',
      'vid',
      'uid',
      'genid',
      'lid',
    ));
    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) {
        $newlids[] = $location['lid'];
        $instance = array(
          'nid' => 0,
          'vid' => 0,
          'uid' => 0,
          'genid' => '',
          'lid' => $location['lid'],
        );
        foreach ($criteria as $k => $v) {
          $instance[$k] = $v;
        }
        $query
          ->values($instance);
      }
    }
    $query
      ->execute();

    // 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_query('SELECT COUNT(*) FROM {location_instance} WHERE lid = :lid', array(
        ':lid' => $check,
      ))
        ->fetchField();
      if ($count !== FALSE && $count == 0) {
        watchdog('location', 'Deleting unreferenced location with LID %lid.', array(
          '%lid' => $check,
        ));
        $location = array(
          'lid' => $check,
        );
        location_invoke_locationapi($location, 'delete');
        db_delete('location')
          ->condition('lid', $location['lid'])
          ->execute();
      }
    }
  }
}