Completed
Pull Request — master (#52)
by Gino
01:54
created

SeriesList::prepareVars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Components;
4
5
use Cms\Classes\Page;
6
use GinoPane\BlogTaxonomy\Plugin;
7
use GinoPane\BlogTaxonomy\Models\Series;
8
use GinoPane\BlogTaxonomy\Classes\ComponentAbstract;
9
use GinoPane\BlogTaxonomy\Classes\TranslateArrayTrait;
10
use GinoPane\BlogTaxonomy\Classes\PostListExceptionsTrait;
11
12
/**
13
 * Class SeriesList
14
 *
15
 * @package GinoPane\BlogTaxonomy\Components
16
 */
17
class SeriesList extends ComponentAbstract
18
{
19
    const NAME = 'seriesList';
20
21
    use TranslateArrayTrait;
22
    use PostListExceptionsTrait;
23
24
    /**
25
     * @var Series
26
     */
27
    public $series;
28
29
    /**
30
     * Reference to the page name for linking to series
31
     *
32
     * @var string
33
     */
34
    protected $seriesPage;
35
36
    /**
37
     * If the series list should be ordered by another attribute
38
     *
39
     * @var string
40
     */
41
    public $orderBy;
42
43
    /**
44
     * Whether display or not empty series
45
     *
46
     * @var bool
47
     */
48
    public $displayEmpty;
49
50
    /**
51
     * Limits the number of records to display
52
     *
53
     * @var int
54
     */
55
    public $limit;
56
57
    /**
58
     * Whether to query related posts or not
59
     *
60
     * @var bool
61
     */
62
    private $fetchPosts;
63
64
    /**
65
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
66
     */
67
    public function componentDetails()
68
    {
69
        return [
70
            'name'        => Plugin::LOCALIZATION_KEY . 'components.series_list.name',
71
            'description' => Plugin::LOCALIZATION_KEY . 'components.series_list.description'
72
        ];
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function defineProperties(): array
79
    {
80
        return array_merge([
81
            'displayEmpty' => [
82
                'title'       =>    Plugin::LOCALIZATION_KEY . 'components.series_list.display_empty_title',
83
                'description' =>    Plugin::LOCALIZATION_KEY . 'components.series_list.display_empty_description',
84
                'type'        =>    'checkbox',
85
                'default'     =>    false,
86
                'showExternalParam' => false
87
            ],
88
            'fetchPosts' => [
89
                'title'             => Plugin::LOCALIZATION_KEY . 'components.series_list.fetch_posts_title',
90
                'description'       => Plugin::LOCALIZATION_KEY . 'components.series_list.fetch_posts_description',
91
                'type'              => 'checkbox',
92
                'default'           => false,
93
                'showExternalParam' => false
94
            ],
95
            'limit' => [
96
                'title'             => Plugin::LOCALIZATION_KEY . 'components.series_list.limit_title',
97
                'description'       => Plugin::LOCALIZATION_KEY . 'components.series_list.limit_description',
98
                'type'              => 'string',
99
                'default'           => '0',
100
                'validationPattern' => '^[0-9]+$',
101
                'validationMessage' => Plugin::LOCALIZATION_KEY . 'components.series_list.limit_validation_message',
102
                'showExternalParam' => false
103
            ],
104
            'orderBy' => [
105
                'title'       =>    Plugin::LOCALIZATION_KEY . 'components.series_list.order_title',
106
                'description' =>    Plugin::LOCALIZATION_KEY . 'components.series_list.order_description',
107
                'type'        =>    'dropdown',
108
                'default'     =>    'title asc',
109
                'showExternalParam' => false
110
            ],
111
            'seriesPage' => [
112
                'group'         =>  Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.links_group',
113
                'title'         =>  Plugin::LOCALIZATION_KEY . 'components.series_list.series_page_title',
114
                'description'   =>  Plugin::LOCALIZATION_KEY . 'components.series_list.series_page_description',
115
                'type'          =>  'dropdown',
116
                'default'       =>  'blog/series',
117
                'showExternalParam' => false
118
            ],
119
        ], $this->getPostExceptionProperties());
120
    }
121
122
    /**
123
     * @return mixed
124
     */
125
    public function getSeriesPageOptions()
126
    {
127
        return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
128
    }
129
130
    /**
131
     * @return string[]
132
     */
133
    public function getOrderByOptions(): array
134
    {
135
        $order = $this->translate(Series::$sortingOptions);
136
137
        asort($order);
138
139
        return $order;
140
    }
141
142
    /**
143
     * Prepare and return a series list
144
     *
145
     * @return mixed
146
     */
147
    public function onRun()
148
    {
149
        $this->prepareVars();
150
151
        // Exceptions
152
        $this->populateExceptions();
153
154
        $this->series = $this->listSeries();
155
    }
156
157
    /**
158
     * Get Series
159
     *
160
     * @return mixed
161
     */
162
    protected function listSeries()
163
    {
164
        $series = Series::listFrontend([
165
            'sort' => $this->orderBy,
166
            'displayEmpty' => $this->displayEmpty,
167
            'limit' => $this->limit,
168
            'fetchPosts' => $this->fetchPosts,
169
            'exceptPosts' => $this->exceptPosts,
170
            'exceptCategories' => $this->exceptCategories
171
        ]);
172
173
        $this->handleUrls($series);
174
175
        return $series;
176
    }
177
178
    /**
179
     * @param $series
180
     */
181
    protected function handleUrls($series)
182
    {
183
        $seriesComponent = $this->getComponent(SeriesPosts::NAME, $this->seriesPage);
184
185
        $this->setUrls(
186
            $series,
187
            $this->seriesPage,
188
            $this->controller,
189
            [
190
                'series' => $this->urlProperty($seriesComponent, 'series')
191
            ]
192
        );
193
    }
194
195
    private function prepareVars(): void
196
    {
197
        $this->seriesPage = $this->getProperty('seriesPage');
198
        $this->orderBy = $this->getProperty('orderBy');
199
        $this->displayEmpty = (bool)$this->getProperty('displayEmpty');
200
        $this->fetchPosts = (bool)$this->getProperty('fetchPosts');
201
        $this->limit = $this->getProperty('limit');
202
    }
203
}
204