Example #1
0
 private function init_address(&$address)
 {
     static $model;
     if (empty($address['country_name'])) {
         $address['country_name'] = waCountryModel::getInstance()->name(ifempty($address['country']));
     }
     if (empty($address['region_name'])) {
         $address['region_name'] = '';
         if (!empty($address['country']) && !empty($address['region'])) {
             if (!$model) {
                 $model = new waRegionModel();
             }
             if ($region = $model->get($address['country'], $address['region'])) {
                 $address['region_name'] = $region['name'];
             }
         }
     }
     if (empty($address['address'])) {
         $fields = array('street', 'city', 'region_name', 'zip', 'country_name');
         $address['address'] = '';
         $chunks = array();
         foreach ($fields as $field) {
             if (!empty($address[$field])) {
                 $chunks[] = $address[$field];
             }
         }
         $address['address'] = implode(', ', $chunks);
         if (preg_match('/^(.{1,119}),\\s+(.*)$/', $address['address'], $matches)) {
             $address['address_1'] = $matches[1];
             $address['address_2'] = $matches[2];
         } else {
             $address['address_1'] = $address['address'];
             $address['address_2'] = '';
         }
         $address['name'] = '';
         $fields = array('firstname', 'lasname', 'middlename');
         foreach ($fields as $field) {
             if (!empty($address[$field])) {
                 $address['name'] .= ' ' . $address[$field];
             }
         }
         $address['name'] = trim($address['name']);
     }
 }
Example #2
0
 /**
  * Returns customer's shipping address written in one string.
  *
  * @param array $order_params 'params' element of order data array returned by getOrder() method of shopOrderModel class
  * @param bool $for_map Whether full or brief address information must be returned; defaults to true
  * @return string
  */
 public static function getShippingAddressText($order_params, $for_map = true)
 {
     $address = array();
     foreach (waContactFields::get('address')->getFields() as $k => $v) {
         $address[$k] = ifset($order_params['shipping_address.' . $k]);
     }
     if ($for_map) {
         $address_f = array();
         foreach (array('country', 'region', 'zip', 'city', 'street') as $k) {
             if (!isset($address[$k])) {
                 continue;
             } elseif ($k == 'country') {
                 $address_f[$k] = waCountryModel::getInstance()->name(ifempty($address['country']));
             } elseif ($k == 'region') {
                 $address_f['region'] = '';
                 if (!empty($address['country']) && !empty($address['region'])) {
                     $model = new waRegionModel();
                     if ($region = $model->get($address['country'], $address['region'])) {
                         $address_f['region'] = $region['name'];
                     }
                 }
             } else {
                 $address_f[$k] = $address[$k];
             }
         }
         return implode(', ', $address_f);
     }
     return implode(', ', $address);
 }
 private function findTo($address)
 {
     $city = mb_strtoupper(ifset($address['city']));
     $pattern = '/(КРАЙ|РАЙОН|(АВТОНОМНАЯ )?ОБЛАСТЬ|РЕСПУБЛИКА|(АВТОНОМНЫЙ )?ОКРУГ)/u';
     $cache = new waSerializeCache(__CLASS__, 86400, 'webasyst');
     if (!($map = $cache->get())) {
         $map = array('city' => array(), 'region' => array());
         $result = $this->request('ems.get.locations', array('type' => 'russia'));
         foreach (ifempty($result['locations'], array()) as $location) {
             switch ($location['type']) {
                 case 'cities':
                     $map['city'][$location['name']] = $location['value'];
                     break;
                 case 'regions':
                     $name = trim(preg_replace($pattern, '', $location['name']));
                     $map['region'][$name] = $location['value'];
                     break;
             }
         }
         if ($map) {
             $cache->set($map);
         }
     }
     $region = trim(mb_strtoupper(ifset($address['region'])));
     $region_name = trim(mb_strtoupper(ifset($address['region_name'])));
     $to = null;
     if ($city && !empty($map['city'][$city])) {
         $to = $map['city'][$city];
     } else {
         if ($region_name && !empty($map['city'][$region_name])) {
             $to = $map['city'][$region_name];
         } elseif ($region && !empty($map['city'][$region])) {
             $to = $map['city'][$region];
         } else {
             $model = new waRegionModel();
             if ($region_name) {
                 $region_name = trim(preg_replace($pattern, '', mb_strtoupper($region_name)));
                 $to = ifset($map['region'][$region_name]);
             } elseif ($region && ($region = $model->get(ifset($address['country']), $region))) {
                 $region = trim(preg_replace($pattern, '', mb_strtoupper($region['name'])));
                 $to = ifset($map['city'][$region], ifset($map['region'][$region]));
             }
         }
     }
     return $to;
 }