Passed
Push — master ( 1b176b...61854e )
by Jonathan
15:21
created

EditController::restore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 20
rs 9.9666
cc 2
nc 2
nop 3
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Core;
4
5
use Kris\LaravelFormBuilder\FormBuilder;
0 ignored issues
show
Bug introduced by
The type Kris\LaravelFormBuilder\FormBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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\Events\BeforeDeleteEvent;
11
use Uccello\Core\Events\AfterDeleteEvent;
12
use Uccello\Core\Models\Domain;
13
use Uccello\Core\Models\Module;
14
use Uccello\Core\Models\Relatedlist;
15
16
class EditController extends Controller
17
{
18
    protected $viewName = 'edit.main';
19
    protected $formBuilder;
20
21
    /**
22
     * Check user permissions
23
     */
24
    protected function checkPermissions()
25
    {
26
        if (request()->has('id')) {
27
            $this->middleware('uccello.permissions:update');
28
        } else {
29
            $this->middleware('uccello.permissions:create');
30
        }
31
    }
32
33
    public function __construct(FormBuilder $formBuilder)
34
    {
35
        $this->formBuilder = $formBuilder;
36
37
        parent::__construct();
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function process(?Domain $domain, Module $module, Request $request)
44
    {
45
        // Pre-process
46
        $this->preProcess($domain, $module, $request);
47
48
        // Retrieve record or get a new empty instance
49
        $record = $this->getRecordFromRequest();
50
51
        // Get form
52
        $form = $this->getForm($record);
53
54
        // Get mode
55
        $mode = !is_null($record->getKey()) ? 'edit' : 'create';
56
57
        return $this->autoView([
58
            'form' => $form,
59
            'record' => $record,
60
            'mode' => $mode
61
        ]);
62
    }
63
64
    /**
65
     * Create or update record into database
66
     *
67
     * @param Domain|null $domain
68
     * @param Module $module
69
     * @param boolean $redirect
70
     * @return void
71
     */
72
    public function save(?Domain $domain, Module $module, Request $request, bool $redirect = true)
73
    {
74
        // Pre-process
75
        $this->preProcess($domain, $module, $request);
76
77
        // Get model class used by the module
78
        $modelClass = $this->module->model_class;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Unused Code introduced by
The assignment to $modelClass is dead and can be removed.
Loading history...
79
80
        // Retrieve record or get a new empty instance
81
        $record = $this->getRecordFromRequest();
82
83
        // Get form
84
        $form = $this->getForm($record);
85
86
        // Redirect if form not valid (the record is made here)
87
        $form->redirectIfNotValid();
88
89
        $mode = $record->getKey() ? 'edit' : 'create';
90
91
        event(new BeforeSaveEvent($domain, $module, $request, $record, $mode));
92
93
        // Save record
94
        $form->getModel()->save();
95
96
        event(new AfterSaveEvent($domain, $module, $request, $record, $mode));
97
98
        // Save relation if necessary
99
        if ($request->input('relatedlist') && $request->input('src_id')) {
100
            $relatedlist = Relatedlist::findOrFail($request->input('relatedlist'));
101
            $sourceRecordId = (int)$request->input('src_id');
102
            $tabId = $request->input('tab');
103
104
            if ($relatedlist->type === 'n-n') {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
105
                $this->saveRelation($relatedlist, $sourceRecordId, $record->id);
106
            }
107
108
            $redirectToSourceRecord = true;
109
        }
110
111
        // Redirect
112
        if ($redirect === true) {
113
            // Redirect to source record if a relation was made
114
            if (isset($relatedlist) && $redirectToSourceRecord === true) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $redirectToSourceRecord does not seem to be defined for all execution paths leading up to this point.
Loading history...
115
                $params = [ 'id' => $sourceRecordId ];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sourceRecordId does not seem to be defined for all execution paths leading up to this point.
Loading history...
116
117
                // Add tab id if defined to select it automaticaly
118
                if ($tabId) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tabId does not seem to be defined for all execution paths leading up to this point.
Loading history...
119
                    $params[ 'tab' ] = $tabId;
120
                }
121
                // Add related list id to select the related tab automaticaly
122
                else {
123
                    $params[ 'relatedlist' ] = $relatedlist->id;
124
                }
125
126
                $route = ucroute('uccello.detail', $domain, $relatedlist->module, $params);
127
            }
128
129
            // Redirect to edit if the user want to create a new record
130
            elseif ($request->input('save_new_hdn') === '1') {
131
                $route = ucroute('uccello.edit', $domain, $module);
132
133
                // Notification
134
                ucnotify(uctrans('notification.record.created', $module), 'success');
135
            }
136
            // Else redirect to detail
137
            else {
138
                $route = ucroute('uccello.detail', $domain, $module, [ 'id' => $record->getKey() ]);
139
            }
140
141
            return redirect($route);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect($route) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type void.
Loading history...
142
        }
143
        // Or return record
144
        else {
145
            return $form->getModel();
146
        }
147
    }
148
149
    /**
150
     * Restore a record from trash
151
     *
152
     * @param Domain|null $domain
153
     * @param Module $module
154
     * @param Request $request
155
     * @return @return \Illuminate\Http\Response
0 ignored issues
show
Documentation Bug introduced by
The doc comment @return at position 0 could not be parsed: Unknown type name '@return' at position 0 in @return.
Loading history...
156
     */
157
    public function restore(?Domain $domain, Module $module, Request $request)
158
    {
159
        // Pre-process
160
        $this->preProcess($domain, $module, $request);
161
162
        // Retrieve record
163
        $recordId = (int) request('id');
164
        $modelClass = $module->model_class;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
165
        $record = $modelClass::onlyTrashed()->findOrFail($recordId);
166
167
        // Restore record if exists
168
        if ($record) {
169
            $record->restore();
170
171
            ucnotify(uctrans('notification.record.restored', $module), 'success');
172
        }
173
174
        // Redirect to the trash list
175
        $route = ucroute('uccello.list', $domain, $module, ['filter' => 'trash']);
176
        return redirect($route);
177
    }
178
179
    /**
180
     * Add a relation between two records
181
     *
182
     * @param Domain|null $domain
183
     * @param Module $module
184
     * @param Request $request
185
     * @return integer|null
186
     */
187
    public function addRelation(?Domain $domain, Module $module, Request $request)
188
    {
189
        // Pre-process
190
        $this->preProcess($domain, $module, $request);
191
192
        // Retrieve record or get a new empty instance
193
        $record = $this->getRecordFromRequest();
194
195
        if ($request->input('relatedlist') && $request->input('related_id')) {
196
            $relatedlist = Relatedlist::findOrFail($request->input('relatedlist'));
197
            $relatedRecordId = (int)$request->input('related_id');
198
199
            $relationId = $this->saveRelation($relatedlist, $record->id, $relatedRecordId);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $relationId is correct as $this->saveRelation($rel...->id, $relatedRecordId) targeting Uccello\Core\Http\Contro...troller::saveRelation() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
200
201
            $response = [
202
                'success' => true,
203
                'data' => $relationId
204
            ];
205
        } else {
206
            $response = [
207
                'success' => false,
208
                'message' => uctrans('error.field.mandatory', $module)
209
            ];
210
        }
211
212
        return $response;
213
    }
214
215
    /**
216
     * Delete a relation between two records
217
     *
218
     * @param Domain|null $domain
219
     * @param Module $module
220
     * @param Request $request
221
     * @return integer|null
222
     */
223
    public function deleteRelation(?Domain $domain, Module $module, Request $request)
224
    {
225
        // Pre-process
226
        $this->preProcess($domain, $module, $request);
227
228
        // Get related list if defined
229
        $relatedListId = $request->get('relatedlist');
230
        if ($relatedListId) {
231
            $relatedList = Relatedlist::find($relatedListId);
232
        } else {
233
            $relatedList = null;
234
        }
235
236
        // Delete relation if it is a N-N relation
237
        if ($relatedList && $relatedList->type === 'n-n') {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
238
            $this->deleteRelationForNN($request);
239
            $message = 'notification.relation.deleted';
240
        }
241
        // Else delete record
242
        else {
243
            $this->deleteRecord($module, $request);
244
            $message = 'notification.record.deleted';
245
        }
246
247
        // Notification
248
        if (!empty($message)) {
249
            ucnotify(uctrans($message, $module), 'success');
250
        }
251
252
        // Redirect to the previous page
253
        $route = $this->getRedirectionRoute();
254
255
        return redirect($route);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect($route) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type integer|null.
Loading history...
256
    }
257
258
    public function getForm($record = null)
259
    {
260
        return $this->formBuilder->create(EditForm::class, [
261
            'model' => $record,
262
            'data' => [
263
                'domain' => $this->domain,
264
                'module' => $this->module,
265
                'request' => $this->request
266
            ]
267
        ]);
268
    }
269
270
    /**
271
     * Save relation between two records
272
     *
273
     * @param Relatedlist $relatedList
274
     * @param integer $recordId
275
     * @param integer $relatedRecordId
276
     * @return void
277
     */
278
    protected function saveRelation(Relatedlist $relatedList, int $recordId, int $relatedRecordId)
279
    {
280
        $relationName = $relatedList->relationName;
281
        $modelClass = $relatedList->module->model_class;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
282
283
        $record = $modelClass::find($recordId);
284
285
        if ($record) {
286
            $relation = $record->$relationName();
287
            $relation->attach($relatedRecordId);
288
        }
289
    }
290
291
    /**
292
     * Retrieve the redirection route from request parameters.
293
     *
294
     * @return string
295
     */
296
    protected function getRedirectionRoute(): string
297
    {
298
        $request = $this->request;
299
        $domain = $this->domain;
300
        $module = $this->module;
301
302
        // Retrieve a related list by its id if it is defined
303
        if ($request->input('relatedlist')) {
304
            $relatedlist = Relatedlist::find($request->input('relatedlist'));
305
        }
306
307
        // Redirect to source record if a relation was deleted
308
        if (isset($relatedlist) && $request->input('id')) {
309
            $params = ['id' => $request->input('id')];
310
311
            // Add tab id if defined to select it automaticaly
312
            if ($request->input('tab')) {
313
                $params['tab'] = $request->input('tab');
314
            }
315
            // Add related list id to select the related tab automaticaly
316
            else {
317
                $params['relatedlist'] = $relatedlist->id;
318
            }
319
320
            $route = ucroute('uccello.detail', $domain, $relatedlist->module, $params);
321
        }
322
        // Else redirect to list
323
        else {
324
            $route = ucroute('uccello.list', $domain, $module);
325
        }
326
327
        return $route;
328
    }
329
330
    /**
331
     * Delete record after retrieving from the request
332
     *
333
     * @param Module $module
334
     * @param Request $request
335
     * @return void
336
     */
337
    protected function deleteRecord(Module $module, Request $request)
338
    {
339
        $relatedRecord = $this->getRecordFromRequest('related_id');
340
341
        // Delete record if exists
342
        if ($relatedRecord) {
343
            event(new BeforeDeleteEvent($this->domain, $module, $request, $relatedRecord, 'delete'));
344
345
            $relatedRecord->delete();
346
347
            event(new AfterDeleteEvent($this->domain, $module, $request, $relatedRecord, 'delete'));
348
        }
349
    }
350
351
    /**
352
     * Delete a relation for a N-N related list
353
     *
354
     * @return void
355
     */
356
    protected function deleteRelationForNN(Request $request)
357
    {
358
        $relatedListId = $request->get('relatedlist');
359
        $recordId = $request->get('id');
360
        $relatedRecordId = $request->get('related_id');
361
362
        // Get related list
363
        $relatedList = Relatedlist::find($relatedListId);
364
        $relationName = $relatedList->relationName;
365
366
        if ($relatedList) {
367
            // Get record
368
            $modelClass = $relatedList->module->model_class;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
369
            $record = $modelClass::find($recordId);
370
371
            // Delete relation
372
            if ($record) {
373
                $record->$relationName()->detach($relatedRecordId);
374
            }
375
        }
376
    }
377
}
378