CommentCrudController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
c 0
b 0
f 0
dl 0
loc 113
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setup() 0 10 1
A setupListOperation() 0 67 1
A setupUpdateOperation() 0 12 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Requests\CommentRequest;
6
use App\Models\Comment;
7
use App\Models\Enrollment;
8
use App\Models\Invoice;
9
use App\Models\Result;
10
use App\Models\Student;
11
use Backpack\CRUD\app\Http\Controllers\CrudController;
12
use Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
13
use Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
14
use Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
15
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
16
17
/**
18
 * Class CommentCrudController
19
 * Monitor comments. This controller is NOT used to add comments, only to view, edit or delete them.
20
 * A comment may be attached to various models (polymorphism).
21
 */
22
class CommentCrudController extends CrudController
23
{
24
    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\Admin\CommentCrudController: $model, $query, $entity_name_plural
Loading history...
25
    use UpdateOperation;
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\Admin\CommentCrudController: $entity_name, $model
Loading history...
26
    use DeleteOperation;
27
28
    public function __construct()
29
    {
30
        parent::__construct();
31
        $this->middleware(['permission:comments.edit']);
32
    }
33
34
    public function setup()
35
    {
36
        /*
37
        |--------------------------------------------------------------------------
38
        | CrudPanel Basic Information
39
        |--------------------------------------------------------------------------
40
        */
41
        CRUD::setModel(Comment::class);
42
        CRUD::setRoute(config('backpack.base.route_prefix').'/comment');
43
        CRUD::setEntityNameStrings(__('comment'), __('comments'));
44
    }
45
46
    /*
47
    |--------------------------------------------------------------------------
48
    | CrudPanel Configuration
49
    |--------------------------------------------------------------------------
50
    */
51
52
    public function setupListOperation()
53
    {
54
        CRUD::setColumns([
55
            [
56
                // Commentable entity
57
                'label' => 'Commentable',
58
                'type' => 'select',
59
                'name' => 'commentable_id',
60
                'entity' => 'commentable',
61
                'attribute' => 'name',
62
            ],
63
64
            [
65
                // Commentable entity
66
                'label' => 'Commentable',
67
                'type' => 'text',
68
                'name' => 'body',
69
            ],
70
71
            [
72
                // Commentable entity
73
                'label' => 'Author',
74
                'type' => 'select',
75
                'name' => 'author_id',
76
                'entity' => 'author',
77
                'attribute' => 'name',
78
            ],
79
80
            [
81
                // Commentable entity
82
                'label' => 'Action',
83
                'type' => 'boolean',
84
                'name' => 'action',
85
            ],
86
        ]);
87
88
        CRUD::addFilter(
89
            [ // simple filter
90
                'type' => 'simple',
91
                'name' => 'action',
92
                'label' => 'Action',
93
            ],
94
            false,
95
            function () {
96
                CRUD::addClause('where', 'action', true);
97
            }
98
        );
99
100
        CRUD::addFilter(
101
            [ // dropdown filter
102
                'name' => 'type',
103
                'type' => 'dropdown',
104
                'label' => 'Type',
105
            ],
106
            [
107
                Student::class => 'Student',
108
                Enrollment::class => 'Enrollments',
109
                Invoice::class => 'Invoice',
110
                Result::class => 'Result',
111
112
            ],
113
            function ($value) {
114
                CRUD::addClause('where', 'commentable_type', '=', $value);
115
            },
116
            function () { // if the filter is not active
117
                CRUD::addClause('where', 'commentable_type', '=', Student::class);
118
                $this->crud->getRequest()->request->add(['commentable_type' => Student::class]); // to make the filter look active
119
            }
120
        );
121
    }
122
123
    public function setupUpdateOperation()
124
    {
125
        CRUD::addFields([
126
            ['label' => 'Comment',
127
                'type' => 'text',
128
                'name' => 'body', ],
129
            ['label' => 'Action',
130
                'type' => 'checkbox',
131
                'name' => 'action', ],
132
        ]);
133
134
        CRUD::setRequiredFields(CommentRequest::class);
135
    }
136
}
137