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
|
|
|
use Ublaboo\DataGrid\Exception\DataGridColumnStatusException; |
15
|
|
|
|
16
|
|
|
class ColumnStatus extends Column |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var callable[] |
21
|
|
|
*/ |
22
|
|
|
public $onChange = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $key; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $options = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var boolean |
36
|
|
|
*/ |
37
|
|
|
protected $caret = TRUE; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param DataGrid $grid |
42
|
|
|
* @param string $key |
43
|
|
|
* @param string $column |
44
|
|
|
* @param string $name |
45
|
|
|
*/ |
46
|
|
|
public function __construct(DataGrid $grid, $key, $column, $name) |
47
|
|
|
{ |
48
|
|
|
parent::__construct($grid, $key, $column, $name); |
49
|
|
|
|
50
|
|
|
$this->key = $key; |
51
|
|
|
|
52
|
|
|
$this->setTemplate(__DIR__ . '/../templates/column_status.latte'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getKey() |
60
|
|
|
{ |
61
|
|
|
return $this->key; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getOptions() |
69
|
|
|
{ |
70
|
|
|
return $this->options; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get particular option |
76
|
|
|
* @param mixed $value |
77
|
|
|
* @return Option |
78
|
|
|
* @throws DataGridColumnStatusException |
79
|
|
|
*/ |
80
|
|
|
public function getOption($value) |
81
|
|
|
{ |
82
|
|
|
foreach ($this->options as $option) { |
83
|
|
|
if ($option->getValue() === $value) { |
84
|
|
|
return $option; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
throw new DataGridColumnStatusException("Option [$value] does not exist"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getColumn() |
96
|
|
|
{ |
97
|
|
|
return $this->column; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Find selected option for current item/row |
103
|
|
|
* @param Row $row |
104
|
|
|
* @return Option|NULL |
105
|
|
|
*/ |
106
|
|
|
public function getCurrentOption(Row $row) |
107
|
|
|
{ |
108
|
|
|
foreach ($this->getOptions() as $option) { |
109
|
|
|
if ($option->getValue() == $row->getValue($this->getColumn())) { |
110
|
|
|
return $option; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return NULL; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Add option to status select |
120
|
|
|
* @param mixed $value |
121
|
|
|
* @param string $text |
122
|
|
|
* @return Option |
123
|
|
|
* @throws DataGridColumnStatusException |
124
|
|
|
*/ |
125
|
|
|
public function addOption($value, $text) |
126
|
|
|
{ |
127
|
|
|
if (!is_scalar($value)) { |
128
|
|
|
throw new DataGridColumnStatusException('Option value has to be scalar'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$option = new Option($this, $value, $text); |
132
|
|
|
|
133
|
|
|
$this->options[] = $option; |
134
|
|
|
|
135
|
|
|
return $option; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set all options at once |
141
|
|
|
* @param array $options |
142
|
|
|
* @return static |
143
|
|
|
*/ |
144
|
|
|
public function setOptions(array $options) |
145
|
|
|
{ |
146
|
|
|
foreach ($options as $value => $text) { |
147
|
|
|
$this->addOption($value, $text); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param mixed $value |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
public function removeOption($value) |
159
|
|
|
{ |
160
|
|
|
foreach ($this->options as $key => $option) { |
161
|
|
|
if ($option->getValue() == $value) { |
162
|
|
|
unset($this->options[$key]); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Column can have variables that will be passed to custom template scope |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
public function getTemplateVariables() |
173
|
|
|
{ |
174
|
|
|
return array_merge($this->template_variables, [ |
175
|
|
|
'options' => $this->getOptions(), |
176
|
|
|
'column' => $this->getColumn(), |
177
|
|
|
'key' => $this->getKey(), |
178
|
|
|
'status' => $this |
179
|
|
|
]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Should be a "caret" present in status dropdown? |
185
|
|
|
* @param bool $use_caret |
186
|
|
|
* @return static |
187
|
|
|
*/ |
188
|
|
|
public function setCaret($use_caret) |
189
|
|
|
{ |
190
|
|
|
$this->caret = (bool) $use_caret; |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return boolean |
198
|
|
|
*/ |
199
|
|
|
public function hasCaret() |
200
|
|
|
{ |
201
|
|
|
return $this->caret; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
} |
205
|
|
|
|