/** * Handle decoration * @param \yii\base\Controller $controller * @param string $viewFile * @param array $params * @return void */ public function decorate($controller, $viewFile, $params) { if (isset($controller->view->blocks['content'])) { $dependency = new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(app\modules\core\models\ContentBlock::className()), ActiveRecordHelper::getCommonTag(get_class($params['model']))]]); $controller->view->blocks['content'] = ContentBlockHelper::compileContentString($controller->view->blocks['content'], get_class($params['model']) . ':' . $params['model']->id, $dependency); } }
/** * Handle decoration * @param \yii\base\Controller $controller * @param string $viewFile * @param array $params * @return void */ public function decorate($controller, $viewFile, $params) { if (!Yii::$app->getModule("backend")->isBackend()) { $baseContentKey = get_class($params['model']) . ':' . (isset($params['model']->id) ? $params['model']->id : '') . ':'; $view = $controller->view; $dependency = new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(app\modules\core\models\ContentBlock::className()), ActiveRecordHelper::getCommonTag(get_class($params['model']))]]); $view->title = $this->processChunks($view->title, $baseContentKey . "title", $dependency); if (!empty($view->blocks["content"])) { $view->blocks["content"] = $this->processChunks($view->blocks["content"], $baseContentKey . "content", $dependency); } if (!empty($view->blocks["announce"])) { $view->blocks["announce"] = $this->processChunks($view->blocks["announce"], $baseContentKey . "announce", $dependency); } } }
/** * Product page view * * @param null $model_id * @return string * @throws NotFoundHttpException * @throws ServerErrorHttpException */ public function actionShow($model_id = null) { if (null === ($object = Object::getForClass(Product::className()))) { throw new ServerErrorHttpException('Object not found.'); } $cacheKey = 'Product:' . $model_id; if (false === ($product = Yii::$app->cache->get($cacheKey))) { if (null === ($product = Product::findById($model_id))) { throw new NotFoundHttpException(); } Yii::$app->cache->set($cacheKey, $product, 86400, new TagDependency(['tags' => [ActiveRecordHelper::getObjectTag(Product::className(), $model_id)]])); } $request = Yii::$app->request; $values_by_property_id = $request->get('properties', []); if (!is_array($values_by_property_id)) { $values_by_property_id = [$values_by_property_id]; } $selected_category_id = $request->get('last_category_id'); $selected_category_ids = $request->get('categories', []); if (!is_array($selected_category_ids)) { $selected_category_ids = [$selected_category_ids]; } $category_group_id = intval($request->get('category_group_id', 0)); // trigger that we are to show product to user! // wow! such product! very events! $specialEvent = new ProductPageShowed(['product_id' => $product->id]); EventTriggeringHelper::triggerSpecialEvent($specialEvent); if (!empty($product->meta_description)) { $this->view->registerMetaTag(['name' => 'description', 'content' => ContentBlockHelper::compileContentString($product->meta_description, Product::className() . ":{$product->id}:meta_description", new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(ContentBlock::className()), ActiveRecordHelper::getCommonTag(Product::className())]]))], 'meta_description'); } $selected_category = $selected_category_id > 0 ? Category::findById($selected_category_id) : null; $this->view->title = $product->title; $this->view->blocks['h1'] = $product->h1; $this->view->blocks['announce'] = $product->announce; $this->view->blocks['content'] = $product->content; $this->view->blocks['title'] = $product->title; return $this->render($this->computeViewFile($product, 'show'), ['model' => $product, 'category_group_id' => $category_group_id, 'values_by_property_id' => $values_by_property_id, 'selected_category_id' => $selected_category_id, 'selected_category' => $selected_category, 'selected_category_ids' => $selected_category_ids, 'object' => $object, 'breadcrumbs' => $this->buildBreadcrumbsArray($selected_category, $product)]); }
/** * preloads chunks with option preload = 1 * and push it to static array * @return array|void */ public static function preloadChunks() { if (is_null(static::$chunksByKey)) { $dependency = new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(ContentBlock::className())]]); static::$chunksByKey = ContentBlock::getDb()->cache(function ($db) { $chunks = ContentBlock::find()->where(['preload' => 1])->asArray()->all(); return ArrayHelper::map($chunks, 'key', 'value'); }, 86400, $dependency); } return static::$chunksByKey; }
/** * Get property values by key. * @param $key * @param bool $asString * @param string $delimiter * @return array|string */ public function property($key, $asString = true, $delimiter = ', ') { $values = []; $propertyValue = $this->getPropertyValuesByKey($key); if (is_null($propertyValue)) { return $asString ? '' : []; } foreach ($propertyValue->values as $value) { $values[] = $value['value']; } if ($asString) { return ContentBlockHelper::compileContentString(implode($delimiter, $values), "{$this->owner->className()}:{$this->owner->id}:property={$key}", new TagDependency(['tags' => [$this->owner->objectTag(), ActiveRecordHelper::getCommonTag(ContentBlock::className())]])); } else { return $values; } }
/** * renders chunks in template files * @param string $key ContentBlock key * @param array $params . Array of params to be replaced while render * @param yii\base\Model $model . Caller model instance to use in caching * @return mixed */ public static function getChunk($key, $params = [], yii\base\Model $model = null) { if (null === ($rawChunk = self::fetchChunkByKey($key))) { return ''; } $tags = [ActiveRecordHelper::getCommonTag(app\modules\core\models\ContentBlock::className())]; $content_key = 'templateChunkRender' . $key; if (null !== $model) { $content_key .= $model->id; $tags[] = ActiveRecordHelper::getObjectTag(get_class($model), $model->id); } $dependency = new TagDependency(['tags' => $tags]); if (false === empty($params)) { $rawChunk = self::compileChunk($rawChunk, $params, $key, $content_key, $dependency); } return self::compileContentString($rawChunk, $content_key, $dependency); }
/** * @param Page $model */ private function registerMetaDescription($model) { if (!empty($model->meta_description)) { $this->view->registerMetaTag(['name' => 'description', 'content' => ContentBlockHelper::compileContentString($model->meta_description, Page::className() . ":{$model->id}:meta_description", new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(ContentBlock::className()), ActiveRecordHelper::getCommonTag(Page::className())]]))], 'meta_description'); } }
/** * @return ContentBlock[] */ public function getContentBlocks() { return $this->hasMany(ContentBlock::className(), ['group_id' => 'id']); }