You are here

function getlocations_fields_save_locations in Get Locations 7

Same name and namespace in other branches
  1. 7.2 modules/getlocations_fields/getlocations_fields.module \getlocations_fields_save_locations()

Parameters

array $locations: An array of locations to be saved

array $criteria: Relationship information

string $mode: The action to be carried out

Return value

array $locations

4 calls to getlocations_fields_save_locations()
getlocations_fields_field_delete in modules/getlocations_fields/getlocations_fields.module
Implements hook_field_delete(). Define custom delete behavior for this module's field types.
getlocations_fields_field_delete_revision in modules/getlocations_fields/getlocations_fields.module
Implements hook_field_delete_revision(). Define custom revision delete behavior for this module's field types.
getlocations_fields_field_insert in modules/getlocations_fields/getlocations_fields.module
Implements hook_field_insert(). Define custom insert behavior for this module's field types.
getlocations_fields_field_update in modules/getlocations_fields/getlocations_fields.module
Implements hook_field_update(). Define custom update behavior for this module's field types.

File

modules/getlocations_fields/getlocations_fields.module, line 1974
getlocations_fields.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_fields_save_locations($locations, $criteria, $settings, $mode) {
  if (isset($locations) && is_array($locations) && !empty($criteria) && is_array($criteria)) {

    // deal with $settings
    if ($settings && isset($settings['map_settings_allow'])) {
      $map_settings_allow = $settings['map_settings_allow'];
    }
    else {
      $map_settings_allow = getlocations_fields_map_settings_allow();
    }
    if ($settings && isset($settings['streetview_settings_allow'])) {
      $streetview_settings_allow = $settings['streetview_settings_allow'];
    }
    else {
      $streetview_settings_allow = getlocations_fields_streetview_settings_allow();
    }
    foreach (array_keys($locations) as $key) {

      // remove empties
      if (empty($locations[$key]['latitude']) && empty($locations[$key]['longitude'])) {
        unset($locations[$key]);
        continue;
      }
      else {

        // normalize
        $locations[$key]['latitude'] = getlocations_normalizelat($locations[$key]['latitude']);
        $locations[$key]['longitude'] = getlocations_normalizelng($locations[$key]['longitude']);
      }

      // dealing with imports
      if (!isset($locations[$key]['name'])) {
        $locations[$key]['name'] = '';
      }
      if (!isset($locations[$key]['street'])) {
        $locations[$key]['street'] = '';
      }
      if (!isset($locations[$key]['additional'])) {
        $locations[$key]['additional'] = '';
      }
      if (!isset($locations[$key]['city'])) {
        $locations[$key]['city'] = '';
      }
      if (!isset($locations[$key]['province'])) {
        $locations[$key]['province'] = '';
      }
      if (!isset($locations[$key]['postal_code'])) {
        $locations[$key]['postal_code'] = '';
      }
      if (!isset($locations[$key]['country'])) {
        $locations[$key]['country'] = '';
      }
      if (!isset($locations[$key]['marker'])) {
        $locations[$key]['marker'] = '';
      }
      if (!isset($locations[$key]['data'])) {
        $locations[$key]['data'] = '';
      }
      $data = array();

      // set defaults
      $dvals = getlocations_fields_data_keys('d');
      foreach ($dvals as $k => $v) {
        $data['data'][$k] = $v;
      }
      if (isset($locations[$key]['phone'])) {
        $locations[$key]['phone'] = trim($locations[$key]['phone']);
        $data['data']['phone'] = check_plain($locations[$key]['phone']);
      }
      if (isset($locations[$key]['mobile'])) {
        $locations[$key]['mobile'] = trim($locations[$key]['mobile']);
        $data['data']['mobile'] = check_plain($locations[$key]['mobile']);
      }
      if (isset($locations[$key]['fax'])) {
        $locations[$key]['fax'] = trim($locations[$key]['fax']);
        $data['data']['fax'] = check_plain($locations[$key]['fax']);
      }

      // streetview_settings
      $data['data']['streetview_settings_allow'] = $streetview_settings_allow;
      if ($streetview_settings_allow) {
        if (isset($locations[$key]['sv_heading']) && is_numeric($locations[$key]['sv_heading'])) {
          $ph = trim($locations[$key]['sv_heading']);

          // sanity check
          while ($ph < 0) {
            $ph = $ph + 360;
          }
          while ($ph > 360) {
            $ph = $ph - 360;
          }
          $data['data']['sv_heading'] = intval($ph);
        }
        if (isset($locations[$key]['sv_zoom']) && is_numeric($locations[$key]['sv_zoom'])) {
          $locations[$key]['sv_zoom'] = trim($locations[$key]['sv_zoom']);

          // sanity check
          if ($locations[$key]['sv_zoom'] > 5) {
            $locations[$key]['sv_zoom'] = 5;
          }
          if ($locations[$key]['sv_zoom'] < 0) {
            $locations[$key]['sv_zoom'] = 0;
          }
          $data['data']['sv_zoom'] = intval($locations[$key]['sv_zoom']);
        }
        if (isset($locations[$key]['sv_pitch']) && is_numeric($locations[$key]['sv_pitch'])) {
          $locations[$key]['sv_pitch'] = trim($locations[$key]['sv_pitch']);

          // sanity check
          $locations[$key]['sv_pitch'] = getlocations_normalizelat($locations[$key]['sv_pitch']);
          $data['data']['sv_pitch'] = intval($locations[$key]['sv_pitch']);
        }
        if (isset($locations[$key]['sv_showfirst']) && is_numeric($locations[$key]['sv_showfirst'])) {
          $data['data']['sv_showfirst'] = intval($locations[$key]['sv_showfirst']);
        }
        if (isset($locations[$key]['sv_enable']) && is_numeric($locations[$key]['sv_enable'])) {
          $data['data']['sv_enable'] = intval($locations[$key]['sv_enable']);
        }

        // display_settings
        if (isset($settings['display_settings'])) {
          foreach ($settings['display_settings'] as $k => $v) {
            $data['data'][$k] = $v;
          }
        }
      }

      // map_settings
      $data['data']['map_settings_allow'] = $map_settings_allow;
      if ($map_settings_allow) {
        if (isset($locations[$key]['mapzoom']) && is_numeric($locations[$key]['mapzoom'])) {
          $locations[$key]['mapzoom'] = trim($locations[$key]['mapzoom']);

          // sanity check
          if ($locations[$key]['mapzoom'] > 21) {
            $locations[$key]['mapzoom'] = 21;
          }
          if ($locations[$key]['mapzoom'] < 0) {
            $locations[$key]['mapzoom'] = 0;
          }
          $data['data']['mapzoom'] = intval($locations[$key]['mapzoom']);
        }
        if (isset($locations[$key]['map_maptype'])) {
          $data['data']['map_maptype'] = check_plain($locations[$key]['map_maptype']);
        }
      }

      // what3words
      $what3words_lic = variable_get('getlocations_what3words_lic', array(
        'key' => '',
        'url' => 'http://api.what3words.com',
      ));
      if ($what3words_lic['key'] && $settings['what3words_collect']) {
        $ll = $locations[$key]['latitude'] . ',' . $locations[$key]['longitude'];
        $ll = getlocations_latlon_check($ll);
        if ($ll) {
          $jd = json_decode(getlocations_cb_w3w_get($ll, 'position'));
          $locations[$key]['what3words'] = implode('.', $jd->words);
          $data['data']['what3words'] = $locations[$key]['what3words'];
        }
      }
      if (count($data)) {
        $locations[$key]['data'] = $data;
      }
      if ($mode == 'insert') {
        if ($locations[$key]['latitude'] && $locations[$key]['longitude']) {
          $result = getlocations_fields_insert_record($locations[$key], $criteria);
          if ($result) {
            $locations[$key]['glid'] = $result;
          }
          else {
            unset($locations[$key]);
            continue;
          }
        }
        else {
          unset($locations[$key]);
          continue;
        }
      }
      elseif ($mode == 'update') {
        if (isset($locations[$key]['active']) && $locations[$key]['active']) {

          // check for elusive glid in feed imports
          if (!isset($locations[$key]['glid']) || !is_numeric($locations[$key]['glid'])) {

            // eek
            $loc = getlocations_fields_load_locations($criteria['nid'], 'nid');
            $locations[$key]['glid'] = $loc[$key]['glid'];
          }
          $result = getlocations_fields_update_record($locations[$key], $criteria);
          if ($result && !isset($locations[$key]['glid'])) {
            $locations[$key]['glid'] = $result;
          }
        }
      }
      elseif ($mode == 'delete') {
        getlocations_fields_delete_record($locations[$key], $criteria);
        unset($locations[$key]);
      }
      elseif ($mode == 'delete_revision') {
        getlocations_fields_delete_revision_record($locations[$key], $criteria);
        unset($locations[$key]);
      }
    }
    return $locations;
  }
}