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 actionIndex($callback, $id) { $model = NavItemPageBlockItem::findOne($id); $block = Block::objectId($model->block_id, $model->id, 'callback'); $method = 'callback' . Inflector::id2camel($callback); return ObjectHelper::callMethodSanitizeArguments($block, $method, Yii::$app->request->get()); }
public function renderPlaceholder($navItemPageId, $placeholderVar, $prevId) { $string = ''; $placeholders = (new \yii\db\Query())->from('cms_nav_item_page_block_item t1')->select('t1.*')->where(['nav_item_page_id' => $navItemPageId, 'placeholder_var' => $placeholderVar, 'prev_id' => $prevId])->orderBy('sort_index ASC')->all(); $twig = Yii::$app->twig->env(new \Twig_Loader_String()); foreach ($placeholders as $key => $placeholder) { $blockObject = \cmsadmin\models\Block::objectId($placeholder['block_id'], 'frontend'); if ($blockObject === false) { continue; } $configValues = json_decode($placeholder['json_config_values'], true); $cfgValues = json_decode($placeholder['json_config_cfg_values'], true); if (empty($configValues)) { $configValues = []; } if (empty($cfgValues)) { $cfgValues = []; } foreach ($this->getOptions() as $optKey => $optValue) { $blockObject->setEnvOption($optKey, $optValue); } $blockObject->setVarValues($configValues); $blockObject->setCfgValues($cfgValues); $jsonConfig = json_decode($blockObject->jsonConfig(), true); $insertedHolders = []; if (isset($jsonConfig['placeholders'])) { foreach ($jsonConfig['placeholders'] as $item) { $insertedHolders[$item['var']] = $this->renderPlaceholder($navItemPageId, $item['var'], $placeholder['id']); } } $string .= $twig->render($blockObject->getTwigFrontendContent(), ['vars' => $configValues, 'cfgs' => $cfgValues, 'placeholders' => $insertedHolders, 'extras' => $blockObject->extraVars()]); } return $string; }
public function actionIndex($callback, $id) { $model = NavItemPageBlockItem::findOne($id); if (!$model) { throw new Exception("Unable to find item id."); } $block = Block::objectId($model->block_id, $model->id, 'callback'); if (!$block) { throw new Exception("Unable to find block object."); } return ObjectHelper::callMethodSanitizeArguments($block, 'callback' . Inflector::id2camel($callback), Yii::$app->request->get()); }
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; }
public function renderPlaceholder($navItemPageId, $placeholderVar, $prevId) { $string = ''; foreach ($this->getPlaceholders($navItemPageId, $placeholderVar, $prevId) as $key => $placeholder) { $cacheKey = NavItemPageBlockItem::cacheName($placeholder['id']); $blockResponse = $this->getHasCache($cacheKey); if ($blockResponse === false) { // create block object $blockObject = Block::objectId($placeholder['block_id'], $placeholder['id'], 'frontend', $this->getNavItem()); // see if its a valid block object if ($blockObject) { if (count($blockObject->assets) > 0) { $controllerObject = $this->getOption('cmsControllerObject'); if ($controllerObject) { foreach ($blockObject->assets as $assetClassName) { $controllerObject->registerAsset($assetClassName); } } } // insert var and cfg values from database $blockObject->setVarValues($this->jsonToArray($placeholder['json_config_values'])); $blockObject->setCfgValues($this->jsonToArray($placeholder['json_config_cfg_values'])); // set env options from current object environment foreach ($this->getOptions() as $optKey => $optValue) { $blockObject->setEnvOption($optKey, $optValue); } // render sub placeholders and set into object $insertedHolders = []; foreach ($blockObject->getPlaceholders() as $item) { $insertedHolders[$item['var']] = $this->renderPlaceholder($navItemPageId, $item['var'], $placeholder['id']); } $blockObject->setPlaceholderValues($insertedHolders); // output buffer the rendered frontend string based on the current twig env $blockResponse = $blockObject->renderFrontend($this->getTwig()); if ($blockObject->cacheEnabled) { $this->setHasCache($cacheKey, $blockResponse, $blockObject->cacheExpiration); //Yii::info($cacheKey . ": will be cached!"); } else { //Yii::info($cacheKey. ": will NOT be cached!"); } } } $string .= $blockResponse; } return $string; }
private function getBlock($blockId) { $blockItem = (new \yii\db\Query())->select('*')->from('cms_nav_item_page_block_item')->where(['id' => $blockId])->one(); $blockObject = \cmsadmin\models\Block::objectId($blockItem['block_id'], $blockItem['id'], 'admin', NavItem::findOne($blockItem['nav_item_page_id'])); if ($blockObject === false) { return false; } $blockItem['json_config_values'] = json_decode($blockItem['json_config_values'], true); $blockItem['json_config_cfg_values'] = json_decode($blockItem['json_config_cfg_values'], true); $blockValue = $blockItem['json_config_values']; $blockCfgValue = $blockItem['json_config_cfg_values']; $blockObject->setVarValues(empty($blockValue) ? [] : $blockValue); $blockObject->setCfgValues(empty($blockCfgValue) ? [] : $blockCfgValue); $placeholders = []; foreach ($blockObject->getPlaceholders() as $pk => $pv) { $pv['nav_item_page_id'] = $blockItem['nav_item_page_id']; $pv['prev_id'] = $blockItem['id']; $placeholderVar = $pv['var']; $pv['__nav_item_page_block_items'] = $this->getSub($placeholderVar, $blockItem['nav_item_page_id'], $blockItem['id']); $placeholder = $pv; $placeholders[] = $placeholder; } if (empty($blockItem['json_config_values'])) { $blockItem['json_config_values'] = new \stdClass(); } if (empty($blockItem['json_config_cfg_values'])) { $blockItem['json_config_cfg_values'] = new \stdClass(); } return ['is_dirty' => (int) $blockItem['is_dirty'], 'is_container' => (int) $blockObject->isContainer, 'id' => $blockItem['id'], 'is_hidden' => $blockItem['is_hidden'], 'name' => $blockObject->name(), 'icon' => $blockObject->icon(), 'full_name' => $blockObject->getFullName(), 'twig_admin' => $blockObject->twigAdmin(), 'vars' => $blockObject->getVars(), 'cfgs' => $blockObject->getCfgs(), 'extras' => $blockObject->extraVars(), 'values' => $blockItem['json_config_values'], 'field_help' => $blockObject->getFieldHelp(), 'cfgvalues' => $blockItem['json_config_cfg_values'], '__placeholders' => $placeholders]; }
public function getBlock() { return $this->hasOne(\cmsadmin\models\Block::className(), ['id' => 'block_id']); }
/** * @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; }