|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GinoPane\BlogTaxonomy\Classes; |
|
4
|
|
|
|
|
5
|
|
|
use Cms\Classes\Page; |
|
6
|
|
|
use Illuminate\Http\Response; |
|
7
|
|
|
use Rainlab\Blog\Models\Post; |
|
8
|
|
|
use GinoPane\BlogTaxonomy\Plugin; |
|
9
|
|
|
use Illuminate\Support\Facades\Redirect; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class PostListAbstract |
|
14
|
|
|
* |
|
15
|
|
|
* @package GinoPane\BlogTaxonomy\Classes |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class PostListAbstract extends ComponentAbstract |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
use TranslateArrayTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Collection | array |
|
23
|
|
|
*/ |
|
24
|
|
|
public $posts = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var integer The current page |
|
28
|
|
|
*/ |
|
29
|
|
|
public $currentPage; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var integer The number of results per page |
|
33
|
|
|
*/ |
|
34
|
|
|
public $resultsPerPage; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* If the post list should be ordered by another attribute |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
public $orderBy; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The attributes on which the post list can be ordered |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
public static $postAllowedSortingOptions = [ |
|
48
|
|
|
'title asc' => Plugin::LOCALIZATION_KEY . 'order_options.title_asc', |
|
49
|
|
|
'title desc' => Plugin::LOCALIZATION_KEY . 'order_options.title_desc', |
|
50
|
|
|
'created_at asc' => Plugin::LOCALIZATION_KEY . 'order_options.created_at_asc', |
|
51
|
|
|
'created_at desc' => Plugin::LOCALIZATION_KEY . 'order_options.created_at_desc', |
|
52
|
|
|
'updated_at asc' => Plugin::LOCALIZATION_KEY . 'order_options.updated_at_asc', |
|
53
|
|
|
'updated_at desc' => Plugin::LOCALIZATION_KEY . 'order_options.updated_at_desc', |
|
54
|
|
|
'published_at asc' => Plugin::LOCALIZATION_KEY . 'order_options.published_at_asc', |
|
55
|
|
|
'published_at desc' => Plugin::LOCALIZATION_KEY . 'order_options.published_at_desc', |
|
56
|
|
|
'random' => Plugin::LOCALIZATION_KEY . 'order_options.random' |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Component Properties |
|
61
|
|
|
* @return array |
|
|
|
|
|
|
62
|
|
|
*/ |
|
63
|
|
|
public function defineProperties() |
|
64
|
|
|
{ |
|
65
|
|
|
return [ |
|
66
|
|
|
'orderBy' => [ |
|
67
|
|
|
'title' => 'rainlab.blog::lang.settings.posts_order', |
|
68
|
|
|
'description' => 'rainlab.blog::lang.settings.posts_order_description', |
|
69
|
|
|
'type' => 'dropdown', |
|
70
|
|
|
'default' => 'published_at asc', |
|
71
|
|
|
'showExternalParam' => false |
|
72
|
|
|
], |
|
73
|
|
|
|
|
74
|
|
|
'page' => [ |
|
75
|
|
|
'group' => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.pagination_group', |
|
76
|
|
|
'title' => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.page_parameter_title', |
|
77
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.page_parameter_description', |
|
78
|
|
|
'default' => '{{ :page }}', |
|
79
|
|
|
'type' => 'string', |
|
80
|
|
|
], |
|
81
|
|
|
'resultsPerPage' => [ |
|
82
|
|
|
'group' => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.pagination_group', |
|
83
|
|
|
'title' => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.pagination_per_page_title', |
|
84
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.pagination_per_page_description', |
|
85
|
|
|
'default' => 10, |
|
86
|
|
|
'type' => 'string', |
|
87
|
|
|
'validationPattern' => '^(0+)?[1-9]\d*$', |
|
88
|
|
|
'validationMessage' => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.pagination_validation_message', |
|
89
|
|
|
'showExternalParam' => false, |
|
90
|
|
|
], |
|
91
|
|
|
|
|
92
|
|
|
'postPage' => [ |
|
93
|
|
|
'group' => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.links_group', |
|
94
|
|
|
'title' => 'rainlab.blog::lang.settings.posts_post', |
|
95
|
|
|
'description' => 'rainlab.blog::lang.settings.posts_description', |
|
96
|
|
|
'type' => 'dropdown', |
|
97
|
|
|
'default' => 'blog/post', |
|
98
|
|
|
'showExternalParam' => false, |
|
99
|
|
|
], |
|
100
|
|
|
'categoryPage' => [ |
|
101
|
|
|
'group' => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.links_group', |
|
102
|
|
|
'title' => 'rainlab.blog::lang.settings.posts_category', |
|
103
|
|
|
'description' => 'rainlab.blog::lang.settings.posts_category_description', |
|
104
|
|
|
'type' => 'dropdown', |
|
105
|
|
|
'default' => 'blog/category', |
|
106
|
|
|
'showExternalParam' => false, |
|
107
|
|
|
] |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @see Post::$allowedSortingOptions |
|
113
|
|
|
* |
|
114
|
|
|
* @return string[] |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getOrderByOptions() |
|
117
|
|
|
{ |
|
118
|
|
|
$order = $this->translate(static::$postAllowedSortingOptions); |
|
119
|
|
|
|
|
120
|
|
|
asort($order); |
|
121
|
|
|
|
|
122
|
|
|
return $order; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Query the tag and posts belonging to it |
|
127
|
|
|
*/ |
|
128
|
|
|
public function onRun() |
|
|
|
|
|
|
129
|
|
|
{ |
|
130
|
|
|
if ($this->prepareContextItem() === null) { |
|
131
|
|
|
return Redirect::to($this->controller->pageUrl(Response::HTTP_NOT_FOUND)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->prepareVars(); |
|
135
|
|
|
|
|
136
|
|
|
$this->listPosts(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Prepare variables |
|
141
|
|
|
*/ |
|
142
|
|
|
abstract protected function prepareContextItem(); |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Prepare variables |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function prepareVars() |
|
148
|
|
|
{ |
|
149
|
|
|
// Paginator settings |
|
150
|
|
|
$this->currentPage = (int)$this->property('page', 1) ?: (int)post('page'); |
|
151
|
|
|
$this->resultsPerPage = (int)$this->property('resultsPerPage') |
|
152
|
|
|
?: $this->defineProperties()['resultsPerPage']['default']; |
|
153
|
|
|
|
|
154
|
|
|
$this->orderBy = $this->page['orderBy'] = $this->property('orderBy'); |
|
155
|
|
|
|
|
156
|
|
|
// Page links |
|
157
|
|
|
$this->postPage = $this->page['postPage' ] = $this->property('postPage'); |
|
158
|
|
|
$this->categoryPage = $this->page['categoryPage'] = $this->property('categoryPage'); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Load a list of posts |
|
163
|
|
|
*/ |
|
164
|
|
|
public function listPosts() |
|
165
|
|
|
{ |
|
166
|
|
|
$query = $this->getPostsQuery(); |
|
167
|
|
|
|
|
168
|
|
|
if (array_key_exists($this->orderBy, self::$postAllowedSortingOptions)) { |
|
169
|
|
|
if ($this->orderBy === 'random') { |
|
170
|
|
|
$query->inRandomOrder(); |
|
171
|
|
|
} else { |
|
172
|
|
|
list($sortField, $sortDirection) = explode(' ', $this->orderBy); |
|
173
|
|
|
|
|
174
|
|
|
$query->orderBy($sortField, $sortDirection); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$posts = $query->paginate($this->resultsPerPage, $this->currentPage); |
|
179
|
|
|
|
|
180
|
|
|
$this->setPostUrls($posts); |
|
181
|
|
|
|
|
182
|
|
|
$this->posts = $posts; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return mixed |
|
187
|
|
|
*/ |
|
188
|
|
|
abstract protected function getPostsQuery(); |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return mixed |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getPostPageOptions() |
|
194
|
|
|
{ |
|
195
|
|
|
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName'); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @return mixed |
|
200
|
|
|
*/ |
|
201
|
|
|
public function getCategoryPageOptions() |
|
202
|
|
|
{ |
|
203
|
|
|
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName'); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.