Пример #1
0
 function beforeSave()
 {
     $named = isset($this->data[$this->name]);
     if ($named) {
         $data = $this->data[$this->name];
     } else {
         $data = $this->data;
     }
     if (isset($data['id'])) {
         $id = $data['id'];
     } else {
         $id = $this->id;
     }
     if (!empty($data['use_shipping'])) {
         if (!empty($data['shipping_address'])) {
             $shipping_data = $data;
         } elseif (!empty($id)) {
             $shipping_data = $this->read(null, $id);
             $shipping_data = $shipping_data[$this->name];
         }
         if (!empty($shipping_data)) {
             $prefix_from = "shipping_";
             $prefix_to = "billing_";
             foreach ($shipping_data as $key => $val) {
                 if (substr($key, 0, strlen($prefix_from)) == $prefix_from) {
                     $sufix = substr($key, strlen($prefix_from));
                     //if(empty($data[$prefix_to.$sufix])){
                     $data[$prefix_to . $sufix] = $shipping_data[$key];
                     //}
                 }
             }
         }
     }
     if (!empty($data['use_billing'])) {
         if (!empty($data['billing_address'])) {
             $billing_data = $data;
         } elseif (!empty($id)) {
             $billing_data = $this->read(null, $id);
             $billing_data = $billing_data[$this->name];
         }
         if (!empty($billing_data)) {
             $prefix_from = "billing_";
             $prefix_to = "shipping_";
             foreach ($billing_data as $key => $val) {
                 if (substr($key, 0, strlen($prefix_from)) == $prefix_from) {
                     $sufix = substr($key, strlen($prefix_from));
                     //if(empty($data[$prefix_to.$sufix])){
                     $data[$prefix_to . $sufix] = $billing_data[$key];
                     //}
                 }
             }
         }
     }
     App::import('Lib', 'Shop.SetMulti');
     $both_keys = SetMulti::pregFilterKey('/^both_/', $data);
     foreach ($both_keys as $key => $val) {
         $key = preg_replace('/^both_/', '', $key);
         if (empty($data["billing_" . $key])) {
             $data["billing_" . $key] = $val;
         }
         if (empty($data["shipping_" . $key])) {
             $data["shipping_" . $key] = $val;
         }
     }
     $fieldsToAlias = array('billing_country' => 'country', 'billing_region' => 'region', 'shipping_country' => 'country', 'shipping_region' => 'region');
     App::import('lib', 'Shop.Alias');
     $data = Alias::applyAliasMulti($data, $fieldsToAlias);
     if ($named) {
         $this->data = array($this->name => $data);
     } else {
         $this->data = $data;
     }
     return true;
 }