grid.php
9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
/**
* @package Joomla.Platform
* @subpackage Grid
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
/**
* JGrid class to dynamically generate HTML tables
*
* @since 1.7.3
* @deprecated 4.0 This class will be removed without any replacement
*/
class JGrid
{
/**
* Array of columns
* @var array
* @since 1.7.3
*/
protected $columns = array();
/**
* Current active row
* @var int
* @since 1.7.3
*/
protected $activeRow = 0;
/**
* Rows of the table (including header and footer rows)
* @var array
* @since 1.7.3
*/
protected $rows = array();
/**
* Header and Footer row-IDs
* @var array
* @since 1.7.3
*/
protected $specialRows = array('header' => array(), 'footer' => array());
/**
* Associative array of attributes for the table-tag
* @var array
* @since 1.7.3
*/
protected $options;
/**
* Constructor for a JGrid object
*
* @param array $options Associative array of attributes for the table-tag
*
* @since 1.7.3
*/
public function __construct($options = array())
{
$this->setTableOptions($options, true);
}
/**
* Magic function to render this object as a table.
*
* @return string
*
* @since 1.7.3
*/
public function __toString()
{
return $this->toString();
}
/**
* Method to set the attributes for a table-tag
*
* @param array $options Associative array of attributes for the table-tag
* @param bool $replace Replace possibly existing attributes
*
* @return JGrid This object for chaining
*
* @since 1.7.3
*/
public function setTableOptions($options = array(), $replace = false)
{
if ($replace)
{
$this->options = $options;
}
else
{
$this->options = array_merge($this->options, $options);
}
return $this;
}
/**
* Get the Attributes of the current table
*
* @return array Associative array of attributes
*
* @since 1.7.3
*/
public function getTableOptions()
{
return $this->options;
}
/**
* Add new column name to process
*
* @param string $name Internal column name
*
* @return JGrid This object for chaining
*
* @since 1.7.3
*/
public function addColumn($name)
{
$this->columns[] = $name;
return $this;
}
/**
* Returns the list of internal columns
*
* @return array List of internal columns
*
* @since 1.7.3
*/
public function getColumns()
{
return $this->columns;
}
/**
* Delete column by name
*
* @param string $name Name of the column to be deleted
*
* @return JGrid This object for chaining
*
* @since 1.7.3
*/
public function deleteColumn($name)
{
$index = array_search($name, $this->columns);
if ($index !== false)
{
unset($this->columns[$index]);
$this->columns = array_values($this->columns);
}
return $this;
}
/**
* Method to set a whole range of columns at once
* This can be used to re-order the columns, too
*
* @param array $columns List of internal column names
*
* @return JGrid This object for chaining
*
* @since 1.7.3
*/
public function setColumns($columns)
{
$this->columns = array_values($columns);
return $this;
}
/**
* Adds a row to the table and sets the currently
* active row to the new row
*
* @param array $options Associative array of attributes for the row
* @param int $special 1 for a new row in the header, 2 for a new row in the footer
*
* @return JGrid This object for chaining
*
* @since 1.7.3
*/
public function addRow($options = array(), $special = false)
{
$this->rows[]['_row'] = $options;
$this->activeRow = count($this->rows) - 1;
if ($special)
{
if ($special === 1)
{
$this->specialRows['header'][] = $this->activeRow;
}
else
{
$this->specialRows['footer'][] = $this->activeRow;
}
}
return $this;
}
/**
* Method to get the attributes of the currently active row
*
* @return array Associative array of attributes
*
* @since 1.7.3
*/
public function getRowOptions()
{
return $this->rows[$this->activeRow]['_row'];
}
/**
* Method to set the attributes of the currently active row
*
* @param array $options Associative array of attributes
*
* @return JGrid This object for chaining
*
* @since 1.7.3
*/
public function setRowOptions($options)
{
$this->rows[$this->activeRow]['_row'] = $options;
return $this;
}
/**
* Get the currently active row ID
*
* @return int ID of the currently active row
*
* @since 1.7.3
*/
public function getActiveRow()
{
return $this->activeRow;
}
/**
* Set the currently active row
*
* @param int $id ID of the row to be set to current
*
* @return JGrid This object for chaining
*
* @since 1.7.3
*/
public function setActiveRow($id)
{
$this->activeRow = (int) $id;
return $this;
}
/**
* Set cell content for a specific column for the
* currently active row
*
* @param string $name Name of the column
* @param string $content Content for the cell
* @param array $option Associative array of attributes for the td-element
* @param bool $replace If false, the content is appended to the current content of the cell
*
* @return JGrid This object for chaining
*
* @since 1.7.3
*/
public function setRowCell($name, $content, $option = array(), $replace = true)
{
if ($replace || !isset($this->rows[$this->activeRow][$name]))
{
$cell = new stdClass;
$cell->options = $option;
$cell->content = $content;
$this->rows[$this->activeRow][$name] = $cell;
}
else
{
$this->rows[$this->activeRow][$name]->content .= $content;
$this->rows[$this->activeRow][$name]->options = $option;
}
return $this;
}
/**
* Get all data for a row
*
* @param int $id ID of the row to return
*
* @return array Array of columns of a table row
*
* @since 1.7.3
*/
public function getRow($id = false)
{
if ($id === false)
{
$id = $this->activeRow;
}
if (isset($this->rows[(int) $id]))
{
return $this->rows[(int) $id];
}
else
{
return false;
}
}
/**
* Get the IDs of all rows in the table
*
* @param int $special false for the standard rows, 1 for the header rows, 2 for the footer rows
*
* @return array Array of IDs
*
* @since 1.7.3
*/
public function getRows($special = false)
{
if ($special)
{
if ($special === 1)
{
return $this->specialRows['header'];
}
else
{
return $this->specialRows['footer'];
}
}
return array_diff(array_keys($this->rows), array_merge($this->specialRows['header'], $this->specialRows['footer']));
}
/**
* Delete a row from the object
*
* @param int $id ID of the row to be deleted
*
* @return JGrid This object for chaining
*
* @since 1.7.3
*/
public function deleteRow($id)
{
unset($this->rows[$id]);
if (in_array($id, $this->specialRows['header']))
{
unset($this->specialRows['header'][array_search($id, $this->specialRows['header'])]);
}
if (in_array($id, $this->specialRows['footer']))
{
unset($this->specialRows['footer'][array_search($id, $this->specialRows['footer'])]);
}
if ($this->activeRow == $id)
{
end($this->rows);
$this->activeRow = key($this->rows);
}
return $this;
}
/**
* Render the HTML table
*
* @return string The rendered HTML table
*
* @since 1.7.3
*/
public function toString()
{
$output = array();
$output[] = '<table' . $this->renderAttributes($this->getTableOptions()) . '>';
if (count($this->specialRows['header']))
{
$output[] = $this->renderArea($this->specialRows['header'], 'thead', 'th');
}
if (count($this->specialRows['footer']))
{
$output[] = $this->renderArea($this->specialRows['footer'], 'tfoot');
}
$ids = array_diff(array_keys($this->rows), array_merge($this->specialRows['header'], $this->specialRows['footer']));
if (count($ids))
{
$output[] = $this->renderArea($ids);
}
$output[] = '</table>';
return implode('', $output);
}
/**
* Render an area of the table
*
* @param array $ids IDs of the rows to render
* @param string $area Name of the area to render. Valid: tbody, tfoot, thead
* @param string $cell Name of the cell to render. Valid: td, th
*
* @return string The rendered table area
*
* @since 1.7.3
*/
protected function renderArea($ids, $area = 'tbody', $cell = 'td')
{
$output = array();
$output[] = '<' . $area . ">\n";
foreach ($ids as $id)
{
$output[] = "\t<tr" . $this->renderAttributes($this->rows[$id]['_row']) . ">\n";
foreach ($this->getColumns() as $name)
{
if (isset($this->rows[$id][$name]))
{
$column = $this->rows[$id][$name];
$output[] = "\t\t<" . $cell . $this->renderAttributes($column->options) . '>' . $column->content . '</' . $cell . ">\n";
}
}
$output[] = "\t</tr>\n";
}
$output[] = '</' . $area . '>';
return implode('', $output);
}
/**
* Renders an HTML attribute from an associative array
*
* @param array $attributes Associative array of attributes
*
* @return string The HTML attribute string
*
* @since 1.7.3
*/
protected function renderAttributes($attributes)
{
if (count((array) $attributes) == 0)
{
return '';
}
$return = array();
foreach ($attributes as $key => $option)
{
$return[] = $key . '="' . $option . '"';
}
return ' ' . implode(' ', $return);
}
}