Esempio n. 1
0
 /**
  * @inheritdoc
  */
 public function attributeLabels()
 {
     $attributeLabels = parent::attributeLabels();
     $attributeLabels['cms_page_i18n_title'] = Yii::t('kalibao', 'model:title');
     $attributeLabels['cms_layout_i18n_name'] = Yii::t('kalibao', 'cms_page:cms_layout_id');
     $attributeLabels['cms_page_i18n_slug'] = Yii::t('kalibao', 'cms_page_i18n:slug');
     $attributeLabels['cms_page_i18n_html_title'] = Yii::t('kalibao', 'cms_page_i18n:html_title');
     $attributeLabels['cms_page_i18n_html_description'] = Yii::t('kalibao', 'cms_page_i18n:html_description');
     $attributeLabels['cms_page_i18n_html_keywords'] = Yii::t('kalibao', 'cms_page_i18n:html_keywords');
     return $attributeLabels;
 }
 /**
  * Find page
  * @throws HttpException
  */
 public function beforeAction()
 {
     // request
     $request = Yii::$app->request;
     if ($slugParam = $request->get($this->slugParam, false)) {
         // controller
         $controller = Yii::$app->controller;
         // view
         $view = $controller->view;
         // cache key
         $cacheKey = 'cms_page_slug:' . $slugParam . ':' . Yii::$app->language;
         // get from cache
         if (($dataPage = Yii::$app->commonCache->get($cacheKey)) === false) {
             $cmsPage = CmsPage::find()->innerJoinWith(['cmsPageI18ns' => function ($query) use($slugParam) {
                 $query->where(['slug' => $slugParam, 'i18n_id' => Yii::$app->language]);
             }, 'cmsLayout'])->where(['activated' => 1])->asArray()->one();
             if ($cmsPage !== null && isset($cmsPage['cmsPageI18ns'][0])) {
                 $cmsPageContents = CmsPageContent::find()->innerJoinWith(['cmsPageContentI18ns' => function ($query) {
                     $query->where(['i18n_id' => Yii::$app->language]);
                 }])->where(['cms_page_id' => $cmsPage['id']])->orderBy(['index' => SORT_ASC])->asArray()->all();
                 Yii::$app->commonCache->set($cacheKey, ['cmsPage' => $cmsPage, 'cmsPageContents' => $cmsPageContents], $cmsPage['cache_duration'], new TagDependency(['tags' => [CmsPageComponent::getCacheTag(), CmsPageComponent::getCacheTag($cmsPage['id']), CmsPageComponent::getCacheTag($cmsPage['id'], $cmsPage['cmsPageI18ns'][0]['slug'], Yii::$app->language)]]));
             }
         } else {
             $cmsPage = $dataPage['cmsPage'];
             $cmsPageContents = $dataPage['cmsPageContents'];
         }
         if (!empty($cmsPage) && !empty($cmsPageContents)) {
             $view->title = $cmsPage['cmsPageI18ns'][0]['html_title'];
             if ($cmsPage['cmsPageI18ns'][0]['html_description'] != '') {
                 $view->registerMetaTag(['name' => 'description', 'content' => $cmsPage['cmsPageI18ns'][0]['html_description']]);
             }
             if ($cmsPage['cmsPageI18ns'][0]['html_keywords'] != '') {
                 $view->registerMetaTag(['name' => 'keywords', 'content' => $cmsPage['cmsPageI18ns'][0]['html_keywords']]);
             }
             $controller->layout = $cmsPage['cmsLayout']['path'];
             $this->renderView = $cmsPage['cmsLayout']['view'];
             $this->parseCmsPageContents($cmsPageContents);
             foreach ($cmsPageContents as &$cmsPageContent) {
                 if (isset($cmsPageContent['cmsPageContentI18ns'][0])) {
                     $view->blocks['cms_block_page_' . $cmsPageContent['index']] = $cmsPageContent['cmsPageContentI18ns'][0]['content'];
                 }
             }
         } else {
             throw new HttpException(404);
         }
     } else {
         throw new HttpException(404);
     }
 }
Esempio n. 3
0
 /**
  * Get slug by ID
  * @param int $id Cms page ID
  * @param string $language Language ID
  * @return bool
  */
 public static function getSlugById($id, $language)
 {
     // cache key
     $cacheKey = 'cms_page_id:' . $id . ':' . $language;
     // get from cache
     if (($slug = Yii::$app->commonCache->get($cacheKey)) === false) {
         $cmsPage = CmsPageModel::find()->select(['id', 'cache_duration'])->innerJoinWith(['cmsPageI18ns' => function ($query) use($language) {
             $query->select(['cms_page_id', 'slug'])->where(['i18n_id' => $language]);
         }])->where(['id' => $id, 'activated' => 1])->asArray()->one();
         if ($cmsPage !== null && isset($cmsPage['cmsPageI18ns'][0])) {
             $slug = $cmsPage['cmsPageI18ns'][0]['slug'];
             // set in cache
             Yii::$app->commonCache->set($cacheKey, $slug, $cmsPage['cache_duration'], new TagDependency(['tags' => [self::getCacheTag(), self::getCacheTag($cmsPage['id']), self::getCacheTag($cmsPage['id'], $cmsPage['cmsPageI18ns'][0]['slug'], Yii::$app->language)]]));
         } else {
             return false;
         }
     }
     return $slug;
 }
Esempio n. 4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCmsPage()
 {
     return $this->hasOne(CmsPage::className(), ['id' => 'cms_page_id']);
 }
Esempio n. 5
0
 /**
  * Load page contents translate models
  * @param array $languages Languages
  * @param array $primaryKey Primary key
  * @param array $models
  * @return bool|integer
  * @throws Exception
  */
 protected function loadPageContentsTranslateModels($languages, $primaryKey, array &$models)
 {
     $pageContentNumber = false;
     if (($cmsPage = CmsPage::find()->select([key($primaryKey), 'cms_layout_id'])->where($primaryKey)->one()) !== null) {
         $cmsLayout = CmsLayout::find()->select('max_container')->where(['id' => $cmsPage->cms_layout_id])->one();
         if ($cmsLayout !== null) {
             $pageContentNumber = $cmsLayout->max_container;
             for ($i = 1; $i <= $pageContentNumber; ++$i) {
                 foreach ($languages as $language) {
                     $models[$language]['CmsPageContentI18n'][$i] = new CmsPageContentI18n(['scenario' => 'insert']);
                     $models[$language]['CmsPageContentI18n'][$i]->i18n_id = $language;
                 }
             }
             if ($cmsPage->id !== null) {
                 $results = CmsPageContent::find()->joinWith(['cmsPageContentI18ns' => function ($query) use($languages) {
                     $query->onCondition(['cms_page_content_i18n.i18n_id' => $languages]);
                 }])->where(['cms_page_id' => $cmsPage->id])->indexBy('index')->all();
                 foreach ($models as $language => $data) {
                     foreach ($data['CmsPageContentI18n'] as $index => $model) {
                         if (isset($results[$index])) {
                             $models[$language]['CmsPageContentI18n'][$index]->cms_page_content_id = $results[$index]->id;
                             foreach ($results[$index]->cmsPageContentI18ns as $cmsPageContentI18n) {
                                 $models[$cmsPageContentI18n->i18n_id]['CmsPageContentI18n'][$index] = $cmsPageContentI18n;
                                 $models[$cmsPageContentI18n->i18n_id]['CmsPageContentI18n'][$index]->scenario = 'update';
                             }
                         }
                     }
                 }
             }
         } else {
             throw new Exception('No layout');
         }
     }
     return $pageContentNumber;
 }