コード例 #1
0
ファイル: waContact.class.php プロジェクト: Lazary/webasyst
 /**
  * Saves contact's data to database.
  *
  * @param array $data Associative array of contact property values.
  * @param bool $validate Flag requiring to validate property values. Defaults to false.
  * @return int|array Zero, if saved successfully, or array of error messages otherwise
  */
 public function save($data = array(), $validate = false)
 {
     $is_user = $this->get('is_user');
     $add = array();
     foreach ($data as $key => $value) {
         if (strpos($key, '.')) {
             $key_parts = explode('.', $key);
             $f = waContactFields::get($key_parts[0]);
             if ($f) {
                 $key = $key_parts[0];
                 if ($key_parts[1] && $f->isExt()) {
                     // add next field
                     $add[$key] = true;
                     if (is_array($value)) {
                         if (!isset($value['value'])) {
                             $value = array('ext' => $key_parts[1], 'value' => $value);
                         }
                     } else {
                         $value = array('ext' => $key_parts[1], 'value' => $value);
                     }
                 }
             }
         } else {
             $f = waContactFields::get($key);
         }
         if ($f) {
             $this->data[$key] = $f->set($this, $value, array(), isset($add[$key]) ? true : false);
         } else {
             if ($key == 'password') {
                 $value = self::getPasswordHash($value);
             }
             $this->data[$key] = $value;
         }
     }
     $this->data['name'] = $this->get('name');
     $this->data['firstname'] = $this->get('firstname');
     $this->data['is_company'] = $this->get('is_company');
     if ($this->id && isset($this->data['is_user'])) {
         $log_model = new waLogModel();
         if ($this->data['is_user'] == '-1' && $is_user != '-1') {
             $log_model->add('access_disable', null, $this->id, wa()->getUser()->getId());
         } else {
             if ($this->data['is_user'] != '-1' && $is_user == '-1') {
                 $log_model->add('access_enable', null, $this->id, wa()->getUser()->getId());
             }
         }
     }
     $save = array();
     $errors = array();
     $contact_model = new waContactModel();
     foreach ($this->data as $field => $value) {
         if ($field == 'login') {
             $f = new waContactStringField('login', _ws('Login'), array('unique' => true, 'storage' => 'info'));
         } else {
             $f = waContactFields::get($field, $this['is_company'] ? 'company' : 'person');
         }
         if ($f) {
             if ($f->isMulti() && !is_array($value)) {
                 $value = array($value);
             }
             if ($f->isMulti()) {
                 foreach ($value as &$val) {
                     if (is_string($val)) {
                         $val = trim($val);
                     } else {
                         if (isset($val['value']) && is_string($val['value'])) {
                             $val['value'] = trim($val['value']);
                         } else {
                             if ($f instanceof waContactCompositeField && isset($val['data']) && is_array($val['data'])) {
                                 foreach ($val['data'] as &$v) {
                                     if (is_string($v)) {
                                         $v = trim($v);
                                     }
                                 }
                                 unset($v);
                             }
                         }
                     }
                 }
                 unset($val);
             } else {
                 if (is_string($value)) {
                     $value = trim($value);
                 } else {
                     if (isset($value['value']) && is_string($value['value'])) {
                         $value['value'] = trim($value['value']);
                     } else {
                         if ($f instanceof waContactCompositeField && isset($value['data']) && is_array($value['data'])) {
                             foreach ($value['data'] as &$v) {
                                 if (is_string($v)) {
                                     $v = trim($v);
                                 }
                             }
                             unset($v);
                         }
                     }
                 }
             }
             if ($validate !== 42) {
                 // this deep dark magic is used when merging contacts
                 if ($validate) {
                     if ($e = $f->validate($value, $this->id)) {
                         $errors[$f->getId()] = $e;
                     }
                 } elseif ($f->isUnique()) {
                     // validate unique
                     if ($e = $f->validateUnique($value, $this->id)) {
                         $errors[$f->getId()] = $e;
                     }
                 }
             }
             if (!$errors && $f->getStorage()) {
                 $save[$f->getStorage()->getType()][$field] = $f->prepareSave($value, $this);
             }
         } elseif ($contact_model->fieldExists($field)) {
             $save['waContactInfoStorage'][$field] = $value;
         } else {
             $save['waContactDataStorage'][$field] = $value;
         }
     }
     // Returns errors
     if ($errors) {
         return $errors;
     }
     $is_add = false;
     // Saving to all storages
     try {
         if (!$this->id) {
             $is_add = true;
             $storage = 'waContactInfoStorage';
             if (wa()->getEnv() == 'frontend') {
                 if ($ref = waRequest::cookie('referer')) {
                     $save['waContactDataStorage']['referer'] = $ref;
                     $save['waContactDataStorage']['referer_host'] = parse_url($ref, PHP_URL_HOST);
                 }
                 if ($utm = waRequest::cookie('utm')) {
                     $utm = json_decode($utm, true);
                     if ($utm && is_array($utm)) {
                         foreach ($utm as $k => $v) {
                             $save['waContactDataStorage']['utm_' . $k] = $v;
                         }
                     }
                 }
             }
             $this->id = waContactFields::getStorage($storage)->set($this, $save[$storage]);
             unset($save[$storage]);
         }
         foreach ($save as $storage => $storage_data) {
             waContactFields::getStorage($storage)->set($this, $storage_data);
         }
         $this->data = array();
         wa()->event(array('contacts', 'save'), $this);
         $this->removeCache();
         $this->clearDisabledFields();
     } catch (Exception $e) {
         // remove created contact
         if ($is_add && $this->id) {
             $this->delete();
             $this->id = null;
         }
         $errors['name'][] = $e->getMessage();
     }
     return $errors ? $errors : 0;
 }
コード例 #2
0
 /**
  * Save unsaved data
  * If saving was succesfully returns 0 and array of errors themselves
  *
  * @param array $data
  * @param bool $validate
  * @return int|array
  */
 public function save($data = array(), $validate = false)
 {
     $add = array();
     foreach ($data as $key => $value) {
         if (strpos($key, '.')) {
             $key_parts = explode('.', $key);
             $f = waContactFields::get($key_parts[0]);
             if ($f) {
                 $key = $key_parts[0];
                 if ($key_parts[1] && $f->isExt()) {
                     // add next field
                     $add[$key] = true;
                     if (is_array($value)) {
                         if (!isset($value['value'])) {
                             $value = array('ext' => $key_parts[1], 'value' => $value);
                         }
                     } else {
                         $value = array('ext' => $key_parts[1], 'value' => $value);
                     }
                 }
             }
         } else {
             $f = waContactFields::get($key);
         }
         if ($f) {
             $this->data[$key] = $f->set($this, $value, array(), isset($add[$key]) ? true : false);
         } else {
             if ($key == 'password') {
                 $value = self::getPasswordHash($value);
             }
             $this->data[$key] = $value;
         }
     }
     $this->data['name'] = $this->get('name');
     $save = array();
     $errors = array();
     $contact_model = new waContactModel();
     foreach ($this->data as $field => $value) {
         if ($field == 'login') {
             $f = new waContactStringField('login', _ws('Login'), array('unique' => true, 'storage' => 'info'));
         } else {
             $f = waContactFields::get($field, $this['is_company'] ? 'company' : 'person');
         }
         if ($f) {
             if ($f->isMulti() && !is_array($value)) {
                 $value = array($value);
             }
             if ($validate !== 42) {
                 // this deep dark magic is used when merging contacts
                 if ($validate) {
                     if ($e = $f->validate($value, $this->id)) {
                         $errors[$f->getId()] = $e;
                     }
                 } elseif ($f->isUnique()) {
                     // validate unique
                     if ($e = $f->validateUnique($value, $this->id)) {
                         $errors[$f->getId()] = $e;
                     }
                 }
             }
             if (!$errors) {
                 $save[$f->getStorage()->getType()][$field] = $value;
             }
         } elseif ($contact_model->fieldExists($field)) {
             $save['waContactInfoStorage'][$field] = $value;
         } else {
             $save['waContactDataStorage'][$field] = $value;
         }
     }
     // Returns errors
     if ($errors) {
         return $errors;
     }
     // Saving to all storages
     try {
         $is_add = false;
         if (!$this->id) {
             $is_add = true;
             $storage = 'waContactInfoStorage';
             $this->id = waContactFields::getStorage($storage)->set($this, $save[$storage]);
             unset($save[$storage]);
         }
         foreach ($save as $storage => $storage_data) {
             waContactFields::getStorage($storage)->set($this, $storage_data);
         }
         $this->data = array();
     } catch (Exception $e) {
         // remove created contact
         if ($is_add && $this->id) {
             $this->delete();
             $this->id = null;
         }
         $errors['name'][] = $e->getMessage();
     }
     return $errors ? $errors : 0;
 }