Issues (54)

src/Http/Controllers/MenuController.php (13 issues)

1
<?php
2
3
namespace PhpCollective\MenuMaker\Http\Controllers;
4
5
use Illuminate\Routing\Controller;
6
use PhpCollective\MenuMaker\Storage\Menu;
7
use PhpCollective\MenuMaker\Storage\Role;
8
use PhpCollective\MenuMaker\Http\Requests\MenuRequest as Request;
9
10
class MenuController extends Controller
11
{
12
    /**
13
     * Display a listing of the resource.
14
     *
15
     * @return \Illuminate\Http\Response
16
     */
17
    public function index()
18
    {
19
        $menus = Menu::with('ancestors')
20
            ->where('parent_id', '>', 0)
21
            ->paginate();
22
        return view('menu-maker::menus.index', compact('menus'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('menu-maker:...dex', compact('menus')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
23
    }
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function filter()
31
    {
32
        $menus = Menu::filter()->pluck('name', 'id');
33
        $parent = request('p_id', '0');
34
        $parentCount = request('pCount', '0');
35
        return view('menu-maker::menus.menus', compact(
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('menu-maker:...arent', 'parentCount')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
36
            'menus', 'parent', 'parentCount'
37
        ));
38
    }
39
40
    /**
41
     * Show the form for creating a new resource.
42
     *
43
     * @return \Illuminate\Http\Response
44
     */
45
    public function create()
46
    {
47
        $sections = Menu::sections()->pluck('name', 'id');
48
49
        if(! $sections->count())
50
        {
51
            return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...s.no-section-exists'))) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
52
                ->route('menu-maker::sections.index')
53
                ->withErrors([__('menu-maker::alerts.no-section-exists')]);
54
        }
55
        $privileges = collect(Menu::$privileges);
56
        return view('menu-maker::menus.create', compact('sections', 'privileges'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('menu-maker:...ctions', 'privileges')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
57
    }
58
59
    /**
60
     * Store a newly created resource in storage.
61
     *
62
     * @param  Request  $request
63
     * @return \Illuminate\Http\Response
64
     */
65
    public function store(Request $request)
66
    {
67
        $data = $request->only([
68
            'name', 'alias', 'link', 'icon', 'class', 'attr', 'privilege', 'visible'
69
        ]);
70
        $data['routes'] = $request->route_list;
71
        $data['parent_id'] = Menu::findParent();
72
        Menu::create($data);
73
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...e' => $request->name))) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
74
            ->route('menu-maker::menus.index')
75
            ->withMessage(__('menu-maker::alerts.created', ['name' => $request->name]));
76
    }
77
78
    /**
79
     * Display the specified resource.
80
     *
81
     * @param  \App\Menu  $menu
0 ignored issues
show
The type App\Menu was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
82
     * @return \Illuminate\Http\Response
83
     */
84
    public function show(Menu $menu)
85
    {
86
        $menu->load('ancestors');
87
        return view('menu-maker::menus.show', compact('menu'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('menu-maker:...show', compact('menu')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
88
    }
89
90
    /**
91
     * Show the form for editing the specified resource.
92
     *
93
     * @param  \App\Menu  $menu
94
     * @return \Illuminate\Http\Response
95
     */
96
    public function edit(Menu $menu)
97
    {
98
        $sections = Menu::sections()->pluck('name', 'id');
99
        $privileges = collect(Menu::$privileges);
100
        $parent_ids = $menu->ancestors()->pluck('id')->toArray();
101
        return view('menu-maker::menus.edit', compact(
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('menu-maker:...ileges', 'parent_ids')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
102
            'menu', 'sections', 'privileges', 'parent_ids'
103
        ));
104
    }
105
106
    /**
107
     * Update the specified resource in storage.
108
     *
109
     * @param  Request  $request
110
     * @param  \App\Menu  $menu
111
     * @return \Illuminate\Http\Response
112
     */
113
    public function update(Request $request, Menu $menu)
114
    {
115
        $data = $request->only([
116
            'name', 'alias', 'link', 'icon', 'class', 'attr', 'privilege', 'visible'
117
        ]);
118
        $data['routes'] = $request->route_list;
119
        $data['parent_id'] = Menu::findParent();
120
121
        $menu->update($data);
122
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->to($r...e' => $request->name))) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
123
            ->to($request->redirects_to)
124
            ->withMessage(__('menu-maker::alerts.updated', ['name' => $request->name]));
125
    }
126
127
    /**
128
     * Remove the specified resource from storage.
129
     *
130
     * @param  \App\Menu  $menu
131
     * @return \Illuminate\Http\Response
132
     */
133
    public function destroy(Menu $menu)
134
    {
135
        $name = $menu->name;
136
137
        $childs = $menu->descendants()->count();
138
        if($childs > 0)
139
        {
140
            return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(... 'count' => $childs)))) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
141
                ->back()
142
                ->withErrors([__('menu-maker::alerts.menu-child-exists', ['name' => $name, 'count' => $childs])]);
143
        }
144
        $menu->delete();
145
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->to(re...rray('name' => $name))) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
146
            ->to(request('redirects_to'))
0 ignored issues
show
It seems like request('redirects_to') can also be of type array; however, parameter $path of Illuminate\Routing\Redirector::to() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

146
            ->to(/** @scrutinizer ignore-type */ request('redirects_to'))
Loading history...
147
            ->withMessage(__('menu-maker::alerts.deleted', ['name' => $name]));
148
    }
149
150
    /**
151
     * Display the specified resource.
152
     *
153
     * @param Menu $node
154
     * @return \Illuminate\Http\Response
155
     */
156
    public function tree(Menu $node)
157
    {
158
        $tree = Menu::descendantsOf($node)
159
            ->where('privilege', 'PROTECTED')
160
            ->toTree($node);
161
        $selected = [];
162
        if(request()->has('g') && request('g') > 0)
163
        {
164
            $selected = Role::findOrFail(request('g'))->menus()->descendantsOf($node)->pluck('id')->toArray();
165
        }
166
        return view('menu-maker::menus.tree', compact('tree', 'selected'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('menu-maker:...ct('tree', 'selected')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
167
    }
168
169
    public function selected() {
170
171
        if (request()->ajax()) {
172
            $group_id = request('g');
173
            $parent_id = request('p');
174
            $selected = Role::findOrFail($group_id)->menus()->descendantsOf($parent_id)->pluck('id')->toArray();
175
            return response()->json(compact('selected'), 200);
176
        }
177
        return response()->json([
178
            'responseText' => 'Not a ajax request'
179
        ], 400);
180
181
    }
182
}
183