Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#51)
by
unknown
10:13
created

ArticleCrudController::fetchCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Backpack\NewsCRUD\app\Http\Controllers\Admin;
4
5
use Backpack\CRUD\app\Http\Controllers\CrudController;
6
use Backpack\NewsCRUD\app\Http\Requests\ArticleRequest;
7
8
class ArticleCrudController extends CrudController
9
{
10
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
11
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
12
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
13
    use \Backpack\CRUD\app\Http\Controllers\Operations\CloneOperation;
14
    use \Backpack\CRUD\app\Http\Controllers\Operations\BulkCloneOperation;
15
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
16
    use \Backpack\CRUD\app\Http\Controllers\Operations\BulkDeleteOperation;
17
    use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
18
    use \Backpack\CRUD\app\Http\Controllers\Operations\FetchOperation;
19
20
    public function setup()
21
    {
22
        /*
23
        |--------------------------------------------------------------------------
24
        | BASIC CRUD INFORMATION
25
        |--------------------------------------------------------------------------
26
        */
27
        $this->crud->setModel(\Backpack\NewsCRUD\app\Models\Article::class);
28
        $this->crud->setRoute(config('backpack.base.route_prefix', 'admin').'/article');
29
        $this->crud->setEntityNameStrings('article', 'articles');
30
31
        /*
32
        |--------------------------------------------------------------------------
33
        | LIST OPERATION
34
        |--------------------------------------------------------------------------
35
        */
36
        $this->crud->operation('list', function () {
37
            $this->crud->addColumn('title');
38
            $this->crud->addColumn([
39
                'name' => 'date',
40
                'label' => 'Date',
41
                'type' => 'date',
42
            ]);
43
            $this->crud->addColumn('status');
44
            $this->crud->addColumn([
45
                'name' => 'featured',
46
                'label' => 'Featured',
47
                'type' => 'check',
48
            ]);
49
            $this->crud->addColumn([
50
                'label' => 'Category',
51
                'type' => 'select',
52
                'name' => 'category_id',
53
                'entity' => 'category',
54
                'attribute' => 'name',
55
                'wrapper'   => [
56
                    'href' => function ($crud, $column, $entry, $related_key) {
57
                        return backpack_url('category/'.$related_key.'/show');
58
                    },
59
                ],
60
            ]);
61
            $this->crud->addColumn('tags');
62
63
            $this->crud->addFilter([ // select2 filter
64
                'name' => 'category_id',
65
                'type' => 'select2',
66
                'label'=> 'Category',
67
            ], function () {
68
                return \Backpack\NewsCRUD\app\Models\Category::all()->keyBy('id')->pluck('name', 'id')->toArray();
69
            }, function ($value) { // if the filter is active
70
                $this->crud->addClause('where', 'category_id', $value);
71
            });
72
73
            $this->crud->addFilter([ // select2_multiple filter
74
                'name' => 'tags',
75
                'type' => 'select2_multiple',
76
                'label'=> 'Tags',
77
            ], function () {
78
                return \Backpack\NewsCRUD\app\Models\Tag::all()->keyBy('id')->pluck('name', 'id')->toArray();
79
            }, function ($values) { // if the filter is active
80
                $this->crud->query = $this->crud->query->whereHas('tags', function ($q) use ($values) {
81
                    foreach (json_decode($values) as $key => $value) {
82
                        if ($key == 0) {
83
                            $q->where('tags.id', $value);
84
                        } else {
85
                            $q->orWhere('tags.id', $value);
86
                        }
87
                    }
88
                });
89
            });
90
        });
91
92
        /*
93
        |--------------------------------------------------------------------------
94
        | CREATE & UPDATE OPERATIONS
95
        |--------------------------------------------------------------------------
96
        */
97
        $this->crud->operation(['create', 'update'], function () {
98
            $this->crud->setValidation(ArticleRequest::class);
99
100
            $this->crud->addField([
101
                'name' => 'title',
102
                'label' => 'Title',
103
                'type' => 'text',
104
                'placeholder' => 'Your title here',
105
            ]);
106
            $this->crud->addField([
107
                'name' => 'slug',
108
                'label' => 'Slug (URL)',
109
                'type' => 'text',
110
                'hint' => 'Will be automatically generated from your title, if left empty.',
111
                // 'disabled' => 'disabled'
112
            ]);
113
            $this->crud->addField([
114
                'name' => 'date',
115
                'label' => 'Date',
116
                'type' => 'date',
117
                'default' => date('Y-m-d'),
118
            ]);
119
            $this->crud->addField([
120
                'name' => 'content',
121
                'label' => 'Content',
122
                'type' => 'ckeditor',
123
                'placeholder' => 'Your textarea text here',
124
            ]);
125
            $this->crud->addField([
126
                'name' => 'image',
127
                'label' => 'Image',
128
                'type' => 'browse',
129
            ]);
130
            $this->crud->addField([
131
                'label' => 'Category',
132
                'type' => 'relationship',
133
                'name' => 'category_id',
134
                'entity' => 'category',
135
                'attribute' => 'name',
136
                'inline_create' => true,
137
                'ajax' => true,
138
            ]);
139
            $this->crud->addField([
140
                'label' => 'Tags',
141
                'type' => 'relationship',
142
                'name' => 'tags', // the method that defines the relationship in your Model
143
                'entity' => 'tags', // the method that defines the relationship in your Model
144
                'attribute' => 'name', // foreign key attribute that is shown to user
145
                'pivot' => true, // on create&update, do you need to add/delete pivot table entries?
146
                'inline_create' => ['entity' => 'tag'],
147
                'ajax' => true,
148
            ]);
149
            $this->crud->addField([
150
                'name' => 'status',
151
                'label' => 'Status',
152
                'type' => 'enum',
153
            ]);
154
            $this->crud->addField([
155
                'name' => 'featured',
156
                'label' => 'Featured item',
157
                'type' => 'checkbox',
158
            ]);
159
        });
160
    }
161
162
    /**
163
     * Respond to AJAX calls from the select2 with entries from the Category model.
164
     * @return JSON
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonRes...on\LengthAwarePaginator?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
165
     */
166
    public function fetchCategory()
167
    {
168
        return $this->fetch(\Backpack\NewsCRUD\app\Models\Category::class);
169
    }
170
171
    /**
172
     * Respond to AJAX calls from the select2 with entries from the Tag model.
173
     * @return JSON
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonRes...on\LengthAwarePaginator?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
174
     */
175
    public function fetchTags()
176
    {
177
        return $this->fetch(\Backpack\NewsCRUD\app\Models\Tag::class);
178
    }
179
}
180