Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 104 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 1 |
tao_helpers_grid_Grid | |
0.00% |
0 / 104 |
|
0.00% |
0 / 14 |
1980 | |
0.00% |
0 / 1 |
__clone | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
addColumn | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
removeColumn | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
addRow | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
removeRow | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
setCellValue | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
setColumnsAdapter | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
toArray | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
72 | |||
sortColumns | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getColumns | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getColumn | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
getColumnsModel | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
56 | |||
setData | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
19 | * (under the project TAO-TRANSFER); |
20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
22 | * |
23 | */ |
24 | |
25 | /** |
26 | * Short description of class tao_helpers_grid_Grid |
27 | * |
28 | * @access public |
29 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
30 | * @package tao |
31 | |
32 | */ |
33 | class tao_helpers_grid_Grid |
34 | { |
35 | // --- ASSOCIATIONS --- |
36 | |
37 | |
38 | // --- ATTRIBUTES --- |
39 | |
40 | /** |
41 | * Short description of attribute columns |
42 | * |
43 | * @access protected |
44 | * @var array |
45 | */ |
46 | protected $columns = []; |
47 | |
48 | /** |
49 | * Short description of attribute rows |
50 | * |
51 | * @access protected |
52 | * @var array |
53 | */ |
54 | protected $rows = []; |
55 | |
56 | /** |
57 | * Short description of attribute options |
58 | * |
59 | * @access protected |
60 | * @var array |
61 | */ |
62 | protected $options = []; |
63 | |
64 | /** |
65 | * Short description of attribute columnsModel |
66 | * |
67 | * @access protected |
68 | * @var array |
69 | */ |
70 | protected $columnsModel = []; |
71 | |
72 | // --- OPERATIONS --- |
73 | |
74 | /** |
75 | * Short description of method __clone |
76 | * |
77 | * @access public |
78 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
79 | * @return tao_helpers_grid_Grid |
80 | */ |
81 | public function __clone() |
82 | { |
83 | $returnValue = null; |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | return $returnValue; |
90 | } |
91 | |
92 | /** |
93 | * Short description of method __construct |
94 | * |
95 | * @access public |
96 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
97 | * @param array options |
98 | * @return mixed |
99 | */ |
100 | public function __construct($options = []) |
101 | { |
102 | |
103 | $this->options = $options; |
104 | } |
105 | |
106 | /** |
107 | * Short description of method addColumn |
108 | * |
109 | * @access public |
110 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
111 | * @param string id |
112 | * @param string title |
113 | * @param array options |
114 | * @return tao_helpers_grid_Column |
115 | */ |
116 | public function addColumn($id, $title, $options = []) |
117 | { |
118 | $returnValue = null; |
119 | |
120 | |
121 | $replace = false; |
122 | if (isset($options['replace'])) { |
123 | $replace = $options['replace']; |
124 | unset($options['replace']); |
125 | } |
126 | if (!$replace && isset($this->columns[$id])) { |
127 | throw new common_Exception('the column with the id ' . $id . ' already exists'); |
128 | } else { |
129 | $this->columns[$id] = new tao_helpers_grid_Column($id, $title, $options); |
130 | //set order as well: |
131 | $returnValue = true; |
132 | } |
133 | |
134 | |
135 | return $returnValue; |
136 | } |
137 | |
138 | /** |
139 | * Short description of method removeColumn |
140 | * |
141 | * @access public |
142 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
143 | * @param string id |
144 | * @return boolean |
145 | */ |
146 | public function removeColumn($id) |
147 | { |
148 | $returnValue = (bool) false; |
149 | |
150 | |
151 | unset($this->columns[$id]); |
152 | $returnValue = true; |
153 | |
154 | |
155 | return (bool) $returnValue; |
156 | } |
157 | |
158 | /** |
159 | * Short description of method addRow |
160 | * |
161 | * @access public |
162 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
163 | * @param string id |
164 | * @param array cells |
165 | * @param boolean replace |
166 | * @return boolean |
167 | */ |
168 | public function addRow($id, $cells = [], $replace = false) |
169 | { |
170 | $returnValue = (bool) false; |
171 | |
172 | |
173 | if (!$replace && isset($this->rows[$id])) { |
174 | throw new common_Exception('the row with the id ' . $id . ' already exists'); |
175 | } else { |
176 | $this->rows[$id] = $cells; |
177 | //@TODO: implement a sort funciton? |
178 | |
179 | $returnValue = true; |
180 | } |
181 | |
182 | |
183 | return (bool) $returnValue; |
184 | } |
185 | |
186 | /** |
187 | * Short description of method removeRow |
188 | * |
189 | * @access public |
190 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
191 | * @param string id |
192 | * @return boolean |
193 | */ |
194 | public function removeRow($id) |
195 | { |
196 | $returnValue = (bool) false; |
197 | |
198 | |
199 | unset($this->rows[$id]); |
200 | $returnValue = true; |
201 | |
202 | |
203 | return (bool) $returnValue; |
204 | } |
205 | |
206 | /** |
207 | * Short description of method setCellValue |
208 | * |
209 | * @access public |
210 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
211 | * @param string columnId |
212 | * @param string rowId |
213 | * @param string content string or Grid |
214 | * @param boolean forceCreation |
215 | * @return boolean |
216 | */ |
217 | public function setCellValue($columnId, $rowId, $content, $forceCreation = false) |
218 | { |
219 | $returnValue = (bool) false; |
220 | |
221 | |
222 | //TODO: for creating row and column if not exists? |
223 | if (isset($this->columns[$columnId])) { |
224 | if (isset($this->rows[$rowId])) { |
225 | $this->rows[$rowId][$columnId] = $content; |
226 | $returnValue = true; |
227 | } |
228 | } |
229 | |
230 | |
231 | return (bool) $returnValue; |
232 | } |
233 | |
234 | /** |
235 | * Short description of method setColumnsAdapter |
236 | * |
237 | * @access public |
238 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
239 | * @param array columnIds |
240 | * @param Adapter adapter |
241 | * @return boolean |
242 | */ |
243 | public function setColumnsAdapter($columnIds, tao_helpers_grid_Cell_Adapter $adapter) |
244 | { |
245 | $returnValue = (bool) false; |
246 | |
247 | |
248 | if (is_string($columnIds)) { |
249 | $columnIds = [$columnIds]; |
250 | } |
251 | if (is_array($columnIds)) { |
252 | foreach ($columnIds as $colId) { |
253 | if (!isset($this->columns[$colId]) || !$this->columns[$colId] instanceof tao_helpers_grid_Column) { |
254 | throw new common_Exception( |
255 | 'cannot set the column\'s adapter : the column with the id ' . $colId . ' does not exist' |
256 | ); |
257 | } else { |
258 | $returnValue = $this->columns[$colId]->setAdapter($adapter); |
259 | } |
260 | } |
261 | } |
262 | |
263 | |
264 | return (bool) $returnValue; |
265 | } |
266 | |
267 | /** |
268 | * Short description of method toArray |
269 | * |
270 | * @access public |
271 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
272 | * @return array |
273 | */ |
274 | public function toArray() |
275 | { |
276 | $returnValue = []; |
277 | |
278 | |
279 | |
280 | //sort columns: |
281 | $this->sortColumns(); |
282 | |
283 | foreach ($this->rows as $rowId => $cells) { |
284 | $returnValue[$rowId] = []; |
285 | |
286 | foreach ($this->columns as $columnId => $column) { |
287 | if ($column->hasAdapter()) { |
288 | //fill content with adapter: |
289 | $data = null; |
290 | if (isset($returnValue[$rowId][$columnId])) { |
291 | $data = $returnValue[$rowId][$columnId]; |
292 | } elseif (isset($cells[$columnId])) { |
293 | $data = $cells[$columnId]; |
294 | } |
295 | $returnValue[$rowId][$columnId] = $column->getAdaptersData($rowId, $data); |
296 | } elseif (isset($cells[$columnId])) { |
297 | if ($cells[$columnId] instanceof tao_helpers_grid_Grid) { |
298 | $returnValue[$rowId][$columnId] = $cells[$columnId]->toArray(); |
299 | } else { |
300 | $returnValue[$rowId][$columnId] = $cells[$columnId]; |
301 | } |
302 | } else { |
303 | $returnValue[$rowId][$columnId] = null;//empty cell |
304 | } |
305 | } |
306 | } |
307 | |
308 | |
309 | return (array) $returnValue; |
310 | } |
311 | |
312 | /** |
313 | * Short description of method sortColumns |
314 | * |
315 | * @access public |
316 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
317 | * @return boolean |
318 | */ |
319 | public function sortColumns() |
320 | { |
321 | $returnValue = (bool) false; |
322 | |
323 | |
324 | |
325 | |
326 | return (bool) $returnValue; |
327 | } |
328 | |
329 | /** |
330 | * Short description of method getColumns |
331 | * |
332 | * @access public |
333 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
334 | * @return array |
335 | */ |
336 | public function getColumns() |
337 | { |
338 | $returnValue = []; |
339 | |
340 | |
341 | $returnValue = $this->columns; |
342 | |
343 | |
344 | return (array) $returnValue; |
345 | } |
346 | |
347 | /** |
348 | * Short description of method getColumn |
349 | * |
350 | * @access public |
351 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
352 | * @param string id |
353 | * @return tao_helpers_grid_Column |
354 | */ |
355 | public function getColumn($id) |
356 | { |
357 | $returnValue = null; |
358 | |
359 | |
360 | if (isset($this->columns[$id]) && $this->columns[$id] instanceof tao_helpers_grid_Column) { |
361 | $returnValue = $this->columns[$id]; |
362 | } |
363 | |
364 | |
365 | return $returnValue; |
366 | } |
367 | |
368 | /** |
369 | * Short description of method getColumnsModel |
370 | * |
371 | * @access public |
372 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
373 | * @param boolean rebuild |
374 | * @return array |
375 | */ |
376 | public function getColumnsModel($rebuild = false) |
377 | { |
378 | $returnValue = []; |
379 | |
380 | |
381 | foreach ($this->columns as $column) { |
382 | if ($column instanceof tao_helpers_grid_Column) { |
383 | $returnValue[$column->getId()] = [ |
384 | 'id' => $column->getId(), |
385 | 'title' => $column->getTitle(), |
386 | 'type' => $column->getType() |
387 | ]; |
388 | |
389 | foreach ($column->getOptions() as $optionsName => $optionValue) { |
390 | $returnValue[$column->getId()][$optionsName] = $optionValue; |
391 | } |
392 | |
393 | if ($column->hasAdapter('tao_helpers_grid_Cell_SubgridAdapter')) { |
394 | $subGridAdapter = null; |
395 | $adapters = $column->getAdapters(); |
396 | $adaptersLength = count($adapters); |
397 | for ($i = $adaptersLength - 1; $i >= 0; $i--) { |
398 | if ($adapters[$i] instanceof tao_helpers_grid_Cell_SubgridAdapter) { |
399 | $subGridAdapter = $adapters[$i]; |
400 | break; |
401 | } |
402 | } |
403 | $returnValue[$column->getId()]['subgrids'] = $subGridAdapter |
404 | ->getGridContainer() |
405 | ->getGrid() |
406 | ->getColumnsModel(); |
407 | } |
408 | } |
409 | } |
410 | |
411 | |
412 | |
413 | return (array) $returnValue; |
414 | } |
415 | |
416 | /** |
417 | * Short description of method setData |
418 | * |
419 | * @access public |
420 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
421 | * @param array data |
422 | * @return mixed |
423 | */ |
424 | public function setData($data = []) |
425 | { |
426 | |
427 | |
428 | //empty local data |
429 | $this->rows = []; |
430 | //fill the local data |
431 | foreach ($data as $rowId => $cells) { |
432 | if (is_array($cells)) { |
433 | $this->addRow($rowId, $cells); |
434 | } elseif (is_string($cells)) { |
435 | $this->addRow($cells); |
436 | } |
437 | } |
438 | } |
439 | } |