You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.8 KiB
94 lines
2.8 KiB
6 years ago
|
<?php
|
||
|
|
||
|
namespace common\modules\blog\repositories\read;
|
||
|
|
||
|
use common\modules\blog\entities\BlogCategory;
|
||
|
use common\modules\blog\entities\BlogPost;
|
||
|
use common\modules\blog\entities\BlogTag;
|
||
|
use yii\data\ActiveDataProvider;
|
||
|
use yii\data\DataProviderInterface;
|
||
|
use yii\db\ActiveQuery;
|
||
|
|
||
|
class BlogPostReadRepository
|
||
|
{
|
||
|
public function count(): int
|
||
|
{
|
||
|
return BlogPost::find()->active()->count();
|
||
|
}
|
||
|
|
||
|
public function getAllByRange($offset, $limit): array
|
||
|
{
|
||
|
return BlogPost::find()->active()->orderBy(['id' => SORT_ASC])->limit($limit)->offset($offset)->all();
|
||
|
}
|
||
|
|
||
|
public function getAll(): DataProviderInterface
|
||
|
{
|
||
|
$query = BlogPost::find()->active()->with('category');
|
||
|
return $this->getProvider($query);
|
||
|
}
|
||
|
|
||
|
public function getAllByCategory(BlogCategory $category): DataProviderInterface
|
||
|
{
|
||
|
$query = BlogPost::find()->active()->andWhere(['category_id' => $category->id])->with('category');
|
||
|
return $this->getProvider($query);
|
||
|
}
|
||
|
|
||
|
public function findNext(int $id): ?BlogPost
|
||
|
{
|
||
|
return BlogPost::find()->active()->andWhere(['>', 'id', $id])->one();
|
||
|
}
|
||
|
|
||
|
public function findPrev(int $id): ?BlogPost
|
||
|
{
|
||
|
return BlogPost::find()->active()->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();
|
||
|
}
|
||
|
}
|