|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Akibatech\Crud\Exceptions\InvalidModelException; |
|
6
|
|
|
use Akibatech\Crud\Exceptions\NoFieldsException; |
|
7
|
|
|
use Akibatech\Crud\Traits\Crudable; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
use Illuminate\Support\Facades\View; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class CrudEntry |
|
13
|
|
|
* |
|
14
|
|
|
* @package Akibatech\Crud\Services |
|
15
|
|
|
*/ |
|
16
|
|
|
class CrudEntry |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var CrudFields |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $fields = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Crudable|Model |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $model; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var CrudManager |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $manager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var CrudValidator |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $validator; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* CrudEntry constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param Model|Crudable $model |
|
42
|
|
|
* @throws \InvalidArgumentException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(Model $model) |
|
45
|
|
|
{ |
|
46
|
|
|
if (in_array(Crudable::class, class_uses($model))) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->model = $model; |
|
49
|
|
|
$this->manager = $model->getCrudManager()->setEntry($this); |
|
50
|
|
|
$this->fields = $model->getCrudFields()->setEntry($this); |
|
51
|
|
|
|
|
52
|
|
|
if ($this->fields->count() === 0) |
|
53
|
|
|
{ |
|
54
|
|
|
throw new NoFieldsException(get_class($model) . ' has no fields.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (!app()->runningUnitTests() && !app()->runningInConsole()) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->fields->hydrateErrorsFromSession(); |
|
60
|
|
|
$this->fields->hydrateFieldsFromSession(); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
else |
|
64
|
|
|
{ |
|
65
|
|
|
throw new InvalidModelException("The model must use Crudable trait."); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param void |
|
71
|
|
|
* @return \Generator |
|
72
|
|
|
*/ |
|
73
|
|
|
public function fields() |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($this->fields->loop() as $field) |
|
76
|
|
|
{ |
|
77
|
|
|
if ($field->isDisplayedInColumns()) |
|
78
|
|
|
{ |
|
79
|
|
|
yield $field; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param void |
|
86
|
|
|
* @return \Generator |
|
87
|
|
|
*/ |
|
88
|
|
|
public function formFields() |
|
89
|
|
|
{ |
|
90
|
|
|
foreach ($this->fields->loop() as $field) |
|
91
|
|
|
{ |
|
92
|
|
|
yield $field; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get the entry ID. |
|
98
|
|
|
* |
|
99
|
|
|
* @param void |
|
100
|
|
|
* @return int|mixed |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getId() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->model->getAttribute($this->model->getKeyName()); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param void |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function row() |
|
112
|
|
|
{ |
|
113
|
|
|
$actions = [ |
|
114
|
|
|
[ |
|
115
|
|
|
'value' => trans('crud::buttons.edit'), |
|
116
|
|
|
'class' => 'btn btn-primary btn-xs', |
|
117
|
|
|
'uri' => $this->manager->getActionRoute('edit') |
|
118
|
|
|
], |
|
119
|
|
|
[ |
|
120
|
|
|
'value' => trans('crud::buttons.destroy'), |
|
121
|
|
|
'class' => 'btn btn-danger btn-xs', |
|
122
|
|
|
'uri' => $this->manager->getActionRoute('destroy') |
|
123
|
|
|
], |
|
124
|
|
|
]; |
|
125
|
|
|
|
|
126
|
|
|
$view = view()->make('crud::row')->with([ |
|
|
|
|
|
|
127
|
|
|
'entry' => $this, |
|
128
|
|
|
'manager' => $this->manager, |
|
129
|
|
|
'actions' => $actions, |
|
130
|
|
|
]); |
|
131
|
|
|
|
|
132
|
|
|
return $view->render(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param void |
|
137
|
|
|
* @return CrudFields |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getFields() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->fields; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Resets the validator. |
|
146
|
|
|
* |
|
147
|
|
|
* @param void |
|
148
|
|
|
* @return self |
|
149
|
|
|
*/ |
|
150
|
|
|
public function resetValidator() |
|
151
|
|
|
{ |
|
152
|
|
|
$this->validator = null; |
|
153
|
|
|
|
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Shortcut to self validate. |
|
159
|
|
|
* |
|
160
|
|
|
* @param array $data |
|
161
|
|
|
* @return CrudValidator |
|
162
|
|
|
*/ |
|
163
|
|
|
public function validate(array $data) |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->getValidator()->validate($data); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Gets the validator. |
|
170
|
|
|
* |
|
171
|
|
|
* @param void |
|
172
|
|
|
* @return CrudValidator |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getValidator() |
|
175
|
|
|
{ |
|
176
|
|
|
if (is_null($this->validator)) |
|
177
|
|
|
{ |
|
178
|
|
|
$this->validator = new CrudValidator($this); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $this->validator; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Saves the model. |
|
186
|
|
|
* |
|
187
|
|
|
* @param void |
|
188
|
|
|
* @return bool |
|
189
|
|
|
*/ |
|
190
|
|
|
public function save() |
|
191
|
|
|
{ |
|
192
|
|
|
foreach ($this->fields->loop() as $field) |
|
193
|
|
|
{ |
|
194
|
|
|
$field->beforeSave(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $this->model->save(); |
|
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param void |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
public function __toString() |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->form(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param void |
|
211
|
|
|
* @return string |
|
212
|
|
|
*/ |
|
213
|
|
|
public function form() |
|
214
|
|
|
{ |
|
215
|
|
|
$is_new = !$this->getModel()->exists; |
|
216
|
|
|
$name = $this->getManager()->getName(); |
|
217
|
|
|
|
|
218
|
|
|
$with = [ |
|
219
|
|
|
'back_url' => $this->manager->getActionRoute('index'), |
|
220
|
|
|
'entry' => $this, |
|
221
|
|
|
'manager' => $this->manager, |
|
222
|
|
|
'errors' => $this->fields->getErrors(), |
|
223
|
|
|
'old' => $this->fields->getOldInput(), |
|
224
|
|
|
'crud_js' => $this->fields->getFieldsScripts(), |
|
225
|
|
|
'crud_css' => $this->fields->getFieldsCss(), |
|
226
|
|
|
'multipart' => $this->fields->getMultipart() ? ' enctype="multipart/form-data"' : '' |
|
227
|
|
|
]; |
|
228
|
|
|
|
|
229
|
|
|
if ($is_new) |
|
230
|
|
|
{ |
|
231
|
|
|
$view_name = 'crud::form-create'; |
|
232
|
|
|
$method = $this->manager->getActionMethod('store'); |
|
233
|
|
|
|
|
234
|
|
|
$with += [ |
|
235
|
|
|
'title' => trans('crud::form.create_title', ['name' => $name]), |
|
236
|
|
|
'form_url' => $this->manager->getActionRoute('store'), |
|
237
|
|
|
'form_method' => $method, |
|
238
|
|
|
'csrf_field' => csrf_field(), |
|
239
|
|
|
'method_field' => method_field($method), |
|
240
|
|
|
]; |
|
241
|
|
|
} |
|
242
|
|
|
else |
|
243
|
|
|
{ |
|
244
|
|
|
$view_name = 'crud::form-update'; |
|
245
|
|
|
$method = $this->manager->getActionMethod('update'); |
|
246
|
|
|
|
|
247
|
|
|
$with += [ |
|
248
|
|
|
'title' => trans('crud::form.update_title', ['name' => $name]), |
|
249
|
|
|
'form_url' => $this->manager->getActionRoute('update'), |
|
250
|
|
|
'form_method' => $method, |
|
251
|
|
|
'csrf_field' => csrf_field(), |
|
252
|
|
|
'method_field' => method_field($method), |
|
253
|
|
|
]; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
$view = View::make($view_name)->with($with); |
|
257
|
|
|
|
|
258
|
|
|
return $view->render(); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Get the entry's model. |
|
263
|
|
|
* |
|
264
|
|
|
* @param void |
|
265
|
|
|
* @return Crudable|Model |
|
266
|
|
|
*/ |
|
267
|
|
|
public function getModel() |
|
268
|
|
|
{ |
|
269
|
|
|
return $this->model; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @param void |
|
274
|
|
|
* @return CrudManager |
|
275
|
|
|
*/ |
|
276
|
|
|
public function getManager() |
|
277
|
|
|
{ |
|
278
|
|
|
return $this->manager; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
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: