/
class_table.php
295 lines (278 loc) · 6.67 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
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* MyBB 1.8
* Copyright 2014 MyBB Group, All Rights Reserved
*
* Website: http://www.mybb.com
* License: http://www.mybb.com/about/license
*
*/
/**
* Generate a data grid/table.
*/
class DefaultTable
{
/**
* @var array Array of cells for the current row.
*/
private $_cells = array();
/**
* @var array Array of rows for the current table.
*/
private $_rows = array();
/**
* @var array Array of headers for the current table.
*/
private $_headers = array();
/**
* Construct an individual cell for this table.
*
* @param string $data The HTML content for this cell.
* @param array $extra Array of extra information about this cell (class, id, colspan, rowspan, width)
*/
function construct_cell($data, $extra=array())
{
$this->_cells[] = array("data" => $data, "extra" => $extra);
}
/**
* Construct a row from the earlier defined constructed cells for the table.
*
* @param array $extra Array of extra information about this row (class, id)
*/
function construct_row($extra = array())
{
$i = 1;
$cells = '';
// We construct individual cells here
foreach($this->_cells as $key => $cell)
{
$cells .= "\t\t\t<td";
if(!isset($cell['extra']['class']))
{
$cell['extra']['class'] = '';
}
if($key == 0)
{
$cell['extra']['class'] .= " first";
}
elseif(!isset($this->_cells[$key+1]))
{
$cell['extra']['class'] .= " last";
}
if($i == 2)
{
$cell['extra']['class'] .= " alt_col";
$i = 0;
}
$i++;
if($cell['extra']['class'])
{
$cells .= " class=\"".trim($cell['extra']['class'])."\"";
}
if(isset($cell['extra']['style']))
{
$cells .= " style=\"".$cell['extra']['style']."\"";
}
if(isset($cell['extra']['id']))
{
$cells .= " id=\"".$cell['extra']['id']."\"";
}
if(isset($cell['extra']['colspan']) && $cell['extra']['colspan'] > 1)
{
$cells .= " colspan=\"".$cell['extra']['colspan']."\"";
}
if(isset($cell['extra']['rowspan']) && $cell['extra']['rowspan'] > 1)
{
$cells .= " rowspan=\"".$cell['extra']['rowspan']."\"";
}
if(isset($cell['extra']['width']))
{
$cells .= " width=\"".$cell['extra']['width']."\"";
}
$cells .= ">";
$cells .= $cell['data'];
$cells .= "</td>\n";
}
$data['cells'] = $cells;
$data['extra'] = $extra;
$this->_rows[] = $data;
$this->_cells = array();
}
/**
* return the cells of a row for the table based row.
*
* @param string $row_id The id of the row you want to give it.
* @param boolean $return Whether or not to return or echo the resultant contents.
* @return string The output of the row cells (optional).
*/
function output_row_cells($row_id, $return=false)
{
$row = $this->_rows[$row_id]['cells'];
if(!$return)
{
echo $row;
}
else
{
return $row;
}
}
/**
* Count the number of rows in the table. Useful for displaying a 'no rows' message.
*
* @return int The number of rows in the table.
*/
function num_rows()
{
return count($this->_rows);
}
/**
* Construct a header cell for this table.
*
* @param string $data The HTML content for this header cell.
* @param array $extra Array of extra information for this header cell (class, style, colspan, width)
*/
function construct_header($data, $extra=array())
{
$this->_headers[] = array("data" => $data, "extra" => $extra);
}
/**
* Output this table to the browser.
*
* @param string $heading The heading for this table.
* @param int $border The border width for this table.
* @param string $class The class for this table.
* @param boolean $return Whether or not to return or echo the resultant contents.
* @return string The output of the row cells (optional).
*/
function output($heading="", $border=1, $class="general", $return=false)
{
if($return == true)
{
return $this->construct_html($heading, $border, $class);
}
else
{
echo $this->construct_html($heading, $border, $class);
}
}
/**
* Fetch the built HTML for this table.
*
* @param string $heading The heading for this table.
* @param int $border The border width for this table.
* @param string $class The class for this table.
* @param string $table_id The id for this table.
* @return string The built HTML.
*/
function construct_html($heading="", $border=1, $class=null, $table_id="")
{
$table = '';
if($border == 1)
{
$table .= "<div class=\"border_wrapper\">\n";
if($heading != "")
{
$table .= " <div class=\"title\">".$heading."</div>\n";
}
}
$table .= "<table";
if(!is_null($class))
{
if(!$class)
{
$class = "general";
}
$table .= " class=\"".$class."\"";
}
if($table_id != "")
{
$table .= " id=\"".$table_id."\"";
}
$table .= " cellspacing=\"0\">\n";
if($this->_headers)
{
$table .= "\t<thead>\n";
$table .= "\t\t<tr>\n";
foreach($this->_headers as $key => $data)
{
$table .= "\t\t\t<th";
if(!isset($data['extra']['class']))
{
$data['extra']['class'] = '';
}
if($key == 0)
{
$data['extra']['class'] .= " first";
}
elseif(!isset($this->_headers[$key+1]))
{
$data['extra']['class'] .= " last";
}
if(!empty($data['extra']['class']))
{
$table .= " class=\"".$data['extra']['class']."\"";
}
if(isset($data['extra']['style']))
{
$table .= " style=\"".$data['extra']['style']."\"";
}
if(isset($data['extra']['width']))
{
$table .= " width=\"".$data['extra']['width']."\"";
}
if(isset($data['extra']['colspan']) && $data['extra']['colspan'] > 1)
{
$table .= " colspan=\"".$data['extra']['colspan']."\"";
}
$table .= ">".$data['data']."</th>\n";
}
$table .= "\t\t</tr>\n";
$table .= "\t</thead>\n";
}
$table .= "\t<tbody>\n";
$i = 1;
foreach($this->_rows as $key => $table_row)
{
$table .= "\t\t<tr";
if(isset($table_row['extra']['id']))
{
$table .= " id=\"{$table_row['extra']['id']}\"";
}
if(!isset($table_row['extra']['class']))
{
$table_row['extra']['class'] = '';
}
if($key == 0)
{
$table_row['extra']['class'] .= " first";
}
else if(!isset($this->_rows[$key+1]))
{
$table_row['extra']['class'] .= " last";
}
if($i == 2 && !isset($table_row['extra']['no_alt_row']))
{
$table_row['extra']['class'] .= " alt_row";
$i = 0;
}
$i++;
if($table_row['extra']['class'])
{
$table .= " class=\"".trim($table_row['extra']['class'])."\"";
}
$table .= ">\n";
$table .= $table_row['cells'];
$table .= "\t\t</tr>\n";
}
$table .= "\t</tbody>\n";
$table .= "</table>\n";
// Clean up
$this->_cells = $this->_rows = $this->_headers = array();
if($border == 1)
{
$table .= "</div>";
}
return $table;
}
}