forked from fxss/class.table.php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.table.php
283 lines (244 loc) · 6.55 KB
/
class.table.php
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
<?php
/**
* Table
*
* Automatic creation of html tables from the data array
*
* @author 0xSS <uxss@ya.ru>
* @version 1.0
*/
class Table
{
/**
* @var array $info Should contain a common table info and settings
*/
public static $info = false;
/**
* Synonym of Table::writeTable()
* Write table from data array
*
* @see writeTable()
* @param array $data array of data
* @param array $tableInfo array of common table info and settings
* @return void
*/
public static function write($data = false, $tableInfo = false)
{
self::writeTable($data, $tableInfo);
}
/**
* Write table from data array
*
* @param array $data array of data
* @param array $tableInfo array of common table info and settings
* @return void
*/
public static function writeTable($data = false, $tableInfo = false)
{
if($data)
{
$args = "";
$colKeys = false;
$titles = false;
//Search and collect information about the table in $tableInfo
//High priority is given to $tableInfo(second parameter)
if(!$tableInfo) $tableInfo = $data['tableInfo'];
elseif($data['tableInfo']) $tableInfo = array_replace_recursive($data['tableInfo'], $tableInfo);
unset($data['tableInfo']);
if($tableInfo)
{
//Following work will be happening with global variable $info
self::$info = $tableInfo;
unset($tableInfo);
//Collecting information about columns(titles and column keys)
if(self::$info['cols'])
{
$colKeys = self::getColKeys(self::$info['cols']);
$titles = self::getTitles(self::$info['cols']);
unset(self::$info['cols']);
}
//Collecting HTML parameters for table
$args = self::convertRulesToHtml(self::$info);
}
//Height of each row will be calculated automatically by the default
if(!isset(self::$info['rowspan'])) self::$info['rowspan'] = true;
echo "\n <table{$args}>";
//If title was set then write
if(is_array($titles) && sizeof($titles))
{
echo "\n <tr>";
foreach($titles as $title)
{
echo "\n <th>{$title}</th>";
}
echo "\n </tr>";
}
//Write the rows
foreach($data as $info)
{
self::writeRow($info, $colKeys);
}
echo "\n </table>";
//Clearing of global variables for the next use a static class
self::$info = false;
}
}
/**
* Write row of table from data array
*
* @param array $data array of data
* @param array $colKeys array of column keys. Using for manage output order
* @return void
*/
private static function writeRow($data, $colKeys)
{
if(is_array($data))
{
$rowRules = false;
$cellsRules = false;
//====================
$rowspan = self::$info['rowspan'];
$countRows = false;
$args = "";
//====================
$subRow = false;
$subKeys = false;
if($data['tableInfo'])
{
$rowRules = $data['tableInfo'];
unset($data['tableInfo']);
if($rowRules['cells'])
{
$cellsRules = $rowRules['cells'];
unset($rowRules['cells']);
}
if($rowRules['rowspan'])
{
$rowspan = $rowRules['rowspan'];
unset($rowRules['rowspan']);
}
$colKeys = self::modifyKeys($colKeys, $rowRules['keys']);
$args = self::convertRulesToHtml($rowRules);
}
foreach($data as $k => $item)
if(is_array($item)) $subRow = $item;
//If do not specify column keys then it is filled with all the elements of the array, except for sub-arrays(all cells)
if(!$colKeys) $colKeys = array_keys(array_filter($data, function($v){return !is_array($v);}));
echo "\n <tr{$args}>";
foreach($colKeys as $i => $key)
{
if(
isset($data[$key]) && $subRow &&
(
(!isset($cellsRules[$key]['rowspan']) && $rowspan)
|| (is_bool($cellsRules[$key]['rowspan']) && $cellsRules[$key]['rowspan'])
)
)
{
if(!$countRows) $countRows = (is_int($rowspan)) ? $rowspan : self::countRows($data);
if(!is_int($cellsRules[$key]['rowspan'])) $cellsRules[$key]['rowspan'] = $countRows;
unset($colKeys[$i]);
}
self::writeCell($data[$key], $cellsRules[$key]);
}
echo "\n </tr>";
if($subRow)
foreach($subRow as $row)
self::writeRow($row, $colKeys);
}
}
/**
* Write cell of table from data array
*
* @param string $data cell content
* @param array $rules parameters of cell
* @return void
*/
private static function writeCell($data, $rules)
{
$args = self::convertRulesToHtml($rules);
echo "\n <td{$args}>{$data}</td>";
}
/**
* Count the number of subrows
*
* @param array $data array of subrows
* @return int
*/
private static function countRows($data)
{
$rows = 0;
if(is_array($data))
{
unset($data['tableInfo']);
$rows = 1;
foreach($data as $item)
if(is_array($item))
foreach($item as $a)
$rows += self::countRows($a);
}
return $rows;
}
/**
* Convert array of parameters to html string
*
* @param array $info array of table/row/cell parameters
* @return string
*/
private static function convertRulesToHtml($info)
{
$args = "";
if(is_int($info['colspan'])) $args .= " colspan='{$info['colspan']}'";
if(is_int($info['rowspan'])) $args .= " rowspan='{$info['rowspan']}'";
if($info['id']) $args .= " id='{$info['id']}'";
if($info['class']) $args .= " class='{$info['class']}'";
if($info['style']) $args .= " style='{$info['style']}'";
if($info['args']) $args .= " {$info['args']}";
return $args;
}
/**
* Modify column keys by rules
*
* @param array $colKeys array of column keys
* @param array $rules array of modification rules
* @return array
*/
private static function modifyKeys($colKeys, $rules)
{
if($rules)
{
if($rules['delete'] == 'all') $colKeys = array();
else
foreach($rules['delete'] as $v)
if(array_search($v, $colKeys))
unset($colKeys[array_search($v, $colKeys)]);
foreach($rules['add'] as $v)
$colKeys[] = $v;
foreach($rules['forwarding'] as $v)
if(array_search($v['src'], $colKeys))
$colKeys[array_search($v['src'], $colKeys)] = $v['dst'];
}
return $colKeys;
}
/**
* Return array of column keys
*
* @param array $cols array of column information
* @return array
*/
private static function getColKeys($cols)
{
return array_filter(array_map(function($v){return $v['key'];}, $cols), 'strlen');
}
/**
* Return array of column headers
*
* @param array $cols array of column information
* @return array
*/
private static function getTitles($cols)
{
return array_filter(array_map(function($v){return $v['title'];}, $cols));
}
}
?>