/**
  * [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 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;
 }