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