/** * Supplies a list data for multi-value fields (selects, radio buttons, * checkbox area, autocomplete). You may also use enum(). This setting * is typically used with a static falues (Male / Female), if your field * values could be described through a different model, use setModel() * or better yet - hasOne(). * * @param array $t Array( id => val ) * * @return array|$this current value if $t=UNDEFINED */ public function listData($t = UNDEFINED) { if ($this->type() === 'boolean' && $t !== UNDEFINED) { $this->owner->addHook('afterLoad,afterUpdate,afterInsert', function ($m) use($t) { // Normalize boolean data $val = array_search($m->data[$this->short_name], $t); if ($val === false) { return; } // do nothing $m->data[$this->short_name] = (bool) (!$val); }); $this->owner->addHook('beforeUpdate,beforeInsert', function ($m, &$data) use($t) { // De-Normalize boolean data $val = (int) (!$data[$this->short_name]); if (!isset($t[$val])) { return; } // do nothing $data[$this->short_name] = $t[$val]; }); } return $this->setterGetter('listData', $t); }
/** * Import model fields in form. * * Use $fields === false if you want to associate form with model, but don't create form fields. * * @param Model $model * @param array|string|bool $fields * * @return void|$this */ public function importFields($model, $fields = UNDEFINED) { $this->model = $model; $this->form = $this->owner; if ($fields === false) { return; } if (!$fields || $fields === UNDEFINED) { $fields = 'editable'; } if (!is_array($fields)) { // note: $fields parameter is only useful if model is SQL_Model $fields = $model->getActualFields($fields); } // import fields one by one foreach ($fields as $field) { $this->importField($field); } // set update hook if (!$this->_hook_set) { $this->owner->addHook('update', array($this, 'update')); $model->addHook('afterLoad', array($this, 'setFields')); $this->_hook_set = true; } return $this; }
/** * Adds a hook to specified model which will encrypt password before save. * This method will be applied on $this->model, so you should not call * it manually. You can call it on a fresh model, however. * * @param Model $model */ public function addEncryptionHook($model) { // If model is saved, encrypt password $t = $this; if (isset($model->has_encryption_hook) && $model->has_encryption_hook) { return; } $model->has_encryption_hook = true; $model->addHook('beforeSave', function ($m) use($t) { if ($m->isDirty($t->password_field) && $m[$t->password_field]) { $m[$t->password_field] = $t->encryptPassword($m[$t->password_field], $m[$t->login_field]); } }); }