Completed
Push — master ( 80c93d...5b1d90 )
by Pavel
06:32
created

ColumnStatus::getTemplateVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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 Ublaboo\DataGrid\Row;
12
use Ublaboo\DataGrid\Status\Option;
13
use Ublaboo\DataGrid\DataGrid;
14
15
class ColumnStatus extends Column
16
{
17
18
	/**
19
	 * @var callable[]
20
	 */
21
	public $onChange = [];
22
23
	/**
24
	 * @var string
25
	 */
26
	protected $key;
27
28
	/**
29
	 * @var array
30
	 */
31
	protected $options = [];
32
33
	/**
34
	 * @var boolean
35
	 */
36
	protected $caret = TRUE;
37
38
39
	/**
40
	 * @param DataGrid $grid
41
	 * @param string   $key
42
	 * @param string   $column
43
	 * @param string   $name
44
	 */
45
	public function __construct(DataGrid $grid, $key, $column, $name)
46
	{
47
		parent::__construct($grid, $column, $name);
48
49
		$this->key = $key;
50
51
		$this->setTemplate(__DIR__ . '/../templates/column_status.latte');
52
	}
53
54
55
	/**
56
	 * @return string
57
	 */
58
	public function getKey()
59
	{
60
		return $this->key;
61
	}
62
63
64
	/**
65
	 * @return array
66
	 */
67
	public function getOptions()
68
	{
69
		return $this->options;
70
	}
71
72
73
	/**
74
	 * @return string
75
	 */
76
	public function getColumn()
77
	{
78
		return $this->column;
79
	}
80
81
82
	/**
83
	 * Find selected option for current item/row
84
	 * @param  Row    $row
85
	 * @return Option|NULL
86
	 */
87
	public function getCurrentOption(Row $row)
88
	{
89
		foreach ($this->getOptions() as $option) {
90
			if ($option->getValue() == $row->getValue($this->getColumn())) {
91
				return $option;
92
			}
93
		}
94
95
		return NULL;
96
	}
97
98
99
	/**
100
	 * Add option to status select
101
	 * @param mixed $value
102
	 * @param string $text
103
	 */
104
	public function addOption($value, $text)
105
	{
106
		$option = new Option($this, $value, $text);
107
108
		$this->options[] = $option;
109
110
		return $option;
111
	}
112
113
114
	/**
115
	 * Column can have variables that will be passed to custom template scope
116
	 * @return array
117
	 */
118
	public function getTemplateVariables()
119
	{
120
		return array_merge($this->template_variables, [
121
			'options' => $this->getOptions(),
122
			'column'  => $this->getColumn(),
123
			'key'     => $this->getKey()
124
		]);
125
	}
126
127
128
	/**
129
	 * Should be a "caret" present in status dropdown?
130
	 * @param bool $use_caret
131
	 * @return static
132
	 */
133
	public function setCaret($use_caret)
134
	{
135
		$this->caret = (bool) $use_caret;
136
137
		return $this;
138
	}
139
140
141
	/**
142
	 * @return boolean
143
	 */
144
	public function hasCaret()
145
	{
146
		return $this->caret;
147
	}
148
149
}
150