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
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getColumn() |
78
|
|
|
{ |
79
|
|
|
return $this->column; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Find selected option for current item/row |
85
|
|
|
* @param Row $row |
86
|
|
|
* @return Option|NULL |
87
|
|
|
*/ |
88
|
|
|
public function getCurrentOption(Row $row) |
89
|
|
|
{ |
90
|
|
|
foreach ($this->getOptions() as $option) { |
91
|
|
|
if ($option->getValue() == $row->getValue($this->getColumn())) { |
92
|
|
|
return $option; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return NULL; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Add option to status select |
102
|
|
|
* @param mixed $value |
103
|
|
|
* @param string $text |
104
|
|
|
*/ |
105
|
|
|
public function addOption($value, $text) |
106
|
|
|
{ |
107
|
|
|
if (!is_scalar($value)) { |
108
|
|
|
throw new DataGridColumnStatusException('Option value has to be scalar'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$option = new Option($this, $value, $text); |
112
|
|
|
|
113
|
|
|
$this->options[] = $option; |
114
|
|
|
|
115
|
|
|
return $option; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param mixed $value |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function removeOption($value) |
124
|
|
|
{ |
125
|
|
|
foreach ($this->options as $key => $option) { |
126
|
|
|
if ($option->getValue() == $value) { |
127
|
|
|
unset($this->options[$key]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Column can have variables that will be passed to custom template scope |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function getTemplateVariables() |
138
|
|
|
{ |
139
|
|
|
return array_merge($this->template_variables, [ |
140
|
|
|
'options' => $this->getOptions(), |
141
|
|
|
'column' => $this->getColumn(), |
142
|
|
|
'key' => $this->getKey(), |
143
|
|
|
'status' => $this |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Should be a "caret" present in status dropdown? |
150
|
|
|
* @param bool $use_caret |
151
|
|
|
* @return static |
152
|
|
|
*/ |
153
|
|
|
public function setCaret($use_caret) |
154
|
|
|
{ |
155
|
|
|
$this->caret = (bool) $use_caret; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return boolean |
163
|
|
|
*/ |
164
|
|
|
public function hasCaret() |
165
|
|
|
{ |
166
|
|
|
return $this->caret; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|