1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GinoPane\BlogTaxonomy\Components; |
4
|
|
|
|
5
|
|
|
use Rainlab\Blog\Models\Post; |
6
|
|
|
use GinoPane\BlogTaxonomy\Plugin; |
7
|
|
|
use GinoPane\BlogTaxonomy\Models\Tag; |
8
|
|
|
use GinoPane\BlogTaxonomy\Classes\PostListAbstract; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class TagPosts |
12
|
|
|
* |
13
|
|
|
* @package GinoPane\BlogTaxonomy\Components |
14
|
|
|
*/ |
15
|
|
|
class TagPosts extends PostListAbstract |
16
|
|
|
{ |
17
|
|
|
const NAME = 'postsWithTag'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Tag |
21
|
|
|
*/ |
22
|
|
|
public $tag; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
protected $includeSeriesPosts = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Component Registration |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function componentDetails(): array |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
'name' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.name', |
37
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.description' |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Component Properties |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function defineProperties(): array |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'tag' => [ |
49
|
|
|
'title' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.tag_title', |
50
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.tag_description', |
51
|
|
|
'default' => '{{ :tag }}', |
52
|
|
|
'type' => 'string' |
53
|
|
|
], |
54
|
|
|
'includeSeriesPosts' => [ |
55
|
|
|
'title' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.include_series_posts_title', |
56
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.include_series_posts_description', |
57
|
|
|
'default' => false, |
58
|
|
|
'type' => 'checkbox', |
59
|
|
|
'showExternalParam' => false |
60
|
|
|
] |
61
|
|
|
] + parent::defineProperties(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
|
|
protected function prepareVars() |
68
|
|
|
{ |
69
|
|
|
parent::prepareVars(); |
70
|
|
|
|
71
|
|
|
$this->includeSeriesPosts = $this->property('includeSeriesPosts', false); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
protected function prepareContextItem() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
// load tag |
80
|
|
|
$this->tag = $this->page['tag'] = Tag::whereTranslatable('slug', $this->property('tag'))->first(); |
81
|
|
|
|
82
|
|
|
return $this->tag; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
protected function getPostsQuery() |
89
|
|
|
{ |
90
|
|
|
$query = Post::whereHas('tags', function ($query) { |
91
|
|
|
$query->whereTranslatable('slug', $this->tag->slug); |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
if ($this->includeSeriesPosts) { |
95
|
|
|
$query->orWhereHas('series', function ($query) { |
96
|
|
|
$query->whereHas('tags', function ($query) { |
97
|
|
|
$query->whereTranslatable('slug', $this->tag->slug); |
98
|
|
|
}); |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$query->isPublished(); |
103
|
|
|
|
104
|
|
|
return $query; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.