Column::setAlign()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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