Completed
Push — master ( 9c2e1e...c9039e )
by Pavel
02:44
created

Column::setDefaultHide()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\Column;
10
11
use Nette\InvalidArgumentException;
12
use Ublaboo;
13
use Ublaboo\DataGrid\DataGrid;
14
use Ublaboo\DataGrid\Row;
15
use Ublaboo\DataGrid\Exception\DataGridException;
16
use Ublaboo\DataGrid\Exception\DataGridColumnRendererException;
17
use Ublaboo\DataGrid\Exception\DataGridHasToBeAttachedToPresenterComponentException;
18
use Nette\Utils\Html;
19
20
abstract class Column extends FilterableColumn
21
{
22
23
	/**
24
	 * @var array
25
	 */
26
	protected $replacements = [];
27
28
	/**
29
	 * @var Renderer|NULL
30
	 */
31
	protected $renderer;
32
33
	/**
34
	 * @var string
35
	 */
36
	protected $template;
37
38
	/**
39
	 * @var bool|string
40
	 */
41
	protected $sortable = FALSE;
42
43
	/**
44
	 * @var bool
45
	 */
46
	protected $sortable_reset_pagination = FALSE;
47
48
	/**
49
	 * @var null|callable
50
	 */
51
	protected $sortable_callback = NULL;
52
53
	/**
54
	 * @var array
55
	 */
56
	protected $sort;
57
58
	/**
59
	 * @var bool
60
	 */
61
	protected $template_escaping = TRUE;
62
63
	/**
64
	 * @var string
65
	 */
66
	protected $align;
67
68
	/**
69
	 * @var array
70
	 */
71
	protected $template_variables = [];
72
73
	/**
74
	 * @var callable
75
	 */
76
	protected $editable_callback;
77
78
	/**
79
	 * Cached html elements
80
	 * @var array
81
	 */
82
	protected $el_cache = [];
83
84
	/**
85
	 * @var bool
86
	 */
87
	protected $default_hide = FALSE;
88
89
90
	/**
91
	 * Render row item into template
92
	 * @param  Row   $row
93
	 * @return mixed
94
	 */
95
	public function render(Row $row)
96
	{
97
		/**
98
		 * Renderer function may be used
99
		 */
100
		try {
101
			return $this->useRenderer($row);
102
		} catch (DataGridColumnRendererException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
103
			/**
104
			 * Do not use renderer
105
			 */
106
		}
107
108
		/**
109
		 * Or replacements may be applied
110
		 */
111
		list($do_replace, $replaced) = $this->applyReplacements($row);
112
		if ($do_replace) {
113
			return $replaced;
114
		}
115
116
		return $this->getColumnValue($row);
117
	}
118
119
120
	/**
121
	 * Try to render item with custom renderer
122
	 * @param  Row   $row
123
	 * @return mixed
124
	 */
125
	public function useRenderer(Row $row)
126
	{
127
		$renderer = $this->getRenderer();
128
129
		if (!$renderer) {
130
			throw new DataGridColumnRendererException;
131
		}
132
133
		if ($renderer->getConditionCallback()) {
134
			if (!call_user_func_array($renderer->getConditionCallback(), [$row->getItem()])) {
135
				throw new DataGridColumnRendererException;
136
			}
137
138
			return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
139
		}
140
141
		return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
142
	}
143
144
145
	/**
146
	 * Should be column values escaped in latte?
147
	 * @param bool $template_escaping
148
	 */
149
	public function setTemplateEscaping($template_escaping = TRUE)
150
	{
151
		$this->template_escaping = (bool) $template_escaping;
152
153
		return $this;
154
	}
155
156
157
	public function isTemplateEscaped()
158
	{
159
		return $this->template_escaping;
160
	}
161
162
163
	/**
164
	 * Set column sortable or not
165
	 * @param bool|string $sortable
166
	 */
167
	public function setSortable($sortable = TRUE)
168
	{
169
		$this->sortable = is_string($sortable) ? $sortable : (bool) $sortable;
170
171
		return $this;
172
	}
173
174
175
	/**
176
	 * Tell whether column is sortable
177
	 * @return bool
178
	 */
179
	public function isSortable()
180
	{
181
		return (bool) $this->sortable;
182
	}
183
184
185
	/**
186
	 * Shoud be the pagination reseted after sorting?
187
	 * @param bool $sortable_reset_pagination
188
	 * @return static
189
	 */
190
	public function setSortableResetPagination($sortable_reset_pagination = TRUE)
191
	{
192
		$this->sortable_reset_pagination = (bool) $sortable_reset_pagination;
193
194
		return $this;
195
	}
196
197
198
	/**
199
	 * DO reset pagination after sorting?
200
	 * @return bool
201
	 */
202
	public function sortableResetPagination()
203
	{
204
		return $this->sortable_reset_pagination;
205
	}
206
207
208
	/**
209
	 * Set custom ORDER BY clause
210
	 * @param callable $sortable_callback
211
	 * @return static
212
	 */
213
	public function setSortableCallback(callable $sortable_callback)
214
	{
215
		$this->sortable_callback = $sortable_callback;
216
217
		return $this;
218
	}
219
220
221
	/**
222
	 * Get custom ORDER BY clause
223
	 * @return callable|null
224
	 */
225
	public function getSortableCallback()
226
	{
227
		return $this->sortable_callback;
228
	}
229
230
231
	/**
232
	 * Get column to sort by
233
	 * @return string
234
	 */
235
	public function getSortingColumn()
236
	{
237
		return is_string($this->sortable) ? $this->sortable : $this->column;
238
	}
239
240
241
	/**
242
	 * Get column name
243
	 * @return string
244
	 */
245
	public function getColumnName()
246
	{
247
		return $this->column;
248
	}
249
250
251
	/**
252
	 * Get column value of row item
253
	 * @param  Row   $row
254
	 * @return mixed
255
	 */
256
	public function getColumnValue(Row $row)
257
	{
258
		return $row->getValue($this->column);
259
	}
260
261
262
	/**
263
	 * @return string
264
	 */
265
	public function getName()
266
	{
267
		return $this->name;
268
	}
269
270
271
	/**
272
	 * Set column replacements
273
	 * @param  array $replacements
274
	 * @return Column
275
	 */
276
	public function setReplacement(array $replacements)
277
	{
278
		$this->replacements = $replacements;
279
280
		return $this;
281
	}
282
283
284
	/**
285
	 * Tell whether columns has replacements
286
	 * @return bool
287
	 */
288
	public function hasReplacements()
289
	{
290
		return (bool) $this->replacements;
291
	}
292
293
294
	/**
295
	 * Apply replacements
296
	 * @param  Row   $row
297
	 * @return array
298
	 */
299
	public function applyReplacements(Row $row)
300
	{
301
		$value = $row->getValue($this->column);
302
303
		if ((is_scalar($value) || is_null($value)) && isset($this->replacements[$value])) {
304
			return [TRUE, $this->replacements[$value]];
305
		}
306
307
		return [FALSE, NULL];
308
	}
309
310
311
	/**
312
	 * Set renderer callback and (it may be optional - the condition callback will decide)
313
	 * @param callable $renderer
314
	 */
315
	public function setRenderer($renderer, $condition_callback = NULL)
316
	{
317
		if ($this->hasReplacements()) {
318
			throw new DataGridException(
319
				"Use either Column::setReplacement() or Column::setRenderer, not both."
320
			);
321
		}
322
323
		if (!is_callable($renderer)) {
324
			throw new DataGridException(
325
				"Renderer (method Column::setRenderer()) must be callable."
326
			);
327
		}
328
329
		if (NULL != $condition_callback && !is_callable($condition_callback)) {
330
			throw new DataGridException(
331
				"Renderer (method Column::setRenderer()) must be callable."
332
			);
333
		}
334
335
		$this->renderer = new Renderer($renderer, $condition_callback);
336
337
		return $this;
338
	}
339
340
341
	/**
342
	 * Set renderer callback just if condition is truthy
343
	 * @param callable $renderer
344
	 */
345
	public function setRendererOnCondition($renderer, $condition_callback)
346
	{
347
		return $this->setRenderer($renderer, $condition_callback);
348
	}
349
350
351
	/**
352
	 * Return custom renderer callback
353
	 * @return Renderer|null
354
	 */
355
	public function getRenderer()
356
	{
357
		return $this->renderer;
358
	}
359
360
361
	/**
362
	 * Column may have its own template
363
	 * @param string $template
364
	 */
365
	public function setTemplate($template, array $template_variables = [])
366
	{
367
		$this->template = $template;
368
		$this->template_variables = $template_variables;
369
370
		return $this;
371
	}
372
373
374
	/**
375
	 * Column can have variables that will be passed to custom template scope
376
	 * @return array
377
	 */
378
	public function getTemplateVariables()
379
	{
380
		return $this->template_variables;
381
	}
382
383
384
	/**
385
	 * Tell whether column has its owntemplate
386
	 * @return bool
387
	 */
388
	public function hasTemplate()
389
	{
390
		return (bool) $this->template;
391
	}
392
393
394
	/**
395
	 * Get column template path
396
	 * @return string
397
	 */
398
	public function getTemplate()
399
	{
400
		return $this->template;
401
	}
402
403
404
	/**
405
	 * Tell whether data source is sorted by this collumn
406
	 * @return bool
407
	 */
408
	public function isSortedBy()
409
	{
410
		return (bool) $this->sort;
411
	}
412
413
414
	/**
415
	 * Tell column his sorting options
416
	 * @param array $sort
417
	 */
418
	public function setSort(array $sort)
419
	{
420
		$this->sort = $sort[$this->getSortingColumn()];
421
422
		return $this;
423
	}
424
425
426
	/**
427
	 * What sorting will be applied after next click?
428
	 * @return array
429
	 */
430
	public function getSortNext()
431
	{
432
		if ($this->sort == 'ASC') {
433
			return [$this->key => 'DESC'];
434
		} else if ($this->sort == 'DESC') {
435
			return [$this->key => NULL];
436
		}
437
438
		return [$this->key => 'ASC'];
439
	}
440
441
442
	/**
443
	 * Is sorting ascending?
444
	 * @return bool
445
	 */
446
	public function isSortAsc()
447
	{
448
		return $this->sort == 'ASC';
449
	}
450
451
452
	/**
453
	 * Set column alignment
454
	 * @param string $align
455
	 */
456
	public function setAlign($align)
457
	{
458
		$this->align = (string) $align;
459
460
		return $this;
461
	}
462
463
464
	/**
465
	 * Has column some alignment?
466
	 * @return bool [description]
467
	 */
468
	public function hasAlign()
469
	{
470
		return (bool) $this->align;
471
	}
472
473
474
	/**
475
	 * Get column alignment
476
	 * @return string
477
	 */
478
	public function getAlign()
479
	{
480
		return $this->align ?: 'left';
481
	}
482
483
484
	/**
485
	 * Set callback that will be called after inline editing
486
	 * @param callable $editable_callback
487
	 */
488
	public function setEditableCallback(callable $editable_callback)
489
	{
490
		$this->editable_callback = $editable_callback;
491
492
		return $this;
493
	}
494
495
496
	/**
497
	 * Return callback that is used after inline editing
498
	 * @return callable
499
	 */
500
	public function getEditableCallback()
501
	{
502
		return $this->editable_callback;
503
	}
504
505
506
	/**
507
	 * Is column editable?
508
	 * @return bool
509
	 */
510
	public function isEditable()
511
	{
512
		return (bool) $this->getEditableCallback();
513
	}
514
515
516
	/**
517
	 * Set attributes for both th and td element
518
	 * @param array $attrs
519
	 * @return static
520
	 */
521
	public function addAttributes(array $attrs)
522
	{
523
		$this->getElementPrototype('td')->addAttributes($attrs);
524
		$this->getElementPrototype('th')->addAttributes($attrs);
525
526
		return $this;
527
	}
528
529
530
	/**
531
	 * Get th/td column element
532
	 * @param  string   $tag th|td
533
	 * @param  string   $key
534
	 * @param  Row|NULL $row
535
	 * @return Html
536
	 */
537
	public function getElementPrototype($tag, $key = NULL, Row $row = NULL)
538
	{
539
		/**
540
		 * Get cached element
541
		 */
542
		if (empty($this->el_cache[$tag])) {
543
			$this->el_cache[$tag] = $el = $el = Html::el($tag);
544
		} else {
545
			$el = $this->el_cache[$tag];
546
		}
547
548
		/**
549
		 * If class was set by user via $el->class = '', fix it
550
		 */
551
		if (!empty($el->class) && is_string($el->class)) {
552
			$class = $el->class;
553
			unset($el->class);
554
555
			$el->class[] = $class;
556
		}
557
558
		$el->class[] = "text-{$this->getAlign()}";
559
560
		/**
561
		 * Method called from datagrid template, set appropriate classes and another attributes
562
		 */
563
		if ($key !== NULL && $row !== NULL) {
564
			$el->class[] = "col-{$key}";
565
566
			if ($tag == 'td') {
567
				if ($this->isEditable()) {
568
					$link = $this->grid->link('edit!', ['key' => $key, 'id' => $row->getId()]);
569
570
					$el->data('datagrid-editable-url', $link);
571
				}
572
			}
573
		}
574
575
		return $el;
576
	}
577
578
579
	/**
580
	 * @param bool $default_hide
581
	 * @return static
582
	 */
583
	public function setDefaultHide($default_hide = TRUE)
584
	{
585
		$this->default_hide = (bool) $default_hide;
586
587
		return $this;
588
	}
589
590
591
	public function getDefaultHide()
592
	{
593
		return $this->default_hide;
594
	}
595
596
597
	/**
598
	 * Create link to custom destination
599
	 * @param  string $href
600
	 * @param  array  $params
601
	 * @return string
602
	 * @throws DataGridHasToBeAttachedToPresenterComponentException
603
	 * @throws InvalidArgumentException
604
	 */
605
	protected function createLink($href, $params)
606
	{
607
		try {
608
			$parent = $this->grid->getParent();
609
610
			return $parent->link($href, $params);
611
		} catch (DataGridHasToBeAttachedToPresenterComponentException $e) {
612
			$parent = $this->grid->getPresenter();
613
614
		} catch (InvalidArgumentException $e) {
615
			$parent = $this->grid->getPresenter();
616
617
		}
618
619
		return $parent->link($href, $params);
620
	}
621
622
623
	/**
624
	 * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...)
625
	 * @param  Row   $row
626
	 * @param  array $params_list
627
	 * @return array
628
	 */
629
	protected function getItemParams(Row $row, array $params_list)
630
	{
631
		$return = [];
632
633
		foreach ($params_list as $param_name => $param) {
634
			$return[is_string($param_name) ? $param_name : $param] = $row->getValue($param);
635
		}
636
637
		return $return;
638
	}
639
640
}
641