1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Displayers; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
|
7
|
|
|
class Editable extends AbstractDisplayer |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $arguments = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Type of editable. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $type = ''; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Options of editable function. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $options = [ |
27
|
|
|
'emptytext' => '<i class="fa fa-pencil"></i>', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $attributes = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Add options for editable. |
37
|
|
|
* |
38
|
|
|
* @param array $options |
39
|
|
|
*/ |
40
|
|
|
public function addOptions($options = []) |
41
|
|
|
{ |
42
|
|
|
$this->options = array_merge($this->options, $options); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add attributes for editable. |
47
|
|
|
* |
48
|
|
|
* @param array $attributes |
49
|
|
|
*/ |
50
|
|
|
public function addAttributes($attributes = []) |
51
|
|
|
{ |
52
|
|
|
$this->attributes = array_merge($this->attributes, $attributes); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Text type editable. |
57
|
|
|
*/ |
58
|
|
|
public function text() |
59
|
|
|
{ |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Textarea type editable. |
64
|
|
|
*/ |
65
|
|
|
public function textarea() |
66
|
|
|
{ |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Select type editable. |
71
|
|
|
* |
72
|
|
|
* @param array|\Closure $options |
73
|
|
|
*/ |
74
|
|
|
public function select($options = []) |
75
|
|
|
{ |
76
|
|
|
$useClosure = false; |
77
|
|
|
|
78
|
|
|
if ($options instanceof \Closure) { |
79
|
|
|
$useClosure = true; |
80
|
|
|
$options = $options->call($this, $this->row); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$source = []; |
84
|
|
|
|
85
|
|
|
foreach ($options as $value => $text) { |
86
|
|
|
$source[] = compact('value', 'text'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($useClosure) { |
90
|
|
|
$this->addAttributes(['data-source' => json_encode($source)]); |
91
|
|
|
} else { |
92
|
|
|
$this->addOptions(compact('source')); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Date type editable. |
98
|
|
|
*/ |
99
|
|
|
public function date() |
100
|
|
|
{ |
101
|
|
|
$this->combodate(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Datetime type editable. |
106
|
|
|
*/ |
107
|
|
|
public function datetime() |
108
|
|
|
{ |
109
|
|
|
$this->combodate('YYYY-MM-DD HH:mm:ss'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Year type editable. |
114
|
|
|
*/ |
115
|
|
|
public function year() |
116
|
|
|
{ |
117
|
|
|
$this->combodate('YYYY'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Month type editable. |
122
|
|
|
*/ |
123
|
|
|
public function month() |
124
|
|
|
{ |
125
|
|
|
$this->combodate('MM'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Day type editable. |
130
|
|
|
*/ |
131
|
|
|
public function day() |
132
|
|
|
{ |
133
|
|
|
$this->combodate('DD'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Combodate type editable. |
138
|
|
|
* |
139
|
|
|
* @param string $format |
140
|
|
|
*/ |
141
|
|
|
public function combodate($format = 'YYYY-MM-DD') |
142
|
|
|
{ |
143
|
|
|
$this->type = 'combodate'; |
144
|
|
|
|
145
|
|
|
$this->addOptions([ |
146
|
|
|
'format' => $format, |
147
|
|
|
'viewformat' => $format, |
148
|
|
|
'template' => $format, |
149
|
|
|
'combodate' => [ |
150
|
|
|
'maxYear' => 2035, |
151
|
|
|
], |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function buildEditableOptions(array $arguments = []) |
156
|
|
|
{ |
157
|
|
|
$this->type = array_get($arguments, 0, 'text'); |
158
|
|
|
|
159
|
|
|
call_user_func_array([$this, $this->type], array_slice($arguments, 1)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function display() |
163
|
|
|
{ |
164
|
|
|
$this->options['name'] = $column = $this->column->getName(); |
165
|
|
|
|
166
|
|
|
$class = 'grid-editable-'.str_replace(['.', '#', '[', ']'], '-', $column); |
167
|
|
|
|
168
|
|
|
$this->buildEditableOptions(func_get_args()); |
169
|
|
|
|
170
|
|
|
$options = json_encode($this->options); |
171
|
|
|
|
172
|
|
|
Admin::script("$('.$class').editable($options);"); |
173
|
|
|
|
174
|
|
|
$attributes = [ |
175
|
|
|
'href' => '#', |
176
|
|
|
'class' => "$class", |
177
|
|
|
'data-type' => $this->type, |
178
|
|
|
'data-pk' => "{$this->getKey()}", |
179
|
|
|
'data-url' => "{$this->grid->resource()}/{$this->getKey()}", |
180
|
|
|
'data-value' => "{$this->value}", |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
if (!empty($this->attributes)) { |
184
|
|
|
$attributes = array_merge($attributes, $this->attributes); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$attributes = collect($attributes)->map(function ($attribute, $name) { |
188
|
|
|
return "$name='$attribute'"; |
189
|
|
|
})->implode(' '); |
190
|
|
|
|
191
|
|
|
$html = $this->type === 'select' ? '' : $this->value; |
192
|
|
|
|
193
|
|
|
return "<a $attributes>{$html}</a>"; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|