Completed
Push — master ( 0411a3...0f768a )
by Alex
03:32
created

Common::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 14
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A Common::listOfButtons() 0 3 1
1
<?php
2
namespace Mezon\Gui\ListBuilder;
3
4
use Mezon\Functional\Fetcher;
5
use Mezon\Gui\WidgetsRegistry\BootstrapWidgets;
6
use Mezon\TemplateEngine\TemplateEngine;
7
use Mezon\Transport\Request;
8
9
/**
10
 * Class ListBuilder
11
 *
12
 * @package CrudService
13
 * @subpackage ListBuilder
14
 * @author Dodonov A.A.
15
 * @version v.1.0 (2019/08/12)
16
 * @copyright Copyright (c) 2019, aeon.org
17
 */
18
19
/**
20
 * Class constructs grids
21
 */
22
class Common extends Base
23
{
24
25
    /**
26
     * Custom actions for each record
27
     *
28
     * @var string
29
     */
30
    private $customActions = null;
31
32
    /**
33
     * Custom header actions
34
     *
35
     * @var string
36
     */
37
    private $customHeaderActions = '';
38
39
    /**
40
     * Method sets custom actions
41
     *
42
     * @param string $actions
43
     */
44
    public function setCustomActions(string $actions): void
45
    {
46
        $this->customActions = $actions;
47
    }
48
49
    /**
50
     * Method sets custom header actions
51
     *
52
     * @param string $actions
53
     */
54
    public function setCustomHeaderActions(string $actions): void
55
    {
56
        $this->customHeaderActions = $actions;
57
    }
58
59
    /**
60
     * Method returns end point for the create page form
61
     *
62
     * @return string Create page endpoint
63
     */
64
    private function getCreatePageEndpoint(): string
65
    {
66
        if (isset($_GET['create-page-endpoint'])) {
67
            return $_GET['create-page-endpoint'];
68
        }
69
70
        return '../create/';
71
    }
72
73
    /**
74
     * Method shows "no records" message instead of listing
75
     *
76
     * @return string Compiled list view
77
     */
78
    private function listingNoItems(): string
79
    {
80
        return str_replace('{create-page-endpoint}', $this->getCreatePageEndpoint(), $this->getNoItemsContent());
81
    }
82
83
    /**
84
     * Method displays list of possible buttons
85
     *
86
     * @param int $id
87
     *            Id of the record
88
     * @return string Compiled list buttons
89
     */
90
    private function listOfButtons(int $id): string
91
    {
92
        return str_replace('{id}', $id, BootstrapWidgets::get('list-of-buttons'));
93
    }
94
95
    /**
96
     * Need to display actions in list
97
     *
98
     * @return bool Do we need add actions
99
     */
100
    private function needActions(): bool
101
    {
102
        if (@$_GET['update-button'] == 1 || @$_GET['delete-button'] == 1 || $this->customActions !== null) {
103
            return true;
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * Method compiles listing items cells
111
     *
112
     * @param array|object $record
113
     *            record data
114
     * @return string Compiled row
115
     */
116
    private function listingItemsCells($record): string
117
    {
118
        $content = '';
119
120
        foreach (array_keys($this->fields) as $name) {
121
            if ($name == 'domain_id') {
122
                continue;
123
            }
124
            if ($name == 'id') {
125
                $content .= BootstrapWidgets::get('listing-row-centered-cell');
126
            } else {
127
                $content .= BootstrapWidgets::get('listing-row-cell');
128
            }
129
            $content = str_replace('{name}', '{' . $name . '}', $content);
130
        }
131
132
        if ($this->needActions()) {
133
            $content .= BootstrapWidgets::get('listing-actions');
134
135
            $content = str_replace(
136
                '{actions}',
137
                $this->customActions === null ? $this->listOfButtons(Fetcher::getField($record, 'id')) : $this->customActions,
0 ignored issues
show
Bug introduced by
It seems like Mezon\Functional\Fetcher::getField($record, 'id') can also be of type null; however, parameter $id of Mezon\Gui\ListBuilder\Common::listOfButtons() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
                $this->customActions === null ? $this->listOfButtons(/** @scrutinizer ignore-type */ Fetcher::getField($record, 'id')) : $this->customActions,
Loading history...
138
                $content);
139
        }
140
141
        return $content;
142
    }
143
144
    /**
145
     * Method transforms database record
146
     *
147
     * @param array $record
148
     *            Transforming record
149
     * @return object Transformed record
150
     */
151
    private function transformRecord(object $record): object
152
    {
153
        // here we assume that we get from service
154
        // already transformed
155
        // and here we provide only additional transformations
156
        if (is_callable($this->recordTransformer)) {
157
            $record = call_user_func($this->recordTransformer, $record);
158
        }
159
160
        return $record;
161
    }
162
163
    /**
164
     * Method compiles listing items
165
     *
166
     * @param array $records
167
     *            Listof records
168
     * @return string Compiled list items
169
     */
170
    private function listingItems(array $records): string
171
    {
172
        $content = '';
173
174
        foreach ($records as $record) {
175
            $content .= BootstrapWidgets::get('listing-row');
176
            $content = str_replace('{items}', $this->listingItemsCells($record), $content);
177
178
            $record = $this->transformRecord($record);
179
180
            $record = $this->listBuilderAdapter->preprocessListItem($record);
181
182
            $content = TemplateEngine::printRecord($content, $record);
183
        }
184
185
        return $content;
186
    }
187
188
    /**
189
     * Method compiles header cells
190
     *
191
     * @return string Compiled header
192
     */
193
    private function listingHeaderCells(): string
194
    {
195
        $content = '';
196
197
        foreach ($this->fields as $name => $data) {
198
            if ($name == 'domain_id') {
199
                continue;
200
            }
201
202
            $idStyle = $name == 'id' ? 'style="text-align: center; width:5%;"' : '';
203
204
            $content .= BootstrapWidgets::get('listing-header-cell');
205
            $content = str_replace([
206
                '{id-style}',
207
                '{title}'
208
            ], [
209
                $idStyle,
210
                $data['title']
211
            ], $content);
212
        }
213
214
        if ($this->needActions()) {
215
            $content .= BootstrapWidgets::get('listing-header-actions');
216
        }
217
218
        return $content;
219
    }
220
221
    /**
222
     * Method returns listing header content
223
     *
224
     * @param
225
     *            string Compiled header
226
     */
227
    private function listingHeaderContent(): string
228
    {
229
        if (@$_GET['create-button'] == 1) {
230
            $content = BootstrapWidgets::get('listing-header');
231
232
            $content = str_replace('{create-page-endpoint}', $this->getCreatePageEndpoint(), $content);
233
        } else {
234
            $content = BootstrapWidgets::get('simple-listing-header');
235
        }
236
237
        return str_replace('{header-actions}', $this->customHeaderActions, $content);
238
    }
239
240
    /**
241
     * Method compiles listing header
242
     *
243
     * @return string Compiled header
244
     */
245
    private function listingHeader(): string
246
    {
247
        $content = $this->listingHeaderContent();
248
249
        $content = str_replace([
250
            '{list-description}',
251
            '{list-title}'
252
        ], [
253
            Request::getParam('list-description', $this->listDescription),
254
            $this->listTitle
255
        ], $content);
256
257
        return str_replace('{cells}', $this->listingHeaderCells(), $content);
258
    }
259
260
    /**
261
     * Method compiles listing form
262
     *
263
     * @return string Compiled listing form
264
     */
265
    public function listingForm(): string
266
    {
267
        $records = $this->listBuilderAdapter->getRecords([
268
            'field' => 'id',
269
            'order' => 'ASC'
270
        ], isset($_GET['from']) ? $_GET['from'] : 0, isset($_GET['limit']) ? $_GET['limit'] : 100);
271
272
        if (! empty($records)) {
273
            $header = $this->listingHeader();
274
275
            $items = $this->listingItems($records);
276
277
            $footer = BootstrapWidgets::get('listing-footer');
278
279
            return $header . $items . $footer;
280
        } else {
281
            return $this->listingNoItems();
282
        }
283
    }
284
285
    /**
286
     * Method returns fields of the list
287
     *
288
     * @return array fields list
289
     */
290
    public function getFields(): array
291
    {
292
        return $this->fields;
293
    }
294
}
295