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.

99 lines
3.2 KiB

7 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()->pubDate()->typePublic()->count();
7 years ago
}
public function getAllByRange($offset, $limit): array
{
return BlogPost::find()->active()->pubDate()->typePublic()->orderBy(['id' => SORT_ASC])->limit($limit)->offset($offset)->all();
7 years ago
}
public function getAll(): DataProviderInterface
{
$query = BlogPost::find()->active()->pubDate()->typePublic()->with('category');
7 years ago
return $this->getProvider($query);
}
public function getAllByCategory(BlogCategory $category): DataProviderInterface
{
$query = BlogPost::find()->active()->pubDate()->typePublic()->andWhere(['category_id' => $category->id])->with('category');
7 years ago
return $this->getProvider($query);
}
public function findNext(int $id): ?BlogPost
{
return BlogPost::find()->active()->pubDate()->typePublic()->andWhere(['>', 'id', $id])->one();
7 years ago
}
public function findPrev(int $id): ?BlogPost
{
return BlogPost::find()->active()->pubDate()->typePublic()->andWhere(['<', 'id', $id])->orderBy(['id' => SORT_DESC])->one();
7 years ago
}
public function getAllByTag(BlogTag $tag): DataProviderInterface
{
$query = BlogPost::find()->alias('p')->active('p')->typePublic('p')->with('category');
7 years ago
$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')->typePublic('p')->with('category');
7 years ago
$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();
}
public function findPreview($id, $language): ?BlogPost
{
return BlogPost::find()->localized($language)->andWhere(['id' => $id])->one();
}
7 years ago
}