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

Passed
Pull Request — master (#3132)
by Cristian
14:13
created

ShowOperation::setupShowDefaults()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 1
nop 0
dl 0
loc 29
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers\Operations;
4
5
use Illuminate\Support\Facades\Route;
6
7
trait ShowOperation
8
{
9
    /**
10
     * Define which routes are needed for this operation.
11
     *
12
     * @param string $segment    Name of the current entity (singular). Used as first URL segment.
13
     * @param string $routeName  Prefix of the route name.
14
     * @param string $controller Name of the current CrudController.
15
     */
16
    protected function setupShowRoutes($segment, $routeName, $controller)
17
    {
18
        Route::get($segment.'/{id}/show', [
19
            'as'        => $routeName.'.show',
20
            'uses'      => $controller.'@show',
21
            'operation' => 'show',
22
        ]);
23
    }
24
25
    /**
26
     * Add the default settings, buttons, etc that this operation needs.
27
     */
28
    protected function setupShowDefaults()
29
    {
30
        $this->crud->allowAccess('show');
31
        $this->crud->setOperationSetting('setFromDb', true);
32
33
        $this->crud->operation('show', function () {
34
            $this->crud->loadDefaultOperationSettingsFromConfig();
35
        });
36
37
        $this->crud->operation('list', function () {
38
            $this->crud->addButton('line', 'show', 'view', 'crud::buttons.show', 'beginning');
39
        });
40
41
        $this->crud->operation(['create', 'update'], function () {
42
            $this->crud->addSaveAction([
43
                'name' => 'save_and_preview',
44
                'visible' => function ($crud) {
45
                    return $crud->hasAccess('show');
46
                },
47
                'redirect' => function ($crud, $request, $itemId = null) {
48
                    $itemId = $itemId ?: $request->input('id');
49
                    $redirectUrl = $crud->route.'/'.$itemId.'/show';
50
                    if ($request->has('locale')) {
51
                        $redirectUrl .= '?locale='.$request->input('locale');
52
                    }
53
54
                    return $redirectUrl;
55
                },
56
                'button_text' => trans('backpack::crud.save_action_save_and_preview'),
57
            ]);
58
        });
59
    }
60
61
    /**
62
     * Display the specified resource.
63
     *
64
     * @param int $id
65
     *
66
     * @return Response
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\app\Http\C...ers\Operations\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
67
     */
68
    public function show($id)
69
    {
70
        $this->crud->hasAccessOrFail('show');
71
72
        // get entry ID from Request (makes sure its the last ID for nested resources)
73
        $id = $this->crud->getCurrentEntryId() ?? $id;
74
75
        // get the info for that entry
76
        $this->data['entry'] = $this->crud->getEntry($id);
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
        $this->data['crud'] = $this->crud;
78
        $this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.preview').' '.$this->crud->entity_name;
0 ignored issues
show
Bug introduced by
Are you sure trans('backpack::crud.preview') of type array|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

78
        $this->data['title'] = $this->crud->getTitle() ?? /** @scrutinizer ignore-type */ trans('backpack::crud.preview').' '.$this->crud->entity_name;
Loading history...
79
80
        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
81
        return view($this->crud->getShowView(), $this->data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view($this->crud-...howView(), $this->data) returns the type Illuminate\View\View which is incompatible with the documented return type Backpack\CRUD\app\Http\C...ers\Operations\Response.
Loading history...
82
    }
83
84
    /**
85
     * Default behaviour for setupShowOperation, in case none has been
86
     * provided by including a setupShowOperation() method in the CrudController.
87
     */
88
    protected function setupShowOperation()
89
    {
90
        // guess which columns to show, from the database table
91
        if ($this->crud->get('show.setFromDb')) {
92
            $this->crud->setFromDb();
93
        }
94
95
        // if the developer has chosen to include a modifyShowOperation() method
96
        // in his/her CrudController, make sure to call it because they probably want to
97
        // add a new column or remove an autoset column
98
        if (method_exists($this, 'modifyShowOperation')) {
99
            $this->modifyShowOperation();
100
        }
101
102
        // remove the columns that usually don't make sense inside the Show operation
103
        $this->removeColumnsThatDontBelongInsideShowOperation();
104
105
        // remove preview button from stack:line
106
        $this->crud->removeButton('show');
107
    }
108
109
    protected function removeColumnsThatDontBelongInsideShowOperation()
110
    {
111
        // cycle through columns
112
        foreach ($this->crud->columns() as $key => $column) {
113
114
            // remove any autoset relationship columns
115
            if (array_key_exists('model', $column) && array_key_exists('autoset', $column) && $column['autoset']) {
116
                $this->crud->removeColumn($column['key']);
117
            }
118
119
            // remove any autoset table columns
120
            if ($column['type'] == 'table' && array_key_exists('autoset', $column) && $column['autoset']) {
121
                $this->crud->removeColumn($column['key']);
122
            }
123
124
            // remove the row_number column, since it doesn't make sense in this context
125
            if ($column['type'] == 'row_number') {
126
                $this->crud->removeColumn($column['key']);
127
            }
128
129
            // remove columns that have visibleInShow set as false
130
            if (isset($column['visibleInShow']) && $column['visibleInShow'] == false) {
131
                $this->crud->removeColumn($column['key']);
132
            }
133
134
            // remove the character limit on columns that take it into account
135
            if (in_array($column['type'], ['text', 'email', 'model_function', 'model_function_attribute', 'phone', 'row_number', 'select'])) {
136
                $this->crud->modifyColumn($column['key'], ['limit' => ($column['limit'] ?? 999)]);
137
            }
138
        }
139
140
        // remove bulk actions colums
141
        $this->crud->removeColumns(['blank_first_column', 'bulk_actions']);
142
    }
143
}
144