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); } } }
/** * [processContent description] * @param [type] $ctype [description] * @param [type] $data [description] * @return [type] [description] */ public function processContent($ctype, $data) { $processedData = []; $filePath = \Yii::getAlias('@app/workspace/elements') . DIRECTORY_SEPARATOR . $this->entryPoint['ctype'] . '.json'; $definitionPath = \Yii::getAlias('@app/workspace/elements') . DIRECTORY_SEPARATOR . $ctype . '.json'; $elementDefinition = CrelishDynamicJsonModel::loadElementDefinition($definitionPath); if ($data) { foreach ($data as $key => $content) { $fieldType = Arrays::find($elementDefinition->fields, function ($value) use($key) { return $value->key == $key; }); if (!empty($fieldType) && is_object($fieldType)) { $fieldType = $fieldType->type; } if (!empty($fieldType)) { // Get processor class. $processorClass = 'giantbits\\crelish\\plugins\\' . strtolower($fieldType) . '\\' . ucfirst($fieldType) . 'ContentProcessor'; if (strpos($fieldType, "widget_") !== false) { $processorClass = str_replace("widget_", "", $fieldType) . 'ContentProcessor'; } if (class_exists($processorClass)) { $processorClass::processData($this, $key, $content, $processedData); } else { $processedData[$key] = $content; } } } } return $processedData; }