/** * Url: /blogs * @param bool|int $id int User id * @return mixed */ public function actionBlogs($id = false) { $query = Post::find()->where(['is_public' => 1, 'content_category_id' => Post::CATEGORY_BLOG]); if ($id) { $query->andWhere(['user_id' => $id]); } $query->orderBy(['created_at' => SORT_DESC]); $postsDataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]); return $this->render('@frontend/views/site/index', ['templateType' => 'col2', 'title' => 'Dynamomania.com | Блоги', 'columnFirst' => ['user_comments' => SiteBlock::getUserComments($id), 'content' => ['view' => '@frontend/views/site/blogs', 'data' => compact('postsDataProvider')]], 'columnSecond' => ['best-blogs' => SiteBlock::getBlogPostsByRating(), 'short-news' => SiteBlock::getShortNews()]]); }
/** * 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(); } }