|
1
|
|
|
<?php namespace VojtaSvoboda\Brands\Components; |
|
2
|
|
|
|
|
3
|
|
|
use Cms\Classes\ComponentBase; |
|
4
|
|
|
use Cms\Classes\Page; |
|
5
|
|
|
use Request; |
|
6
|
|
|
use VojtaSvoboda\Brands\Models\Brand; |
|
|
|
|
|
|
7
|
|
|
use VojtaSvoboda\Brands\Models\Category; |
|
8
|
|
|
|
|
9
|
|
|
class Brands extends ComponentBase |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var \Illuminate\Pagination\LengthAwarePaginator $brands A collection of items to display. */ |
|
12
|
|
|
public $brands; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string $pagePath Full page URL. */ |
|
15
|
|
|
public $pagePath; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Category $category */ |
|
18
|
|
|
public $category; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string $letter */ |
|
21
|
|
|
public $letter; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string $detailPage Reference to the page name for linking to brand detail. */ |
|
24
|
|
|
public $brandPage; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string $categoryPage Reference to the page name for linking to categories. */ |
|
27
|
|
|
public $categoryPage; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string $logoWidth */ |
|
30
|
|
|
public $logoWidth; |
|
31
|
|
|
|
|
32
|
|
|
/** @var string $logoHeight */ |
|
33
|
|
|
public $logoHeight; |
|
34
|
|
|
|
|
35
|
|
|
/** @var int $columnSize */ |
|
36
|
|
|
public $columnSize; |
|
37
|
|
|
|
|
38
|
|
|
public function componentDetails() |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
'name' => 'Brands', |
|
42
|
|
|
'description' => 'Show all brands paginated', |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function defineProperties() |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
49
|
|
|
'brandPage' => [ |
|
50
|
|
|
'title' => 'Brand page', |
|
51
|
|
|
'description' => 'Page for showing brand detail', |
|
52
|
|
|
'type' => 'dropdown', |
|
53
|
|
|
'default' => 'brand-detail', |
|
54
|
|
|
], |
|
55
|
|
|
'categoryPage' => [ |
|
56
|
|
|
'title' => 'Category page', |
|
57
|
|
|
'description' => 'Page for showing brand category', |
|
58
|
|
|
'type' => 'dropdown', |
|
59
|
|
|
'default' => 'brands', |
|
60
|
|
|
], |
|
61
|
|
|
'categoryFilter' => [ |
|
62
|
|
|
'title' => 'Category slug', |
|
63
|
|
|
'description' => 'Show only brands from some category', |
|
64
|
|
|
'type' => 'string', |
|
65
|
|
|
'default' => '{{ :category }}', |
|
66
|
|
|
'group' => 'Category', |
|
67
|
|
|
], |
|
68
|
|
|
'letterFilter' => [ |
|
69
|
|
|
'title' => 'Starts with letter', |
|
70
|
|
|
'description' => 'Show only brands starts with this letter', |
|
71
|
|
|
'type' => 'string', |
|
72
|
|
|
'default' => '{{ :letter }}', |
|
73
|
|
|
'group' => 'Letter', |
|
74
|
|
|
], |
|
75
|
|
|
'perPage' => [ |
|
76
|
|
|
'title' => 'Brands per page', |
|
77
|
|
|
'description' => 'How many brands show at one page', |
|
78
|
|
|
'type' => 'string', |
|
79
|
|
|
'validationPattern' => '^[\d]+$', |
|
80
|
|
|
'validationMessage' => 'Per page should be numeric value', |
|
81
|
|
|
'default' => 24, |
|
82
|
|
|
'group' => 'Paginator', |
|
83
|
|
|
], |
|
84
|
|
|
'sortOrder' => [ |
|
85
|
|
|
'title' => 'Sort order', |
|
86
|
|
|
'description' => 'If brands will be rendered ascendent or descendent', |
|
87
|
|
|
'type' => 'dropdown', |
|
88
|
|
|
'default' => 'ASC', |
|
89
|
|
|
'group' => 'Paginator', |
|
90
|
|
|
], |
|
91
|
|
|
'pageNumber' => [ |
|
92
|
|
|
'title' => 'Page number', |
|
93
|
|
|
'description' => 'Which page should be displayed', |
|
94
|
|
|
'type' => 'string', |
|
95
|
|
|
'default' => '{{ page }}', |
|
96
|
|
|
'group' => 'Paginator', |
|
97
|
|
|
], |
|
98
|
|
|
'perRow' => [ |
|
99
|
|
|
'title' => 'Brands per row (1-12)', |
|
100
|
|
|
'description' => 'How many brands show in one row', |
|
101
|
|
|
'type' => 'string', |
|
102
|
|
|
'validationPattern' => '^[\d]+$', |
|
103
|
|
|
'validationMessage' => 'Per row should be numeric value between 1 and 12', |
|
104
|
|
|
'default' => 6, |
|
105
|
|
|
'group' => 'Layout', |
|
106
|
|
|
], |
|
107
|
|
|
'logoWidth' => [ |
|
108
|
|
|
'title' => 'Logo width', |
|
109
|
|
|
'description' => 'Width of the logo in pixels', |
|
110
|
|
|
'type' => 'string', |
|
111
|
|
|
'validationPattern' => '^[\d]+$', |
|
112
|
|
|
'validationMessage' => 'Logo width should be numeric value', |
|
113
|
|
|
'default' => 300, |
|
114
|
|
|
'group' => 'Layout', |
|
115
|
|
|
], |
|
116
|
|
|
'logoHeight' => [ |
|
117
|
|
|
'title' => 'Logo height', |
|
118
|
|
|
'description' => 'Height of the logo in pixels', |
|
119
|
|
|
'type' => 'string', |
|
120
|
|
|
'validationPattern' => '^[\d]+$', |
|
121
|
|
|
'validationMessage' => 'Logo height should be numeric value', |
|
122
|
|
|
'default' => 300, |
|
123
|
|
|
'group' => 'Layout', |
|
124
|
|
|
], |
|
125
|
|
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function onRun() |
|
129
|
|
|
{ |
|
130
|
|
|
// category filter |
|
131
|
|
|
if ($category = $this->property('categoryFilter')) { |
|
132
|
|
|
$this->category = $this->getCategory($category); |
|
133
|
|
|
} |
|
134
|
|
|
$this->page['category'] = $this->category; |
|
135
|
|
|
|
|
136
|
|
|
// letter filter |
|
137
|
|
|
$this->page['letter'] = $this->letter = $this->property('letterFilter'); |
|
138
|
|
|
|
|
139
|
|
|
// page links |
|
140
|
|
|
$this->brandPage = $this->page['brandPage'] = $this->property('brandPage'); |
|
141
|
|
|
$this->categoryPage = $this->page['categoryPage'] = $this->property('categoryPage'); |
|
142
|
|
|
|
|
143
|
|
|
// brands vars |
|
144
|
|
|
$this->brands = $this->page['brands'] = $this->listItems(); |
|
|
|
|
|
|
145
|
|
|
$this->pagePath = $this->page['pagePath'] = Request::path(); |
|
146
|
|
|
$this->columnSize = 12 / $this->property('perRow'); |
|
|
|
|
|
|
147
|
|
|
$this->logoWidth = $this->property('logoWidth'); |
|
148
|
|
|
$this->logoHeight = $this->property('logoHeight'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get all brands with pagination. |
|
153
|
|
|
* |
|
154
|
|
|
* @return mixed |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function listItems() |
|
157
|
|
|
{ |
|
158
|
|
|
$parameters = [ |
|
159
|
|
|
'page' => $this->property('pageNumber'), |
|
160
|
|
|
'perPage' => $this->property('perPage'), |
|
161
|
|
|
'sortOrder' => $this->property('sortOrder'), |
|
162
|
|
|
]; |
|
163
|
|
|
|
|
164
|
|
|
if ($this->category) { |
|
165
|
|
|
$parameters['category'] = $this->category; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if ($this->letter) { |
|
169
|
|
|
$parameters['letter'] = $this->letter; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$items = Brand::listFrontEnd($parameters); |
|
173
|
|
|
|
|
174
|
|
|
return $this->addLinksTo($items); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Add links to brands. |
|
179
|
|
|
* |
|
180
|
|
|
* @param $items |
|
181
|
|
|
* |
|
182
|
|
|
* @return array |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function addLinksTo($items) |
|
185
|
|
|
{ |
|
186
|
|
|
$detailPage = $this->brandPage; |
|
187
|
|
|
$categoryPage = $this->categoryPage; |
|
188
|
|
|
|
|
189
|
|
|
$items->each(function ($item) use ($detailPage, $categoryPage) |
|
190
|
|
|
{ |
|
191
|
|
|
if ($item->no_link) { |
|
192
|
|
|
$item->url = null; |
|
193
|
|
|
} elseif ($item->external_link) { |
|
194
|
|
|
$item->url = $item->external_link; |
|
195
|
|
|
} else { |
|
196
|
|
|
$item->url = $this->controller->pageUrl($detailPage, [ |
|
197
|
|
|
'slug' => $item->slug, |
|
198
|
|
|
]); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$item->categories->each(function($category) use ($categoryPage) { |
|
202
|
|
|
$category->url = $this->controller->pageUrl($categoryPage, [ |
|
203
|
|
|
'category' => $category->slug, |
|
204
|
|
|
]); |
|
205
|
|
|
}); |
|
206
|
|
|
}); |
|
207
|
|
|
|
|
208
|
|
|
return $items; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Get category by slug. |
|
213
|
|
|
* |
|
214
|
|
|
* @param $category |
|
215
|
|
|
* |
|
216
|
|
|
* @return mixed |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getCategory($category) |
|
219
|
|
|
{ |
|
220
|
|
|
return Category::where('slug', $category)->first(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Get options for the dropdown where the link to the brand page can be selected. |
|
225
|
|
|
* |
|
226
|
|
|
* @return array |
|
227
|
|
|
*/ |
|
228
|
|
|
public function getBrandPageOptions() |
|
229
|
|
|
{ |
|
230
|
|
|
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName'); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Get options for the dropdown whre the link to the category page can be selected. |
|
235
|
|
|
* |
|
236
|
|
|
* @return mixed |
|
237
|
|
|
*/ |
|
238
|
|
|
public function getCategoryPageOptions() |
|
239
|
|
|
{ |
|
240
|
|
|
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName'); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Get options for the dropdown selecting brand's list ordering method. |
|
245
|
|
|
* |
|
246
|
|
|
* @return array |
|
247
|
|
|
*/ |
|
248
|
|
|
public function getSortOrderOptions() |
|
249
|
|
|
{ |
|
250
|
|
|
return [ |
|
251
|
|
|
'ASC' => 'Ascending', |
|
252
|
|
|
'DESC' => 'Descending', |
|
253
|
|
|
]; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: