-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.php
314 lines (283 loc) · 6.46 KB
/
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* The Ztools_Table component is used to maintain set of data and
* to create nice looking dataset table for the view.
*
* @copyright 2011 Artur Gajewski
* @license http://framework.zend.com/license BSD License
* @version Release: @package_version@
* @link http://ztools.arturgajewski.com
* @since Class available since Release 1.2.5
*/
class Ztools_Table
{
/**
* Data string
*
* @var string $_data
*/
private $_data = array();
/**
* Table output container
*
* @var string $_table
*/
private $_table;
/**
* List of columns to be rendered in the table
*
* @var array $_columns
*/
private $_columns = array();
/**
* ID of the table to be rendered
*
* @var string $_id
*/
private $_id;
/**
* CSS class of the table to be rendered
*
* @var string $_class
*/
private $_class;
/**
* Should the counter be visible
* Possible values: above / below
* @var boolean $_count
*/
private $_count = false;
/**
* Resultset's minimum to be displayed
* @var int $_min
*/
private $_min = 1;
/**
* Resultset's minimum + offset to be displayed
* @var int $_offset
*/
private $_offset = 100;
/**
* Sets the data container columns.
*
* @param array $columns
* @throws Ztools_Table_Exception
* @return Ztools_Table
*/
public function setColumns($columns)
{
if (!$this->isAssociativeArray($columns)) {
throw new Ztools_Table_Exception("Array must have \"key => value\" data for each column");
}
if (is_array($columns)) {
$this->_columns = $columns;
} else {
throw new Ztools_Table_Exception("Column data must be an array");
}
return $this;
}
/**
* Gets the data container columns.
*
* @return $this->_columns
*/
public function getColumns()
{
return $this->_columns;
}
/**
* Sets the table ID value.
*
* @param string $id
* @throws Ztools_Table_Exception
* @return Ztools_Table
*/
public function setId($id)
{
if (!$id) {
throw new Ztools_Table_Exception("Table ID must have a value");
}
$this->_id = $id;
return $this;
}
/**
* Get the table ID value.
*
* @return $this->_id
*/
public function getId()
{
return $this->_id;
}
/**
* Sets the table CLASS value.
*
* @param string $class
* @throws Ztools_Table_Exception
* @return Ztools_Table
*/
public function setClass($class)
{
if (!$class) {
throw new Ztools_Table_Exception("Table CLASS must have a value");
}
$this->_class = $class;
return $this;
}
/**
* Get the table CLASS value.
*
* @return $this->_class
*/
public function getClass()
{
return $this->_class;
}
/**
* Sets the result row count.
*
* @param boolean $value
* @throws Ztools_Table_Exception
* @return Ztools_Table
*/
public function setCount($value)
{
$this->_count = $value;
return $this;
}
/**
* Gets the result row count.
*
* @return $this->_count
*/
public function getCount()
{
return $this->_count;
}
/**
* Sets the first row's value.
*
* @param int $min
* @throws Ztools_Table_Exception
* @return Ztools_Table
*/
public function setMin($min)
{
$this->_min = $min;
return $this;
}
/**
* Gets the first row's value.
*
* @return $this->_min
*/
public function getMin()
{
return $this->_min;
}
/**
* Sets the last row's value (min+offset).
*
* @param int $offset
* @throws Ztools_Table_Exception
* @return Ztools_Table
*/
public function setOffset($offset)
{
$this->_offset = $offset;
return $this;
}
/**
* Gets the last row's value (min+offset).
*
* @return $this->_offset
*/
public function getOffset()
{
return $this->_offset;
}
/**
* Sets the data container.
*
* @param string $value
* @return Ztools_Table
*/
public function setData($value)
{
$this->_data = $value;
return $this;
}
/**
* Gets the data container.
*
* @return $this->_data
*/
public function getData()
{
return $this->_data;
}
/**
* Adds the tag to be removed.
*
* @param mixed $value
* @return $this->_table
*/
public function createTable()
{
$this->_table = '';
if (!is_array($this->_data)) {
throw new Ztools_Table_Exception('Data is not an array');
}
$tableOptions = array();
if ($this->_id) {
$tableOptions[] = 'id="' . $this->_id . '"';
}
if ($this->_class) {
$tableOptions[] = 'class="' . $this->_class . '"';
}
if ($tableOptions) {
$this->_table .= '<table ' . implode(" ", $tableOptions) . '>';
} else {
$this->_table .= '<table>';
}
$this->_table .= '<tr>';
if ($this->_count) {
$this->_table .= '<th></th>';
}
foreach ($this->_columns as $key => $value) {
$this->_table .= '<th>' . $value . '</th>';
}
$this->_table .= '</tr>';
$count = 1;
$current = 1;
foreach ($this->_data as $row) {
if ($current >= $this->_min && $current < ($this->_min + $this->_offset)) {
$this->_table .= '<tr>';
if ($this->_count) {
$this->_table .= '<td>' . $count . '</td>';
}
foreach ($this->_columns as $key => $value) {
$this->_table .= '<td>' . $row[$key] . '</td>';
}
$count++;
}
$this->_table .= '</tr>';
$current++;
}
$this->_table .= '</table>';
return $this->_table;
}
/**
* Check to see if array is of type associative
*
* @param array $array
* @return boolean
*/
private function isAssociativeArray($array)
{
if (is_array($array) && !is_numeric(array_shift(array_keys($array)))) {
return true;
}
return false;
}
}