1
|
|
|
<?php namespace jlourenco\blog\Controllers;
|
2
|
|
|
|
3
|
|
|
use Illuminate\Http\Request;
|
4
|
|
|
use App\Http\Requests;
|
5
|
|
|
use App\Http\Controllers\Controller;
|
6
|
|
|
use Blog;
|
7
|
|
|
use Sentinel;
|
8
|
|
|
use Searchy;
|
9
|
|
|
use Validator;
|
10
|
|
|
use Input;
|
11
|
|
|
use Base;
|
12
|
|
|
use Redirect;
|
13
|
|
|
use Lang;
|
14
|
|
|
|
15
|
|
|
class CategoryController extends Controller
|
16
|
|
|
{
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* Declare the rules for the form validation
|
20
|
|
|
*
|
21
|
|
|
* @var array
|
22
|
|
|
*/
|
23
|
|
|
protected $validationRules = array(
|
24
|
|
|
'name' => 'required|min:3',
|
25
|
|
|
'slug' => 'required|min:3|unique:BlogCategory,slug',
|
26
|
|
|
'description' => 'required|min:3',
|
27
|
|
|
);
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* Show list of categories.
|
31
|
|
|
*
|
32
|
|
|
* @return \Illuminate\Http\Response
|
33
|
|
|
*/
|
34
|
|
|
public function index()
|
35
|
|
|
{
|
36
|
|
|
$cats = Blog::getCategoriesRepository()->all();
|
37
|
|
|
|
38
|
|
|
return view('admin.category.list', compact('cats'));
|
39
|
|
|
}
|
40
|
|
|
|
41
|
|
|
/**
|
42
|
|
|
* Show details of a category,
|
43
|
|
|
*
|
44
|
|
|
* @param int $id
|
45
|
|
|
* @return View
|
46
|
|
|
*/
|
47
|
|
|
public function show($id)
|
48
|
|
|
{
|
49
|
|
|
$cat = Blog::getCategoriesRepository()->findOrFail($id);
|
50
|
|
|
|
51
|
|
|
// Show the page
|
52
|
|
|
return View('admin.category.show', compact('cat'));
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* Category update.
|
57
|
|
|
*
|
58
|
|
|
* @param int $id
|
59
|
|
|
* @return View
|
60
|
|
|
*/
|
61
|
|
|
public function getEdit($id = null)
|
62
|
|
|
{
|
63
|
|
|
$cat = Blog::getCategoriesRepository()->find($id);
|
64
|
|
|
|
65
|
|
|
// Get the category information
|
66
|
|
|
if($cat == null)
|
67
|
|
|
{
|
68
|
|
|
// Prepare the error message
|
69
|
|
|
$error = Lang::get('blog.category.not_found');
|
70
|
|
|
|
71
|
|
|
// Redirect to the category management page
|
72
|
|
|
return Redirect::route('categories')->with('error', $error);
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
// Show the page
|
76
|
|
|
return View('admin.category.edit', compact('cat'));
|
77
|
|
|
}
|
78
|
|
|
|
79
|
|
|
/**
|
80
|
|
|
* Category update form processing page.
|
81
|
|
|
*
|
82
|
|
|
* @param int $id
|
83
|
|
|
* @return Redirect
|
84
|
|
|
*/
|
85
|
|
|
public function postEdit($id = null)
|
86
|
|
|
{
|
87
|
|
|
// Get the category information
|
88
|
|
|
$cat = Blog::getCategoriesRepository()->find($id);
|
89
|
|
|
|
90
|
|
|
if ($cat == null)
|
91
|
|
|
{
|
92
|
|
|
// Prepare the error message
|
93
|
|
|
$error = Lang::get('blog.category.not_found');
|
94
|
|
|
|
95
|
|
|
// Redirect to the category management page
|
96
|
|
|
return Redirect::route('admin.category.show')->with('error', $error);
|
97
|
|
|
}
|
98
|
|
|
|
99
|
|
|
$slug = str_slug(Input::get('name'), '_');
|
100
|
|
|
|
101
|
|
|
unset($this->validationRules['slug']);
|
102
|
|
|
$this->validationRules['slug'] = "required|min:3|unique:BlogCategory,slug,{$cat->slug},slug";
|
103
|
|
|
|
104
|
|
|
$input = Input::all();
|
105
|
|
|
$input['slug'] = $slug;
|
106
|
|
|
|
107
|
|
|
// Create a new validator instance from our validation rules
|
108
|
|
|
$validator = Validator::make($input, $this->validationRules);
|
109
|
|
|
|
110
|
|
|
// If validation fails, we'll exit the operation now.
|
111
|
|
|
if ($validator->fails()) {
|
112
|
|
|
// Ooops.. something went wrong
|
113
|
|
|
return Redirect::back()->withInput()->withErrors($validator);
|
|
|
|
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
// Update the category
|
117
|
|
|
$cat->name = Input::get('name');
|
118
|
|
|
$cat->slug = $slug;
|
119
|
|
|
$cat->description = Input::get('description');
|
120
|
|
|
|
121
|
|
|
// Was the category updated?
|
122
|
|
|
if ($cat->save())
|
123
|
|
|
{
|
124
|
|
|
Base::Log('Category (' . $cat->name . ') was edited.');
|
125
|
|
|
|
126
|
|
|
// Prepare the success message
|
127
|
|
|
$success = Lang::get('blog.category.changed');
|
128
|
|
|
|
129
|
|
|
// Redirect to the user page
|
130
|
|
|
return Redirect::route('category.update', $id)->with('success', $success);
|
|
|
|
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
$error = Lang::get('blog.category.error');
|
134
|
|
|
|
135
|
|
|
// Redirect to the category page
|
136
|
|
|
return Redirect::route('category.update', $id)->withInput()->with('error', $error);
|
|
|
|
|
137
|
|
|
}
|
138
|
|
|
|
139
|
|
|
/**
|
140
|
|
|
* Create new category
|
141
|
|
|
*
|
142
|
|
|
* @return View
|
143
|
|
|
*/
|
144
|
|
|
public function getCreate()
|
145
|
|
|
{
|
146
|
|
|
// Show the page
|
147
|
|
|
return View('admin.category.create');
|
148
|
|
|
}
|
149
|
|
|
|
150
|
|
|
/**
|
151
|
|
|
* Category create form processing.
|
152
|
|
|
*
|
153
|
|
|
* @return Redirect
|
154
|
|
|
*/
|
155
|
|
|
public function postCreate()
|
156
|
|
|
{
|
157
|
|
|
$slug = str_slug(Input::get('name'), '_');
|
158
|
|
|
|
159
|
|
|
$input = Input::all();
|
160
|
|
|
$input['slug'] = $slug;
|
161
|
|
|
|
162
|
|
|
// Create a new validator instance from our validation rules
|
163
|
|
|
$validator = Validator::make($input, $this->validationRules);
|
164
|
|
|
|
165
|
|
|
// If validation fails, we'll exit the operation now.
|
166
|
|
|
if ($validator->fails()) {
|
167
|
|
|
// Ooops.. something went wrong
|
168
|
|
|
return Redirect::back()->withInput()->withErrors($validator);
|
169
|
|
|
}
|
170
|
|
|
|
171
|
|
|
$cat = Blog::getCategoriesRepository()->findBySlug($slug);
|
172
|
|
|
|
173
|
|
|
if ($cat != null)
|
174
|
|
|
return Redirect::route("categories")->with('error', Lang::get('blog.category.already_exists'));
|
175
|
|
|
|
176
|
|
|
$cat = Blog::getCategoriesRepository()->create([
|
177
|
|
|
'name' => Input::get('name'),
|
178
|
|
|
'slug' => $slug,
|
179
|
|
|
'description' => Input::get('name'),
|
180
|
|
|
]);
|
181
|
|
|
|
182
|
|
|
$cat->save();
|
183
|
|
|
|
184
|
|
|
Base::Log('A new category (' . $cat->name . ') was created.');
|
185
|
|
|
|
186
|
|
|
// Redirect to the home page with success menu
|
187
|
|
|
return Redirect::route("categories")->with('success', Lang::get('blog.category.created'));
|
188
|
|
|
}
|
189
|
|
|
|
190
|
|
|
/**
|
191
|
|
|
* Delete Confirm
|
192
|
|
|
*
|
193
|
|
|
* @param int $id
|
194
|
|
|
* @return View
|
195
|
|
|
*/
|
196
|
|
View Code Duplication |
public function getModalDelete($id = null)
|
|
|
|
|
197
|
|
|
{
|
198
|
|
|
$confirm_route = $error = null;
|
199
|
|
|
|
200
|
|
|
$title = 'Delete category';
|
201
|
|
|
$message = 'Are you sure to delete this category?';
|
202
|
|
|
|
203
|
|
|
// Get category information
|
204
|
|
|
$cat = Blog::getCategoriesRepository()->findOrFail($id);
|
205
|
|
|
|
206
|
|
|
if ($cat == null)
|
207
|
|
|
{
|
208
|
|
|
// Prepare the error message
|
209
|
|
|
$error = Lang::get('blog.category.not_found');
|
210
|
|
|
return View('layouts.modal_confirmation', compact('title', 'message', 'error', 'model', 'confirm_route'));
|
211
|
|
|
}
|
212
|
|
|
|
213
|
|
|
$confirm_route = route('delete/category', ['id' => $cat->id]);
|
214
|
|
|
return View('layouts.modal_confirmation', compact('title', 'message', 'error', 'model', 'confirm_route'));
|
215
|
|
|
}
|
216
|
|
|
|
217
|
|
|
/**
|
218
|
|
|
* Delete the given category.
|
219
|
|
|
*
|
220
|
|
|
* @param int $id
|
221
|
|
|
* @return Redirect
|
222
|
|
|
*/
|
223
|
|
View Code Duplication |
public function getDelete($id = null)
|
|
|
|
|
224
|
|
|
{
|
225
|
|
|
// Get category information
|
226
|
|
|
$cat = Blog::getCategoriesRepository()->find($id);
|
227
|
|
|
|
228
|
|
|
if ($cat == null)
|
229
|
|
|
{
|
230
|
|
|
// Prepare the error message
|
231
|
|
|
$error = Lang::get('blog.category.not_found');
|
232
|
|
|
|
233
|
|
|
// Redirect to the category management page
|
234
|
|
|
return Redirect::route('categories')->with('error', $error);
|
235
|
|
|
}
|
236
|
|
|
|
237
|
|
|
Base::Log('Category (' . $cat->name . ') was deleted.');
|
238
|
|
|
|
239
|
|
|
// Delete the category
|
240
|
|
|
$cat->delete();
|
241
|
|
|
|
242
|
|
|
// Prepare the success message
|
243
|
|
|
$success = Lang::get('blog.category.deleted');
|
244
|
|
|
|
245
|
|
|
// Redirect to the category management page
|
246
|
|
|
return Redirect::route('categories')->with('success', $success);
|
247
|
|
|
}
|
248
|
|
|
|
249
|
|
|
/**
|
250
|
|
|
* Show a list of all the deleted categories.
|
251
|
|
|
*
|
252
|
|
|
* @return View
|
253
|
|
|
*/
|
254
|
|
|
public function getDeletedCategories()
|
255
|
|
|
{
|
256
|
|
|
// Grab deleted categories
|
257
|
|
|
$cats = Blog::getCategoriesRepository()->onlyTrashed()->get();
|
258
|
|
|
|
259
|
|
|
// Show the page
|
260
|
|
|
return View('admin.category.deleted', compact('cats'));
|
261
|
|
|
}
|
262
|
|
|
|
263
|
|
|
/**
|
264
|
|
|
* Restore a deleted category.
|
265
|
|
|
*
|
266
|
|
|
* @param int $id
|
267
|
|
|
* @return Redirect
|
268
|
|
|
*/
|
269
|
|
View Code Duplication |
public function getRestore($id = null)
|
|
|
|
|
270
|
|
|
{
|
271
|
|
|
// Get category information
|
272
|
|
|
$cat = Blog::getCategoriesRepository()->withTrashed()->find($id);
|
273
|
|
|
|
274
|
|
|
if ($cat == null)
|
275
|
|
|
{
|
276
|
|
|
// Prepare the error message
|
277
|
|
|
$error = Lang::get('blog.category.not_found');
|
278
|
|
|
|
279
|
|
|
// Redirect to the category management page
|
280
|
|
|
return Redirect::route('category.deleted')->with('error', $error);
|
281
|
|
|
}
|
282
|
|
|
|
283
|
|
|
Base::Log('Category (' . $cat->name . ') was restored.');
|
284
|
|
|
|
285
|
|
|
// Restore the category
|
286
|
|
|
$cat->restore();
|
287
|
|
|
|
288
|
|
|
// Prepare the success message
|
289
|
|
|
$success = Lang::get('blog.category.restored');
|
290
|
|
|
|
291
|
|
|
// Redirect to the category management page
|
292
|
|
|
return Redirect::route('categories.deleted')->with('success', $success);
|
293
|
|
|
}
|
294
|
|
|
|
295
|
|
|
}
|
296
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.