--- location.module	2009-01-21 15:14:20.000000000 -0800
+++ location.module	2009-02-09 12:43:49.000000000 -0800
@@ -1,5 +1,5 @@
 <?php
-// $Id: location.module,v 1.222.2.15 2009/01/21 23:14:20 bdragon Exp $
+// $Id: location.module,v 1.222 2008/12/10 21:55:02 bdragon Exp $
 
 /**
  * @file
@@ -77,7 +77,7 @@ function location_menu() {
  *
  */
 function location_api_variant() {
-  return 2;
+  return 1;
 }
 
 /**
@@ -123,7 +123,7 @@ function location_elements() {
       '#required' => FALSE,
       '#attributes' => array('class' => 'location'),
       // Element level validation.
-      '#element_validate' => array('location_element_validate'),
+      '#validate' => array('location_element_validate' => array()),
     ),
     'location_settings' => array(
       '#input' => TRUE,
@@ -242,33 +242,25 @@ function _location_expand_location($elem
 
   // $settings -> $settings['form']['fields']
 
-  // $defaults is not necessarily what we want.
-  // If #default_value was already specified, we want to use that, because
-  // otherwise we will lose our values on preview!
-  $fdefaults = $defaults;
-  foreach ($element['#default_value'] as $k => $v) {
-    $fdefaults[$k] = $v;
-  }
-
   $fields = location_field_names();
   foreach ($fields as $field => $title) {
     if (!isset($element[$field])) {
       // @@@ Permission check hook?
       if ($fsettings[$field]['collect'] != 0) {
-        $element[$field] = location_invoke_locationapi($fdefaults[$field], 'field_expand', $field, $fsettings[$field]['collect'], $fdefaults);
+        $element[$field] = location_invoke_locationapi($defaults[$field], 'field_expand', $field, $fsettings[$field]['collect'], $defaults);
         $element[$field]['#weight'] = (int)$fsettings[$field]['weight'];
       }
 
       // Only include 'Street Additional' if 'Street' is 'allowed' or 'required'
-      if ($field == 'street' && $fsettings[$field]['collect']) {
+      if ($field == 'street' && $fsettings[$field]['collect'] && $fsettings['additional']['collect']) {
         $element['additional'] = location_invoke_locationapi($defaults['additional'], 'field_expand', 'additional', 1, $defaults);
         $element['additional']['#weight'] = (int)$fsettings['additional']['weight'];
       }
     }
   }
   // @@@ Split into submit and view permissions?
-  if (user_access('submit latitude/longitude') && $fsettings['locpick']['collect']) {
-    $element['locpick'] = array('#weight' => $fsettings['locpick']['weight']);
+  if (user_access('submit latitude/longitude')) {
+    $element['locpick'] = array();
 
     if (location_has_coordinates($defaults, FALSE)) {
       $element['locpick']['current'] = array(
@@ -370,15 +362,15 @@ function _location_expand_location($elem
         '#suffix' => '</div>',
       );
     }
-  }
 
-  if (isset($defaults['lid']) && !empty($defaults['lid'])) {
-    $element['delete_location'] = array(
-      '#type' => 'checkbox',
-      '#title' => t('Delete'),
-      '#default_value' => isset($fdefaults['delete_location']) ? $fdefaults['delete_location'] : FALSE,
-      '#description' => t('Check this box to delete this location.'),
-    );
+    if (isset($defaults['lid']) && !empty($defaults['lid'])) {
+      $element['delete_location'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Delete'),
+        '#default_value' => FALSE,
+        '#description' => t('Check this box to delete this location.'),
+      );
+    }
   }
 
   $element += _element_info('fieldset');
@@ -542,7 +534,7 @@ function location_field_names($all = FAL
 function location_locationapi(&$obj, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
   switch ($op) {
     case 'fields':
-      return array('name' => t('Location name'), 'street' => t('Street location'), 'additional' => t('Additional'), 'city' => t('City'), 'province' => t('State/Province'), 'postal_code' => t('Postal code'), 'country' => t('Country'), 'locpick' => t('Coordinate Chooser'));
+      return array('name' => t('Location name'), 'street' => t('Street location'), 'additional' => t('Additional'), 'city' => t('City'), 'province' => t('State/Province'), 'postal_code' => t('Postal code'), 'country' => t('Country'));
 
     case 'virtual fields':
       return array('province_name' => t('Province name'), 'country_name' => t('Country name'), 'map_link' => t('Map link'), 'coords' => t('Coordinates'));
@@ -557,12 +549,10 @@ function location_locationapi(&$obj, $op
         'province'    => array('default' => '', 'collect' => 0, 'weight' => 10),
         'postal_code' => array('default' => '', 'collect' => 0, 'weight' => 12),
         'country'     => array('default' => variable_get('location_default_country', 'us'), 'collect' => 1, 'weight' => 14), // @@@ Fix weight?
-        'locpick'     => array('default' => FALSE, 'collect' => 1, 'weight' => 20, 'nodiff' => TRUE),
         'latitude'    => array('default' => 0),
         'longitude'   => array('default' => 0),
         'source'      => array('default' => LOCATION_LATLON_UNDEFINED),
         'is_primary'  => array('default' => 0), // @@@
-        'delete_location' => array('default' => FALSE, 'nodiff' => TRUE),
       );
 
     case 'validate':
@@ -583,15 +573,13 @@ function location_locationapi(&$obj, $op
         }
       }
 
-      if (!empty($obj['locpick']) && is_array($obj['locpick'])) {
-        // Can't specify just latitude or just longitude.
-        if (_location_floats_are_equal($obj['locpick']['user_latitude'], 0) xor _location_floats_are_equal($obj['locpick']['user_longitude'], 0)) {
-          $ref = &$a3['locpick']['user_latitude'];
-          if (_location_floats_are_equal($obj['locpick']['user_longitude'], 0)) {
-            $ref = &$a3['locpick']['user_longitude'];
-          }
-          form_error($ref, t('You must fill out both latitude and longitude or you must leave them both blank.'));
+      // Can't specify just latitude or just longitude.
+      if (_location_floats_are_equal($obj['locpick']['user_latitude'], 0) xor _location_floats_are_equal($obj['locpick']['user_longitude'], 0)) {
+        $ref = &$a3['locpick']['user_latitude'];
+        if (_location_floats_are_equal($obj['locpick']['user_longitude'], 0)) {
+          $ref = &$a3['locpick']['user_longitude'];
         }
+        form_error($ref, t('You must fill out both latitude and longitude or you must leave them both blank.'));
       }
 
       break;
@@ -664,7 +652,7 @@ function location_locationapi(&$obj, $op
           if ($a4 == 4) {
             return array(
               '#type' => 'value',
-              '#value' => $obj,
+              '#value' => variable_get('location_default_country', 'us'),
             );
           }
           else {
@@ -721,8 +709,6 @@ function location_locationapi(&$obj, $op
         case 'country_name':
         case 'map_link':
         case 'coords':
-        case 'locpick':
-        case 'delete_location':
           // Always considered unchanged.
           return TRUE;
       }
@@ -876,10 +862,8 @@ function location_node_settings_validate
   if (!empty($form_state['values']['location_settings']['multiple']['max']) && empty($form_state['values']['location_settings']['multiple']['add'])) {
     form_error($form['location_settings']['multiple']['add'], t("You must have at least one empty location form enabled if you are going to allow locations to be submitted for nodes of this content type. If you don't intend to allow locations to be submitted for nodes of this content type, set the maximum number of locations allowed for this content type to 0."));
   }
-  elseif ($form_state['values']['location_settings']['multiple']['max'] > 0) {
-    if ($form_state['values']['location_settings']['multiple']['add'] > $form_state['values']['location_settings']['multiple']['max']) {
-      form_error($form['location_settings']['multiple']['add'], t("You can't show more empty location forms than the maximum number of locations allowed for this content type."));
-    }
+  elseif ($form_state['values']['location_settings']['multiple']['add'] > $form_state['values']['location_settings']['multiple']['max']) {
+    form_error($form['location_settings']['multiple']['add'], t("You can't show more empty location forms than the maximum number of locations allowed for this content type."));
   }
 }
 
@@ -1288,7 +1272,7 @@ function location_save(&$location, $cow 
   }
 
   $changed = array();
-  if (!location_calc_difference($oldloc, $location, $changed)) {
+  if (!location_diff($oldloc, $location, $changed)) {
     // We didn't actually need to save anything.
     if (!empty($location['lid'])) {
       return $location['lid'];
@@ -1323,7 +1307,7 @@ function location_save(&$location, $cow 
         }
         $querybase = 'FROM {location_instance} WHERE '. implode(' AND ', $qfrags);
 
-        $associated = db_result(db_query("SELECT COUNT(*) $querybase", $args));
+        $associated = db_result(db_query("SELECT COUNT(*) $querybase"));
         $all = db_result(db_query("SELECT COUNT(*) FROM {location_instance} WHERE lid = %d", $location['lid']));
         if ($associated != $all) {
           // If there were a different number of instances than instances matching the criteria,
@@ -1339,13 +1323,12 @@ function location_save(&$location, $cow 
   }
 
   if (!empty($location['lid'])) {
-    watchdog('location', 'Conserving lid %lid due to uniqueness.', array('%lid' => $location['lid']));
-    drupal_write_record('location', $location, array('lid'));
+    db_query('DELETE FROM {location} WHERE lid = %d', $location['lid']);
   }
   else {
     unset($location['lid']);
-    drupal_write_record('location', $location);
   }
+  drupal_write_record('location', $location);
 
   location_invoke_locationapi($location, 'save');
   return $location['lid'];
@@ -1359,7 +1342,7 @@ function location_save(&$location, $cow 
  *   The keys are field names, and the values are boolean FALSE and TRUE.
  * @return Whether or not there were any changes.
  */
-function location_calc_difference($oldloc, $newloc, &$changes) {
+function location_diff($oldloc, $newloc, &$changes) {
   location_strip($oldloc);
   location_strip($newloc);
   $location_changed = FALSE;
@@ -1413,7 +1396,7 @@ function location_is_empty($location, &$
   $settings = isset($location['location_settings']) ? $location['location_settings'] : array();
   $emptyloc = location_empty_location($settings);
 
-  return !location_calc_difference($emptyloc, $location, $filled);
+  return !location_diff($emptyloc, $location, $filled);
 }
 
 /**
@@ -1423,9 +1406,7 @@ function location_empty_location($settin
   $defaults = location_invoke_locationapi($location, 'defaults');
   if (isset($settings['form']['fields'])) {
     foreach ($settings['form']['fields'] as $k => $v) {
-      if (isset($defaults[$k])) {
-        $defaults[$k] = array_merge($defaults[$k], $v);
-      }
+      $defaults[$k] = array_merge($defaults[$k], $v);
     }
   }
 
@@ -1447,9 +1428,7 @@ function location_strip(&$location) {
     $tmp = array();
     $defaults = location_invoke_locationapi($location, 'defaults');
     foreach ($defaults as $k => $v) {
-      if (!isset($v['nodiff'])) {
-        $tmp[$k] = TRUE;
-      }
+      $tmp[$k] = TRUE;
     }
   }
   foreach ($location as $k => $v) {
@@ -1487,11 +1466,9 @@ function location_normalize_settings(&$s
   if (!$required) {
     // Relax non-required settings.
     foreach ($settings['form']['fields'] as $k => $v) {
-      if (isset($v['collect'])) {
-        if ($v['collect'] == 2) {
-          // Required -> Optional.
-          $settings['form']['fields'][$k]['collect'] = 1;
-        }
+      if (isset($settings['form']['fields'][$k]['collect'])) {
+        // Block 2 (Required).
+        $settings['form']['fields'][$k]['collect'] = $settings['form']['fields'][$k]['collect'] & ~2;
       }
     }
   }
@@ -1568,9 +1545,8 @@ function _location_geo_logic(&$location,
 /**
  * Perform validation against a location fieldset.
  */
-function location_element_validate($element, &$form_state) {
-  // @@@ TODO -- future API change -- Send $form_state as param 0 so implementations can set values.
-  location_invoke_locationapi($element['#value'], 'validate', $element);
+function location_element_validate($form, &$form_state) {
+  location_invoke_locationapi($form['#value'], 'validate', $form);
 }
 
 /**
@@ -1664,8 +1640,7 @@ function template_preprocess_location(&$
   $fields = location_field_names(TRUE);
   foreach ($fields as $key => $value) {
     $variables[$key] = '';
-    // Arrays can't be converted, ignore them.
-    if (!empty($location[$key]) && !is_array($location[$key])) {
+    if (!empty($location[$key])) {
       $variables[$key] = check_plain($location[$key]);
     }
   }
