1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Fields; |
4
|
|
|
|
5
|
|
|
use Akibatech\Crud\Services\CrudFields; |
6
|
|
|
use Akibatech\Crud\Traits\FieldHasUiModifiers; |
7
|
|
|
use Illuminate\Validation\Validator; |
8
|
|
|
use Illuminate\View\View; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Field |
12
|
|
|
* |
13
|
|
|
* @package Akibatech\Crud\Fields |
14
|
|
|
*/ |
15
|
|
|
abstract class Field |
16
|
|
|
{ |
17
|
|
|
use FieldHasUiModifiers; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
const TYPE = 'type'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
const MULTIPART = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CrudFields |
31
|
|
|
*/ |
32
|
|
|
protected $fields; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $identifier; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $rules; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Field constructor. |
46
|
|
|
* |
47
|
|
|
* @param string $identifier |
48
|
|
|
* @param array|string $rules |
49
|
|
|
*/ |
50
|
|
|
public function __construct($identifier, $rules = null) |
51
|
|
|
{ |
52
|
|
|
$this->identifier = $identifier; |
53
|
|
|
|
54
|
|
|
if (!is_null($rules)) |
55
|
|
|
{ |
56
|
|
|
$this->withRules($rules); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Constructs staticly. |
62
|
|
|
* |
63
|
|
|
* @param string $idenfitier |
64
|
|
|
* @param null|string|array $rules |
65
|
|
|
* @return static |
66
|
|
|
*/ |
67
|
|
|
public static function handle($idenfitier, $rules = null) |
68
|
|
|
{ |
69
|
|
|
return (new static($idenfitier, $rules)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add validation rules to the field. |
75
|
|
|
* |
76
|
|
|
* @param string|array $rules |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function withRules($rules) |
80
|
|
|
{ |
81
|
|
|
if (is_array($rules)) |
82
|
|
|
{ |
83
|
|
|
foreach ($rules as $rule) |
84
|
|
|
{ |
85
|
|
|
$this->addRule($rule); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
else |
89
|
|
|
{ |
90
|
|
|
if (is_string($rules)) |
91
|
|
|
{ |
92
|
|
|
if (stripos($rules, '|') !== false) |
93
|
|
|
{ |
94
|
|
|
$rules = explode('|', $rules); |
95
|
|
|
|
96
|
|
|
return $this->withRules($rules); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->withRules([$rules]); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add a validation rule. |
108
|
|
|
* |
109
|
|
|
* @param string $rule |
110
|
|
|
* @return self |
111
|
|
|
*/ |
112
|
|
|
public function addRule($rule) |
113
|
|
|
{ |
114
|
|
|
$this->rules[] = $rule; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param CrudFields $fields |
121
|
|
|
* @return self |
122
|
|
|
*/ |
123
|
|
|
public function setFields(CrudFields $fields) |
124
|
|
|
{ |
125
|
|
|
$this->fields = $fields; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the field identifier. |
132
|
|
|
* |
133
|
|
|
* @param void |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getIdentifier() |
137
|
|
|
{ |
138
|
|
|
return $this->identifier; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Render the field form. |
143
|
|
|
* |
144
|
|
|
* @param void |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function form() |
148
|
|
|
{ |
149
|
|
|
return $this->getForm()->render(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the form view. |
154
|
|
|
* |
155
|
|
|
* @param void |
156
|
|
|
* @return View |
157
|
|
|
*/ |
158
|
|
|
protected function getForm() |
159
|
|
|
{ |
160
|
|
|
return view()->make($this->getViewName())->with($this->getViewBaseVariables()); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the field view name. |
165
|
|
|
* |
166
|
|
|
* @param void |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
abstract public function getViewName(); |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns additionnal variables to the views. |
173
|
|
|
* |
174
|
|
|
* @param void |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
|
|
protected function getViewVariables() |
178
|
|
|
{ |
179
|
|
|
return []; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Returns all base variables for the view. |
184
|
|
|
* |
185
|
|
|
* @param void |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
protected function getViewBaseVariables() |
189
|
|
|
{ |
190
|
|
|
$base_variables = [ |
191
|
|
|
'field' => $this, |
192
|
|
|
'has_error' => $this->hasError(), |
193
|
|
|
'error' => $this->getError(), |
194
|
|
|
'placeholder' => $this->getPlaceholder(), |
195
|
|
|
'help' => $this->getHelp(), |
196
|
|
|
'has_old' => $this->hasOld(), |
197
|
|
|
'old' => $this->getOld(), |
198
|
|
|
'label' => $this->getLabel(), |
199
|
|
|
'name' => $this->identifier, |
200
|
|
|
'id' => 'field-' . $this->identifier, |
201
|
|
|
'value' => $this->getValue() |
202
|
|
|
]; |
203
|
|
|
|
204
|
|
|
return array_merge($base_variables, $this->getViewVariables()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Checks if the field has an error. |
209
|
|
|
* |
210
|
|
|
* @param void |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
|
public function hasError() |
214
|
|
|
{ |
215
|
|
|
return $this->fields->getErrors()->has($this->identifier); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Returns the error. |
220
|
|
|
* |
221
|
|
|
* @param void |
222
|
|
|
* @return null|string |
223
|
|
|
*/ |
224
|
|
|
public function getError() |
225
|
|
|
{ |
226
|
|
|
if ($this->hasError()) |
227
|
|
|
{ |
228
|
|
|
return $this->fields->getErrors()->first($this->identifier); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return null; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Checks if the field has a previous value. |
236
|
|
|
* |
237
|
|
|
* @param void |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
|
|
public function hasOld() |
241
|
|
|
{ |
242
|
|
|
return $this->fields->getOldInput()->has($this->identifier); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Returns the old value. |
247
|
|
|
* |
248
|
|
|
* @param void |
249
|
|
|
* @return string|null |
250
|
|
|
*/ |
251
|
|
|
public function getOld() |
252
|
|
|
{ |
253
|
|
|
if ($this->hasOld()) |
254
|
|
|
{ |
255
|
|
|
return $this->fields->getOldInput()->first($this->identifier); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return null; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get the field value. |
263
|
|
|
* |
264
|
|
|
* @param void |
265
|
|
|
* @return mixed |
266
|
|
|
*/ |
267
|
|
|
public function getValue() |
268
|
|
|
{ |
269
|
|
|
if ($this->fields->getEntry()) |
270
|
|
|
{ |
271
|
|
|
return $this->fields->getEntry()->getModel()->getAttributeValue($this->identifier); |
|
|
|
|
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return null; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Get the value to be displayed on a table. |
279
|
|
|
* |
280
|
|
|
* @param void |
281
|
|
|
* @return mixed |
282
|
|
|
*/ |
283
|
|
|
public function getTableValue() |
284
|
|
|
{ |
285
|
|
|
return $this->getValue(); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Set a new value to the model. |
290
|
|
|
* |
291
|
|
|
* @param mixed $value |
292
|
|
|
* @return self |
293
|
|
|
*/ |
294
|
|
|
public function newValue($value) |
295
|
|
|
{ |
296
|
|
|
if ($value != $this->getValue()) |
297
|
|
|
{ |
298
|
|
|
$this->fields->getEntry()->getModel()->setAttribute($this->identifier, $value); |
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param void |
306
|
|
|
* @return string |
307
|
|
|
*/ |
308
|
|
|
public function getRules() |
309
|
|
|
{ |
310
|
|
|
return $this->rules; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param Validator $validator |
315
|
|
|
* @return Validator |
316
|
|
|
*/ |
317
|
|
|
public function beforeValidation(Validator $validator) |
318
|
|
|
{ |
319
|
|
|
return $validator; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param void |
324
|
|
|
* @return self |
325
|
|
|
*/ |
326
|
|
|
public function beforeSave() |
327
|
|
|
{ |
328
|
|
|
return $this; |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: