Created
July 27, 2012 19:48
-
-
Save bazilio91/3190141 to your computer and use it in GitHub Desktop.
Yii DynamicFormModel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Created by JetBrains PhpStorm. | |
| * User: bazilio | |
| * Date: 7/24/12 | |
| * Time: 12:41 PM | |
| */ | |
| class DynamicFormModel extends CFormModel | |
| { | |
| private $_dynamicData = array(); | |
| private $_dynamicFields = array(); | |
| private $_dynamicRules = array(); | |
| private $_dynamicLabels = array(); | |
| private $_dynamicItems = array(); | |
| private $_dynamicRelations = array(); | |
| public function rules() | |
| { | |
| return $this->_dynamicRules; | |
| } | |
| public function attributeNames() | |
| { | |
| return array_merge( | |
| parent::attributeNames(), | |
| array_keys($this->_dynamicFields) | |
| ); | |
| } | |
| public function attributeLabels() | |
| { | |
| return $this->_dynamicLabels; | |
| } | |
| /** | |
| * Returns the value for a dynamic attribute, if not, falls back to parent | |
| * method | |
| * | |
| * @param type $name | |
| * @return type | |
| */ | |
| public function __get($name) | |
| { | |
| if (!empty($this->_dynamicFields[$name])) { | |
| if (isset($this->_dynamicData[$name])) { | |
| return $this->_dynamicData[$name]; | |
| } else { | |
| return null; | |
| } | |
| } else { | |
| return parent::__get($name); | |
| } | |
| } | |
| public function getType($name) | |
| { | |
| if (isset($this->_dynamicFields[$name])) return $this->_dynamicFields[$name]; | |
| return null; | |
| } | |
| /** | |
| * @param $name | |
| * @param $val | |
| * @param $label | |
| * @param $rules array(array('attribute','validator')) | |
| */ | |
| public function addAttribute($name, $val, $type = 'text', $label = null, $rules = null, $items = null) | |
| { | |
| $this->_dynamicFields[$name] = $type; | |
| $this->_dynamicData[$name] = $val; | |
| if ($rules) { | |
| $this->_dynamicRules[] = $rules; | |
| } | |
| if ($label) { | |
| $this->_dynamicLabels[$name] = $label; | |
| } | |
| if ($items) { | |
| $this->_dynamicItems[$name] = $items; | |
| } | |
| } | |
| public function setAttributes($values, $safeOnly = true) | |
| { | |
| if (!is_array($values)) | |
| return; | |
| $attributes = array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames()); | |
| foreach ($values as $name => $value) { | |
| if (isset($attributes[$name]) && isset($this->$name)) | |
| $this->$name = $value; | |
| else if (isset($this->_dynamicFields[$name])) { | |
| $this->_dynamicData[$name] = $value; | |
| } | |
| else if ($safeOnly) | |
| $this->onUnsafeAttribute($name, $value); | |
| } | |
| } | |
| public function getAttributes($names = null) | |
| { | |
| $values = array(); | |
| foreach ($this->attributeNames() as $k => $name) | |
| $values[$name] = $this->$name; | |
| if (is_array($names)) { | |
| $values2 = array(); | |
| foreach ($names as $name => $v) { | |
| $values2[$name] = isset($values[$name]) ? $values[$name] : null; | |
| if (!$values2[$name]) | |
| $values2[$name] = isset($this->_dynamicData[$name]) ? $this->_dynamicData[$name] : null; | |
| } | |
| return $values2; | |
| } | |
| else | |
| return $values; | |
| } | |
| public function getItems($name) | |
| { | |
| return (!empty($this->_dynamicItems[$name])) ? $this->_dynamicItems[$name] : array(); | |
| } | |
| public function addRelation($name,$relation) { | |
| $this->_dynamicRelations[$name] = $relation; | |
| } | |
| public function getRelations() { | |
| return $this->_dynamicRelations; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment