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

GridBuilder::resetColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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[] = ['text' => $columnData->getName(), 'width' => $columnData->getWidthCoeficiency()];
204
        }
205
206
        $frame->addChild($this->titleLineFactory->create($frame->getWidth(), $data));
207
        $posY -= $lineHeight + 1;
208
209
        /*
210
         * Display the main content.
211
         */
212
        $this->dataCollection->setPageSize(floor(($frame->getHeight() + $posY - $lineHeight - 2) / $lineHeight));
213
214
        $lines = $this->dataCollection->getData($this->currentPage);
215
        foreach ($lines as $i => $lineData) {
216
            $data = [];
217
            foreach ($this->columns as $columnData) {
218
                if ($columnData instanceof TextColumn) {
219
                    $data[] = [
220
                        'text' => $this->dataCollection->getLineData($lineData, $columnData->getKey()),
221
                        'width' => $columnData->getWidthCoeficiency(),
222
                        'translatable' => $columnData->getTranslatable()
223
                    ];
224
                } elseif($columnData instanceof ActionColumn) {
225
                    $action = $this->actionFactory
226
                        ->createManialinkAction($this->manialink, $columnData->getCallable(), $lineData);
227
                    $this->temporaryActions[] = $action;
228
                    $data[] = [
229
                        'renderer' => clone $columnData->getRenderer(),
230
                        'width' => $columnData->getWidthCoeficiency(),
231
                        'action' => $action,
232
                    ];
233
                }
234
            }
235
            $line = $this->lineFactory->create($frame->getWidth(), $data, $i);
236
            $line->setPosition(0, $posY);
237
            $frame->addChild($line);
238
            $posY -= $lineHeight;
239
        }
240
241
        /*
242
         * Handle the pager.
243
         */
244
        $posY = ($frame->getHeight() -7) * -1;
245
        $pager = $this->pagerFactory->create(
246
            $frame->getWidth(),
247
            $this->currentPage,
248
            $this->dataCollection->getLastPageNumber(),
249
            $this->actionFirstPage,
250
            $this->actionPreviousPage,
251
            $this->actionNextPage,
252
            $this->actionLastPage
253
        );
254
        $pager->setPosition(0, $posY);
255
        $frame->addChild($pager);
256
257
        return $frame;
258
    }
259
260
    /**
261
     * Action callback to go to the first page.
262
     */
263
    public function goToFirstPage()
264
    {
265
        $this->changePage(1);
266
    }
267
268
    /**
269
     * Action callback to go to the previous page.
270
     */
271
    public function goToPreviousPage()
272
    {
273
        if ($this->currentPage - 1 >= 1) {
274
            $this->changePage($this->currentPage - 1);
275
        }
276
    }
277
278
    /**
279
     * Action callback to go to the next page.
280
     */
281
    public function goToNextPage()
282
    {
283
        if ($this->currentPage + 1 <= $this->dataCollection->getLastPageNumber()) {
284
            $this->changePage($this->currentPage + 1);
285
        }
286
    }
287
288
    /**
289
     * Action callback to go to the last page.
290
     */
291
    public function goToLastPage()
292
    {
293
        $this->changePage($this->dataCollection->getLastPageNumber());
294
    }
295
296
    /**
297
     * Handle page change & refresh user window.
298
     *
299
     * @param $page
300
     */
301
    protected function changePage($page)
302
    {
303
        $this->currentPage = $page;
304
        $this->manialinkFactory->update($this->manialink->getUserGroup());
305
    }
306
}