Completed
Push — master ( 16ebaf...a3d07b )
by Arjay
14:09
created

WidgetsController::templates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 9
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 14
rs 9.4285
1
<?php
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use Yajra\CMS\DataTables\WidgetsDataTable;
6
use Yajra\CMS\Entities\Widget;
7
use Yajra\CMS\Http\Requests\WidgetFormRequest;
8
use Yajra\CMS\Widgets\Repository;
9
10
class WidgetsController extends Controller
11
{
12
    /**
13
     * Controller specific permission ability map.
14
     *
15
     * @var array
16
     */
17
    protected $customPermissionMap = [
18
        'publish' => 'update',
19
    ];
20
21
    /**
22
     * @var \Yajra\CMS\Widgets\Repository
23
     */
24
    protected $repository;
25
26
    /**
27
     * WidgetsController constructor.
28
     *
29
     * @param \Yajra\CMS\Widgets\Repository $repository
30
     */
31
    public function __construct(Repository $repository)
32
    {
33
        $this->authorizePermissionResource('widget');
34
        $this->repository = $repository;
35
    }
36
37
    /**
38
     * Display list of widgets.
39
     *
40
     * @param \Yajra\CMS\DataTables\WidgetsDataTable $dataTable
41
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
42
     */
43
    public function index(WidgetsDataTable $dataTable)
44
    {
45
        return $dataTable->render('administrator.widgets.index');
46
    }
47
48
    /**
49
     * Show widget form.
50
     *
51
     * @param \Yajra\CMS\Entities\Widget $widget
52
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
53
     */
54
    public function create(Widget $widget)
55
    {
56
        $widget->order    = 1;
57
        $widget->type     = old('type', 'wysiwyg');
58
        $widget->template = old('template', 'widgets.wysiwyg.default');
59
60
        return view('administrator.widgets.create', compact('widget'));
61
    }
62
63
    /**
64
     * Store a newly created widget.
65
     *
66
     * @param \Yajra\CMS\Http\Requests\WidgetFormRequest $request
67
     * @return \Illuminate\Http\RedirectResponse
68
     */
69 View Code Duplication
    public function store(WidgetFormRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $widget = new Widget;
72
        $widget->fill($request->all());
73
        $widget->published     = $request->get('published', false);
74
        $widget->authenticated = $request->get('authenticated', false);
75
        $widget->show_title    = $request->get('show_title', false);
76
        $widget->save();
77
78
        $widget->syncPermissions($request->get('permissions', []));
79
        $widget->syncMenuAssignment($request->get('menu', []), $request->get('assignment', Widget::ALL_PAGES));
80
81
        flash()->success(trans('cms::widget.store.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::widget.store.success') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Laracasts\Flash\FlashNotifier::success() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
82
83
        return redirect()->route('administrator.widgets.index');
84
    }
85
86
    /**
87
     * Show and edit selected widget.
88
     *
89
     * @param \Yajra\CMS\Entities\Widget $widget
90
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
91
     */
92
    public function edit(Widget $widget)
93
    {
94
        $widget->type     = old('type', $widget->type);
95
        $widget->template = old('template', $widget->template);
96
97
        return view('administrator.widgets.edit', compact('widget'));
98
    }
99
100
    /**
101
     * Update selected widget.
102
     *
103
     * @param \Yajra\CMS\Entities\Widget $widget
104
     * @param \Yajra\CMS\Http\Requests\WidgetFormRequest $request
105
     * @return \Illuminate\Http\RedirectResponse
106
     */
107 View Code Duplication
    public function update(Widget $widget, WidgetFormRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $widget->fill($request->all());
110
        $widget->published     = $request->get('published', false);
111
        $widget->authenticated = $request->get('authenticated', false);
112
        $widget->show_title    = $request->get('show_title', false);
113
        $widget->save();
114
115
        $widget->syncPermissions($request->get('permissions', []));
116
        $widget->syncMenuAssignment($request->get('menu', []), $request->get('assignment', Widget::ALL_PAGES));
117
118
        flash()->success(trans('cms::widget.update.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::widget.update.success') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Laracasts\Flash\FlashNotifier::success() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
119
120
        return redirect()->route('administrator.widgets.index');
121
    }
122
123
    /**
124
     * Remove selected widget.
125
     *
126
     * @param \Yajra\CMS\Entities\Widget $widget
127
     * @return \Illuminate\Http\JsonResponse
128
     * @throws \Exception
129
     */
130
    public function destroy(Widget $widget)
131
    {
132
        $widget->delete();
133
134
        return $this->notifySuccess(trans('cms::widget.destroy.success'));
135
    }
136
137
    /**
138
     * Publish/Unpublish a widget.
139
     *
140
     * @param \Yajra\CMS\Entities\Widget $widget
141
     * @return \Illuminate\Http\JsonResponse
142
     */
143
    public function publish(Widget $widget)
144
    {
145
        $widget->published = ! $widget->published;
146
        $widget->save();
147
148
        return $this->notifySuccess(trans('cms::widget.update.publish', [
149
                'task' => $widget->published ? 'published' : 'unpublished',
150
            ])
151
        );
152
    }
153
154
    /**
155
     * Get all widget types.
156
     *
157
     * @param string $type
158
     * @return string
159
     */
160
    public function templates($type)
161
    {
162
        $data = [];
163
        foreach ($this->repository->all()->where('name', $type) as $widget) {
164
            foreach ($widget->templates as $key => $value) {
165
                $data[] = ['key' => $key, 'value' => $value];
166
            }
167
        }
168
169
        return response()->json([
170
            'selected' => $type,
171
            'data'     => $data,
172
        ], 200);
173
    }
174
}
175