1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html\Editor; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Support\Fluent; |
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @see https://editor.datatables.net/reference/option/ |
11
|
|
|
*/ |
12
|
|
|
class Field extends Fluent |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Field type. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $type = 'text'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Password constructor. |
23
|
|
|
*/ |
24
|
|
|
public function __construct($attributes = []) |
25
|
|
|
{ |
26
|
|
|
$attributes['type'] = $attributes['type'] ?? $this->type; |
27
|
|
|
|
28
|
|
|
parent::__construct($attributes); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $name |
33
|
|
|
* @param string $label |
34
|
|
|
* @return Field|Select|Password|DateTime|Checkbox|Radio|Hidden|ReadOnly|TextArea |
35
|
|
|
*/ |
36
|
|
|
public static function make($name, $label = '') |
37
|
|
|
{ |
38
|
|
|
if (is_array($name)) { |
39
|
|
|
return new static($name); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$data = [ |
43
|
|
|
'name' => $name, |
44
|
|
|
'label' => $label ?: Str::title($name), |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
return new static($data); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $label |
52
|
|
|
* @return $this |
53
|
|
|
* @see https://editor.datatables.net/reference/option/fields.label |
54
|
|
|
*/ |
55
|
|
|
public function label($label) |
56
|
|
|
{ |
57
|
|
|
$this->attributes['label'] = $label; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $name |
64
|
|
|
* @return $this |
65
|
|
|
* @see https://editor.datatables.net/reference/option/fields.name |
66
|
|
|
*/ |
67
|
|
|
public function name($name) |
68
|
|
|
{ |
69
|
|
|
$this->attributes['name'] = $name; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $data |
76
|
|
|
* @return $this |
77
|
|
|
* @see https://editor.datatables.net/reference/option/fields.data |
78
|
|
|
*/ |
79
|
|
|
public function data($data) |
80
|
|
|
{ |
81
|
|
|
$this->attributes['data'] = $data; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $type |
88
|
|
|
* @return $this |
89
|
|
|
* @see https://editor.datatables.net/reference/option/fields.type |
90
|
|
|
*/ |
91
|
|
|
public function type($type) |
92
|
|
|
{ |
93
|
|
|
$this->attributes['type'] = $type; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get options from a model. |
100
|
|
|
* |
101
|
|
|
* @param mixed $model |
102
|
|
|
* @param string $value |
103
|
|
|
* @param string $key |
104
|
|
|
* @return Field |
105
|
|
|
*/ |
106
|
|
|
public function modelOptions($model, $value, $key = 'id') |
107
|
|
|
{ |
108
|
|
|
return $this->options( |
109
|
|
|
Options::model($model, $value, $key) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set select options. |
115
|
|
|
* |
116
|
|
|
* @param array|mixed $options |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function options($options) |
120
|
|
|
{ |
121
|
|
|
if ($options instanceof Arrayable) { |
122
|
|
|
$options = $options->toArray(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->attributes['options'] = $options; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get options from a table. |
132
|
|
|
* |
133
|
|
|
* @param mixed $table |
134
|
|
|
* @param string $value |
135
|
|
|
* @param string $key |
136
|
|
|
* @param \Closure $whereCallback |
137
|
|
|
* @param string|null $key |
138
|
|
|
* @return Field |
139
|
|
|
*/ |
140
|
|
|
public function tableOptions($table, $value, $key = 'id', \Closure $whereCallback = null, $connection = null) |
141
|
|
|
{ |
142
|
|
|
return $this->options( |
143
|
|
|
Options::table($table, $value, $key, $whereCallback, $connection) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set checkbox separator. |
149
|
|
|
* |
150
|
|
|
* @param string $separator |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
|
|
public function separator($separator = ',') |
154
|
|
|
{ |
155
|
|
|
$this->attributes['separator'] = $separator; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set dateTime format. |
162
|
|
|
* |
163
|
|
|
* @param string $format |
164
|
|
|
* @return $this |
165
|
|
|
* @see https://editor.datatables.net/reference/field/datetime |
166
|
|
|
*/ |
167
|
|
|
public function format($format) |
168
|
|
|
{ |
169
|
|
|
$this->attributes['format'] = $format; |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set field default value. |
176
|
|
|
* |
177
|
|
|
* @param mixed $value |
178
|
|
|
* @return $this |
179
|
|
|
* @see https://editor.datatables.net/reference/option/fields.def |
180
|
|
|
*/ |
181
|
|
|
public function default($value) |
182
|
|
|
{ |
183
|
|
|
$this->attributes['def'] = $value; |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Set field message value. |
190
|
|
|
* |
191
|
|
|
* @param string $value |
192
|
|
|
* @return $this |
193
|
|
|
* @see https://editor.datatables.net/reference/option/fields.message |
194
|
|
|
*/ |
195
|
|
|
public function message($value) |
196
|
|
|
{ |
197
|
|
|
$this->attributes['message'] = $value; |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Set field fieldInfo value. |
204
|
|
|
* |
205
|
|
|
* @param string $value |
206
|
|
|
* @return $this |
207
|
|
|
* @see https://editor.datatables.net/reference/option/fields.fieldInfo |
208
|
|
|
*/ |
209
|
|
|
public function fieldInfo($value) |
210
|
|
|
{ |
211
|
|
|
$this->attributes['fieldInfo'] = $value; |
212
|
|
|
|
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Set field labelInfo value. |
218
|
|
|
* |
219
|
|
|
* @param string $value |
220
|
|
|
* @return $this |
221
|
|
|
* @see https://editor.datatables.net/reference/option/fields.labelInfo |
222
|
|
|
*/ |
223
|
|
|
public function labelInfo($value) |
224
|
|
|
{ |
225
|
|
|
$this->attributes['labelInfo'] = $value; |
226
|
|
|
|
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Set field entityDecode value. |
232
|
|
|
* |
233
|
|
|
* @param mixed|bool $value |
234
|
|
|
* @return $this |
235
|
|
|
* @see https://editor.datatables.net/reference/option/fields.entityDecode |
236
|
|
|
*/ |
237
|
|
|
public function entityDecode($value) |
238
|
|
|
{ |
239
|
|
|
$this->attributes['entityDecode'] = $value; |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Set field multiEditable value. |
246
|
|
|
* |
247
|
|
|
* @param mixed|bool $value |
248
|
|
|
* @return $this |
249
|
|
|
* @see https://editor.datatables.net/reference/option/fields.multiEditable |
250
|
|
|
*/ |
251
|
|
|
public function multiEditable($value) |
252
|
|
|
{ |
253
|
|
|
$this->attributes['multiEditable'] = $value; |
254
|
|
|
|
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Set field id value. |
260
|
|
|
* |
261
|
|
|
* @param string $value |
262
|
|
|
* @return $this |
263
|
|
|
* @see https://editor.datatables.net/reference/option/fields.id |
264
|
|
|
*/ |
265
|
|
|
public function id($value) |
266
|
|
|
{ |
267
|
|
|
$this->attributes['id'] = $value; |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Set field submit value. |
274
|
|
|
* |
275
|
|
|
* @param bool $value |
276
|
|
|
* @return $this |
277
|
|
|
* @see https://editor.datatables.net/reference/option/fields.submit |
278
|
|
|
*/ |
279
|
|
|
public function submit($value) |
280
|
|
|
{ |
281
|
|
|
$this->attributes['submit'] = $value; |
282
|
|
|
|
283
|
|
|
return $this; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Set field compare value. |
288
|
|
|
* |
289
|
|
|
* @param bool $value |
290
|
|
|
* @return $this |
291
|
|
|
* @see https://editor.datatables.net/reference/option/fields.compare |
292
|
|
|
*/ |
293
|
|
|
public function compare($value) |
294
|
|
|
{ |
295
|
|
|
$this->attributes['compare'] = $value; |
296
|
|
|
|
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|