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
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function componentDetails(): array |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
'name' => Plugin::LOCALIZATION_KEY . 'components.series_posts.name', |
34
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.series_posts.description' |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function defineProperties(): array |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
'series' => [ |
45
|
|
|
'title' => Plugin::LOCALIZATION_KEY . 'components.series_posts.series_title', |
46
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.series_posts.series_description', |
47
|
|
|
'type' => 'string', |
48
|
|
|
'default' => '{{ :series }}', |
49
|
|
|
], |
50
|
|
|
] + parent::defineProperties(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
protected function prepareContextItem() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
// load series |
59
|
|
|
$this->series = Series::whereTranslatable('slug', $this->property('series'))->first(); |
60
|
|
|
|
61
|
|
|
return $this->series; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
protected function getPostsQuery() |
68
|
|
|
{ |
69
|
|
|
$query = Post::whereHas('series', function ($query) { |
70
|
|
|
$query->whereTranslatable('slug', $this->series->slug); |
71
|
|
|
})->isPublished(); |
72
|
|
|
|
73
|
|
|
return $query; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function setPostUrl(Post $post) |
77
|
|
|
{ |
78
|
|
|
$post->setUrl( |
79
|
|
|
$this->postPage, |
80
|
|
|
$this->controller, |
81
|
|
|
[ |
82
|
|
|
self::URL_PARAM_NAME => $this->series->slug |
83
|
|
|
] |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.