/** * [init description]. * * @return [type] [description] */ public function init() { parent::init(); $this->ctype = 'user'; $this->uuid = !empty(\Yii::$app->getRequest()->getQueryParam('uuid')) ? \Yii::$app->getRequest()->getQueryParam('uuid') : null; // create default user element, if none is present $workspacePath = realpath(\Yii::getAlias('@webroot') . '/../workspace'); if ($workspacePath === false) { throw new \yii\web\ServerErrorHttpException('The *workspace* folder could not be found - please create it in your project root'); } $elementsPath = realpath($workspacePath . '/elements'); if ($elementsPath === false) { throw new \yii\web\ServerErrorHttpException('The *elements* folder could not be found - please create it in your workspace folder'); } $modelJson = realpath($elementsPath . '/' . $this->ctype . '.json'); if ($modelJson === false) { file_put_contents($elementsPath . '/' . $this->ctype . '.json', '{"key":"user","label":"User","tabs":[{"label":"Login","key":"login","visible":false,"groups":[{"label":"Login","key":"login","fields":["email","password","login"]}]},{"label":"User","key":"user","groups":[{"label":"User","key":"user","fields":["email","password","state"]}]}],"fields":[{"label":"Email address","key":"email","type":"textInput","visibleInGrid":true,"rules":[["required"],["email"],["string",{"max":128}]]},{"label":"Password","key":"password","type":"passwordInput","visibleInGrid":false,"rules":[["required"],["string",{"max":128}]],"transform":"hash"},{"label":"Login","key":"login","type":"submitButton","visibleInGrid":false}, {"label":"Auth-Key","key":"authKey","type":"text","visibleInGrid":false}]}'); } \Yii::$app->cache->flush(); $usersProvider = new CrelishJsonDataProvider('user'); $users = $usersProvider->rawAll(); if (sizeof($users) == 0) { // Generate default admin. $adminUser = new CrelishDynamicJsonModel(['email', 'password', 'login', 'state'], ['ctype' => 'user']); $adminUser->email = '*****@*****.**'; $adminUser->login = '******'; $adminUser->password = '******'; $adminUser->state = 1; $adminUser->authKey = \Yii::$app->security->generateRandomString(); $adminUser->save(); } }
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 actionUpload() { $file = UploadedFile::getInstanceByName('file'); if ($file) { $destName = time() . '_' . $file->name; $targetFile = \Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . '_lib' . DIRECTORY_SEPARATOR . $destName; $file->saveAs($targetFile); $filePath = \Yii::getAlias('@app/workspace/elements') . DIRECTORY_SEPARATOR . 'asset' . '.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"], ["string", {"max": 128}]], "options": {"prompt":"Please set state"}, "items": {"0":"Offline", "1":"Draft", "2":"Online", "3":"Archived"}}', false); $fields = []; foreach ($elementDefinition->fields as $field) { array_push($fields, $field->key); } $model = new CrelishDynamicJsonModel($fields); $model->identifier = 'asset'; $model->systitle = $destName; $model->title = $destName; $model->src = \Yii::getAlias('@web') . '/' . '_lib' . '/' . $destName; $model->mime = $file->type; $model->size = $file->size; $model->state = 2; $model->save(); try { $domColor = ColorThief::getColor($targetFile, 20); $palColor = ColorThief::getPalette(\Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . '_lib' . DIRECTORY_SEPARATOR . $destName); $model->colormain_rgb = Json::encode($domColor); $model->colormain_hex = '#' . sprintf('%02x', $domColor[0]) . sprintf('%02x', $domColor[1]) . sprintf('%02x', $domColor[2]); $model->save(); $model->colorpalette = Json::encode($palColor); $model->save(); } catch (Exception $e) { \Yii::$app->session->setFlash('secondary', 'Color theft could not be completed. (Image too large?)'); } } return false; }
/** * [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; }