|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Uccello\Core\Http\Controllers\Core; |
|
4
|
|
|
|
|
5
|
|
|
use Kris\LaravelFormBuilder\FormBuilder; |
|
|
|
|
|
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Uccello\Core\Forms\EditForm; |
|
8
|
|
|
use Uccello\Core\Events\BeforeSaveEvent; |
|
9
|
|
|
use Uccello\Core\Events\AfterSaveEvent; |
|
10
|
|
|
use Uccello\Core\Models\Domain; |
|
11
|
|
|
use Uccello\Core\Models\Module; |
|
12
|
|
|
use Uccello\Core\Models\Relation; |
|
13
|
|
|
use Uccello\Core\Models\Relatedlist; |
|
14
|
|
|
|
|
15
|
|
|
class EditController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
protected $viewName = 'edit.main'; |
|
18
|
|
|
protected $formBuilder; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Check user permissions |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function checkPermissions() |
|
24
|
|
|
{ |
|
25
|
|
|
if (request()->has('id')) { |
|
26
|
|
|
$this->middleware('uccello.permissions:update'); |
|
27
|
|
|
} else { |
|
28
|
|
|
$this->middleware('uccello.permissions:create'); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(FormBuilder $formBuilder) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->formBuilder = $formBuilder; |
|
35
|
|
|
|
|
36
|
|
|
parent::__construct(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function process(?Domain $domain, Module $module, Request $request) |
|
43
|
|
|
{ |
|
44
|
|
|
// Pre-process |
|
45
|
|
|
$this->preProcess($domain, $module, $request); |
|
46
|
|
|
|
|
47
|
|
|
// Retrieve record or get a new empty instance |
|
48
|
|
|
$record = $this->getRecordFromRequest(); |
|
49
|
|
|
|
|
50
|
|
|
// Get form |
|
51
|
|
|
$form = $this->getForm($record); |
|
52
|
|
|
|
|
53
|
|
|
// Get mode |
|
54
|
|
|
$mode = !is_null($record->getKey()) ? 'edit' : 'create'; |
|
55
|
|
|
|
|
56
|
|
|
return $this->autoView([ |
|
57
|
|
|
'form' => $form, |
|
58
|
|
|
'record' => $record, |
|
59
|
|
|
'mode' => $mode |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Create or update record into database |
|
65
|
|
|
* |
|
66
|
|
|
* @param Domain|null $domain |
|
67
|
|
|
* @param Module $module |
|
68
|
|
|
* @param boolean $redirect |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function save(?Domain $domain, Module $module, Request $request, bool $redirect = true) |
|
72
|
|
|
{ |
|
73
|
|
|
// Pre-process |
|
74
|
|
|
$this->preProcess($domain, $module, $request); |
|
75
|
|
|
|
|
76
|
|
|
// Get model class used by the module |
|
77
|
|
|
$modelClass = $this->module->model_class; |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
// Retrieve record or get a new empty instance |
|
80
|
|
|
$record = $this->getRecordFromRequest(); |
|
81
|
|
|
|
|
82
|
|
|
// Get form |
|
83
|
|
|
$form = $this->getForm($record); |
|
84
|
|
|
|
|
85
|
|
|
// Redirect if form not valid (the record is made here) |
|
86
|
|
|
$form->redirectIfNotValid(); |
|
87
|
|
|
|
|
88
|
|
|
$mode = $record->getKey() ? 'edit' : 'create'; |
|
89
|
|
|
|
|
90
|
|
|
event(new BeforeSaveEvent($domain, $module, $request, $record, $mode)); |
|
91
|
|
|
|
|
92
|
|
|
// Save record |
|
93
|
|
|
$form->getModel()->save(); |
|
94
|
|
|
|
|
95
|
|
|
event(new AfterSaveEvent($domain, $module, $request, $record, $mode)); |
|
96
|
|
|
|
|
97
|
|
|
// Save relation if necessary |
|
98
|
|
|
if ($request->input('relatedlist') && $request->input('src_id')) { |
|
99
|
|
|
$relatedlist = Relatedlist::findOrFail($request->input('relatedlist')); |
|
100
|
|
|
$sourceRecordId = $request->input('src_id'); |
|
101
|
|
|
$tabId = $request->input('tab'); |
|
102
|
|
|
|
|
103
|
|
|
$this->saveRelation($relatedlist, $sourceRecordId, $record->id); |
|
104
|
|
|
|
|
105
|
|
|
$redirectToSourceRecord = true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Redirect |
|
109
|
|
|
if ($redirect === true) { |
|
110
|
|
|
// Redirect to source record if a relation was made |
|
111
|
|
|
if (isset($relatedlist) && $redirectToSourceRecord === true) { |
|
|
|
|
|
|
112
|
|
|
$params = [ 'id' => $sourceRecordId ]; |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
// Add tab id if defined to select it automaticaly |
|
115
|
|
|
if ($tabId) { |
|
|
|
|
|
|
116
|
|
|
$params[ 'tab' ] = $tabId; |
|
117
|
|
|
} |
|
118
|
|
|
// Add related list id to select the related tab automaticaly |
|
119
|
|
|
else { |
|
120
|
|
|
$params[ 'relatedlist' ] = $relatedlist->id; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$route = ucroute('uccello.detail', $domain, $relatedlist->module, $params); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Redirect to edit if the user want to create a new record |
|
127
|
|
|
elseif ($request->input('save_new_hdn') === '1') { |
|
128
|
|
|
$route = ucroute('uccello.edit', $domain, $module); |
|
129
|
|
|
|
|
130
|
|
|
// Notification |
|
131
|
|
|
ucnotify(uctrans('notification.record.created', $module), 'success'); |
|
132
|
|
|
} |
|
133
|
|
|
// Else redirect to detail |
|
134
|
|
|
else { |
|
135
|
|
|
$route = ucroute('uccello.detail', $domain, $module, [ 'id' => $record->getKey() ]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return redirect($route); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
// Or return record |
|
141
|
|
|
else { |
|
142
|
|
|
return $form->getModel(); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Add a relation between two records |
|
148
|
|
|
* |
|
149
|
|
|
* @param Domain|null $domain |
|
150
|
|
|
* @param Module $module |
|
151
|
|
|
* @param Request $request |
|
152
|
|
|
* @return integer|null |
|
153
|
|
|
*/ |
|
154
|
|
|
public function addRelation(?Domain $domain, Module $module, Request $request) |
|
155
|
|
|
{ |
|
156
|
|
|
// Pre-process |
|
157
|
|
|
$this->preProcess($domain, $module, $request); |
|
158
|
|
|
|
|
159
|
|
|
// Retrieve record or get a new empty instance |
|
160
|
|
|
$record = $this->getRecordFromRequest(); |
|
161
|
|
|
|
|
162
|
|
|
if ($request->input('relatedlist') && $request->input('related_id')) { |
|
163
|
|
|
$relatedlist = Relatedlist::findOrFail($request->input('relatedlist')); |
|
164
|
|
|
$relatedRecordId = $request->input('related_id'); |
|
165
|
|
|
|
|
166
|
|
|
$relationId = $this->saveRelation($relatedlist, $record->id, $relatedRecordId); |
|
167
|
|
|
|
|
168
|
|
|
$response = [ |
|
169
|
|
|
'success' => true, |
|
170
|
|
|
'data' => $relationId |
|
171
|
|
|
]; |
|
172
|
|
|
} else { |
|
173
|
|
|
$response = [ |
|
174
|
|
|
'success' => false, |
|
175
|
|
|
'message' => uctrans('error.mandatory.fields', $module) |
|
176
|
|
|
]; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return $response; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function getForm($record = null) |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->formBuilder->create(EditForm::class, [ |
|
185
|
|
|
'model' => $record, |
|
186
|
|
|
'data' => [ |
|
187
|
|
|
'domain' => $this->domain, |
|
188
|
|
|
'module' => $this->module, |
|
189
|
|
|
'request' => $this->request |
|
190
|
|
|
] |
|
191
|
|
|
]); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Save relation between two records |
|
196
|
|
|
* |
|
197
|
|
|
* @param Relatedlist $relatedList |
|
198
|
|
|
* @param integer $recordId |
|
199
|
|
|
* @param integer $relatedRecordId |
|
200
|
|
|
* @return integer |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function saveRelation(Relatedlist $relatedList, int $recordId, int $relatedRecordId) : int |
|
203
|
|
|
{ |
|
204
|
|
|
$relation = Relation::firstOrCreate([ |
|
205
|
|
|
'module_id' => $relatedList->module_id, |
|
|
|
|
|
|
206
|
|
|
'related_module_id' => $relatedList->related_module_id, |
|
|
|
|
|
|
207
|
|
|
'record_id' => $recordId, |
|
208
|
|
|
'related_record_id' => $relatedRecordId, |
|
209
|
|
|
'relatedlist_id' => $relatedList->id |
|
210
|
|
|
]); |
|
211
|
|
|
|
|
212
|
|
|
return $relation->id; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths