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\Series; |
8
|
|
|
use GinoPane\BlogTaxonomy\Classes\PostListAbstract; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class SeriesPosts |
12
|
|
|
* |
13
|
|
|
* @package GinoPane\BlogTaxonomy\Components |
14
|
|
|
*/ |
15
|
|
|
class SeriesPosts extends PostListAbstract |
16
|
|
|
{ |
17
|
|
|
// Param name to be used in URLs: ":series" |
18
|
|
|
const URL_PARAM_NAME = 'series'; |
19
|
|
|
|
20
|
|
|
const NAME = 'postsInSeries'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Series |
24
|
|
|
*/ |
25
|
|
|
public $series; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $includeTaggedPosts = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function componentDetails(): array |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
'name' => Plugin::LOCALIZATION_KEY . 'components.series_posts.name', |
39
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.series_posts.description' |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function defineProperties(): array |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
'series' => [ |
50
|
|
|
'title' => Plugin::LOCALIZATION_KEY . 'components.series_posts.series_title', |
51
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.series_posts.series_description', |
52
|
|
|
'type' => 'string', |
53
|
|
|
'default' => '{{ :series }}', |
54
|
|
|
], |
55
|
|
|
'includeTaggedPosts' => [ |
56
|
|
|
'title' => Plugin::LOCALIZATION_KEY . 'components.series_posts.include_tagged_posts_title', |
57
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.series_posts.include_tagged_posts_description', |
58
|
|
|
'default' => false, |
59
|
|
|
'type' => 'checkbox', |
60
|
|
|
'showExternalParam' => false |
61
|
|
|
] |
62
|
|
|
] + parent::defineProperties(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
protected function prepareVars() |
69
|
|
|
{ |
70
|
|
|
parent::prepareVars(); |
71
|
|
|
|
72
|
|
|
$this->includeTaggedPosts = $this->property('includeTaggedPosts', false); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
|
|
protected function prepareContextItem() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
// load series |
81
|
|
|
$this->series = Series::whereTranslatable('slug', $this->property('series'))->first(); |
82
|
|
|
|
83
|
|
|
return $this->series; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
protected function getPostsQuery() |
90
|
|
|
{ |
91
|
|
|
$query = Post::whereHas('series', function ($query) { |
92
|
|
|
$query->whereTranslatable('slug', $this->series->slug); |
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
$tagIds = $this->series->tags->lists('id'); |
96
|
|
|
|
97
|
|
|
if (!empty($tagIds) && $this->includeTaggedPosts) { |
98
|
|
|
$query->orWhereHas('tags', function ($tag) use ($tagIds) { |
99
|
|
|
$tag->whereIn('id', $tagIds); |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$query->isPublished(); |
104
|
|
|
|
105
|
|
|
return $query; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function setPostUrl(Post $post) |
109
|
|
|
{ |
110
|
|
|
$post->setUrl( |
111
|
|
|
$this->postPage, |
112
|
|
|
$this->controller, |
113
|
|
|
[ |
114
|
|
|
self::URL_PARAM_NAME => $this->series->slug |
115
|
|
|
] |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.