1
|
|
|
<?php namespace Arcanesoft\Blog\Http\Controllers\Admin; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Blog\Http\Requests\Admin\Categories\CreateCategoryRequest; |
4
|
|
|
use Arcanesoft\Blog\Http\Requests\Admin\Categories\UpdateCategoryRequest; |
5
|
|
|
use Arcanesoft\Blog\Models\Category; |
6
|
|
|
use Illuminate\Support\Facades\Log; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class CategoriesController |
10
|
|
|
* |
11
|
|
|
* @package Arcanesoft\Blog\Http\Controllers\Admin |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class CategoriesController extends Controller |
15
|
|
|
{ |
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
17
|
|
|
| Properties |
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
19
|
|
|
*/ |
20
|
|
|
/** |
21
|
|
|
* The category model. |
22
|
|
|
* |
23
|
|
|
* @var \Arcanesoft\Blog\Models\Category |
24
|
|
|
*/ |
25
|
|
|
private $category; |
26
|
|
|
|
27
|
|
|
/* ------------------------------------------------------------------------------------------------ |
28
|
|
|
| Constructor |
29
|
|
|
| ------------------------------------------------------------------------------------------------ |
30
|
|
|
*/ |
31
|
|
|
/** |
32
|
|
|
* Instantiate the controller. |
33
|
|
|
* |
34
|
|
|
* @param \Arcanesoft\Blog\Models\Category $category |
35
|
|
|
*/ |
36
|
|
|
public function __construct(Category $category) |
37
|
|
|
{ |
38
|
|
|
parent::__construct(); |
39
|
|
|
|
40
|
|
|
$this->category = $category; |
41
|
|
|
|
42
|
|
|
$this->setCurrentPage('blog-categories'); |
43
|
|
|
$this->addBreadcrumbRoute('Categories', 'admin::blog.categories.index'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/* ------------------------------------------------------------------------------------------------ |
47
|
|
|
| Main Functions |
48
|
|
|
| ------------------------------------------------------------------------------------------------ |
49
|
|
|
*/ |
50
|
|
|
public function index($trashed = false) |
51
|
|
|
{ |
52
|
|
|
$this->authorize('blog.categories.list'); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$categories = $this->category->with(['posts']); |
55
|
|
|
$categories = $trashed |
56
|
|
|
? $categories->onlyTrashed()->paginate(30) |
57
|
|
|
: $categories->paginate(30); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->setTitle($title = 'Blog - Categories'); |
60
|
|
|
$this->addBreadcrumb('List all categories'); |
61
|
|
|
|
62
|
|
|
return $this->view('admin.categories.list', compact('categories', 'trashed')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function trash() |
66
|
|
|
{ |
67
|
|
|
return $this->index(true); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function create() |
71
|
|
|
{ |
72
|
|
|
$this->authorize('blog.categories.create'); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->setTitle('Blog - Categories'); |
75
|
|
|
$this->addBreadcrumb('Create category'); |
76
|
|
|
|
77
|
|
|
return $this->view('admin.categories.create'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function store(CreateCategoryRequest $request, Category $category) |
81
|
|
|
{ |
82
|
|
|
$this->authorize('blog.categories.create'); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$category->fill($request->only(['name'])); |
85
|
|
|
$category->save(); |
86
|
|
|
|
87
|
|
|
$message = "The category {$category->name} was created successfully !"; |
88
|
|
|
Log::info($message, $category->toArray()); |
89
|
|
|
$this->notifySuccess($message, 'Category created !'); |
90
|
|
|
|
91
|
|
|
return redirect()->route('admin::blog.categories.index'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function show(Category $category) |
95
|
|
|
{ |
96
|
|
|
$this->authorize('blog.categories.show'); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$category->load(['posts']); |
99
|
|
|
|
100
|
|
|
$this->setTitle('Blog - Categories'); |
101
|
|
|
$this->addBreadcrumb("Category - {$category->name}"); |
102
|
|
|
|
103
|
|
|
return $this->view('admin.categories.show', compact('category')); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function edit(Category $category) |
107
|
|
|
{ |
108
|
|
|
$this->authorize('blog.categories.update'); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$this->setTitle('Blog - Categories'); |
111
|
|
|
$this->addBreadcrumb('Update category'); |
112
|
|
|
|
113
|
|
|
return $this->view('admin.categories.edit', compact('category')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function update(UpdateCategoryRequest $request, Category $category) |
117
|
|
|
{ |
118
|
|
|
$this->authorize('blog.categories.update'); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$category->update($request->only(['name'])); |
121
|
|
|
|
122
|
|
|
$message = "The category {$category->name} was updated successfully !"; |
123
|
|
|
Log::info($message, $category->toArray()); |
124
|
|
|
$this->notifySuccess($message, 'Category updated !'); |
125
|
|
|
|
126
|
|
|
return redirect()->route('admin::blog.categories.show', [$category]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function restore(Category $category) |
130
|
|
|
{ |
131
|
|
|
self::onlyAjax(); |
132
|
|
|
$this->authorize('blog.categories.update'); |
|
|
|
|
133
|
|
|
|
134
|
|
|
try { |
135
|
|
|
$category->restore(); |
136
|
|
|
|
137
|
|
|
$message = "The category {$category->name} has been successfully restored !"; |
138
|
|
|
Log::info($message, $category->toArray()); |
139
|
|
|
$this->notifySuccess($message, 'Category restored !'); |
140
|
|
|
|
141
|
|
|
return response()->json([ |
142
|
|
|
'status' => 'success', |
143
|
|
|
'message' => $message, |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
catch (\Exception $e) { |
147
|
|
|
return response()->json([ |
148
|
|
|
'status' => 'error', |
149
|
|
|
'message' => $e->getMessage(), |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function delete(Category $category) |
155
|
|
|
{ |
156
|
|
|
self::onlyAjax(); |
157
|
|
|
$this->authorize('blog.categories.delete'); |
|
|
|
|
158
|
|
|
|
159
|
|
|
try { |
160
|
|
|
$category->trashed() |
161
|
|
|
? $category->forceDelete() |
162
|
|
|
: $category->delete(); |
163
|
|
|
|
164
|
|
|
$message = "The category {$category->name} has been successfully deleted !"; |
165
|
|
|
Log::info($message, $category->toArray()); |
166
|
|
|
$this->notifySuccess($message, 'Category deleted !'); |
167
|
|
|
|
168
|
|
|
return response()->json([ |
169
|
|
|
'status' => 'success', |
170
|
|
|
'message' => $message, |
171
|
|
|
]); |
172
|
|
|
} |
173
|
|
|
catch(\Exception $e) { |
174
|
|
|
return response()->json([ |
175
|
|
|
'status' => 'error', |
176
|
|
|
'message' => $e->getMessage(), |
177
|
|
|
]); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: