EvaluationTypeCrudController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
c 0
b 0
f 0
dl 0
loc 92
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setupListOperation() 0 4 1
A overrideTranslations() 0 6 2
A setup() 0 5 1
A setupCreateOperation() 0 37 1
A store() 0 9 1
A update() 0 9 1
A setupUpdateOperation() 0 3 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Requests\EvaluationTypeRequest as StoreRequest;
6
use App\Models\EvaluationType;
7
use App\Models\GradeType;
8
use App\Models\Skills\Skill;
9
use Backpack\CRUD\app\Http\Controllers\CrudController;
10
use Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
11
use Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
12
use Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
13
use Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
14
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
15
16
class EvaluationTypeCrudController extends CrudController
17
{
18
    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...
19
    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...
20
    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...
21
    use DeleteOperation;
22
23
    public function setup()
24
    {
25
        CRUD::setModel(EvaluationType::class);
26
        CRUD::setRoute(config('backpack.base.route_prefix').'/evaluationtype');
27
        CRUD::setEntityNameStrings(__('evaluation type'), __('evaluation types'));
28
    }
29
30
    protected function setupListOperation()
31
    {
32
        CRUD::addColumn(['name' => 'name',
33
            'label' => 'Name', ]);
34
    }
35
36
    protected function setupCreateOperation()
37
    {
38
        CRUD::addField(['name' => 'name',
39
            'label' => 'Name',
40
            'type' => 'text', ]);
41
42
        CRUD::addField([    // Select2Multiple = n-n relationship (with pivot table)
43
            'label' => __('Grade Types'),
44
            'type' => 'select2_multiple',
45
            'name' => 'gradeTypes',
46
47
            // optional
48
            'entity' => 'gradeTypes',
49
            'model' => GradeType::class,
50
            'attribute' => 'complete_name',
51
            'pivot' => true,
52
            // on create&update, do you need to add/delete pivot table entries?
53
            'select_all' => true,
54
            // show Select All and Clear buttons?
55
        ]);
56
57
        CRUD::addField([    // Select2Multiple = n-n relationship (with pivot table)
58
            'label' => __('Skills'),
59
            'type' => 'select2_multiple',
60
            'name' => 'skills',
61
62
            // optional
63
            'entity' => 'skills',
64
            'model' => Skill::class,
65
            'attribute' => 'complete_name',
66
            'pivot' => true,
67
            // on create&update, do you need to add/delete pivot table entries?
68
            'select_all' => true,
69
            // show Select All and Clear buttons?
70
        ]);
71
72
        CRUD::setValidation(StoreRequest::class);
73
    }
74
75
    protected function setupUpdateOperation()
76
    {
77
        $this->setupCreateOperation();
78
    }
79
80
    public function store()
81
    {
82
        $value = $this->crud->getRequest()->input('name');
83
        $response = $this->traitStore();
84
        $entry = $this->crud->getCurrentEntry();
85
86
        $this->overrideTranslations($entry, $value);
87
88
        return $response;
89
    }
90
91
    private function overrideTranslations($entry, $value)
92
    {
93
        foreach (config('app.languages') as $i => $locale) {
94
            $entry->setTranslation('name', $locale, $value);
95
        }
96
        $entry->save();
97
    }
98
99
    public function update()
100
    {
101
        $value = $this->crud->getRequest()->input('name');
102
        $response = $this->traitUpdate();
103
        $entry = $this->crud->getCurrentEntry();
104
105
        $this->overrideTranslations($entry, $value);
106
107
        return $response;
108
    }
109
}
110