You are here

function location_save_locations in Location 7.3

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.4 location.module \location_save_locations()

Save associated locations.

Parameters

array $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.

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

14 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_delete in contrib/location_cck/location_cck.module
Implement hook_field_delete().
location_cck_field_delete_revision in contrib/location_cck/location_cck.module
Implement hook_field_delete_revision().
location_cck_field_insert in contrib/location_cck/location_cck.module
Implement hook_field_insert().
location_cck_field_update in contrib/location_cck/location_cck.module
Implement hook_field_update().

... See full list

File

./location.module, line 1161
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();
    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 $key => $value) {
          $instance[$key] = $value;
        }
        db_insert('location_instance')
          ->fields($instance)
          ->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();
      }
    }
  }
}