1 | <?php namespace VojtaSvoboda\Brands\Components; |
||
7 | class Letters extends ComponentBase |
||
8 | { |
||
9 | public $characters; |
||
10 | |||
11 | public function componentDetails() |
||
18 | |||
19 | public function defineProperties() |
||
30 | |||
31 | public function onRun() |
||
35 | |||
36 | /** |
||
37 | * Get sorted list of brand letters. |
||
38 | * |
||
39 | * @return array |
||
40 | */ |
||
41 | public function getBrandCharacters() |
||
42 | { |
||
43 | // get unique letters |
||
44 | $letters = []; |
||
45 | Model::all()->each(function($brand) use (&$letters) |
||
46 | { |
||
47 | // get letter |
||
48 | $letter = mb_strtolower(mb_substr($brand->name, 0, 1)); |
||
49 | |||
50 | // init array when doesn't exists |
||
51 | if (!isset($letters[$letter])) { |
||
52 | $letters[$letter] = [ |
||
53 | 'name' => $letter, |
||
54 | 'count' => 0, |
||
55 | 'url' => $this->controller->pageUrl($this->property('brandsPage'), [ |
||
56 | 'letter' => $letter, |
||
57 | ]), |
||
58 | ]; |
||
59 | } |
||
60 | $letters[$letter]['count']++; |
||
61 | }); |
||
62 | |||
63 | // sort them |
||
64 | usort($letters, function($a, $b) { |
||
65 | if ($a['name'] == $b['name']) { |
||
66 | return 0; |
||
67 | } |
||
68 | |||
69 | return $a['name'] > $b['name'] ? 1 : -1; |
||
70 | }); |
||
71 | |||
72 | return $letters; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get options for the dropdown where the link to the brand page can be selected. |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | public function getBrandsPageOptions() |
||
84 | } |
||
85 |