public function init() { parent::init(); // Build definitions. if (!empty($this->ctype)) { $filePath = \Yii::getAlias('@app/workspace/elements') . DIRECTORY_SEPARATOR . $this->ctype . '.json'; $elementDefinition = Json::decode(file_get_contents($filePath), false); // Add core fields. $elementDefinition->fields[] = Json::decode('{ "label": "UUID", "key": "uuid", "type": "textInput", "visibleInGrid": true, "rules": [["string", {"max": 128}]], "options": {"disabled":true}}', false); $elementDefinition->fields[] = Json::decode('{ "label": "Path", "key": "path", "type": "textInput", "visibleInGrid": true, "rules": [["string", {"max": 128}]]}', false); $elementDefinition->fields[] = Json::decode('{ "label": "Slug", "key": "slug", "type": "textInput", "visibleInGrid": true, "rules": [["string", {"max": 128}]]}', false); $elementDefinition->fields[] = Json::decode('{ "label": "State", "key": "state", "type": "dropDownList", "visibleInGrid": true, "rules": [["required"], ["integer"]], "options": {"prompt":"Please set state"}, "items": {"0":"Offline", "1":"Draft", "2":"Online", "3":"Archived"}}', false); $elementDefinition->fields[] = Json::decode('{ "label": "Created", "key": "created", "type": "textInput", "visibleInGrid": true, "rules": [["string", {"max": 128}]]}', false); $elementDefinition->fields[] = Json::decode('{ "label": "Updated", "key": "updated", "type": "textInput", "visibleInGrid": true, "rules": [["string", {"max": 128}]]}', false); $elementDefinition->fields[] = Json::decode('{ "label": "Publish from", "key": "from", "type": "textInput", "visibleInGrid": true, "rules": [["string", {"max": 128}]]}', false); $elementDefinition->fields[] = Json::decode('{ "label": "Publish to", "key": "to", "type": "textInput", "visibleInGrid": true, "rules": [["string", {"max": 128}]]}', false); $this->fieldDefinitions = $elementDefinition; $fields = []; // Build field array. foreach ($elementDefinition->fields as $field) { array_push($fields, $field->key); } $this->identifier = $this->ctype; // Populate attributes. foreach ($fields as $name => $value) { if (is_int($name)) { $this->defineAttribute($value, null); } else { $this->defineAttribute($name, $value); } } // Add validation rules. foreach ($elementDefinition->fields as $field) { $this->defineLabel($field->key, $field->label); if (!empty($field->rules)) { foreach ($field->rules as $rule) { if (empty($rule[1])) { $this->addRule([$field->key], $rule[0]); } else { $this->addRule([$field->key], $rule[0], (array) $rule[1]); } } } } // Load model from file. if (!empty($this->uuid)) { $data['CrelishDynamicJsonModel'] = Json::decode(file_get_contents(\Yii::getAlias('@app/workspace/data/') . DIRECTORY_SEPARATOR . $this->ctype . DIRECTORY_SEPARATOR . $this->uuid . '.json')); $this->load($data); } } }
public function init() { parent::init(); if ($this->relatedElementModel->relatedProperties) { foreach ($this->relatedElementModel->relatedProperties as $property) { $this->defineAttribute($property->code, $property->handler->isMultiple ? [] : null); $property->relatedPropertiesModel = $this; $property->addRules(); $this->_properties[$property->code] = $property; } } if ($relatedElementProperties = $this->relatedElementModel->relatedElementProperties) { foreach ($this->_properties as $code => $property) { if ($property->handler->isMultiple) { $values = []; $valuesModels = []; foreach ($relatedElementProperties as $propertyElementVal) { if ($propertyElementVal->property_id == $property->id) { $values[$propertyElementVal->id] = $propertyElementVal->value; $valuesModels[$propertyElementVal->id] = $propertyElementVal; } } $values = $property->handler->initValue($values); $this->setAttribute($code, $values); $this->_propertyValues[$code] = $valuesModels; } else { $value = null; $valueModel = null; foreach ($relatedElementProperties as $propertyElementVal) { if ($propertyElementVal->property_id == $property->id) { $value = $propertyElementVal->value; $valueModel = $propertyElementVal; break; } } $value = $property->handler->initValue($value); $this->setAttribute($code, $value); $this->_propertyValues[$code] = $valueModel; } } } }
public function init() { parent::init(); // Build definitions. if (!empty($this->ctype)) { $filePath = \Yii::getAlias('@app/workspace/elements') . DIRECTORY_SEPARATOR . $this->ctype . '.json'; $elementDefinition = CrelishDynamicJsonModel::loadElementDefinition($filePath); $this->fieldDefinitions = $elementDefinition; $fields = []; // Build field array. foreach ($elementDefinition->fields as $field) { array_push($fields, $field->key); } $this->identifier = $this->ctype; // Populate attributes. foreach ($fields as $name => $value) { if (is_int($name)) { $this->defineAttribute($value, null); } else { $this->defineAttribute($name, $value); } } // Add validation rules. foreach ($elementDefinition->fields as $field) { $this->defineLabel($field->key, $field->label); if (!empty($field->rules)) { foreach ($field->rules as $rule) { if (empty($rule[1])) { $this->addRule([$field->key], $rule[0]); } else { $this->addRule([$field->key], $rule[0], (array) $rule[1]); } } } } // Load model from file. if (!empty($this->uuid)) { $data['CrelishDynamicJsonModel'] = Json::decode(file_get_contents(\Yii::getAlias('@app/workspace/data/') . DIRECTORY_SEPARATOR . $this->ctype . DIRECTORY_SEPARATOR . $this->uuid . '.json')); $this->load($data); } } }
public function init() { parent::init(); $this->_owner = Instance::ensure($this->_owner, Params::className()); }