public function run() { $cmslayouts = Yii::getAlias('@app/views/cmslayouts'); $layoutFiles = []; if (file_exists($cmslayouts)) { foreach (scandir($cmslayouts) as $file) { if ($file == '.' || $file == '..') { continue; } $layoutFiles[] = $file; $layoutItem = Layout::find()->where(['view_file' => $file])->one(); $content = file_get_contents($cmslayouts . DIRECTORY_SEPARATOR . $file); // find all twig brackets preg_match_all("/\\{\\{(.*?)\\}\\}/", $content, $results); // local vars $_placeholders = []; // explode the specific vars for each type foreach ($results[1] as $match) { $parts = explode('.', trim($match)); switch ($parts[0]) { case 'placeholders': if (!$this->verifyVariable($parts[1])) { throw new Exception("Wrong variable name detected '" . $parts[1] . "'. Special chars are not allowed in placeholder variables, allowed chars are a-zA-Z0-9"); } $_placeholders[] = ['label' => $parts[1], 'var' => $parts[1]]; break; } } $_placeholders = ['placeholders' => $_placeholders]; if ($layoutItem) { $match = $this->comparePlaceholders($_placeholders, json_decode($layoutItem->json_config, true)); if ($match) { $this->getImporter()->addLog('layouts', 'existing cmslayout ' . $file . ' does not have changed.'); continue; } $layoutItem->scenario = 'restupdate'; $layoutItem->setAttributes(['name' => ucfirst($file), 'view_file' => $file, 'json_config' => json_encode($_placeholders)]); $layoutItem->save(); $this->getImporter()->addLog('layouts', 'existing cmslayout ' . $file . ' updated'); } else { // add item into the database table $data = new Layout(); $data->scenario = 'restcreate'; $data->setAttributes(['name' => ucfirst($file), 'view_file' => $file, 'json_config' => json_encode($_placeholders)]); $data->save(); $this->getImporter()->addLog('layouts', 'new cmslayout ' . $file . ' found and added to databse.'); } } foreach (Layout::find()->where(['not in', 'view_file', $layoutFiles])->all() as $layoutItem) { $layoutItem->delete(); } } }
public function actionDataLayouts() { return ArrayHelper::typeCast(Layout::find()->asArray()->all()); }
/** * @todo do not only import, also update changes in the template * @todo how do we send back values into the executblae controller for output purposes? */ public function import(ExecutableController $exec) { $_log = ['blocks' => [], 'layouts' => []]; $allblocks = Block::find()->all(); $exists = []; foreach ($exec->getFilesNamespace('blocks') as $ns) { $model = Block::find()->where(['class' => $ns])->asArray()->one(); if (!$model) { $block = new Block(); $block->scenario = 'commandinsert'; $block->setAttributes(['group_id' => 1, 'system_block' => 0, 'class' => $ns]); $block->insert(); $_log['blocks'][$ns] = 'new block has been added to database.'; } else { $_log['blocks'][$ns] = 'already in the database.'; $exists[] = $model['id']; } } foreach ($allblocks as $block) { if (!in_array($block->id, $exists)) { $block->delete(); } } /* import project specific layouts */ $cmslayouts = Yii::getAlias('@app/views/cmslayouts'); $layoutFiles = []; if (file_exists($cmslayouts)) { foreach (scandir($cmslayouts) as $file) { if ($file == '.' || $file == '..') { continue; } $layoutFiles[] = $file; $layoutItem = Layout::find()->where(['view_file' => $file])->one(); $content = file_get_contents($cmslayouts . DIRECTORY_SEPARATOR . $file); // find all twig brackets preg_match_all("/\\{\\{(.*?)\\}\\}/", $content, $results); // local vars $_placeholders = []; $_vars = []; // explode the specific vars for each type foreach ($results[1] as $match) { $parts = explode('.', trim($match)); switch ($parts[0]) { case 'placeholders': $_placeholders[] = ['label' => $parts[1], 'var' => $parts[1]]; break; case 'vars': $_vars = $parts[1]; break; } } $_placeholders = ['placeholders' => $_placeholders]; if ($layoutItem) { $match = $this->comparePlaceholders($_placeholders, json_decode($layoutItem->json_config, true)); if ($match) { continue; } $layoutItem->scenario = 'restupdate'; $layoutItem->setAttributes(['name' => ucfirst($file), 'view_file' => $file, 'json_config' => json_encode($_placeholders)]); $layoutItem->save(); $_log['layouts'][$file] = "existing cmslayout {$file} updated."; } else { // add item into the database table $data = new Layout(); $data->scenario = 'restcreate'; $data->setAttributes(['name' => ucfirst($file), 'view_file' => $file, 'json_config' => json_encode($_placeholders)]); $data->save(); $_log['layouts'][$file] = "new cmslayout {$file} found and inserted."; } } foreach (Layout::find()->where(['not in', 'view_file', $layoutFiles])->all() as $layoutItem) { $layoutItem->delete(); } } return $_log; }