forked from janmarek/gridito
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Column.php
390 lines (320 loc) · 7.04 KB
/
Column.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
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
<?php
namespace Gridito;
use Nette\Utils\Strings;
use Nette\Utils\Html;
/**
* Grid column
*
* @author Jan Marek
* @license MIT
*/
class Column extends \Nette\Application\UI\Control
{
// <editor-fold defaultstate="collapsed" desc="variables">
/** @var string */
private $label;
/** @var callback */
private $renderer = null;
/** @var int */
private $maxlen = null;
/** @var string */
private $type = 'string';
/** @var bool */
private $sortable = false;
/** @var bool */
private $editable = false;
/** @var string */
private $dateTimeFormat = "j.n.Y G:i";
/** @var string|callable */
private $cellClass = null;
/** @var string */
private $format = null;
/** @var string */
public $spamProtection = null;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="getters & setters">
public function setCellClass($class)
{
$this->cellClass = $class;
return $this;
}
public function getCellClass($iterator, $row)
{
if (is_callable($this->cellClass)) {
return call_user_func($this->cellClass, $iterator, $row);
} elseif (is_string($this->cellClass)) {
return $this->cellClass;
} else {
return null;
}
}
/**
* Get label
* @return string
*/
public function getLabel()
{
return $this->label;
}
/**
* Set label
* @param string label
* @return Column
*/
public function setLabel($label)
{
$this->label = $label;
return $this;
}
/**
* Get cell renderer
* @return callback
*/
public function getRenderer()
{
return $this->renderer;
}
/**
* Set cell renderer
* @param callback cell renderer
* @return Column
*/
public function setRenderer($cellRenderer)
{
$this->renderer = $cellRenderer;
return $this;
}
/**
* Set maximal length of cell
* @param $maxlen
* @return Column
*/
public function setLength($maxlen)
{
$this->maxlen = $maxlen;
return $this;
}
/**
* Get maximal length of cell
* @return int
*/
public function getLength()
{
return $this->maxlen;
}
/**
* Set the type of cell
* @param string type
* @return Column
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get the type of cell
* @return string type
*/
public function getType($type)
{
return $this->type;
}
/**
* Set format of the cell
* @param mixed format
* @return Column
*/
public function setFormat($format)
{
$this->format = $format;
return $this;
}
/**
* Get the format
* @return mixed
*/
public function getFormat()
{
return $this->format;
}
/**
* Is sortable?
* @return bool
*/
public function isSortable() {
return $this->sortable;
}
/**
* Set sortable
* @param bool sortable
* @return Column
*/
public function setSortable($sortable) {
$this->sortable = $sortable;
return $this;
}
/**
* Set editable
* @param bool editable
* @return Column
*/
public function setEditable($editable) {
$this->editable = $editable;
return $this;
}
/**
* Is editable?
* @return bool
*/
public function isEditable() {
return $this->editable;
}
/**
* Get sorting
* @return string|null asc, desc or null
*/
public function getSorting()
{
$grid = $this->getGrid();
if ($grid->sortColumn === $this->getName()) {
return $grid->sortType;
} else {
return null;
}
}
/**
* Get date/time format
* @return string
*/
public function getDateTimeFormat() {
return $this->dateTimeFormat;
}
/**
* Set date/time format
* @param string datetime format
* @return Column
*/
public function setDateTimeFormat($dateTimeFormat) {
$this->dateTimeFormat = $dateTimeFormat;
return $this;
}
/**
* Get grid
* @return Grid
*/
public function getGrid() {
return $this->getParent()->getParent();
}
// </editor-fold>
/**
* Render boolean
* @param bool value
*/
public static function renderBoolean($value)
{
$icon = $value ? "check" : "closethick";
$el = Html::el('span');
$el->data['value'] = $value ? '1' : '0';
$el->data['type'] = 'bool';
$el->add(Html::el("span class='ui-icon ui-icon-$icon'"));
return $el;
}
/**
* Render datetime
* @param Datetime value
* @param string datetime format
*/
public static function renderDateTime($value, $format)
{
return $value->format($format);
}
/**
* Render the text, takes care of length
* @param string $text text to render
* @param int $maxlen maximum length of text
*/
public static function renderText($text, $maxlen)
{
if (is_null($maxlen) || Strings::length($text) < $maxlen) {
return Html::el('span')->setText($text);
} else {
return Html::el('span')->title($text)
->setText(Strings::truncate($text, $maxlen));
}
}
/**
* Render the email address, takes care of length
* @param string $email email address
* @param int $maxlen maximum length of text
* @return mixed
*/
public static function renderEmail($email, $maxlen)
{
if (is_null($this->spamProtection) {
$href = $email;
$text = htmlspecialchars($email, ENT_QUOTES);
} else {
$href = str_replace('@', $this->spamProtection, $email);
$text = str_replace('@',
'@<span style="display:none;">'
. $this->spamProtection
. '</span>',
htmlspecialchars($email, ENT_QUOTES)
);
}
$el = Html::el('a')->href('mailto:' . $href);
if (is_null($maxlen) || Strings::length($email) < $maxlen) {
return $el->setHtml($text);
} else {
return $el->title($href)
->setText(Strings::truncate($email, $maxlen));
}
}
/**
* Default cell renderer
* @param mixed $record
* @param Column $column
* @return mixed
*/
public function defaultCellRenderer($record, $column) {
$name = $column->getName();
$value = $record->$name;
// boolean
if (in_array($this->type, array('bool', 'boolean')) || is_bool($value)) {
return self::renderBoolean($value);
// date
} elseif ($value instanceof \DateTime) {
return self::renderDateTime($value, $this->dateTimeFormat);
// email
} elseif ($this->type == 'email') {
return self::renderEmail($value, $this->maxlen);
// other
} else {
if (!is_null($this->format)) {
$value = Grid::formatRecordString($record, $this->format);
}
return self::renderText($value, $this->maxlen);
}
}
/**
* Render cell
* @param mixed record
*/
public function renderCell($record) {
$column = call_user_func($this->renderer ?: array($this, "defaultCellRenderer"), $record, $this);
if (!($column instanceOf Html)) {
$column = Html::el('span')->setText($column);
}
if ($this->editable) {
$column->class[] = 'editable';
if (!isset($column->data['value'])) {
$column->data['value'] = $column->getText();
}
$column->data['url'] = $this->getGrid()->link('edit!');
$column->data['name'] = $this->getName();
$column->data['id'] = $this->getGrid()->getModel()->getUniqueId($record);
}
echo $column;
}
}