/** * Initialize a behaviour for a model * * @param \Quaver\Model\Base $model */ public function initialize($model) { $model->on('getChange', function ($change, $event) { $model = $event['target']; $field = $event['field']; if (ucfirst($model->fields[$field]['type']) === 'Hash') { if ($model->{$field} !== null && $model->{$field} !== $model->getOriginalValue($field)) { if (!empty($model->{$field}) || $model->getOriginalValue($field) !== null) { return $model->{$field}; } else { return null; } } } return $change; }); $model->on('hasChanges', function ($hasChanges, $event) { if (!$hasChanges) { $model = $event['target']; foreach ($model->fields as $field => &$def) { if (ucfirst($def['type']) === 'Hash' && $model->{$field} !== $model->getOriginalValue($field)) { if (!empty($model->{$field}) || $model->getOriginalValue($field) !== null) { return true; } } } } return $hasChanges; }); $model->on('getChanges', function ($changes, $event) { $model = $event['target']; foreach ($model->fields as $field => &$def) { if (ucfirst($def['type']) === 'Hash' && $model->{$field} !== $model->getOriginalValue($field)) { if (!empty($model->{$field}) || $model->getOriginalValue($field) !== null) { $changes[$field] = $model->{$field}; } else { unset($changes[$field]); } } } return $changes; }); }
/** * Initialize a behaviour for a model * * @param \Quaver\Model\Base $model * @param array $options */ public function initialize($model, $options) { $model->on('preSave', [$this, 'preSave']); }
/** * {@inheritdoc} */ public function save($fields = null) { if (empty($this->nameFrom)) { $this->nameFrom = Config::get('mail.CONTACT_NAME'); } if (empty($this->emailFrom)) { $this->emailFrom = Config::get('mail.CONTACT_EMAIL'); } $old = $this->mailObjects; if (!empty($this->mailObjects)) { $mailObjectsSerialize = []; foreach ($this->mailObjects as $object => $value) { $mailObjectsSerialize[$object] = is_object($value) ? $value->id : $value; } $this->mailObjects = serialize($mailObjectsSerialize); } if (!parent::save($fields)) { $this->mailObjects = $old; return false; } else { $this->mailObjects = $old; } return true; }
/** * Initialize a behaviour for a model * * @param \Quaver\Model\Base $model * @param array $options */ public function initialize($model, $options) { $model->on('preDelete', [$this, 'preDelete']); $model->on('isDeleted', [$this, 'isDeleted']); }
/** * setValidate. * * @return bool */ public function setNewEmailAsValid() { if (!$this->isValidatingNewEmail()) { $this->setError(null, 'error-not-validating'); return false; } parent::set('email', $this->get('emailValidating')); $this->set('recoveryToken', null); $this->set('emailValidating', null); }
/** * {@inheritdoc} */ public function getModelTitle() { return $this->action ? $this->action : parent::getModelTitle(); }