You are here

function location_extra_form_submit in Location 5

File

./location.module, line 1192

Code

function location_extra_form_submit($form_id, &$form_values) {

  // handy to have these field names later
  $location_fields = array();
  $required_fields = array();
  foreach (array_keys(location_field_names()) as $field_name) {
    $workflow_setting = variable_get('location_' . $field_name . '_' . $node->type, $field_name == 'country' ? 1 : 0);
    if ($workflow_setting) {
      $location_fields[] = $field_name;
      if ($workflow_setting == 2) {
        $required_fields[] = $field_name;
      }
    }
  }
  $form_values['location'] = location_form2api($form_values['location']);
  if (user_access('submit latitude/longitude')) {
    $form_values['location']['latitude'] = trim($form_values['location']['latitude']);
    $form_values['location']['longitude'] = trim($form_values['location']['longitude']);

    // At this point, we know that the user has permission to submit lat/lons and that the
    // submitted lat/lons are either blank or valid numbers.  Now, we need to find out determine
    // the source of these lat/lons since they can either be prefilled from postalcode data
    // or manually entered by the user.
    if (!empty($form_values['location']['latitude']) && !empty($form_values['location']['longitude'])) {
      if (($data = location_latlon_exact($form_values['location'])) && _location_floats_are_equal(floatval($form_values['location']['latitude']), floatval($data['lat'])) && _location_floats_are_equal(floatval($form_values['location']['longitude']), floatval($data['lon']))) {
        $form_values['location']['lat'] = $form_values['location']['latitude'];
        $form_values['location']['lon'] = $form_values['location']['longitude'];
        $form_values['location']['source'] = LOCATION_LATLON_GEOCODED_EXACT;
      }
      elseif (($data = location_get_postalcode_data($form_values['location'])) && _location_floats_are_equal(floatval($form_values['location']['latitude']), floatval($data['lat'])) && _location_floats_are_equal(floatval($form_values['location']['longitude']), floatval($data['lon']))) {
        $form_values['location']['lat'] = $form_values['location']['latitude'];
        $form_values['location']['lon'] = $form_values['location']['longitude'];
        $form_values['location']['source'] = LOCATION_LATLON_GEOCODED_APPROX;
      }
      else {
        $form_values['location']['lat'] = $form_values['location']['latitude'];
        $form_values['location']['lon'] = $form_values['location']['longitude'];
        $form_values['location']['source'] = LOCATION_LATLON_USER_SUBMITTED;
      }
    }
    else {
      if ($data = location_latlon_exact($form_values['location'])) {
        $form_values['location']['latitude'] = $data['lat'];
        $form_values['location']['longitude'] = $data['lon'];
        $form_values['location']['lat'] = $data['lat'];
        $form_values['location']['lon'] = $data['lon'];
        $form_values['location']['source'] = LOCATION_LATLON_GEOCODED_EXACT;
      }
      elseif ($data = location_get_postalcode_data($form_values['location'])) {
        $form_values['location']['latitude'] = $data['lat'];
        $form_values['location']['longitude'] = $data['lon'];
        $form_values['location']['lat'] = $data['lat'];
        $form_values['location']['lon'] = $data['lon'];
        $form_values['location']['source'] = LOCATION_LATLON_GEOCODED_APPROX;
      }
      else {
        unset($form_values['location']['latitude']);
        unset($form_values['location']['longitude']);
        unset($form_values['location']['lat']);
        unset($form_values['location']['lon']);
        $form_values['location']['source'] = LOCATION_LATLON_UNDEFINED;
      }
    }
  }

  // If no city and/or province were supplied in the address (whether the user left it blank or whether the user did not have the option
  // of entering them) then we want to use the data we have based strictly on the postal code.
  if ($data = location_get_postalcode_data($form_values['location'])) {
    $form_values['location']['city'] = !isset($form_values['location']['city']) || strlen($form_values['location']['city']) == 0 ? $data['city'] : $form_values['location']['city'];
    $form_values['location']['province'] = !isset($form_values['location']['province']) || strlen($form_values['location']['province']) == 0 ? $data['province'] : $form_values['location']['province'];
  }
  $node = node_load($form_values['location']['nid']);
  _location_save($form_values['location'] ? $form_values['location'] : array(), $node, 'node');

  //======================================================================
  drupal_set_message(t('Your new location has been added. (See below)'));
  return 'node/' . $form_values['location']['nid'];
}