Completed
Pull Request — master (#51)
by De Cramer
02:42
created

GridBuilder::addActionColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 5
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui\Grid;
4
use eXpansion\Framework\Core\Model\Gui\Action;
5
use eXpansion\Framework\Core\Model\Gui\Factory\LabelFactory;
6
use eXpansion\Framework\Core\Model\Gui\Factory\LineFactory;
7
use eXpansion\Framework\Core\Model\Gui\Factory\PagerFactory;
8
use eXpansion\Framework\Core\Model\Gui\Grid\Column\AbstractColumn;
9
use eXpansion\Framework\Core\Model\Gui\Grid\Column\ActionColumn;
10
use eXpansion\Framework\Core\Model\Gui\Grid\Column\TextColumn;
11
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
12
use eXpansion\Framework\Core\Plugins\Gui\ActionFactory;
13
use eXpansion\Framework\Core\Plugins\Gui\ManialinkFactory;
14
use FML\Controls\Frame;
15
use FML\Controls\Label;
16
use FML\Controls\Quads\Quad_Icons64x64_1;
17
18
19
/**
20
 * Class GridBuilder
21
 *
22
 * @TODO Add possibility to add actions on elements.
23
 *
24
 * @package eXpansion\Framework\Core\Model\Gui\Grid;
25
 * @author  oliver de Cramer <[email protected]>
26
 */
27
class GridBuilder
28
{
29
    /** @var  ActionFactory */
30
    protected $actionFactory;
31
32
    /** @var LineFactory */
33
    protected $titleLineFactory;
34
35
    /** @var LineFactory */
36
    protected $lineFactory;
37
38
    /** @var PagerFactory */
39
    protected $pagerFactory;
40
41
    /** @var DataCollectionInterface */
42
    protected $dataCollection;
43
44
    /** @var ManialinkInterface */
45
    protected $manialink;
46
47
    /** @var ManialinkFactory */
48
    protected $manialinkFactory;
49
50
    /** @var AbstractColumn[] */
51
    protected $columns;
52
53
    /** @var  float */
54
    protected $totalWidthCoefficency = 0;
55
56
    /** @var int */
57
    protected $currentPage = 1;
58
59
    /** @var string */
60
    protected $pageKey;
61
62
    /** @var Action */
63
    protected $actionPreviousPage;
64
    /** @var Action */
65
    protected $actionNextPage;
66
    /** @var Action */
67
    protected $actionLastPage;
68
    /** @var Action */
69
    protected $actionFirstPage;
70
71
    /** @var Action[] */
72
    protected $temporaryActions = [];
73
74
    /**
75
     * GridBuilder constructor.
76
     *
77
     * @param ActionFactory $actionFactory
78
     * @param LineFactory   $lineFactory
79
     * @param LineFactory   $titleLineFactory
80
     * @param PagerFactory  $pagerFactory
81
     */
82 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
        ActionFactory $actionFactory,
84
        LineFactory $lineFactory,
85
        LineFactory $titleLineFactory,
86
        PagerFactory $pagerFactory
87
    ) {
88
        $this->actionFactory = $actionFactory;
89
        $this->titleLineFactory = $titleLineFactory;
90
        $this->lineFactory = $lineFactory;
91
        $this->pagerFactory = $pagerFactory;
92
93
        $this->pageKey = spl_object_hash($this) . "_key";
94
    }
95
96
    /**
97
     * Set the data collection.
98
     *
99
     * @param DataCollectionInterface $dataCollection
100
     *
101
     * @return $this
102
     */
103
    public function setDataCollection(DataCollectionInterface $dataCollection)
104
    {
105
        $this->dataCollection = $dataCollection;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Set the manialink the content is generated for.
112
     *
113
     * @param ManialinkInterface $manialink
114
     *
115
     * @return $this
116
     */
117
    public function setManialink(ManialinkInterface $manialink)
118
    {
119
        $this->manialink = $manialink;
120
121
        $this->actionFirstPage = $this->actionFactory
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->actionFactory->cr...ToFirstPage'), array()) of type string is incompatible with the declared type object<eXpansion\Framework\Core\Model\Gui\Action> of property $actionFirstPage.

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...
122
            ->createManialinkAction($manialink, array($this, 'goToFirstPage'), []);
123
        $this->actionPreviousPage = $this->actionFactory
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->actionFactory->cr...reviousPage'), array()) of type string is incompatible with the declared type object<eXpansion\Framework\Core\Model\Gui\Action> of property $actionPreviousPage.

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...
124
            ->createManialinkAction($manialink, array($this, 'goToPreviousPage'), []);
125
        $this->actionNextPage = $this->actionFactory
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->actionFactory->cr...oToNextPage'), array()) of type string is incompatible with the declared type object<eXpansion\Framework\Core\Model\Gui\Action> of property $actionNextPage.

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...
126
            ->createManialinkAction($manialink, array($this, 'goToNextPage'), []);
127
        $this->actionLastPage = $this->actionFactory
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->actionFactory->cr...oToLastPage'), array()) of type string is incompatible with the declared type object<eXpansion\Framework\Core\Model\Gui\Action> of property $actionLastPage.

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...
128
            ->createManialinkAction($manialink, array($this, 'goToLastPage'), []);
129
130
        return $this;
131
    }
132
133
    /**
134
     * Set the manialink factory responsible with the manialink.
135
     *
136
     * @param ManialinkFactory $manialinkFactory
137
     *
138
     * @return $this
139
     */
140
    public function setManialinkFactory($manialinkFactory)
141
    {
142
        $this->manialinkFactory = $manialinkFactory;
143
144
        return $this;
145
    }
146
147
    /**
148
     * @param      $key
149
     * @param      $name
150
     * @param      $widthCoefficiency
151
     * @param bool $sortable
152
     * @param bool $translatable
153
     *
154
     * @return $this
155
     */
156
    public function addTextColumn($key, $name, $widthCoefficiency, $sortable = false, $translatable = false)
157
    {
158
        $this->columns[] = new TextColumn($key, $name, $widthCoefficiency, $sortable, $translatable);
159
160
        return $this;
161
    }
162
163
    /**
164
     * Add an action into a column.
165
     *
166
     * @param $key
167
     * @param $name
168
     * @param $widthCoefficiency
169
     * @param $action
170
     * @param $renderer
171
     */
172
    public function addActionColumn($key, $name, $widthCoefficiency, $action, $renderer)
173
    {
174
        $this->columns[] = new ActionColumn($key, $name, $widthCoefficiency, $action, $renderer);
175
    }
176
177
    /**
178
     * Remove all columns.
179
     */
180
    public function resetColumns()
181
    {
182
        $this->columns = [];
183
        $this->totalWidthCoefficency = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $totalWidthCoefficency was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
184
    }
185
186
    public function build($width, $height)
187
    {
188
        foreach ($this->temporaryActions as $action) {
189
            $this->actionFactory->destroyAction($action);
190
        }
191
192
        $lineHeight = 5 + 0.5;
193
194
        $frame = new Frame();
195
        $frame->setPosition(0,0);
196
        $frame->setSize($width, $height);
197
198
        $posY = 0;
199
        // Generating headers.
200
        // TODO if sortable create actions...
201
        $data = [];
202
        foreach ($this->columns as $columnData) {
203
            $data[] = [
204
                'text' => $columnData->getName(),
205
                'width' => $columnData->getWidthCoeficiency(),
206
                'translatable' => true
207
            ];
208
        }
209
210
        $frame->addChild($this->titleLineFactory->create($frame->getWidth(), $data));
211
        $posY -= $lineHeight + 1;
212
213
        /*
214
         * Display the main content.
215
         */
216
        $this->dataCollection->setPageSize(floor(($frame->getHeight() + $posY - $lineHeight - 2) / $lineHeight));
217
218
        $lines = $this->dataCollection->getData($this->currentPage);
219
        foreach ($lines as $i => $lineData) {
220
            $data = [];
221
            foreach ($this->columns as $columnData) {
222
                if ($columnData instanceof TextColumn) {
223
                    $data[] = [
224
                        'text' => $this->dataCollection->getLineData($lineData, $columnData->getKey()),
225
                        'width' => $columnData->getWidthCoeficiency(),
226
                        'translatable' => $columnData->getTranslatable()
227
                    ];
228
                } elseif($columnData instanceof ActionColumn) {
229
                    $action = $this->actionFactory
230
                        ->createManialinkAction($this->manialink, $columnData->getCallable(), $lineData);
231
                    $this->temporaryActions[] = $action;
232
                    $data[] = [
233
                        'renderer' => clone $columnData->getRenderer(),
234
                        'width' => $columnData->getWidthCoeficiency(),
235
                        'action' => $action,
236
                    ];
237
                }
238
            }
239
            $line = $this->lineFactory->create($frame->getWidth(), $data, $i);
240
            $line->setPosition(0, $posY);
241
            $frame->addChild($line);
242
            $posY -= $lineHeight;
243
        }
244
245
        /*
246
         * Handle the pager.
247
         */
248
        $posY = ($frame->getHeight() -7) * -1;
249
        $pager = $this->pagerFactory->create(
250
            $frame->getWidth(),
251
            $this->currentPage,
252
            $this->dataCollection->getLastPageNumber(),
253
            $this->actionFirstPage,
254
            $this->actionPreviousPage,
255
            $this->actionNextPage,
256
            $this->actionLastPage
257
        );
258
        $pager->setPosition(0, $posY);
259
        $frame->addChild($pager);
260
261
        return $frame;
262
    }
263
264
    /**
265
     * Action callback to go to the first page.
266
     */
267
    public function goToFirstPage()
268
    {
269
        $this->changePage(1);
270
    }
271
272
    /**
273
     * Action callback to go to the previous page.
274
     */
275
    public function goToPreviousPage()
276
    {
277
        if ($this->currentPage - 1 >= 1) {
278
            $this->changePage($this->currentPage - 1);
279
        }
280
    }
281
282
    /**
283
     * Action callback to go to the next page.
284
     */
285
    public function goToNextPage()
286
    {
287
        if ($this->currentPage + 1 <= $this->dataCollection->getLastPageNumber()) {
288
            $this->changePage($this->currentPage + 1);
289
        }
290
    }
291
292
    /**
293
     * Action callback to go to the last page.
294
     */
295
    public function goToLastPage()
296
    {
297
        $this->changePage($this->dataCollection->getLastPageNumber());
298
    }
299
300
    /**
301
     * Handle page change & refresh user window.
302
     *
303
     * @param $page
304
     */
305
    protected function changePage($page)
306
    {
307
        $this->currentPage = $page;
308
        $this->manialinkFactory->update($this->manialink->getUserGroup());
309
    }
310
}