public function run() { $allblocks = Block::find()->all(); $exists = []; foreach ($this->getImporter()->getDirectoryFiles('blocks') as $file) { $ns = $file['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(); $this->getImporter()->addLog('block', $ns . ' new block has been added to database.'); } else { $this->getImporter()->addLog('block', $ns . ' already in the database.'); $exists[] = $model['id']; } } foreach ($allblocks as $block) { if (!in_array($block->id, $exists)) { $this->getImporter()->addLog('block', 'block id ' . $block->id . ' removed from database.'); $block->delete(); } } }
public function actionDataBlocks() { $groups = []; foreach (BlockGroup::find()->asArray()->all() as $group) { $blocks = []; foreach (Block::find()->where(['group_id' => $group['id']])->all() as $block) { $obj = Block::objectId($block['id'], 0, 'admin'); if (!$obj) { continue; } $blocks[] = ['id' => $block['id'], 'name' => $obj->name(), 'full_name' => $obj->getFullName()]; } $groups[] = ['group' => $group, 'blocks' => $blocks]; } return $groups; }
/** * @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; }