Completed
Pull Request — master (#99)
by De Cramer
02:57
created

GridBuilder::addTextColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 5
crap 1
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui\Grid;
4
5
use eXpansion\Framework\Core\Model\Gui\Factory\LineFactory;
6
use eXpansion\Framework\Core\Model\Gui\Factory\PagerFactory;
7
use eXpansion\Framework\Core\Model\Gui\Grid\Column\AbstractColumn;
8
use eXpansion\Framework\Core\Model\Gui\Grid\Column\ActionColumn;
9
use eXpansion\Framework\Core\Model\Gui\Grid\Column\TextColumn;
10
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
11
use eXpansion\Framework\Core\Plugins\Gui\ActionFactory;
12
use eXpansion\Framework\Core\Plugins\Gui\ManialinkFactory;
13
use FML\Controls\Frame;
14
use FML\Controls\Label;
15
16
17
/**
18
 * Class GridBuilder
19
 *
20
 * @TODO Add possibility to add actions on elements.
21
 *
22
 * @package eXpansion\Framework\Core\Model\Gui\Grid;
23
 * @author  oliver de Cramer <[email protected]>
24
 */
25
class GridBuilder
26
{
27
    /** @var  ActionFactory */
28
    protected $actionFactory;
29
30
    /** @var LineFactory */
31
    protected $titleLineFactory;
32
33
    /** @var LineFactory */
34
    protected $lineFactory;
35
36
    /** @var PagerFactory */
37
    protected $pagerFactory;
38
39
    /** @var DataCollectionInterface */
40
    protected $dataCollection;
41
42
    /** @var ManialinkInterface */
43
    protected $manialink;
44
45
    /** @var ManialinkFactory */
46
    protected $manialinkFactory;
47
48
    /** @var AbstractColumn[] */
49
    protected $columns;
50
51
    /** @var  float */
52
    protected $totalWidthCoefficency = 0.;
53
54
    /** @var int */
55
    protected $currentPage = 1;
56
57
    /** @var string */
58
    protected $pageKey;
59
60
    /** @var string */
61
    protected $actionPreviousPage;
62
    /** @var string */
63
    protected $actionNextPage;
64
    /** @var string */
65
    protected $actionLastPage;
66
    /** @var string */
67
    protected $actionFirstPage;
68
69
    /** @var string[] */
70
    protected $temporaryActions = [];
71
72
    /**
73
     * GridBuilder constructor.
74
     *
75
     * @param ActionFactory $actionFactory
76
     * @param LineFactory $lineFactory
77
     * @param LineFactory $titleLineFactory
78
     * @param PagerFactory $pagerFactory
79
     */
80 5 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...
81
        ActionFactory $actionFactory,
82
        LineFactory $lineFactory,
83
        LineFactory $titleLineFactory,
84
        PagerFactory $pagerFactory
85
    ) {
86 5
        $this->actionFactory = $actionFactory;
87 5
        $this->titleLineFactory = $titleLineFactory;
88 5
        $this->lineFactory = $lineFactory;
89 5
        $this->pagerFactory = $pagerFactory;
90
91 5
        $this->pageKey = "key_".spl_object_hash($this);
92 5
    }
93
94
    /**
95
     * Set the data collection.
96
     *
97
     * @param DataCollectionInterface $dataCollection
98
     *
99
     * @return $this
100
     */
101 5
    public function setDataCollection(DataCollectionInterface $dataCollection)
102
    {
103 5
        $this->dataCollection = $dataCollection;
104
105 5
        return $this;
106
    }
107
108
    /**
109
     * Set the manialink the content is generated for.
110
     *
111
     * @param ManialinkInterface $manialink
112
     *
113
     * @return $this
114
     */
115 5
    public function setManialink(ManialinkInterface $manialink)
116
    {
117 5
        $this->manialink = $manialink;
118
119 5
        $this->actionFirstPage = $this->actionFactory
120 5
            ->createManialinkAction($manialink, array($this, 'goToFirstPage'), []);
121 5
        $this->actionPreviousPage = $this->actionFactory
122 5
            ->createManialinkAction($manialink, array($this, 'goToPreviousPage'), []);
123 5
        $this->actionNextPage = $this->actionFactory
124 5
            ->createManialinkAction($manialink, array($this, 'goToNextPage'), []);
125 5
        $this->actionLastPage = $this->actionFactory
126 5
            ->createManialinkAction($manialink, array($this, 'goToLastPage'), []);
127
128 5
        return $this;
129
    }
130
131
    /**
132
     * Set the manialink factory responsible with the manialink.
133
     *
134
     * @param ManialinkFactory $manialinkFactory
135
     *
136
     * @return $this
137
     */
138 5
    public function setManialinkFactory($manialinkFactory)
139
    {
140 5
        $this->manialinkFactory = $manialinkFactory;
141
142 5
        return $this;
143
    }
144
145
    /**
146
     * @param      string $key
147
     * @param      string $name
148
     * @param      integer $widthCoefficiency
149
     * @param bool $sortable
150
     * @param bool $translatable
151
     *
152
     * @return $this
153
     */
154 5
    public function addTextColumn($key, $name, $widthCoefficiency, $sortable = false, $translatable = false)
155
    {
156 5
        $this->columns[] = new TextColumn($key, $name, $widthCoefficiency, $sortable, $translatable);
157
158 5
        return $this;
159
    }
160
161
    /**
162
     * Add an action into a column.
163
     *
164
     * @param string $key
165
     * @param string $name
166
     * @param integer $widthCoefficiency
167
     * @param $action
168
     * @param Label $renderer
169
     *
170
     * @return $this
171
     */
172 5
    public function addActionColumn($key, $name, $widthCoefficiency, $action, $renderer)
173
    {
174 5
        $this->columns[] = new ActionColumn($key, $name, $widthCoefficiency, $action, $renderer);
175
176 5
        return $this;
177
    }
178
179
    /**
180
     * Remove all columns.
181
     */
182 1
    public function resetColumns()
183
    {
184 1
        $this->columns = [];
185 1
        $this->totalWidthCoefficency = 0.;
186 1
    }
187
188
    /**
189
     * Build a grid.
190
     *
191
     * @param double $width
192
     * @param double $height
193
     *
194
     * @return Frame
195
     */
196 5
    public function build($width, $height)
197
    {
198 5
        foreach ($this->temporaryActions as $action) {
199 1
            $this->actionFactory->destroyAction($action);
200
        }
201
202 5
        $lineHeight = 5 + 0.5;
203
204 5
        $frame = new Frame();
205 5
        $frame->setPosition(0, 0);
206 5
        $frame->setSize($width, $height);
207
208 5
        $posY = 0.;
209
        // Generating headers.
210
        // TODO if sortable create actions...
211 5
        $data = [];
212 5
        foreach ($this->columns as $columnData) {
213 5
            $data[] = [
214 5
                'text' => $columnData->getName(),
215 5
                'width' => $columnData->getWidthCoeficiency(),
216
                'translatable' => true,
217
            ];
218
        }
219
220 5
        $frame->addChild($this->titleLineFactory->create($frame->getWidth(), $data));
221 5
        $posY -= $lineHeight + 1;
222
223
        /*
224
         * Display the main content.
225
         */
226 5
        $this->dataCollection->setPageSize(floor(($frame->getHeight() + $posY - $lineHeight - 2) / $lineHeight));
227
228 5
        $lines = $this->dataCollection->getData($this->currentPage);
229 5
        $idx = 0;
230 5
        foreach ($lines as $i => $lineData) {
231 5
            $data = [];
232 5
            foreach ($this->columns as $columnData) {
233 5
                if ($columnData instanceof TextColumn) {
234 5
                    $data[] = [
235 5
                        'text' => $this->dataCollection->getLineData($lineData, $columnData->getKey()),
236 5
                        'width' => $columnData->getWidthCoeficiency(),
237 5
                        'translatable' => $columnData->getTranslatable(),
238
                    ];
239
                } elseif ($columnData instanceof ActionColumn) {
240 5
                    $action = $this->actionFactory
241 5
                        ->createManialinkAction($this->manialink, $columnData->getCallable(), $lineData);
242 5
                    $this->temporaryActions[] = $action;
243 5
                    $data[] = [
244 5
                        'renderer' => clone $columnData->getRenderer(),
245 5
                        'width' => $columnData->getWidthCoeficiency(),
246 5
                        'action' => $action,
247
                    ];
248
                }
249
            }
250 5
            $line = $this->lineFactory->create($frame->getWidth(), $data, $idx++);
251 5
            $line->setPosition(0, $posY);
252 5
            $frame->addChild($line);
253 5
            $posY -= $lineHeight;
254
        }
255
256
        /*
257
         * Handle the pager.
258
         */
259 5
        $posY = ($frame->getHeight() - 7) * -1;
260 5
        $pager = $this->pagerFactory->create(
261 5
            $frame->getWidth(),
262 5
            $this->currentPage,
263 5
            $this->dataCollection->getLastPageNumber(),
264 5
            $this->actionFirstPage,
265 5
            $this->actionPreviousPage,
266 5
            $this->actionNextPage,
267 5
            $this->actionLastPage
268
        );
269 5
        $pager->setPosition(0, $posY);
270 5
        $frame->addChild($pager);
271
272 5
        return $frame;
273
    }
274
275
    /**
276
     * Action callback to go to the first page.
277
     */
278 1
    public function goToFirstPage()
279
    {
280 1
        $this->changePage(1);
281 1
    }
282
283
    /**
284
     * Action callback to go to the previous page.
285
     */
286 1
    public function goToPreviousPage()
287
    {
288 1
        if ($this->currentPage - 1 >= 1) {
289 1
            $this->changePage($this->currentPage - 1);
290
        }
291 1
    }
292
293
    /**
294
     * Action callback to go to the next page.
295
     */
296 3
    public function goToNextPage()
297
    {
298 3
        if ($this->currentPage + 1 <= $this->dataCollection->getLastPageNumber()) {
299 3
            $this->changePage($this->currentPage + 1);
300
        }
301 3
    }
302
303
    /**
304
     * Action callback to go to the last page.
305
     */
306 1
    public function goToLastPage()
307
    {
308 1
        $this->changePage($this->dataCollection->getLastPageNumber());
309 1
    }
310
311
    /**
312
     * Handle page change & refresh user window.
313
     *
314
     * @param integer $page
315
     */
316 4
    protected function changePage($page)
317
    {
318 4
        $this->currentPage = $page;
319 4
        $this->manialinkFactory->update($this->manialink->getUserGroup());
320 4
    }
321
}
322