|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Mark |
|
5
|
|
|
* Date: 05/03/2016 |
|
6
|
|
|
* Time: 20:30. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Plugins\Articles; |
|
10
|
|
|
|
|
11
|
|
|
use Carbon\Carbon; |
|
12
|
|
|
use App\Model\Article; |
|
13
|
|
|
use App\Model\Activity; |
|
14
|
|
|
use Illuminate\Http\Request; |
|
15
|
|
|
use App\Plugins\PluginEngine; |
|
16
|
|
|
use App\Model\ArticleCategory; |
|
17
|
|
|
use Illuminate\Validation\Rule; |
|
18
|
|
|
use App\Classes\Repositories\ArticleRepository; |
|
19
|
|
|
use App\Classes\Repositories\ArticleCategoryRepository; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Controller. |
|
23
|
|
|
*/ |
|
24
|
|
|
class BackendController extends PluginEngine |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ArticleRepository |
|
28
|
|
|
*/ |
|
29
|
|
|
private $articles; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ArticleCategoryRepository |
|
33
|
|
|
*/ |
|
34
|
|
|
private $categories; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* BackendController constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param ArticleRepository $repository |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(ArticleRepository $repository, ArticleCategoryRepository $categories) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->articles = $repository; |
|
44
|
|
|
|
|
45
|
|
|
$this->categories = $categories; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Display a listing of the resource. |
|
50
|
|
|
* |
|
51
|
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Http\Response |
|
52
|
|
|
*/ |
|
53
|
|
|
public function index() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->make('index')->with('articles', $this->articles->all()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Show the form for creating a new resource. |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Http\Response |
|
62
|
|
|
*/ |
|
63
|
|
|
public function create() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->form(new Article); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Generate a form for editing or creating a model. |
|
70
|
|
|
* |
|
71
|
|
|
* @param Article $article model to be used. |
|
72
|
|
|
* @return void |
|
|
|
|
|
|
73
|
|
|
*/ |
|
74
|
|
|
public function form(Article $article) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->make('form')->with('categories', $this->categories->all())->with('article', $article); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Store a newly created resource in storage. |
|
81
|
|
|
* |
|
82
|
|
|
* @param Article $article |
|
83
|
|
|
* @param \Illuminate\Http\Request $request |
|
84
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
|
|
85
|
|
|
*/ |
|
86
|
|
View Code Duplication |
public function store(Article $article, Request $request) |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
// use the global save function. |
|
89
|
|
|
$this->save($request, $article); |
|
90
|
|
|
|
|
91
|
|
|
account()->record(Activity::$created, $article); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
// redirect back to articles index. |
|
94
|
|
|
return redirect()->route('admin.articles.index'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Display the specified resource. |
|
99
|
|
|
* |
|
100
|
|
|
* @param int $id |
|
101
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
|
|
102
|
|
|
*/ |
|
103
|
|
|
public function show($id) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
// |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Show the form for editing the specified resource. |
|
110
|
|
|
* |
|
111
|
|
|
* @param $slug |
|
112
|
|
|
* @return \Illuminate\Contracts\View\View |
|
113
|
|
|
*/ |
|
114
|
|
|
public function edit($slug) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->form($this->articles->whereSlug($slug)); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Update the specified resource in storage. |
|
121
|
|
|
* |
|
122
|
|
|
* @param \Illuminate\Http\Request $request |
|
123
|
|
|
* @param $slug |
|
124
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
|
|
125
|
|
|
* @internal param int $id |
|
126
|
|
|
*/ |
|
127
|
|
View Code Duplication |
public function update(Request $request, $slug) |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
$article = $this->articles->whereSlug($slug); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
$this->save($request, $article); |
|
132
|
|
|
|
|
133
|
|
|
account()->record(Activity::$updated, $article); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
return redirect()->route('admin.articles.index'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Remove the specified resource from storage. |
|
140
|
|
|
* |
|
141
|
|
|
* @param int $id |
|
|
|
|
|
|
142
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
|
|
143
|
|
|
*/ |
|
144
|
|
View Code Duplication |
public function destroy($slug) |
|
|
|
|
|
|
145
|
|
|
{ |
|
146
|
|
|
$article = $this->articles->whereSlug($slug); |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
$article->delete(); |
|
149
|
|
|
|
|
150
|
|
|
account()->record(Activity::$deleted, $article); |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
return redirect()->route('admin.articles.index'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Category Index. |
|
157
|
|
|
* @param ArticleCategoryRepository $categoryRepository |
|
158
|
|
|
* @return \Illuminate\Contracts\View\View |
|
159
|
|
|
*/ |
|
160
|
|
|
public function categories(ArticleCategoryRepository $categoryRepository) |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->make('categories')->with('categories', $categoryRepository->all()); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param Request $request |
|
167
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
168
|
|
|
*/ |
|
169
|
|
View Code Duplication |
public function categories_store(Request $request) |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
$this->validate($request, ['unique:title']); |
|
172
|
|
|
|
|
173
|
|
|
$category = new ArticleCategory; |
|
174
|
|
|
$category->title = $request['name']; |
|
175
|
|
|
$category->auditSave(); |
|
176
|
|
|
|
|
177
|
|
|
account()->record(Activity::$created, $category); |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
return redirect()->route('admin.articles.categories.index'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param int $id |
|
184
|
|
|
* @param ArticleCategoryRepository $categoryRepository |
|
185
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
186
|
|
|
*/ |
|
187
|
|
|
public function categories_destroy(int $id, ArticleCategoryRepository $categoryRepository) |
|
|
|
|
|
|
188
|
|
|
{ |
|
189
|
|
|
$category = $categoryRepository->whereID($id); |
|
190
|
|
|
|
|
191
|
|
|
account()->record(Activity::$deleted, $category); |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
$category->delete(); |
|
194
|
|
|
|
|
195
|
|
|
return redirect()->route('admin.articles.categories.index'); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Save the data for the menu to the database. |
|
200
|
|
|
* |
|
201
|
|
|
* @param Request $request |
|
202
|
|
|
* @param Article $article |
|
203
|
|
|
* @return bool |
|
204
|
|
|
* @internal param Article $menu |
|
205
|
|
|
*/ |
|
206
|
|
|
public function save(Request $request, Article $article) |
|
207
|
|
|
{ |
|
208
|
|
|
$this->validate($request, [ |
|
209
|
|
|
'title' => ['min:3|max:255', Rule::unique('articles')->ignore($article->id)], 'content' => ['min:3'], |
|
210
|
|
|
'publish_date' => 'required|date', |
|
211
|
|
|
]); |
|
212
|
|
|
|
|
213
|
|
|
if ($request['unpublish_date']) { |
|
214
|
|
|
$this->validate($request, ['unpublish_date' => 'sometimes|date|after:publish_date']); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
// set attribute for the model. |
|
218
|
|
|
$article->setAttribute('title', $request['title']); |
|
219
|
|
|
$article->setAttribute('content', $request['content']); |
|
220
|
|
|
$article->setAttribute('category_id', $request['category']); |
|
221
|
|
|
$article->setAttribute('status', $request['status']); |
|
222
|
|
|
$article->setAttribute('featured_img', $request['image']); |
|
223
|
|
|
|
|
224
|
|
|
// 09/05/2018 $publish_date = |
|
|
|
|
|
|
225
|
|
|
$publish_date = $request['publish_date'] ? Carbon::createFromFormat('m/d/Y', $request['publish_date']) : null; |
|
|
|
|
|
|
226
|
|
|
$unpublish_date = $request['unpublish_date'] ? Carbon::createFromFormat('m/d/Y', $request['unpublish_date']) : null; |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
// Store the carbon dates to the database. |
|
229
|
|
|
$article->setAttribute('publish_date', $publish_date); |
|
230
|
|
|
$article->setAttribute('unpublish_date', $unpublish_date); |
|
231
|
|
|
|
|
232
|
|
|
// save the article as an audit. |
|
233
|
|
|
return $article->auditSave(); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.