1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Fields; |
4
|
|
|
|
5
|
|
|
use Akibatech\Crud\Services\CrudFields; |
6
|
|
|
use Illuminate\View\View; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Field |
10
|
|
|
* |
11
|
|
|
* @package Akibatech\Crud\Fields |
12
|
|
|
*/ |
13
|
|
|
abstract class Field |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
const TYPE = 'type'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var CrudFields |
22
|
|
|
*/ |
23
|
|
|
protected $fields; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $identifier; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $label; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $placeholder; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $rules; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Field constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $identifier |
49
|
|
|
* @param array|string $rules |
50
|
|
|
*/ |
51
|
|
|
public function __construct($identifier, $rules = null) |
52
|
|
|
{ |
53
|
|
|
$this->identifier = $identifier; |
54
|
|
|
|
55
|
|
|
if (!is_null($rules)) |
56
|
|
|
{ |
57
|
|
|
$this->withRules($rules); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructs staticly. |
63
|
|
|
* |
64
|
|
|
* @param string $idenfitier |
65
|
|
|
* @param null|string|array $rules |
66
|
|
|
* @return static |
67
|
|
|
*/ |
68
|
|
|
public static function handle($idenfitier, $rules = null) |
69
|
|
|
{ |
70
|
|
|
return (new static($idenfitier, $rules)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add validation rules to the field. |
76
|
|
|
* |
77
|
|
|
* @param string|array $rules |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
public function withRules($rules) |
81
|
|
|
{ |
82
|
|
|
if (is_array($rules)) |
83
|
|
|
{ |
84
|
|
|
foreach ($rules as $rule) |
85
|
|
|
{ |
86
|
|
|
$this->addRule($rule); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
else |
90
|
|
|
{ |
91
|
|
|
if (is_string($rules)) |
92
|
|
|
{ |
93
|
|
|
if (stripos($rules, '|') !== false) |
94
|
|
|
{ |
95
|
|
|
$rules = explode('|', $rules); |
96
|
|
|
|
97
|
|
|
return $this->withRules($rules); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->withRules([$rules]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Add a validation rule. |
109
|
|
|
* |
110
|
|
|
* @param string $rule |
111
|
|
|
* @return self |
112
|
|
|
*/ |
113
|
|
|
public function addRule($rule) |
114
|
|
|
{ |
115
|
|
|
$this->rules[] = $rule; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param CrudFields $fields |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
|
|
public function setFields(CrudFields $fields) |
125
|
|
|
{ |
126
|
|
|
$this->fields = $fields; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the field identifier. |
133
|
|
|
* |
134
|
|
|
* @param void |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function getIdentifier() |
138
|
|
|
{ |
139
|
|
|
return $this->identifier; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set a custom label for the field. |
144
|
|
|
* |
145
|
|
|
* @param string $name |
146
|
|
|
* @return self |
147
|
|
|
*/ |
148
|
|
|
public function withLabel($name) |
149
|
|
|
{ |
150
|
|
|
$this->label = $name; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Defines a placeholder for the field. |
157
|
|
|
* |
158
|
|
|
* @param string $placeholder |
159
|
|
|
* @return self |
160
|
|
|
*/ |
161
|
|
|
public function withPlaceholder($placeholder) |
162
|
|
|
{ |
163
|
|
|
$this->placeholder = $placeholder; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Appends an help message to the input. |
170
|
|
|
* |
171
|
|
|
* @param string $help |
172
|
|
|
* @return self |
173
|
|
|
*/ |
174
|
|
|
public function withHelp($help) |
175
|
|
|
{ |
176
|
|
|
$this->help = $help; |
|
|
|
|
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Render the field form. |
183
|
|
|
* |
184
|
|
|
* @param void |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function form() |
188
|
|
|
{ |
189
|
|
|
return $this->getForm()->render(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the form view. |
194
|
|
|
* |
195
|
|
|
* @param void |
196
|
|
|
* @return View |
197
|
|
|
*/ |
198
|
|
|
protected function getForm() |
199
|
|
|
{ |
200
|
|
|
return view()->make($this->getViewName())->with($this->getViewBaseVariables()); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the field view name. |
205
|
|
|
* |
206
|
|
|
* @param void |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
abstract public function getViewName(); |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns additionnal variables to the views. |
213
|
|
|
* |
214
|
|
|
* @param void |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
|
|
protected function getViewVariables() |
218
|
|
|
{ |
219
|
|
|
return []; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Returns all base variables for the view. |
224
|
|
|
* |
225
|
|
|
* @param void |
226
|
|
|
* @return array |
227
|
|
|
*/ |
228
|
|
|
protected function getViewBaseVariables() |
229
|
|
|
{ |
230
|
|
|
$base_variables = [ |
231
|
|
|
'field' => $this, |
232
|
|
|
'has_error' => $this->hasError(), |
233
|
|
|
'error' => $this->getError(), |
234
|
|
|
'placeholder' => $this->getPlaceholder(), |
235
|
|
|
'help' => $this->getHelp(), |
236
|
|
|
'has_old' => $this->hasOld(), |
237
|
|
|
'old' => $this->getOld(), |
238
|
|
|
'label' => $this->getLabel(), |
239
|
|
|
'name' => $this->identifier, |
240
|
|
|
'id' => 'field-' . $this->identifier, |
241
|
|
|
'value' => $this->getValue() |
242
|
|
|
]; |
243
|
|
|
|
244
|
|
|
return array_merge($base_variables, $this->getViewVariables()); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Checks if the field has an error. |
249
|
|
|
* |
250
|
|
|
* @param void |
251
|
|
|
* @return bool |
252
|
|
|
*/ |
253
|
|
|
public function hasError() |
254
|
|
|
{ |
255
|
|
|
return $this->fields->getErrors()->has($this->identifier); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Returns the error. |
260
|
|
|
* |
261
|
|
|
* @param void |
262
|
|
|
* @return null|string |
263
|
|
|
*/ |
264
|
|
|
public function getError() |
265
|
|
|
{ |
266
|
|
|
if ($this->hasError()) |
267
|
|
|
{ |
268
|
|
|
return $this->fields->getErrors()->first($this->identifier); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return null; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Returns the field's placeholder. |
276
|
|
|
* |
277
|
|
|
* @param void |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
public function getPlaceholder() |
281
|
|
|
{ |
282
|
|
|
if (empty($this->placeholder)) |
283
|
|
|
{ |
284
|
|
|
return null; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $this->placeholder; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Returns the field's help. |
292
|
|
|
* |
293
|
|
|
* @param void |
294
|
|
|
* @return string |
295
|
|
|
*/ |
296
|
|
|
public function getHelp() |
297
|
|
|
{ |
298
|
|
|
if (empty($this->help)) |
299
|
|
|
{ |
300
|
|
|
return null; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
return $this->help; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Checks if the field has a previous value. |
308
|
|
|
* |
309
|
|
|
* @param void |
310
|
|
|
* @return bool |
311
|
|
|
*/ |
312
|
|
|
public function hasOld() |
313
|
|
|
{ |
314
|
|
|
return $this->fields->getOldInput()->has($this->identifier); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Returns the old value. |
319
|
|
|
* |
320
|
|
|
* @param void |
321
|
|
|
* @return string|null |
322
|
|
|
*/ |
323
|
|
|
public function getOld() |
324
|
|
|
{ |
325
|
|
|
if ($this->hasOld()) |
326
|
|
|
{ |
327
|
|
|
return $this->fields->getOldInput()->first($this->identifier); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
return null; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Returns the field's label. |
335
|
|
|
* |
336
|
|
|
* @param void |
337
|
|
|
* @return string |
338
|
|
|
*/ |
339
|
|
|
public function getLabel() |
340
|
|
|
{ |
341
|
|
|
if (empty($this->label)) |
342
|
|
|
{ |
343
|
|
|
return title_case($this->identifier); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return $this->label; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Get the field value. |
351
|
|
|
* |
352
|
|
|
* @param void |
353
|
|
|
* @return mixed |
354
|
|
|
*/ |
355
|
|
|
public function getValue() |
356
|
|
|
{ |
357
|
|
|
if ($this->fields->getEntry()) |
358
|
|
|
{ |
359
|
|
|
return $this->fields->getEntry()->getModel()->getAttributeValue($this->identifier); |
|
|
|
|
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return null; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Get the value to be displayed on a table. |
367
|
|
|
* |
368
|
|
|
* @param void |
369
|
|
|
* @return mixed |
370
|
|
|
*/ |
371
|
|
|
public function getTableValue() |
372
|
|
|
{ |
373
|
|
|
return $this->getValue(); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Set a new value to the model. |
378
|
|
|
* |
379
|
|
|
* @param mixed $value |
380
|
|
|
* @return self |
381
|
|
|
*/ |
382
|
|
|
public function newValue($value) |
383
|
|
|
{ |
384
|
|
|
$this->fields->getEntry()->getModel()->setAttribute($this->identifier, $value); |
|
|
|
|
385
|
|
|
|
386
|
|
|
return $this; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @param void |
391
|
|
|
* @return string |
392
|
|
|
*/ |
393
|
|
|
public function getRules() |
394
|
|
|
{ |
395
|
|
|
return $this->rules; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Return fields specific scripts files from public folder. |
400
|
|
|
* Example: ['js/field.js'] |
401
|
|
|
* |
402
|
|
|
* @param void |
403
|
|
|
* @return array |
404
|
|
|
*/ |
405
|
|
|
public function getScripts() |
406
|
|
|
{ |
407
|
|
|
return []; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Return fields specific stylesheets files from public folder. |
412
|
|
|
* Example: ['css/field.css'] |
413
|
|
|
* |
414
|
|
|
* @param void |
415
|
|
|
* @return array |
416
|
|
|
*/ |
417
|
|
|
public function getCss() |
418
|
|
|
{ |
419
|
|
|
return []; |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: