|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\CMS\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Yajra\CMS\DataTables\CategoriesDataTable; |
|
6
|
|
|
use Yajra\CMS\Entities\Category; |
|
7
|
|
|
use Yajra\CMS\Http\Requests\CategoriesFormRequest; |
|
8
|
|
|
|
|
9
|
|
|
class CategoriesController extends Controller |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Controller specific permission ability map. |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $customPermissionMap = [ |
|
17
|
|
|
'publish' => 'update', |
|
18
|
|
|
]; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* CategoriesController constructor. |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->authorizePermissionResource('category'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Display a listing of the resource. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \Yajra\CMS\DataTables\CategoriesDataTable $dataTable |
|
32
|
|
|
* @return \Illuminate\Http\Response |
|
33
|
|
|
*/ |
|
34
|
|
|
public function index(CategoriesDataTable $dataTable) |
|
35
|
|
|
{ |
|
36
|
|
|
return $dataTable->render('administrator.categories.index'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Show the form for creating a new resource. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Yajra\CMS\Entities\Category $category |
|
43
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
44
|
|
|
*/ |
|
45
|
|
|
public function create(Category $category) |
|
46
|
|
|
{ |
|
47
|
|
|
$category->published = true; |
|
48
|
|
|
|
|
49
|
|
|
return view('administrator.categories.create', compact('category')); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Store a newly created resource in storage. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \Yajra\CMS\Http\Requests\CategoriesFormRequest $request |
|
56
|
|
|
* @param \Yajra\CMS\Entities\Category $category |
|
57
|
|
|
* @return \Illuminate\Http\Response |
|
58
|
|
|
*/ |
|
59
|
|
View Code Duplication |
public function store(CategoriesFormRequest $request, Category $category) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
$category->fill($request->all()); |
|
62
|
|
|
$category->published = $request->get('published', false); |
|
63
|
|
|
$category->authenticated = $request->get('authenticated', false); |
|
64
|
|
|
$category->save(); |
|
65
|
|
|
flash()->success(trans('cms::categories.alert.success', ['stat' => 'Created'])); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
return redirect()->route('administrator.categories.index'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Show the form for editing the specified resource. |
|
72
|
|
|
* |
|
73
|
|
|
* @param \Yajra\CMS\Entities\Category $category |
|
74
|
|
|
* @return \Illuminate\Http\Response |
|
75
|
|
|
*/ |
|
76
|
|
|
public function edit(Category $category) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($category->isRoot()) { |
|
79
|
|
|
abort(404); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return view('administrator.categories.edit', compact('category')); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Update the specified resource in storage. |
|
87
|
|
|
* |
|
88
|
|
|
* @param CategoriesFormRequest $request |
|
89
|
|
|
* @param \Yajra\CMS\Entities\Category $category |
|
90
|
|
|
* @return \Illuminate\Http\Response |
|
91
|
|
|
*/ |
|
92
|
|
View Code Duplication |
public function update(CategoriesFormRequest $request, Category $category) |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
$category->fill($request->all()); |
|
95
|
|
|
$category->published = $request->get('published', false); |
|
96
|
|
|
$category->authenticated = $request->get('authenticated', false); |
|
97
|
|
|
$category->save(); |
|
98
|
|
|
flash()->success(trans('cms::categories.alert.success', ['stat' => 'Updated'])); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
return redirect()->route('administrator.categories.index'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Remove the specified resource from storage. |
|
105
|
|
|
* |
|
106
|
|
|
* @param \Yajra\CMS\Entities\Category $category |
|
107
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
108
|
|
|
*/ |
|
109
|
|
|
public function destroy(Category $category) |
|
110
|
|
|
{ |
|
111
|
|
|
if ($category->isRoot()) { |
|
112
|
|
|
abort(404); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ($category->has('articles')) { |
|
116
|
|
|
return $this->notifyError(trans('cms::categories.destroy.error')); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$category->delete(); |
|
120
|
|
|
|
|
121
|
|
|
return $this->notifySuccess(trans('cms::categories.destroy.success')); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Button Response from DataTable request to publish status. |
|
126
|
|
|
* |
|
127
|
|
|
* @param \Yajra\CMS\Entities\Category $category |
|
128
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
129
|
|
|
*/ |
|
130
|
|
|
public function publish(Category $category) |
|
131
|
|
|
{ |
|
132
|
|
|
$category->togglePublishedState(); |
|
133
|
|
|
|
|
134
|
|
|
$category->getDescendants()->each( |
|
135
|
|
|
function (Category $cat) use ($category) { |
|
136
|
|
|
$cat->published = $category->published; |
|
137
|
|
|
$cat->save(); |
|
138
|
|
|
} |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
return $this->notifySuccess( |
|
142
|
|
|
sprintf( |
|
143
|
|
|
'Category successfully %s!', |
|
144
|
|
|
$category->published ? 'published' : 'unpublished' |
|
145
|
|
|
) |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
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.