|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ZfTable ( Module for Zend Framework 2) |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2013 Piotr Duda [email protected] |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace ZfTable; |
|
10
|
|
|
|
|
11
|
|
|
use ZfTable\AbstractElement; |
|
12
|
|
|
use ZfTable\Cell; |
|
13
|
|
|
use ZfTable\Decorator\DecoratorFactory; |
|
14
|
|
|
|
|
15
|
|
|
class Header extends AbstractElement |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Name of header (should be the same like name in database) |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $name; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Title of header |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $title; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Width of the column |
|
33
|
|
|
* |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $width; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Name of table alias for defined column |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $tableAlias; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Cell object |
|
48
|
|
|
* @var Cell |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $cell; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Flag to inform if column should be sortable |
|
54
|
|
|
* |
|
55
|
|
|
* @var boolean |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $sortable = true; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Check if column is separatable |
|
61
|
|
|
* |
|
62
|
|
|
* @var boolean |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $separatable = false; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Check if column is editable |
|
68
|
|
|
* |
|
69
|
|
|
* @var boolean |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $editable = false; |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Table of options |
|
76
|
|
|
* |
|
77
|
|
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $options = array(); |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Static array exchanging ordering (when column is ascending, in data-ordering should be desc) |
|
83
|
|
|
* |
|
84
|
|
|
* @var array |
|
85
|
|
|
*/ |
|
86
|
|
|
protected static $orderReverse = array( |
|
87
|
|
|
'asc' => 'desc', |
|
88
|
|
|
'desc' => 'asc' |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Array of options |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $name |
|
95
|
|
|
* @param array $options |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __construct($name, $options = array()) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->name = $name; |
|
100
|
|
|
$this->cell = new Cell($this); |
|
101
|
|
|
$this->setOptions($options); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set options like title, width, order, sortable |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $name |
|
108
|
|
|
* @param array $options |
|
109
|
|
|
* @return Decorator\Header\AbstractHeaderDecorator |
|
110
|
|
|
*/ |
|
111
|
|
|
public function addDecorator($name, $options = array()) |
|
112
|
|
|
{ |
|
113
|
|
|
$decorator = DecoratorFactory::factoryHeader($name, $options); |
|
114
|
|
|
$this->attachDecorator($decorator); |
|
115
|
|
|
$decorator->setHeader($this); |
|
116
|
|
|
return $decorator; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Set options like title, width, order |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $options |
|
123
|
|
|
* @return $this |
|
124
|
|
|
*/ |
|
125
|
|
|
public function setOptions($options) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->title = (isset($options['title'])) ? $options['title'] : ''; |
|
128
|
|
|
$this->width = (isset($options['width'])) ? $options['width'] : ''; |
|
129
|
|
|
$this->order = (isset($options['order'])) ? $options['order'] : true; |
|
|
|
|
|
|
130
|
|
|
$this->sortable = (isset($options['sortable'])) ? $options['sortable'] : true; |
|
131
|
|
|
$this->separatable = (isset($options['separatable'])) ? $options['separatable'] : $this->getSeparatable(); |
|
132
|
|
|
$this->tableAlias = (isset($options['tableAlias'])) ? $options['tableAlias'] : ''; |
|
133
|
|
|
|
|
134
|
|
|
if (isset($options['editable']) && $options['editable'] == true) { |
|
135
|
|
|
$this->editable = $options['editable']; |
|
136
|
|
|
$this->getCell()->addDecorator('editable', array()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get width of column |
|
145
|
|
|
* |
|
146
|
|
|
* @return int |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getWidth() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->width; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Set width of column |
|
155
|
|
|
* |
|
156
|
|
|
* @param int $width |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setWidth($width) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->width = $width; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Return cell object |
|
165
|
|
|
* |
|
166
|
|
|
* @return Cell |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getCell() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->cell; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Set cell object |
|
175
|
|
|
* |
|
176
|
|
|
* @param Cell $cell |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setCell($cell) |
|
179
|
|
|
{ |
|
180
|
|
|
$this->cell = $cell; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Set name of header |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $name |
|
187
|
|
|
* @return $this |
|
188
|
|
|
*/ |
|
189
|
|
|
public function setName($name) |
|
190
|
|
|
{ |
|
191
|
|
|
$this->name = $name; |
|
192
|
|
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Get name of header |
|
197
|
|
|
* |
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getName() |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->name; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Set header title |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $title |
|
209
|
|
|
* @return $this |
|
210
|
|
|
*/ |
|
211
|
|
|
public function setTitle($title) |
|
212
|
|
|
{ |
|
213
|
|
|
$this->title = $title; |
|
214
|
|
|
return $this; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Get header title |
|
219
|
|
|
* |
|
220
|
|
|
* @return string |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getTitle() |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->title; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Get sortable flag |
|
229
|
|
|
* |
|
230
|
|
|
* @return boolean |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getSortable() |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->sortable; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Set flat to inform about sortable |
|
239
|
|
|
* |
|
240
|
|
|
* @param boolean $sortable |
|
241
|
|
|
*/ |
|
242
|
|
|
public function setSortable($sortable) |
|
243
|
|
|
{ |
|
244
|
|
|
$this->sortable = $sortable; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Get flat to inform about separable |
|
249
|
|
|
* |
|
250
|
|
|
* @return boolean |
|
251
|
|
|
*/ |
|
252
|
|
|
public function getSeparatable() |
|
253
|
|
|
{ |
|
254
|
|
|
return $this->separatable; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Flag to inform about for all cell in header |
|
259
|
|
|
* |
|
260
|
|
|
* @param boolean $separatable |
|
261
|
|
|
*/ |
|
262
|
|
|
public function setSeparatable($separatable) |
|
263
|
|
|
{ |
|
264
|
|
|
$this->separatable = $separatable; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* |
|
269
|
|
|
* @return boolean |
|
270
|
|
|
*/ |
|
271
|
|
|
public function getEditable() |
|
272
|
|
|
{ |
|
273
|
|
|
return $this->editable; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Flag to inform about for all cell in header |
|
278
|
|
|
* |
|
279
|
|
|
* @param boolean $editable |
|
280
|
|
|
*/ |
|
281
|
|
|
public function setEditable($editable) |
|
282
|
|
|
{ |
|
283
|
|
|
$this->editable = $editable; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Get list of options |
|
288
|
|
|
* |
|
289
|
|
|
* @return array |
|
290
|
|
|
*/ |
|
291
|
|
|
public function getOptions() |
|
292
|
|
|
{ |
|
293
|
|
|
return $this->options; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Get header title |
|
298
|
|
|
* |
|
299
|
|
|
* @return string |
|
300
|
|
|
*/ |
|
301
|
|
|
public function getTableAlias() |
|
302
|
|
|
{ |
|
303
|
|
|
return $this->tableAlias; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Set reference to table |
|
308
|
|
|
* |
|
309
|
|
|
* @param $table |
|
310
|
|
|
* @return void|\ZfTable\AbstractCommon |
|
311
|
|
|
*/ |
|
312
|
|
|
public function setTable($table) |
|
313
|
|
|
{ |
|
314
|
|
|
$this->table = $table; |
|
315
|
|
|
$this->getCell()->setTable($table); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Init header (like asc, desc, column name ) |
|
320
|
|
|
*/ |
|
321
|
|
|
protected function initRendering() |
|
322
|
|
|
{ |
|
323
|
|
|
$paramColumn = $this->getTable()->getParamAdapter()->getColumn(); |
|
324
|
|
|
$paramOrder = $this->getTable()->getParamAdapter()->getOrder(); |
|
325
|
|
|
$order = ($paramColumn == $this->getName()) ? self::$orderReverse[$paramOrder] : 'asc'; |
|
326
|
|
|
|
|
327
|
|
|
if ($this->getSortable()) { |
|
328
|
|
|
$classSorting = ($paramColumn == $this->getName()) |
|
329
|
|
|
? 'sorting_' . self::$orderReverse[$paramOrder] : 'sorting'; |
|
330
|
|
|
$this->addClass($classSorting); |
|
331
|
|
|
$this->addClass('sortable'); |
|
332
|
|
|
$this->addAttr('data-order', $order); |
|
333
|
|
|
$this->addAttr('data-column', $this->getName()); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
|
|
337
|
|
|
if ($this->width) { |
|
338
|
|
|
$this->addAttr('width', $this->width); |
|
339
|
|
|
} |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Rendering header element |
|
344
|
|
|
* |
|
345
|
|
|
* @return string |
|
346
|
|
|
*/ |
|
347
|
|
|
public function render() |
|
348
|
|
|
{ |
|
349
|
|
|
$this->initRendering(); |
|
350
|
|
|
$render = $this->getTitle(); |
|
351
|
|
|
|
|
352
|
|
|
foreach ($this->decorators as $decorator) { |
|
353
|
|
|
$render = $decorator->render($render); |
|
354
|
|
|
} |
|
355
|
|
|
return sprintf('<th %s >%s</th>', $this->getAttributes(), $render); |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: