Passed
Push — master ( 5e5783...65d409 )
by Jonathan
15:34 queued 05:32
created

PopupEditController::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 1
nc 1
nop 3
1
<?php
2
namespace Uccello\Core\Http\Controllers\Core;
3
4
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...
5
use Illuminate\Http\Request;
6
use Uccello\Core\Forms\PopupEditForm;
7
use Uccello\Core\Events\BeforeSaveEvent;
8
use Uccello\Core\Events\AfterSaveEvent;
9
use Uccello\Core\Models\Domain;
10
use Uccello\Core\Models\Module;
11
use Uccello\Core\Models\Relatedlist;
12
13
class PopupEditController extends Controller
14
{
15
    protected $viewName = 'edit.popup';
16
    protected $formBuilder;
17
    /**
18
     * Check user permissions
19
     */
20
    protected function checkPermissions()
21
    {
22
        $this->middleware('uccello.permissions:create');
23
    }
24
    public function __construct(FormBuilder $formBuilder)
25
    {
26
        $this->formBuilder = $formBuilder;
27
        parent::__construct();
28
    }
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function process(?Domain $domain, Module $module, Request $request)
33
    {
34
        // Pre-process
35
        $this->preProcess($domain, $module, $request);
36
        // Retrieve record or get a new empty instance
37
        $record = $this->getRecordFromRequest();
38
        // Get form
39
        $form = $this->getForm($domain, $module, $request);
40
        // Get mode
41
        $mode = 'create';
42
        return $this->autoView([
43
            'form' => $form,
44
            'record' => $record,
45
            'mode' => $mode
46
        ]);
47
    }
48
    /**
49
     * Create or update record into database
50
     *
51
     * @param Domain|null $domain
52
     * @param Module $module
53
     * @param boolean $redirect
54
     * @return void
55
     */
56
    public function save(?Domain $domain, Module $module, Request $request, bool $redirect = false)
57
    {
58
        // Pre-process
59
        $this->preProcess($domain, $module, $request);
60
        // Retrieve record or get a new empty instance
61
        $record = $this->getRecordFromRequest();
62
        // Get form
63
        $form = $this->getForm($domain, $module, $request, $record);
64
        $values = $form->getFieldValues();
65
        $record = $form->getModel();
66
        $request = $form->getRequest();
67
        //Add fields value to record
68
        foreach ($values as $fieldName => $value) {
69
            $field = $module->getField($fieldName);
70
            // If the field exists format the value and store it in the good model column
71
            if (!is_null($field)) {
72
                $column = $field->column;
73
                $form->getModel()->$column = uitype($field->uitype_id)->getFormattedValueToSave($request, $field, $value, $record, $domain, $module);
0 ignored issues
show
Bug introduced by
The property uitype_id does not exist on Uccello\Core\Models\Field. Did you mean uitype?
Loading history...
74
            }
75
        }
76
        // Redirect if form not valid
77
        if (! $form->isValid()) {
78
            ucnotify(uctrans('notification.form.not_valid', $module), 'error');
79
            $response = redirect(route('uccello.popup.edit', ['domain' => $domain->slug, 'module' => $module->name]));
0 ignored issues
show
Bug introduced by
The property name 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...
80
            $response = $response->withErrors($form->getErrors(), $form->getErrorBag())->withInput();
81
            return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type void.
Loading history...
82
        }
83
        event(new BeforeSaveEvent($domain, $module, $request, $record, 'create'));
84
        // Save record
85
        $record->save();
86
        event(new AfterSaveEvent($domain, $module, $request, $record, 'create'));
87
        return $record;
88
    }
89
    public function getForm($domain, $module, $request, $record = null)
90
    {
91
        if ($record === null) {
92
            $modelClass = $module->model_class;
93
            $record = new $modelClass;
94
        }
95
        return $this->formBuilder->create(PopupEditForm::class, [
96
            'model' => $record,
97
            'data' => [
98
                'domain' => $domain,
99
                'module' => $module,
100
                'request' => $request
101
            ]
102
        ]);
103
    }
104
}
105