/[drupal]/contributions/modules/uc_location/uc_location.module
ViewVC logotype

Diff of /contributions/modules/uc_location/uc_location.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.2 by mearnest, Tue Oct 6 22:12:31 2009 UTC revision 1.2.2.1 by mearnest, Fri Nov 20 22:46:32 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: uc_location.module,v 1.1 2009/10/05 21:15:52 mearnest Exp $  // $Id: uc_location.module,v 1.8 2009/11/18 16:22:03 mearnest Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 72  function uc_location_form_uc_cart_checko Line 72  function uc_location_form_uc_cart_checko
72    global $user;    global $user;
73    
74    $is_billable = !empty($form['panes']['billing']);    $is_billable = !empty($form['panes']['billing']);
75    $is_deliverable = !empty($form['panes']['delivery']);    $is_deliverable = !empty($form['panes']['delivery']) && (uc_cart_is_shippable() || !variable_get('uc_cart_delivery_not_shippable', TRUE));
   $is_shippable =  (uc_cart_is_shippable() || !variable_get('uc_cart_delivery_not_shippable', TRUE));  
76    
77    // If new user, remove the address selection fields.    // If new user, remove the address selection fields.
78    if ($user->uid == 0) {    if ($user->uid == 0) {
79      unset($form['panes']['delivery']['delivery_address_select']);      unset($form['panes']['delivery']['delivery_address_select']);
80      unset($form['panes']['billing']['billing_address_select']);      unset($form['panes']['billing']['billing_address_select']);
81    }    }
82    else {    elseif (($is_billable || $is_deliverable) && $address_list = _uc_location_create_address_list()) {
83      // Recreate address selection field(s) for billing and/or delivery.      drupal_add_js(drupal_get_path('module', 'uc_location') .'/uc_location.js');
84    
85      if ($is_billable || ($is_shippable && $is_deliverable) && !empty($user->locations) ) {      if (user_access('set own user location')) {
86        drupal_add_js(drupal_get_path('module', 'uc_location') .'/uc_location.js');        $address_book_icon = l(uc_store_get_icon('file:address_book', FALSE, 'address-book-icon'),
87            'user/'. $user->uid .'/edit', array('html' => TRUE));
88        // Create book icon and address list.      }
89        $address_book_icon = l(uc_store_get_icon('file:address_book', FALSE, 'address-book-icon'),      else {
90          'user/'. $user->uid .'/edit', array('html' => TRUE));        $address_book_icon = '';
       $address_list = _uc_location_create_address_list();  
91      }      }
92    
93      if ($is_billable) {      if ($is_billable) {
94        _uc_location_recreate_address_select_field(        _uc_location_recreate_address_select_field($form['panes']['billing'], 'billing', $address_list, $address_book_icon);
95          $form['panes']['billing'], 'billing', $address_list, $address_book_icon);      }
96    
97        if ($is_deliverable) {
98          _uc_location_recreate_address_select_field($form['panes']['delivery'], 'delivery', $address_list, $address_book_icon);
99      }      }
     if ($is_shippable && $is_deliverable) {  
       _uc_location_recreate_address_select_field(  
         $form['panes']['delivery'], 'delivery', $address_list, $address_book_icon);  
     }  
100    }    }
101  }  }
102    
# Line 122  function uc_location_save_location(&$ord Line 119  function uc_location_save_location(&$ord
119      $order_str = _uc_location_order_to_str($order_location);      $order_str = _uc_location_order_to_str($order_location);
120      $orders[$type] = $order_str;      $orders[$type] = $order_str;
121      $orders_are_equal = ($orders['billing'] == $orders['delivery']);      $orders_are_equal = ($orders['billing'] == $orders['delivery']);
122    
123      // Add a location when the order info exists, the delivery order isn't the same      // Add a location when the order info exists, the delivery order isn't the same
124      // as the billing (so a duplicate isn't added), and the order info doesn't      // as the billing (so a duplicate isn't added), and the order info doesn't
125      // already exist in locations. A duplicate is added when billing & delivery are      // already exist in locations. A duplicate is added when billing & delivery are
# Line 134  function uc_location_save_location(&$ord Line 131  function uc_location_save_location(&$ord
131        $index = _uc_location_find($locations, $order_str);        $index = _uc_location_find($locations, $order_str);
132    
133        if ($index === FALSE) {        if ($index === FALSE) {
134          $set_primary = ( ($type == 'billing') && (_uc_location_primary_not_set(&$locations)) );          $set_primary = ( ($type == 'billing') && (_uc_location_primary_not_set($locations)) );
135          _uc_location_add_to_locations($order->uid, $order_location, $locations, $set_primary);          _uc_location_add_to_locations($order->uid, $order_location, $locations, $set_primary);
136        }        }
137      }      }
# Line 147  function uc_location_save_location(&$ord Line 144  function uc_location_save_location(&$ord
144    
145  /**  /**
146   * Creates address list from user locations.   * Creates address list from user locations.
  * "$user->locations" needs to be set when calling this function.  
147   *   *
148   * @return   * @return
149   *   A keyed array of user location addresses in json format.   *   A keyed array of user location addresses in json format.
150  **/  **/
151  function _uc_location_create_address_list() {  function _uc_location_create_address_list() {
152    global $user;    global $user;
153    
154    if (!isset($user->locations)) {    // Reload user account as the locations are not always loaded.
155      // Not acceptable.    $account = user_load($user->uid);
     // Note that this module has the "location" module as a dependency,  
     // so this probably should never get here.  
     // Also, it's okay if the array exists, but is empty.  
   
     // can't operate - fail fast  
     drupal_set_message(t('_uc_location_create_address_list: user locations not available.'), 'error');  
     return array();  
   }  
156    
157    $address_list = array('0' => t('Select one...'));    $address_list = array('0' => t('Select one...'));
158    
159    foreach ($user->locations as $location) {    foreach ($account->locations as $location) {
160      $uc_address = _uc_location_to_uc_address($location);      $uc_address = _uc_location_to_uc_address($location);
161    
162      if ($location['name']) {      if ($location['name']) {
# Line 232  function _uc_location_to_uc_address(&$lo Line 220  function _uc_location_to_uc_address(&$lo
220    $address['street1'] = $location['street'];    $address['street1'] = $location['street'];
221    $address['street2'] = $location['additional'];    $address['street2'] = $location['additional'];
222    $address['city'] = $location['city'];    $address['city'] = $location['city'];
223    
224    $address['zone'] = $location['province_name'];    $address['zone'] = $location['province_name'];
225    $address['postal_code'] = $location['postal_code'];    $address['postal_code'] = $location['postal_code'];
226    $address['country'] = $location['country_name'];  
227    $address['phone'] = $location['phone'];    $country = ($location[country] == 'uk' ? 'GB' : drupal_strtoupper($location[country]));
228    
229      // apply_address js function compares iso_code_2 so need to look it up
230      $query = "SELECT country_id FROM {uc_countries} WHERE country_iso_code_2 = '%s'";
231      $country = db_fetch_object( db_query($query, $country) );
232    
233      // In case location stores a different code and query returns null as a result, lookup by name
234      if (empty($country)) {
235        $query = "SELECT country_id FROM {uc_countries} WHERE country_name = '%s'";
236        $country = db_fetch_object( db_query($query, drupal_strtoupper($location['country_name'])) );
237      }
238    
239      $address['country'] = $country->country_id;
240      $address['phone'] = isset($location['phone']) ? $location['phone'] : '';
241    
242    return $address;    return $address;
243  }  }
# Line 311  function _uc_location_add_to_locations($ Line 313  function _uc_location_add_to_locations($
313    }    }
314    $criteria = array('uid' => $uid);    $criteria = array('uid' => $uid);
315    $locations[] = $location;    $locations[] = $location;
316    
317    location_save_locations($locations, $criteria);    location_save_locations($locations, $criteria);
318  }  }
319    
# Line 337  function _uc_location_order_to_location( Line 340  function _uc_location_order_to_location(
340    // If query returns false instead of of query object, create object with code and empty name value.    // If query returns false instead of of query object, create object with code and empty name value.
341    if ($zone == FALSE) {    if ($zone == FALSE) {
342      $zone = (object) array(      $zone = (object) array(
343          'zone_code' => $order->{$type .'_zone'},          'zone_code' => $order->{$type .'_zone'} ? $order->{$type .'_zone'} : '',
344          'zone_name' => '',          'zone_name' => '',
345        );        );
346    }    }
# Line 365  function _uc_location_order_to_location( Line 368  function _uc_location_order_to_location(
368    $location['province_name'] = $zone->zone_name;    $location['province_name'] = $zone->zone_name;
369    $location['postal_code'] = trim($order->{$type .'_postal_code'});    $location['postal_code'] = trim($order->{$type .'_postal_code'});
370    
371    // Location stores country code in lower case.    // Convert United Kingdom code to Location-compatible code (GB)
372    $location['country'] = drupal_strtolower($country->country_code);    if($country->country_code == 'GB') {
373        $location['country'] = 'uk';
374      }
375      else {
376        // Location stores country code in lower case.
377        $location['country'] = drupal_strtolower($country->country_code);
378      }
379    
380    $location['country_name'] = $country->country_name;    $location['country_name'] = $country->country_name;
381    $location['phone'] = trim($order->{$type .'_phone'});    $location['phone'] = trim($order->{$type .'_phone'});
382    
# Line 426  function _uc_location_find(&$locations, Line 436  function _uc_location_find(&$locations,
436   * @return   * @return
437   *   A lowercase json string.   *   A lowercase json string.
438  **/  **/
439  function _uc_location_order_to_str(&$order) {  function _uc_location_order_to_str($order) {
440      // Country name can vary between Ubercart and Location, so remove.
441      unset($order['country_name']);
442    return drupal_strtolower(drupal_to_js($order));    return drupal_strtolower(drupal_to_js($order));
443  }  }
444    
# Line 446  function _uc_location_location_to_str(&$ Line 458  function _uc_location_location_to_str(&$
458      $fields = array_merge($fields, array('first_name', 'last_name', 'organization'));      $fields = array_merge($fields, array('first_name', 'last_name', 'organization'));
459    }    }
460    
461    $fields = array_merge($fields, array(    // No longer using country_name because it can vary between Ubercart and Location
462      'street', 'additional', 'city', 'province', 'province_name', 'postal_code', 'country', 'country_name', 'phone'));    $fields = array_merge($fields, array('street', 'additional', 'city', 'province', 'province_name', 'postal_code', 'country', 'phone'));
   
463    $address = array();    $address = array();
464    
465    // Just copy the specified fields.    // Just copy the specified fields.
# Line 468  function _uc_location_location_to_str(&$ Line 479  function _uc_location_location_to_str(&$
479   *   TRUE if no location's is_primary field is set to 1;   *   TRUE if no location's is_primary field is set to 1;
480  **/  **/
481  function _uc_location_primary_not_set(&$locations) {  function _uc_location_primary_not_set(&$locations) {
   if (!isset($locations)) {  
     // Not acceptable.  
     // Note that this module has the "location" module as a dependency,  
     // so this probably should never get here.  
     // Also, it's okay if the array exists, but is empty.  
   
     // Can't operate - return FALSE so calling code doesn't try to set a primary location.  
     drupal_set_message(t('_uc_location_is_primary_set: user locations not available.'), 'error');  
     return FALSE;  
   }  
   
482    foreach ($locations as $location) {    foreach ($locations as $location) {
483      if ($location['is_primary'] == 1) {      if ($location['is_primary'] == 1) {
484        return FALSE;        return FALSE;

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.2.2.1

  ViewVC Help
Powered by ViewVC 1.1.3