/**
  * After save update cache of blocks
  * @inheritdoc
  */
 public function afterSave($insert, $changedAttributes)
 {
     $machineName = 'lastBlogPosts';
     $cacheBlock = CacheBlock::find()->where(['machine_name' => $machineName])->one();
     if (!isset($cacheBlock)) {
         $cacheBlock = new CacheBlock();
         $cacheBlock->machine_name = $machineName;
     }
     $cacheBlock->content = SiteBlock::getBlogPosts(false);
     $cacheBlock->save();
     return parent::afterSave($insert, $changedAttributes);
 }
Example #2
0
 /**
  * Get block with top 6 news
  * @return array Data
  */
 public static function getTop6News($cache = true)
 {
     if ($cache) {
         $cacheBlock = CacheBlock::find()->where(['machine_name' => 'top6News'])->one();
         if (isset($cacheBlock)) {
             return ['view' => '@frontend/views/blocks/cache_block', 'data' => ['content' => $cacheBlock->content]];
         }
     }
     $postTable = Post::tableName();
     $assetTable = Asset::tableName();
     // TOP 6
     $query = Post::find()->innerJoin($assetTable, "{$assetTable}.assetable_id = {$postTable}.id")->where(['is_public' => 1, 'is_top' => 1, 'content_category_id' => Post::CATEGORY_NEWS, "{$assetTable}.assetable_type" => Asset::ASSETABLE_POST, "{$assetTable}.thumbnail" => Asset::THUMBNAIL_NEWS]);
     $top6News = $query->andWhere(['not in', "{$postTable}.id", self::$postExcludeIds])->orderBy(['created_at' => SORT_DESC])->limit(6)->all();
     foreach ($top6News as $post) {
         self::$postExcludeIds[] = $post->id;
     }
     $block = ['view' => '@frontend/views/blocks/main_news_block', 'data' => ['top6News' => $top6News]];
     if (!$cache) {
         $view = new \yii\base\View();
         return $view->renderFile($block['view'] . '.php', $block['data']);
     }
     return $block;
 }
Example #3
0
 /**
  * After save and before delete update cache of blocks
  */
 public function updateCacheBlocks($changedAttributes)
 {
     $newsPosts50 = Post::find()->where(['is_public' => 1, 'content_category_id' => Post::CATEGORY_NEWS])->orderBy(['created_at' => SORT_DESC])->limit(50)->all();
     $newsPosts20 = array_slice($newsPosts50, 0, 20);
     $cacheBlocksData = CacheBlock::find()->all();
     $cacheBlocks = [];
     foreach ($cacheBlocksData as $block) {
         $cacheBlocks[$block->machine_name] = $block;
     }
     $cacheStatus = false;
     if (isset($changedAttributes['content_category_id']) && $changedAttributes['content_category_id'] == self::CATEGORY_NEWS && $this->content_category_id != self::CATEGORY_NEWS || $this->content_category_id == self::CATEGORY_NEWS) {
         $machineName = 'shortNews50';
         $enableBanners = false;
         if (!isset($cacheBlocks[$machineName])) {
             $cacheBlock = new CacheBlock();
             $cacheBlock->machine_name = $machineName;
         } else {
             $cacheBlock = $cacheBlocks[$machineName];
         }
         $cacheBlock->content = SiteBlock::getShortNews(50, $enableBanners, $cacheStatus, $newsPosts50);
         $cacheBlock->save();
         $machineName = 'shortNews50banners';
         $enableBanners = true;
         SiteBlock::$postedBannerIds = [];
         if (!isset($cacheBlocks[$machineName])) {
             $cacheBlock = new CacheBlock();
             $cacheBlock->machine_name = $machineName;
         } else {
             $cacheBlock = $cacheBlocks[$machineName];
         }
         $cacheBlock->content = SiteBlock::getShortNews(50, $enableBanners, $cacheStatus, $newsPosts50);
         $cacheBlock->save();
     }
     if (isset($changedAttributes['is_index']) && $this->is_index != $changedAttributes['is_index'] || isset($changedAttributes['is_top']) && $this->is_top != $changedAttributes['is_top'] || $this->is_top || $this->is_index) {
         $machineName = 'top3News';
         if (!isset($cacheBlocks[$machineName])) {
             $cacheBlock = new CacheBlock();
             $cacheBlock->machine_name = $machineName;
         } else {
             $cacheBlock = $cacheBlocks[$machineName];
         }
         $cacheBlock->content = SiteBlock::getTop3News($cacheStatus);
         $cacheBlock->save();
         $machineName = 'top6News';
         if (!isset($cacheBlocks[$machineName])) {
             $cacheBlock = new CacheBlock();
             $cacheBlock->machine_name = $machineName;
         } else {
             $cacheBlock = $cacheBlocks[$machineName];
         }
         $cacheBlock->content = SiteBlock::getTop6News($cacheStatus);
         $cacheBlock->save();
     }
     if ($this->content_category_id == self::CATEGORY_BLOG) {
         $machineName = 'lastBlogPosts';
         if (!isset($cacheBlocks[$machineName])) {
             $cacheBlock = new CacheBlock();
             $cacheBlock->machine_name = $machineName;
         } else {
             $cacheBlock = $cacheBlocks[$machineName];
         }
         $cacheBlock->content = SiteBlock::getBlogPosts($cacheStatus);
         $cacheBlock->save();
         $machineName = 'blogPostsByRating';
         if (!isset($cacheBlocks[$machineName])) {
             $cacheBlock = new CacheBlock();
             $cacheBlock->machine_name = $machineName;
         } else {
             $cacheBlock = $cacheBlocks[$machineName];
         }
         $cacheBlock->content = SiteBlock::getBlogPostsByRating($cacheStatus);
         $cacheBlock->save();
     }
 }