ProjectReleaseCrudController   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 299
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 118
dl 0
loc 299
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 3 1
A showDetailsRow() 0 3 1
A edit() 0 3 1
A show() 0 3 1
A restoreRevision() 0 3 1
A listRevisions() 0 3 1
A update() 0 7 1
C setup() 0 199 10
A store() 0 7 1
1
<?php
2
3
namespace App\Http\Controllers\Frontend\Project;
4
5
use App\Http\Requests\Frontend\Project\ProjectReleaseRequest as StoreRequest;
6
use App\Http\Requests\Frontend\Project\ProjectReleaseRequest as UpdateRequest;
7
use App\Http\Traits\UsesPolicies;
8
use App\Jobs\Publish;
9
use App\Models\Access\User\User;
10
use App\Models\Elementset;
11
// VALIDATION: change the requests to match your own file names if you need form validation
12
use App\Models\Project;
13
use App\Models\Release;
14
use App\Models\Vocabulary;
15
use Backpack\CRUD\app\Http\Controllers\CrudController;
16
use Illuminate\Database\Eloquent\Builder;
17
use Illuminate\Support\Facades\Gate;
18
use Illuminate\Support\Facades\Route;
19
20
class ProjectReleaseCrudController extends CrudController
21
{
22
    //use UsesPolicies;
23
24
    /**
25
     * @throws \Exception
26
     */
27
    public function setup()
28
    {
29
30
        /*
31
        |--------------------------------------------------------------------------
32
        | BASIC CRUD INFORMATION
33
        |--------------------------------------------------------------------------
34
        */
35
36
        $project_id = Route::current()->parameter('project_id') ?? Route::current()->parameter('project');
37
38
        $this->crud->setModel(Release::class);
39
        $this->crud->setEntityNameStrings('Release', 'Releases');
40
        $this->crud->setRoute(config('backpack.base.route_prefix') . '/projects/' . $project_id . '/releases');
0 ignored issues
show
Bug introduced by
Are you sure $project_id of type object|string can be used in concatenation? ( Ignorable by Annotation )

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

40
        $this->crud->setRoute(config('backpack.base.route_prefix') . '/projects/' . /** @scrutinizer ignore-type */ $project_id . '/releases');
Loading history...
41
42
        if ($project_id) {
43
            $project = Project::findOrFail($project_id);
44
            $this->crud->addClause('where', 'agent_id', $project_id);
45
            Vocabulary::addGlobalScope('project_id', function (Builder $builder) use ($project_id) {
46
                $builder->where('agent_id', $project_id);
47
            });
48
            Elementset::addGlobalScope('project_id', function (Builder $builder) use ($project_id) {
49
                $builder->where('agent_id', $project_id);
50
            });
51
        }
52
53
        /*
54
        |--------------------------------------------------------------------------
55
        | BASIC CRUD INFORMATION
56
        |--------------------------------------------------------------------------
57
        */
58
59
        $this->crud->setFromDb();
60
61
        // ------ CRUD FIELDS
62
        // $this->crud->addField($options, 'update/create/both');
63
        // $this->crud->addFields($array_of_arrays, 'update/create/both');
64
        // $this->crud->removeField('name', 'update/create/both');
65
        $this->crud->removeFields(['user_id', 'agent_id', 'name', 'body', 'github_response', 'github_id', 'tag_name', 'target_commitish', 'is_draft', 'is_prerelease', 'published_at']);
66
        $this->crud->addFields([
67
            [
68
                'name'    => 'user_id',
69
                'type'    => 'hidden',
70
                'default' => request()->user() ? request()->user()->id : null,
71
            ],
72
            [
73
                'name'    => 'agent_id',
74
                'type'    => 'hidden',
75
                'default' => $project_id,
76
            ],
77
            [
78
                'name'       => 'name',
79
                'label'      => 'Release Title',
80
                'type'       => 'text',
81
                'attributes' => [
82
                    'placeholder' => 'The descriptive title of this release',
83
                ],
84
            ],
85
            [
86
                'name'       => 'body',
87
                'label'      => 'Release Notes',
88
                'type'       => 'textarea',
89
                'attributes' => [
90
                    'placeholder' => 'Add some description of the changes in this release',
91
                    'rows'        => 10,
92
                ],
93
            ],
94
            [
95
                'name'       => 'tag_name',
96
                'label'      => 'Tag Name',
97
                'type'       => 'text',
98
                'attributes' => [
99
                    'placeholder' => 'The name of the GIT tag to assign to this release',
100
                ],
101
            ],
102
            [
103
                'name'  => 'is_prerelease',
104
                'label' => 'Pre-Release',
105
                'type'  => 'checkbox',
106
                'hint'  => 'Notify consuming systems that this release is not final',
107
108
            ],
109
            [
110
                'label'       => 'Value Vocabularies to publish...',
111
                'type'        => 'select_multiple',
112
                'name'        => 'vocabularies', // the method that defines the relationship in your Model
113
                'entity'      => 'vocabularies', // the method that defines the relationship in your Model
114
                'attribute'   => 'name', // foreign key attribute that is shown to user
115
                'model'       => Vocabulary::class, // foreign key model
116
                'pivot'       => true, // on create&update, do you need to add/delete pivot table entries?
117
                'pivotFields' => [], // an array of pivot table fields
118
                'morph'       => true,
119
                'allows_null' => false,
120
                'hint'        => 'Use shift- or ctrl-click (Windows) or cmd-click (mac) to select.',
121
            ],
122
            [
123
                'label'       => 'Element Sets to publish...',
124
                'type'        => 'select_multiple',
125
                'name'        => 'elementsets', // the method that defines the relationship in your Model
126
                'entity'      => 'elementsets', // the method that defines the relationship in your Model
127
                'attribute'   => 'name', // foreign key attribute that is shown to user
128
                'model'       => Elementset::class, // foreign key model
129
                'pivot'       => true, // on create&update, do you need to add/delete pivot table entries?
130
                'pivotFields' => [], // an array of pivot table fields
131
                'morph'       => true,
132
                'allows_null' => false,
133
                'hint'        => 'Use shift- or ctrl-click (Windows) or cmd-click (mac) to select.',
134
            ],
135
        ]);
136
137
        // ------ CRUD COLUMNS
138
        // $this->crud->addColumn(); // add a single column, at the end of the stack
139
        // $this->crud->addColumns(); // add multiple columns, at the end of the stack
140
        //$this->crud->removeColumn('github_response'); // remove a column from the stack
141
        $this->crud->removeColumns(['user_id', 'agent_id', 'github_response', 'github_id', 'target_commitish', 'is_draft', 'deleted_at']); // remove an array of columns from the stack
142
        // $this->crud->setColumnDetails('column_name', ['attribute' => 'value']); // adjusts the properties of the passed in column (by name)
143
        $this->crud->addColumns([
144
            [
145
                // n-n relationship (with pivot table)
146
                'label'     => 'Vocabularies', // Table column heading
147
                'type'      => 'select_multiple',
148
                'name'      => 'vocabularies', // the method that defines the relationship in your Model
149
                'entity'    => 'vocabularies', // the method that defines the relationship in your Model
150
                'attribute' => 'name', // foreign key attribute that is shown to user
151
                'model'     => Vocabulary::class, // foreign key model
152
153
            ],
154
            [
155
                // n-n relationship (with pivot table)
156
                'label'     => 'Element Sets', // Table column heading
157
                'type'      => 'select_multiple',
158
                'name'      => 'elementsets', // the method that defines the relationship in your Model
159
                'entity'    => 'elementsets', // the method that defines the relationship in your Model
160
                'attribute' => 'name', // foreign key attribute that is shown to user
161
                'model'     => Elementset::class, // foreign key model
162
163
            ],
164
            [
165
                'label'     => 'Released by', // Table column heading
166
                'type'      => 'select',
167
                'name'      => 'user', // the method that defines the relationship in your Model
168
                'entity'    => 'user', // the method that defines the relationship in your Model
169
                'attribute' => 'name', // foreign key attribute that is shown to user
170
                'model'     => User::class, // foreign key model
171
172
            ],
173
        ]);
174
175
        $this->crud->setColumnDetails('is_prerelease',
176
            [
177
                'type'  => 'boolean',
178
                'label' => 'Pre-release?',
179
180
            ]);
181
        $this->crud->setColumnDetails('body',
182
            [
183
                'type'  => 'markdown',
184
            ]);
185
186
        $this->crud->setColumnsDetails(['body', 'tag_name'],
187
            [
188
                'list'    => false,
189
            ]);
190
191
        // $this->crud->setColumnsDetails(['column_1', 'column_2'], ['attribute' => 'value']);
192
193
        // ------ CRUD BUTTONS
194
        // possible positions: 'beginning' and 'end'; defaults to 'beginning' for the 'line' stack, 'end' for the others;
195
        $this->crud->addButton('line', 'publish', 'view', 'backpack::crud.buttons.publish', 'end'); // add a button; possible types are: view, model_function
196
        // $this->crud->addButtonFromModelFunction($stack, $name, $model_function_name, $position); // add a button whose HTML is returned by a method in the CRUD model
197
        // $this->crud->addButtonFromView($stack, $name, $view, $position); // add a button whose HTML is in a view placed at resources\views\vendor\backpack\crud\buttons
198
        // $this->crud->removeButton($name);
199
        // $this->crud->removeButtonFromStack($name, $stack);
200
        // $this->crud->removeAllButtons();
201
        // $this->crud->removeAllButtonsFromStack('line');
202
203
        $this->crud->denyAccess(['create', 'update', 'delete', 'import', 'publish']);
204
        if ($project->is_private) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $project does not seem to be defined for all execution paths leading up to this point.
Loading history...
205
            if (Gate::allows('index', $project)) {
206
                $this->crud->allowAccess(['index']);
207
            }
208
            if (Gate::allows('view', $project)) {
209
                $this->crud->allowAccess(['show']);
210
            }
211
        } else {
212
            $this->crud->allowAccess(['index']);
213
            $this->crud->allowAccess(['show']);
214
        }
215
        if (Gate::allows('create', $project)) {
216
            $this->crud->allowAccess(['create']);
217
        }
218
        if (Gate::allows('update', $project)) {
219
            $this->crud->allowAccess(['update']);
220
        }
221
        if (Gate::allows('delete', $project)) {
222
            $this->crud->allowAccess(['delete']);
223
        }
224
        if (Gate::allows('publish', $project)) {
225
            $this->crud->allowAccess(['publish']);
226
        }
227
        // ------ CRUD ACCESS
228
        // $this->crud->allowAccess([ 'index', 'show' ]);
229
230
        //$this->authorizeAll();
231
232
        // ------ CRUD REORDER
233
        // $this->crud->enableReorder('label_name', MAX_TREE_LEVEL);
234
        // NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('reorder');
235
236
        // ------ CRUD DETAILS ROW
237
        // $this->crud->enableDetailsRow();
238
        // NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('details_row');
239
        // NOTE: you also need to do overwrite the showDetailsRow($id) method in your EntityCrudController to show whatever you'd like in the details row OR overwrite the views/backpack/crud/details_row.blade.php
240
241
        // ------ REVISIONS
242
        // You also need to use \Venturecraft\Revisionable\RevisionableTrait;
243
        // Please check out: https://laravel-backpack.readme.io/docs/crud#revisions
244
        // $this->crud->allowAccess('revisions');
245
246
        // ------ AJAX TABLE VIEW
247
        // Please note the drawbacks of this though:
248
        // - 1-n and n-n columns are not searchable
249
        // - date and datetime columns won't be sortable anymore
250
        // $this->crud->enableAjaxTable();
251
252
        // ------ DATATABLE EXPORT BUTTONS
253
        // Show export to PDF, CSV, XLS and Print buttons on the table view.
254
        // Does not work well with AJAX datatables.
255
        // $this->crud->enableExportButtons();
256
257
        // ------ ADVANCED QUERIES
258
        // $this->crud->addClause('active');
259
        // $this->crud->addClause('type', 'car');
260
        // $this->crud->addClause('where', 'name', '==', 'car');
261
        // $this->crud->addClause('whereName', 'car');
262
        // $this->crud->addClause('whereHas', 'posts', function($query) {
263
        //     $query->activePosts();
264
        // });
265
        // $this->crud->addClause('withoutGlobalScopes');
266
        // $this->crud->addClause('withoutGlobalScope', VisibleScope::class);
267
        // $this->crud->with(); // eager load relationships
268
        // $this->crud->orderBy();
269
        // $this->crud->groupBy();
270
        // $this->crud->limit();
271
    }
272
273
    public function show($id)
274
    {
275
        return parent::show($this->request->release);
276
    }
277
278
    public function listRevisions($id)
279
    {
280
        return parent::listRevisions($this->request->release);
281
    }
282
283
    public function restoreRevision($id)
284
    {
285
        return parent::restoreRevision($this->request->release);
286
    }
287
288
    public function showDetailsRow($id)
289
    {
290
        return parent::showDetailsRow($this->request->release);
291
    }
292
293
    public function edit($id)
294
    {
295
        return parent::edit($this->request->release);
296
    }
297
298
    public function destroy($id)
299
    {
300
        return parent::destroy($this->request->release);
301
    }
302
303
    public function store(StoreRequest $request)
304
    {
305
        // your additional operations before save here
306
        $redirect_location = parent::storeCrud($request);
307
        // your additional operations after save here
308
        // use $this->data['entry'] or $this->crud->entry
309
        return $redirect_location;
310
    }
311
312
    public function update(UpdateRequest $request)
313
    {
314
        // your additional operations before save here
315
        $redirect_location = parent::updateCrud($request);
316
        // your additional operations after save here
317
        // use $this->data['entry'] or $this->crud->entry
318
        return $redirect_location;
319
    }
320
}
321