active()->pubDate()->count(); } public function getAllByRange($offset, $limit): array { return BlogPost::find()->active()->pubDate()->orderBy(['id' => SORT_ASC])->limit($limit)->offset($offset)->all(); } public function getAll(): DataProviderInterface { $query = BlogPost::find()->active()->pubDate()->with('category'); return $this->getProvider($query); } public function getAllByCategory(BlogCategory $category): DataProviderInterface { $query = BlogPost::find()->active()->pubDate()->andWhere(['category_id' => $category->id])->with('category'); return $this->getProvider($query); } public function findNext(int $id): ?BlogPost { return BlogPost::find()->active()->pubDate()->andWhere(['>', 'id', $id])->one(); } public function findPrev(int $id): ?BlogPost { return BlogPost::find()->active()->pubDate()->andWhere(['<', 'id', $id])->orderBy(['id' => SORT_DESC])->one(); } public function getAllByTag(BlogTag $tag): DataProviderInterface { $query = BlogPost::find()->alias('p')->active('p')->with('category'); $query->joinWith(['blogTagAssignments ta'], false); $query->andWhere(['ta.tag_id' => $tag->id]); $query->groupBy('p.id'); return $this->getProvider($query); } public function getByTagsId(BlogPost $post, array $tag_ids, int $limit = 15): DataProviderInterface { $query = BlogPost::find()->alias('p')->active('p')->with('category'); $query->joinWith(['blogTagAssignments ta'], false); $query->andWhere(['ta.tag_id' => $tag_ids]); $query->andWhere(['!=', 'p.id', $post->id]); $query->groupBy('p.id'); $query->limit($limit); return $this->getProvider($query); } public function getLast($limit): array { return BlogPost::find()->with('category')->orderBy(['id' => SORT_DESC])->limit($limit)->all(); } public function getPopular($limit): array { return BlogPost::find()->with('category')->orderBy(['comments_count' => SORT_DESC])->limit($limit)->all(); } public function find($id): ?BlogPost { return BlogPost::find()->active()->andWhere(['id' => $id])->one(); } private function getProvider(ActiveQuery $query): ActiveDataProvider { return new ActiveDataProvider([ 'query' => $query, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]], ]); } public function findBySlug($slug): ?BlogPost { return BlogPost::find()->andWhere(['slug' => $slug])->one(); } }