Manager::getRenderer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace AtDataGrid;
4
5
use AtDataGrid\Filter\FilterInterface;
6
use AtDataGrid\Form\FormBuilder;
7
use AtDataGrid\Renderer\RendererInterface;
8
use AtDataGrid\Row\Action;
9
use Zend\Cache\Storage\StorageInterface;
10
use Zend\Form\Element;
11
use Zend\Form\Form;
12
13
class Manager
14
{
15
    /**
16
     * @var DataGrid
17
     */
18
    protected $grid;
19
20
    /**
21
     * @var array
22
     */
23
    protected $rawData;
24
25
    /**
26
     * @var array
27
     */
28
    protected $data;
29
30
    /**
31
     * @var RendererInterface
32
     */
33
    protected $renderer;
34
35
    /**
36
     * @var FormBuilder
37
     */
38
    protected $formBuilder;
39
40
    /**
41
     * @var Form
42
     */
43
    protected $filtersForm;
44
45
    /**
46
     * @var StorageInterface
47
     */
48
    protected $cache;
49
50
    /**
51
     * @var bool
52
     */
53
    protected $allowCreate = true;
54
55
    /**
56
     * @var bool
57
     */
58
    protected $allowDelete = true;
59
60
    /**
61
     * @var bool
62
     */
63
    protected $allowEdit = true;
64
65
    /**
66
     * Row actions
67
     *
68
     * @var array
69
     */
70
    protected $actions = [];
71
72
    /**
73
     * @param DataGrid $grid
74
     */
75
    public function __construct(DataGrid $grid)
76
    {
77
        $this->grid = $grid;
78
    }
79
80
    /**
81
     * @return DataGrid
82
     */
83
    public function getGrid()
84
    {
85
        return $this->grid;
86
    }
87
88
    /**
89
     * @param RendererInterface $renderer
90
     * @return $this
91
     */
92
    public function setRenderer(RendererInterface $renderer)
93
    {
94
        $this->renderer = $renderer;
95
        return $this;
96
    }
97
98
    /**
99
     * @return RendererInterface
100
     */
101
    public function getRenderer()
102
    {
103
        return $this->renderer;
104
    }
105
106
    /**
107
     * @param StorageInterface $cache
108
     */
109
    public function setCache(StorageInterface $cache)
110
    {
111
        $this->cache = $cache;
112
    }
113
114
    /**
115
     * @return StorageInterface
116
     */
117
    public function getCache()
118
    {
119
        return $this->cache;
120
    }
121
122
    /**
123
     * @param bool $flag
124
     * @return $this
125
     */
126
    public function setAllowCreate($flag = true)
127
    {
128
        $this->allowCreate = $flag;
129
        return $this;
130
    }
131
132
    /**
133
     * Alias for setAllowCreate
134
     *
135
     * @param bool $flag
136
     * @return DataGrid
137
     */
138
    public function allowCreate($flag = true)
139
    {
140
        $this->setAllowCreate($flag);
141
        return $this;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147
    public function isAllowCreate()
148
    {
149
        return $this->allowCreate;
150
    }
151
152
    /**
153
     * @param bool $flag
154
     * @return $this
155
     */
156
    public function setAllowDelete($flag = true)
157
    {
158
        $this->allowDelete = $flag;
159
        return $this;
160
    }
161
162
    /**
163
     * Alias for setAllowDelete
164
     *
165
     * @param bool $flag
166
     * @return $this
167
     */
168
    public function allowDelete($flag = true)
169
    {
170
        $this->setAllowDelete($flag);
171
        return $this;
172
    }
173
174
    /**
175
     * @return bool
176
     */
177
    public function isAllowDelete()
178
    {
179
        return $this->allowDelete;
180
    }
181
182
    /**
183
     * @param bool $flag
184
     * @return $this
185
     */
186
    public function setAllowEdit($flag = true)
187
    {
188
        $this->allowEdit = $flag;
189
        return $this;
190
    }
191
192
    /**
193
     * Alias for setAllowEdit
194
     *
195
     * @param bool $flag
196
     * @return DataGrid
197
     */
198
    public function allowEdit($flag = true)
199
    {
200
        $this->setAllowEdit($flag);
201
        return $this;
202
    }
203
204
    /**
205
     * @return bool
206
     */
207
    public function isAllowEdit()
208
    {
209
        return $this->allowEdit;
210
    }
211
212
    /**
213
     * @return Form
214
     */
215
    public function getFiltersForm()
216
    {
217
        if (!$this->filtersForm) {
218
            $this->buildFiltersForm();
219
        }
220
221
        return $this->filtersForm;
222
    }
223
224
    /**
225
     * @return FormBuilder
226
     */
227
    public function getFormBuilder()
228
    {
229
        if (! $this->formBuilder) {
230
            $this->formBuilder  = new FormBuilder();
231
        }
232
233
        return $this->formBuilder;
234
    }
235
236
    /**
237
     * @param FormBuilder $formBuilder
238
     */
239
    public function setFormBuilder($formBuilder)
240
    {
241
        $this->formBuilder = $formBuilder;
242
    }
243
244
    /**
245
     * @return Form
246
     */
247
    protected function buildFiltersForm()
248
    {
249
        $grid = $this->getGrid();
250
        $form = new Form('at-datagrid-filters-form');
251
252
        /** @var FilterInterface $filter */
253
        foreach ($grid->getFilters() as $filter) {
254
            $formElement = $filter->getFormElement();
255
            if (!$formElement) {
256
                $column = $grid->getColumn($filter->getName());
257
                $formElement = clone $column->getFormElement();
258
259
                if ($formElement instanceof Element\Select) {
260
                    $formElement->setEmptyOption('');
261
                }
262
263
                $formElement->setName($filter->getName());
264
                $filter->setFormElement($formElement);
265
            }
266
267
            $form->add($formElement);
268
        }
269
270
        // Apply button
271
        $form->add(new Element\Submit('apply', ['label' => 'Search']));
272
273
        $this->filtersForm = $form;
274
        return $this->filtersForm;
275
    }
276
277
    /**
278
     * @param Action $action
279
     * @return $this
280
     * @throws \Exception
281
     */
282
    public function addAction(Action $action)
283
    {
284
        $this->actions[$action->getName()] = $action;
285
        return $this;
286
    }
287
288
    /**
289
     * @param array $actions
290
     * @return $this
291
     */
292
    public function addActions(array $actions = [])
293
    {
294
        foreach ($actions as $action) {
295
            $this->addAction($action);
296
        }
297
298
        return $this;
299
    }
300
301
    /**
302
     * @param $name
303
     * @return $this
304
     */
305
    public function removeAction($name)
306
    {
307
        if (array_key_exists($name, $this->actions)) {
308
            unset($this->actions[$name]);
309
        }
310
311
        return $this;
312
    }
313
314
    /**
315
     * @param $name
316
     * @return bool
317
     */
318
    public function getAction($name)
319
    {
320
        if (array_key_exists($name, $this->actions)) {
321
            return $this->actions[$name];
322
        }
323
324
        return false;
325
    }
326
327
    /**
328
     * @return array
329
     */
330
    public function getActions()
331
    {
332
        return $this->actions;
333
    }
334
335
    /**
336
     * @return array
337
     */
338
    public function getBulkActions()
339
    {
340
        $bulkActions = [];
341
        foreach ($this->getActions() as $action) {
342
            if ($action->isBulk()) {
343
                $bulkActions[] = $action;
344
            }
345
        }
346
347
        return $bulkActions;
348
    }
349
350
351
    public function getRawData()
352
    {
353
        return $this->rawData;
354
    }
355
356
    /**
357
     * @return array
358
     * @throws \Exception
359
     */
360
    public function getData()
361
    {
362
        $grid = $this->getGrid();
363
        $data = $grid->getData();
364
365
        $this->rawData = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data of type * is incompatible with the declared type array of property $rawData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
366
367
        // Add all columns from grid (not only from source)
368
        foreach (array_keys($grid->getColumns()) as $name) {
369
            foreach ($data as &$row) {
370
                if (!array_key_exists($name, $row)) {
371
                    $row[$name] = '';
372
                }
373
            }
374
        }
375
        unset($row);
376
377
        // Apply decorators
378
        $decoratedData = [];
379
        /** @var \ArrayObject $row */
380
        foreach ($data as $row) {
381
            $decoratedRow = [];
382
            foreach ($row as $colName => $value) {
383
                $column = $grid->getColumn($colName);
384
                $decoratedRow[$colName] = $value;
385
                // only for visible columns
386
                if ($column->isVisible()) {
387
                    $params = $row->getArrayCopy();
388
                    $decoratedRow[$colName] = $column->render($value, $params);
389
                }
390
            }
391
            $decoratedData[] = $decoratedRow;
392
        }
393
394
        return $decoratedData;
395
    }
396
397
    /**
398
     * @return mixed
399
     */
400
    public function render()
401
    {
402
        return $this->getRenderer()->render([
403
            'gridManager' => $this
404
        ]);
405
    }
406
}