|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Table\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Yaro\Jarboe\Table\CRUD; |
|
7
|
|
|
use Yaro\Jarboe\Table\Fields\Interfaces\FieldPropsInterface; |
|
8
|
|
|
use Yaro\Jarboe\Table\Filters\AbstractFilter; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractField implements FieldPropsInterface |
|
11
|
|
|
{ |
|
12
|
|
|
const DEFAULT_TAB_IDENT = '[extra]'; |
|
13
|
|
|
|
|
14
|
|
|
protected $model = ''; |
|
15
|
|
|
protected $title = ''; |
|
16
|
|
|
protected $name = ''; |
|
17
|
|
|
protected $readonly = false; |
|
18
|
|
|
protected $default = null; |
|
19
|
|
|
protected $hidden = [ |
|
20
|
|
|
'list' => false, |
|
21
|
|
|
'edit' => false, |
|
22
|
|
|
'create' => false, |
|
23
|
|
|
]; |
|
24
|
|
|
protected $width; |
|
25
|
|
|
protected $filter = null; |
|
26
|
|
|
protected $tab = self::DEFAULT_TAB_IDENT; |
|
27
|
|
|
protected $col = 12; |
|
28
|
|
|
|
|
29
|
372 |
|
public static function make($name = '', $title = '') |
|
30
|
|
|
{ |
|
31
|
372 |
|
$field = new static; |
|
32
|
|
|
|
|
33
|
372 |
|
if (!$title) { |
|
34
|
339 |
|
$title = preg_replace('~_~', ' ', ucfirst($name)); |
|
35
|
|
|
} |
|
36
|
372 |
|
$field->title($title); |
|
37
|
372 |
|
$field->name($name); |
|
38
|
|
|
|
|
39
|
372 |
|
return $field; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
11 |
|
public function setModel($model) |
|
43
|
|
|
{ |
|
44
|
11 |
|
$this->model = $model; |
|
45
|
11 |
|
} |
|
46
|
|
|
|
|
47
|
11 |
|
public function getModel() |
|
48
|
|
|
{ |
|
49
|
11 |
|
return $this->model; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
8 |
|
public function value(Request $request) |
|
53
|
|
|
{ |
|
54
|
8 |
|
$value = $request->get($this->name()); |
|
55
|
8 |
|
if (!$value && $this->isNullable()) { |
|
56
|
1 |
|
return null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
8 |
|
return $value; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function shouldSkip(Request $request) |
|
63
|
|
|
{ |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
11 |
|
public function col(int $col) |
|
68
|
|
|
{ |
|
69
|
11 |
|
$this->col = $col; |
|
70
|
|
|
|
|
71
|
11 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
22 |
|
public function getCol() |
|
75
|
|
|
{ |
|
76
|
22 |
|
return $this->col; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
22 |
|
public function filter(AbstractFilter $filter = null) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
22 |
|
if (is_null($filter)) { |
|
82
|
22 |
|
return $this->filter; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
11 |
|
$filter->field($this); |
|
86
|
|
|
|
|
87
|
11 |
|
$this->filter = $filter; |
|
88
|
|
|
|
|
89
|
11 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
11 |
|
public function tab(string $tab) |
|
93
|
|
|
{ |
|
94
|
11 |
|
$this->tab = $tab; |
|
95
|
|
|
|
|
96
|
11 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
22 |
|
public function getTab() |
|
100
|
|
|
{ |
|
101
|
22 |
|
return $this->tab; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
372 |
|
public function title(string $title = null) |
|
105
|
|
|
{ |
|
106
|
372 |
|
if (is_null($title)) { |
|
107
|
44 |
|
return $this->title; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
372 |
|
$this->title = $title; |
|
111
|
|
|
|
|
112
|
372 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
372 |
|
public function name(string $name = null) |
|
116
|
|
|
{ |
|
117
|
372 |
|
if (is_null($name)) { |
|
118
|
32 |
|
return $this->name; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
372 |
|
$this->name = $name; |
|
122
|
|
|
|
|
123
|
372 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
11 |
|
public function readonly(bool $isReadonly = true) |
|
127
|
|
|
{ |
|
128
|
11 |
|
$this->readonly = $isReadonly; |
|
129
|
|
|
|
|
130
|
11 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
34 |
|
public function isReadonly() |
|
134
|
|
|
{ |
|
135
|
34 |
|
return $this->readonly; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
10 |
|
public function default($value) |
|
139
|
|
|
{ |
|
140
|
10 |
|
$this->default = $value; |
|
141
|
|
|
|
|
142
|
10 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
24 |
|
public function getDefault() |
|
146
|
|
|
{ |
|
147
|
24 |
|
return $this->default; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function old($name = null) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
if (is_null($name)) { |
|
153
|
|
|
$name = $this->name(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return old($name); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function hasOld($name = null) |
|
160
|
|
|
{ |
|
161
|
|
|
return !is_null($this->old($name)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function oldOrDefault($locale = null) |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
$name = $this->name(); |
|
167
|
|
|
if ($locale) { |
|
168
|
|
|
$name .= '.'. $locale; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if ($this->hasOld($name)) { |
|
172
|
|
|
return $this->old($name); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $this->getDefault(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function oldOrAttribute($model, $default = null, $locale = null) |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
$name = $this->name(); |
|
181
|
|
|
if ($locale) { |
|
182
|
|
|
$name .= '.'. $locale; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if ($this->hasOld($name)) { |
|
186
|
|
|
return $this->old($name); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if ($this->isTranslatable()) { |
|
190
|
|
|
return $model->getTranslation($this->name(), $locale) ?: $default; |
|
191
|
|
|
} |
|
192
|
|
|
return $model->{$this->name()} ?: $default; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function getAttribute($model, $locale = null) |
|
|
|
|
|
|
196
|
|
|
{ |
|
197
|
|
|
if ($locale && $this->isTranslatable()) { |
|
198
|
|
|
return $model->getTranslation($this->name(), $locale); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $model->{$this->name()}; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
11 |
|
public function width(int $width) |
|
205
|
|
|
{ |
|
206
|
11 |
|
$this->width = $width; |
|
207
|
|
|
|
|
208
|
11 |
|
return $this; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
22 |
|
public function getWidth() |
|
212
|
|
|
{ |
|
213
|
22 |
|
return $this->width; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function hide(bool $edit = false, bool $create = false, bool $list = false) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->hidden = [ |
|
219
|
|
|
'list' => $list, |
|
220
|
|
|
'edit' => $edit, |
|
221
|
|
|
'create' => $create, |
|
222
|
|
|
]; |
|
223
|
|
|
|
|
224
|
|
|
return $this; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
10 |
|
public function hideList(bool $hide = false) |
|
228
|
|
|
{ |
|
229
|
10 |
|
$this->hidden['list'] = $hide; |
|
230
|
|
|
|
|
231
|
10 |
|
return $this; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
10 |
|
public function hideEdit(bool $hide = false) |
|
235
|
|
|
{ |
|
236
|
10 |
|
$this->hidden['edit'] = $hide; |
|
237
|
|
|
|
|
238
|
10 |
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
10 |
|
public function hideCreate(bool $hide = false) |
|
242
|
|
|
{ |
|
243
|
10 |
|
$this->hidden['create'] = $hide; |
|
244
|
|
|
|
|
245
|
10 |
|
return $this; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
22 |
|
public function hidden(string $type) |
|
|
|
|
|
|
249
|
|
|
{ |
|
250
|
22 |
|
if (!array_key_exists($type, $this->hidden)) { |
|
251
|
11 |
|
throw new \RuntimeException(sprintf("Wrong type [%s] for field's hidden attribute", $type)); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
11 |
|
return $this->hidden[$type]; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function afterStore($model, Request $request) |
|
258
|
|
|
{ |
|
259
|
|
|
// dummy |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
public function afterUpdate($model, Request $request) |
|
263
|
|
|
{ |
|
264
|
|
|
// dummy |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
7 |
|
public function isInline() |
|
268
|
|
|
{ |
|
269
|
7 |
|
return false; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
public function isRelationField() |
|
273
|
|
|
{ |
|
274
|
|
|
return false; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
public function isMultiple() |
|
278
|
|
|
{ |
|
279
|
|
|
return false; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
public function isEncode() |
|
283
|
|
|
{ |
|
284
|
|
|
return false; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
public function isMarkupRow() |
|
288
|
|
|
{ |
|
289
|
|
|
return false; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
public function isOrderable() |
|
293
|
|
|
{ |
|
294
|
|
|
return false; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
public function isAjax() |
|
298
|
|
|
{ |
|
299
|
|
|
return false; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
3 |
|
public function isNullable() |
|
303
|
|
|
{ |
|
304
|
3 |
|
return false; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
public function hasTooltip() |
|
308
|
|
|
{ |
|
309
|
|
|
return false; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
public function hasClipboardButton() |
|
313
|
|
|
{ |
|
314
|
|
|
return false; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
public function isTranslatable() |
|
318
|
|
|
{ |
|
319
|
|
|
return false; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
public function isMaskable() |
|
323
|
|
|
{ |
|
324
|
|
|
return false; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
public function getPlaceholder() |
|
328
|
|
|
{ |
|
329
|
|
|
return ''; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
abstract public function getListValue($model); |
|
|
|
|
|
|
333
|
|
|
|
|
334
|
|
|
abstract public function getEditFormValue($model); |
|
|
|
|
|
|
335
|
|
|
|
|
336
|
|
|
abstract public function getCreateFormValue(); |
|
|
|
|
|
|
337
|
|
|
|
|
338
|
|
|
// TODO: interface type-hinting |
|
339
|
|
|
public function prepare(CRUD $crud) |
|
340
|
|
|
{ |
|
341
|
|
|
if ($this->filter()) { |
|
342
|
|
|
$this->filter()->value( |
|
343
|
|
|
$this->filter()->getValue($crud->tableIdentifier()) |
|
344
|
|
|
); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
$this->setModel($crud->getModel()); |
|
348
|
|
|
|
|
349
|
|
|
if (method_exists($this, 'setRelationSearchUrl')) { |
|
350
|
|
|
$this->setRelationSearchUrl($crud->relationSearchUrl()); |
|
|
|
|
|
|
351
|
|
|
} |
|
352
|
|
|
if (method_exists($this, 'setInlineUrl')) { |
|
353
|
|
|
$this->setInlineUrl($crud->inlineUrl()); |
|
|
|
|
|
|
354
|
|
|
} |
|
355
|
|
|
if ($this->isTranslatable()) { |
|
356
|
|
|
$this->locales($crud->getLocales()); |
|
|
|
|
|
|
357
|
|
|
$this->setCurrentLocale($crud->getCurrentLocale()); |
|
|
|
|
|
|
358
|
|
|
} |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.