Passed
Push — master ( 93213b...560c2c )
by Thomas
11:31 queued 05:31
created

overrideTranslations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Requests\EvaluationTypeRequest as StoreRequest;
6
// VALIDATION: change the requests to match your own file names if you need form validation
7
use App\Models\EvaluationType;
8
use Backpack\CRUD\app\Http\Controllers\CrudController;
9
use Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
10
use Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
11
use Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
12
use Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
13
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
14
15
class EvaluationTypeCrudController extends CrudController
16
{
17
    use ListOperation;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...perations\ListOperation requires some properties which are not provided by App\Http\Controllers\Adm...ationTypeCrudController: $model, $query, $entity_name_plural
Loading history...
18
    use CreateOperation {store as traitStore;}
0 ignored issues
show
Bug introduced by
The trait Backpack\CRUD\app\Http\C...rations\CreateOperation requires the property $entity_name which is not provided by App\Http\Controllers\Adm...ationTypeCrudController.
Loading history...
19
    use UpdateOperation {update as traitUpdate;}
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...rations\UpdateOperation requires some properties which are not provided by App\Http\Controllers\Adm...ationTypeCrudController: $entity_name, $model
Loading history...
20
    use DeleteOperation;
21
22
    public function setup()
23
    {
24
        CRUD::setModel(EvaluationType::class);
25
        CRUD::setRoute(config('backpack.base.route_prefix').'/evaluationtype');
26
        CRUD::setEntityNameStrings(__('evaluation type'), __('evaluation types'));
0 ignored issues
show
Bug introduced by
It seems like __('evaluation type') can also be of type array and array; however, parameter $singular of Backpack\CRUD\app\Librar...:setEntityNameStrings() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        CRUD::setEntityNameStrings(/** @scrutinizer ignore-type */ __('evaluation type'), __('evaluation types'));
Loading history...
Bug introduced by
It seems like __('evaluation types') can also be of type array and array; however, parameter $plural of Backpack\CRUD\app\Librar...:setEntityNameStrings() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        CRUD::setEntityNameStrings(__('evaluation type'), /** @scrutinizer ignore-type */ __('evaluation types'));
Loading history...
27
    }
28
29
    protected function setupListOperation()
30
    {
31
        CRUD::addColumn(['name' => 'name', 'label' => 'Name']);
32
    }
33
34
    protected function setupCreateOperation()
35
    {
36
        CRUD::addField(['name' => 'name', 'label' => 'Name', 'type' => 'text']);
37
38
        CRUD::addField([    // Select2Multiple = n-n relationship (with pivot table)
39
            'label'     => __('Grade Types'),
40
            'type'      => 'select2_multiple',
41
            'name'      => 'gradeTypes', // the method that defines the relationship in your Model
42
43
            // optional
44
            'entity'    => 'gradeTypes', // the method that defines the relationship in your Model
45
            'model'     => "App\Models\GradeType", // foreign key model
46
            'attribute' => 'complete_name', // foreign key attribute that is shown to user
47
            'pivot'     => true, // on create&update, do you need to add/delete pivot table entries?
48
            'select_all' => true, // show Select All and Clear buttons?
49
        ]);
50
51
        CRUD::addField([    // Select2Multiple = n-n relationship (with pivot table)
52
            'label'     => __('Skills'),
53
            'type'      => 'select2_multiple',
54
            'name'      => 'skills', // the method that defines the relationship in your Model
55
56
            // optional
57
            'entity'    => 'skills', // the method that defines the relationship in your Model
58
            'model'     => "App\Models\Skills\Skill", // foreign key model
59
            'attribute' => 'complete_name', // foreign key attribute that is shown to user
60
            'pivot'     => true, // on create&update, do you need to add/delete pivot table entries?
61
            'select_all' => true, // show Select All and Clear buttons?
62
        ]);
63
64
        CRUD::setValidation(StoreRequest::class);
65
    }
66
67
    protected function setupUpdateOperation()
68
    {
69
        $this->setupCreateOperation();
70
    }
71
72
    public function store()
73
    {
74
        $value = $this->crud->getRequest()->input('name');
0 ignored issues
show
Bug introduced by
The method input() does not exist on Illuminate\Http\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        $value = $this->crud->getRequest()->/** @scrutinizer ignore-call */ input('name');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
        $response = $this->traitStore();
76
        $entry = $this->crud->getCurrentEntry();
77
78
        $this->overrideTranslations($entry, $value);
79
        
80
        return $response;
81
    }
82
83
    private function overrideTranslations($entry, $value) {
84
        foreach (config('app.languages') as $i => $locale) {
85
            $entry->setTranslation('name', $locale, $value);
86
        }
87
        $entry->save();
88
    }
89
90
    public function update()
91
    {
92
        $value = $this->crud->getRequest()->input('name');
93
        $response = $this->traitStore();
94
        $entry = $this->crud->getCurrentEntry();
95
        
96
        $this->overrideTranslations($entry, $value);
97
98
        return $response;
99
    }
100
}
101