Passed
Push — feature/resources ( 00a731...ac736e )
by Yonathan
05:58 queued 12s
created

ResourcesCrudController::setupCreateOperation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 21
rs 9.7998
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php namespace App\Http\Controllers\Admin;
2
3
use App\Http\Requests\ResourceCrudRequest;
4
use Backpack\CRUD\app\Http\Controllers\CrudController;
5
use Illuminate\Support\Facades\App;
6
7
class ResourcesCrudController extends CrudController {
8
9
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
10
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
11
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
12
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
13
    use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
14
15
    /**
16
     * Setup for the Resource CRUD panel. Everything here is applied on ALL operations.
17
     *
18
     * @return void
19
    */
20
    public function setup()
21
    {
22
          $this->crud->setModel('App\Models\Resource');
23
          $this->crud->setRoute('admin/resource');
24
          $this->crud->setEntityNameStrings('resource', 'resources');
25
          $this->crud->removeButton('preview');
26
    }
27
28
    /**
29
     * Setup list of resources.
30
     *
31
     * @return void
32
     */
33
    protected function setupListOperation()
34
    {
35
        $locale = 'en';
36
        if (null !== $this->request->input('locale')) {
37
            $locale = $this->request->input('locale');
38
        }
39
        App::setLocale($locale);
40
        $this->crud->addColumn([
41
            'name' => 'id',
42
            'type' => 'text',
43
            'label' => 'ID',
44
        ]);
45
46
        $this->crud->addColumn([
47
            'name' => 'name',
48
            'type' => 'text',
49
            'label' => 'Title',
50
            'orderLogic' => function ($query, $column, $columnDirection) use ($locale) {
51
                return $query->orderBy('name->' . $locale, $columnDirection)->select('*');
52
            },
53
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale) : void {
54
                $query->orWhere('name->' . $locale, 'ilike', "%$searchTerm%");
55
          },
56
        ]);
57
    }
58
59
    /**
60
     * Create resource operation.
61
     *
62
     * @return void
63
     */
64
    protected function setupCreateOperation()
65
    {
66
        $this->crud->setValidation(ResourceCrudRequest::class);
67
68
        $this->crud->addField([
69
          'name' => 'name',
70
          'type' => 'text',
71
          'label' => 'Title',
72
        ]);
73
74
        $this->crud->addField([
75
          'name' => 'name',
76
          'type' => 'text',
77
          'label' => 'Title'
78
        ]);
79
80
        $this->crud->addField([
81
          'name' => 'file',
82
          'label' => 'File',
83
          'type' => 'upload',
84
          'upload' => true,
85
        ]);
86
    }
87
88
    /**
89
     * Update resource operation.
90
     *
91
     * @return void
92
     */
93
    protected function setupUpdateOperation()
94
    {
95
        $this->setupCreateOperation();
96
    }
97
  }
98