/** * @return bool */ protected function saveFromPost($form, $contact) { $data = $form->post(); if (!$data || !is_array($data)) { return false; } // save photo before all $photo_file = waRequest::file('photo_file'); if (array_key_exists('photo', $data)) { if ($photo_file->uploaded() && ($avatar = $photo_file->waImage())) { // add/update photo $square = min($avatar->height, $avatar->width); // setPhoto with crop $rand = mt_rand(); $path = wa()->getDataPath(waContact::getPhotoDir($contact->getId()), true, 'contacts', false); // delete old image if (file_exists($path)) { waFiles::delete($path); } waFiles::create($path); $filename = $path . $rand . ".original.jpg"; waFiles::create($filename); waImage::factory($photo_file)->save($filename, 90); $filename = $path . $rand . ".jpg"; waFiles::create($filename); waImage::factory($photo_file)->crop($square, $square)->save($filename, 90); waContactFields::getStorage('waContactInfoStorage')->set($contact, array('photo' => $rand)); } elseif (empty($data['photo'])) { // remove photo $contact->set('photo', ""); } $this->form->values['photo'] = $data['photo'] = $contact->get('photo'); } // Validation if (!$form->isValid($contact)) { return false; } // Password validation if (!empty($data['password']) && $data['password'] !== $data['password_confirm']) { $form->errors('password', _ws('Passwords do not match')); return false; } elseif (empty($data['password']) || empty($data['password_confirm'])) { unset($data['password']); } unset($data['password_confirm']); // get old data for logging if ($this->contact) { $old_data = array(); foreach ($data as $field_id => $field_value) { $old_data[$field_id] = $this->contact->get($field_id); } } foreach ($data as $field => $value) { $contact->set($field, $value); } $errors = $contact->save(); // If something went wrong during save for any reason, // show it to user. In theory it shouldn't but better be safe. if ($errors) { foreach ($errors as $field => $errs) { foreach ($errs as $e) { $form->errors($field, $e); } } return false; } // get new data for logging $new_data = array(); foreach ($data as $field_id => $field_value) { if (!isset($errors[$field_id])) { $new_data[$field_id] = $this->contact->get($field_id); } } $this->logProfileEdit($old_data, $new_data); return true; }
public function clearDisabledFields() { if (!$this->id) { return; } $disabled_fields = waContactFields::getAll($this['is_company'] ? 'company_disabled' : 'person_disabled', true); $data = array(); foreach ($disabled_fields as $field_id => $field) { $data[$field->getStorage()->getType()][$field_id] = $field->prepareSave(null); } foreach ($data as $storage => $storage_data) { waContactFields::getStorage($storage)->set($this, $storage_data); } }
/** * @param bool $name * @return waContactStorage|string */ public function getStorage($name = null) { if ($name) { return $this->options['storage']; } if (!$this->options['storage']) { return null; } return waContactFields::getStorage($this->options['storage']); }
/** * 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; }