Completed
Push — develop ( 4d770d...ad4c76 )
by Alexander
04:37
created

Manager::getCache()   A

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