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
|
|
|
yield $field; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the entry ID. |
83
|
|
|
* |
84
|
|
|
* @param void |
85
|
|
|
* @return int|mixed |
86
|
|
|
*/ |
87
|
|
|
public function getId() |
88
|
|
|
{ |
89
|
|
|
return $this->model->getAttribute($this->model->getKeyName()); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param void |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function row() |
97
|
|
|
{ |
98
|
|
|
$actions = [ |
99
|
|
|
[ |
100
|
|
|
'value' => trans('crud::buttons.edit'), |
101
|
|
|
'class' => 'btn btn-primary btn-xs', |
102
|
|
|
'uri' => $this->manager->getActionRoute('edit') |
103
|
|
|
], |
104
|
|
|
[ |
105
|
|
|
'value' => trans('crud::buttons.destroy'), |
106
|
|
|
'class' => 'btn btn-danger btn-xs', |
107
|
|
|
'uri' => $this->manager->getActionRoute('destroy') |
108
|
|
|
], |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
$view = view()->make('crud::row')->with([ |
|
|
|
|
112
|
|
|
'entry' => $this, |
113
|
|
|
'manager' => $this->manager, |
114
|
|
|
'actions' => $actions, |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
return $view->render(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param void |
122
|
|
|
* @return CrudFields |
123
|
|
|
*/ |
124
|
|
|
public function getFields() |
125
|
|
|
{ |
126
|
|
|
return $this->fields; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Resets the validator. |
131
|
|
|
* |
132
|
|
|
* @param void |
133
|
|
|
* @return self |
134
|
|
|
*/ |
135
|
|
|
public function resetValidator() |
136
|
|
|
{ |
137
|
|
|
$this->validator = null; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Shortcut to self validate. |
144
|
|
|
* |
145
|
|
|
* @param array $data |
146
|
|
|
* @return CrudValidator |
147
|
|
|
*/ |
148
|
|
|
public function validate(array $data) |
149
|
|
|
{ |
150
|
|
|
return $this->getValidator()->validate($data); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Gets the validator. |
155
|
|
|
* |
156
|
|
|
* @param void |
157
|
|
|
* @return CrudValidator |
158
|
|
|
*/ |
159
|
|
|
public function getValidator() |
160
|
|
|
{ |
161
|
|
|
if (is_null($this->validator)) |
162
|
|
|
{ |
163
|
|
|
$this->validator = new CrudValidator($this); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this->validator; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Saves the model. |
171
|
|
|
* |
172
|
|
|
* @param void |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
|
|
public function save() |
176
|
|
|
{ |
177
|
|
|
return $this->model->save(); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param void |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function __toString() |
185
|
|
|
{ |
186
|
|
|
return $this->form(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param void |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
public function form() |
194
|
|
|
{ |
195
|
|
|
$is_new = !$this->getModel()->exists; |
196
|
|
|
$name = $this->getManager()->getName(); |
197
|
|
|
|
198
|
|
|
$with = [ |
199
|
|
|
'back_url' => $this->manager->getActionRoute('index'), |
200
|
|
|
'entry' => $this, |
201
|
|
|
'manager' => $this->manager, |
202
|
|
|
'errors' => $this->fields->getErrors(), |
203
|
|
|
'old' => $this->fields->getOldInput(), |
204
|
|
|
'crud_js' => $this->fields->getFieldsScripts(), |
205
|
|
|
'crud_css' => $this->fields->getFieldsCss(), |
206
|
|
|
'multipart' => $this->fields->getMultipart() ? ' enctype="multipart/form-data"' : '' |
207
|
|
|
]; |
208
|
|
|
|
209
|
|
|
if ($is_new) |
210
|
|
|
{ |
211
|
|
|
$view_name = 'crud::form-create'; |
212
|
|
|
$method = $this->manager->getActionMethod('store'); |
213
|
|
|
|
214
|
|
|
$with += [ |
215
|
|
|
'title' => trans('crud::form.create_title', ['name' => $name]), |
216
|
|
|
'form_url' => $this->manager->getActionRoute('store'), |
217
|
|
|
'form_method' => $method, |
218
|
|
|
'csrf_field' => csrf_field(), |
219
|
|
|
'method_field' => method_field($method), |
220
|
|
|
]; |
221
|
|
|
} |
222
|
|
|
else |
223
|
|
|
{ |
224
|
|
|
$view_name = 'crud::form-update'; |
225
|
|
|
$method = $this->manager->getActionMethod('update'); |
226
|
|
|
|
227
|
|
|
$with += [ |
228
|
|
|
'title' => trans('crud::form.update_title', ['name' => $name]), |
229
|
|
|
'form_url' => $this->manager->getActionRoute('update'), |
230
|
|
|
'form_method' => $method, |
231
|
|
|
'csrf_field' => csrf_field(), |
232
|
|
|
'method_field' => method_field($method), |
233
|
|
|
]; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$view = View::make($view_name)->with($with); |
237
|
|
|
|
238
|
|
|
return $view->render(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get the entry's model. |
243
|
|
|
* |
244
|
|
|
* @param void |
245
|
|
|
* @return Crudable|Model |
246
|
|
|
*/ |
247
|
|
|
public function getModel() |
248
|
|
|
{ |
249
|
|
|
return $this->model; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param void |
254
|
|
|
* @return CrudManager |
255
|
|
|
*/ |
256
|
|
|
public function getManager() |
257
|
|
|
{ |
258
|
|
|
return $this->manager; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
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: