Completed
Pull Request — master (#162)
by Martin
02:54
created

Column::createLink()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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