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]; }