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
|
|
|
return $this->model->save(); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param void |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function __toString() |
200
|
|
|
{ |
201
|
|
|
return $this->form(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param void |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
public function form() |
209
|
|
|
{ |
210
|
|
|
$is_new = !$this->getModel()->exists; |
211
|
|
|
$name = $this->getManager()->getName(); |
212
|
|
|
|
213
|
|
|
$with = [ |
214
|
|
|
'back_url' => $this->manager->getActionRoute('index'), |
215
|
|
|
'entry' => $this, |
216
|
|
|
'manager' => $this->manager, |
217
|
|
|
'errors' => $this->fields->getErrors(), |
218
|
|
|
'old' => $this->fields->getOldInput(), |
219
|
|
|
'crud_js' => $this->fields->getFieldsScripts(), |
220
|
|
|
'crud_css' => $this->fields->getFieldsCss(), |
221
|
|
|
'multipart' => $this->fields->getMultipart() ? ' enctype="multipart/form-data"' : '' |
222
|
|
|
]; |
223
|
|
|
|
224
|
|
|
if ($is_new) |
225
|
|
|
{ |
226
|
|
|
$view_name = 'crud::form-create'; |
227
|
|
|
$method = $this->manager->getActionMethod('store'); |
228
|
|
|
|
229
|
|
|
$with += [ |
230
|
|
|
'title' => trans('crud::form.create_title', ['name' => $name]), |
231
|
|
|
'form_url' => $this->manager->getActionRoute('store'), |
232
|
|
|
'form_method' => $method, |
233
|
|
|
'csrf_field' => csrf_field(), |
234
|
|
|
'method_field' => method_field($method), |
235
|
|
|
]; |
236
|
|
|
} |
237
|
|
|
else |
238
|
|
|
{ |
239
|
|
|
$view_name = 'crud::form-update'; |
240
|
|
|
$method = $this->manager->getActionMethod('update'); |
241
|
|
|
|
242
|
|
|
$with += [ |
243
|
|
|
'title' => trans('crud::form.update_title', ['name' => $name]), |
244
|
|
|
'form_url' => $this->manager->getActionRoute('update'), |
245
|
|
|
'form_method' => $method, |
246
|
|
|
'csrf_field' => csrf_field(), |
247
|
|
|
'method_field' => method_field($method), |
248
|
|
|
]; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
$view = View::make($view_name)->with($with); |
252
|
|
|
|
253
|
|
|
return $view->render(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Get the entry's model. |
258
|
|
|
* |
259
|
|
|
* @param void |
260
|
|
|
* @return Crudable|Model |
261
|
|
|
*/ |
262
|
|
|
public function getModel() |
263
|
|
|
{ |
264
|
|
|
return $this->model; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param void |
269
|
|
|
* @return CrudManager |
270
|
|
|
*/ |
271
|
|
|
public function getManager() |
272
|
|
|
{ |
273
|
|
|
return $this->manager; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
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: