1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GinoPane\BlogTaxonomy\Components; |
4
|
|
|
|
5
|
|
|
use Cms\Classes\Page; |
6
|
|
|
use Illuminate\Http\Response; |
7
|
|
|
use Rainlab\Blog\Models\Post; |
8
|
|
|
use Cms\Classes\ComponentBase; |
9
|
|
|
use Illuminate\Database\Eloquent\Collection; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PostListAbstract |
13
|
|
|
* |
14
|
|
|
* @package GinoPane\BlogTaxonomy\Components |
15
|
|
|
*/ |
16
|
|
|
abstract class PostListAbstract extends ComponentBase |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
use UrlHelperTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Collection | array |
22
|
|
|
*/ |
23
|
|
|
public $posts = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var integer The current page |
27
|
|
|
*/ |
28
|
|
|
public $currentPage; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var integer The number of results per page |
32
|
|
|
*/ |
33
|
|
|
public $resultsPerPage; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Message to display when there are no posts |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $noPostsMessage; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* If the post list should be ordered by another attribute |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
public $orderBy; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The attributes on which the post list can be ordered |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
public static $postAllowedSortingOptions = [ |
|
|
|
|
54
|
|
|
'title asc' => 'Title (ascending)', |
55
|
|
|
'title desc' => 'Title (descending)', |
56
|
|
|
'created_at asc' => 'Created (ascending)', |
57
|
|
|
'created_at desc' => 'Created (descending)', |
58
|
|
|
'updated_at asc' => 'Updated (ascending)', |
59
|
|
|
'updated_at desc' => 'Updated (descending)', |
60
|
|
|
'published_at asc' => 'Published (ascending)', |
61
|
|
|
'published_at desc' => 'Published (descending)', |
62
|
|
|
'random' => 'Random' |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Component Properties |
67
|
|
|
* @return array |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
public function defineProperties() |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
'noPostsMessage' => [ |
73
|
|
|
'title' => 'rainlab.blog::lang.settings.posts_no_posts', |
74
|
|
|
'description' => 'rainlab.blog::lang.settings.posts_no_posts_description', |
75
|
|
|
'type' => 'string', |
76
|
|
|
'showExternalParam' => false |
77
|
|
|
], |
78
|
|
|
'orderBy' => [ |
79
|
|
|
'title' => 'rainlab.blog::lang.settings.posts_order', |
80
|
|
|
'description' => 'rainlab.blog::lang.settings.posts_order_description', |
81
|
|
|
'type' => 'dropdown', |
82
|
|
|
'default' => 'published_at asc', |
83
|
|
|
'showExternalParam' => false |
84
|
|
|
], |
85
|
|
|
|
86
|
|
|
'page' => [ |
87
|
|
|
'title' => 'Page', |
88
|
|
|
'description' => 'The URL parameter defining the page number.', |
89
|
|
|
'default' => '{{ :page }}', |
90
|
|
|
'type' => 'string', |
91
|
|
|
'group' => 'Pagination', |
92
|
|
|
], |
93
|
|
|
'resultsPerPage' => [ |
94
|
|
|
'title' => 'Results', |
95
|
|
|
'description' => 'The number of posts to display per page.', |
96
|
|
|
'default' => 10, |
97
|
|
|
'type' => 'string', |
98
|
|
|
'validationPattern' => '^(0+)?[1-9]\d*$', |
99
|
|
|
'validationMessage' => 'Results per page must be a positive whole number.', |
100
|
|
|
'showExternalParam' => false, |
101
|
|
|
'group' => 'Pagination', |
102
|
|
|
], |
103
|
|
|
|
104
|
|
|
'postPage' => [ |
105
|
|
|
'title' => 'Post page', |
106
|
|
|
'description' => 'Page to show linked posts', |
107
|
|
|
'type' => 'dropdown', |
108
|
|
|
'default' => 'blog/post', |
109
|
|
|
'group' => 'Links', |
110
|
|
|
], |
111
|
|
|
'categoryPage' => [ |
112
|
|
|
'title' => 'rainlab.blog::lang.settings.posts_category', |
113
|
|
|
'description' => 'rainlab.blog::lang.settings.posts_category_description', |
114
|
|
|
'type' => 'dropdown', |
115
|
|
|
'default' => 'blog/category', |
116
|
|
|
'group' => 'Links', |
117
|
|
|
] |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @see Post::$allowedSortingOptions |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function getOrderByOptions() |
127
|
|
|
{ |
128
|
|
|
return static::$postAllowedSortingOptions; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Query the tag and posts belonging to it |
133
|
|
|
*/ |
134
|
|
|
public function onRun() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
if (is_null($this->prepareContextItem())) { |
137
|
|
|
return $this->controller->run(Response::HTTP_NOT_FOUND); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->prepareVars(); |
141
|
|
|
|
142
|
|
|
$this->listPosts(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Prepare variables |
147
|
|
|
*/ |
148
|
|
|
abstract protected function prepareContextItem(); |
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Prepare variables |
152
|
|
|
*/ |
153
|
|
|
protected function prepareVars() |
154
|
|
|
{ |
155
|
|
|
$this->currentPage = intval($this->property('page', 1)) ?: intval(post('page')); |
156
|
|
|
$this->resultsPerPage = intval($this->property('resultsPerPage')) |
157
|
|
|
?: $this->defineProperties()['resultsPerPage']['default']; |
158
|
|
|
|
159
|
|
|
$this->noPostsMessage = $this->page['noPostsMessage'] = $this->property('noPostsMessage'); |
160
|
|
|
$this->orderBy = $this->page['orderBy'] = $this->property('orderBy'); |
161
|
|
|
|
162
|
|
|
// Page links |
163
|
|
|
$this->postPage = $this->page['postPage' ] = $this->property('postPage'); |
164
|
|
|
$this->categoryPage = $this->page['categoryPage'] = $this->property('categoryPage'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Load a list of posts |
169
|
|
|
*/ |
170
|
|
|
public function listPosts() |
171
|
|
|
{ |
172
|
|
|
$query = $this->getPostsQuery(); |
173
|
|
|
|
174
|
|
|
if (in_array($this->orderBy, array_keys(self::$postAllowedSortingOptions))) { |
175
|
|
|
if ($this->orderBy == 'random') { |
176
|
|
|
$query->inRandomOrder(); |
177
|
|
|
} else { |
178
|
|
|
list($sortField, $sortDirection) = explode(' ', $this->orderBy); |
179
|
|
|
|
180
|
|
|
$query->orderBy($sortField, $sortDirection); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$posts = $query->paginate($this->resultsPerPage, $this->currentPage); |
185
|
|
|
|
186
|
|
|
// Add a "url" helper attribute for linking to each post and category |
187
|
|
|
if ($posts && $posts->count()) { |
188
|
|
|
$posts->each([$this, 'setPostUrls']); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->posts = $posts; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return mixed |
196
|
|
|
*/ |
197
|
|
|
abstract protected function getPostsQuery(); |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return mixed |
201
|
|
|
*/ |
202
|
|
|
public function getPostPageOptions() |
203
|
|
|
{ |
204
|
|
|
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return mixed |
209
|
|
|
*/ |
210
|
|
|
public function getCategoryPageOptions() |
211
|
|
|
{ |
212
|
|
|
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName'); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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.