|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\CMS\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Yajra\CMS\DataTables\ArticlesDataTable; |
|
6
|
|
|
use Yajra\CMS\Entities\Article; |
|
7
|
|
|
use Yajra\CMS\Events\Articles\ArticleWasCreated; |
|
8
|
|
|
use Yajra\CMS\Events\Articles\ArticleWasUpdated; |
|
9
|
|
|
use Yajra\CMS\Http\Requests\ArticlesFormRequest; |
|
10
|
|
|
|
|
11
|
|
|
class ArticlesController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Controller specific permission ability map. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $customPermissionMap = [ |
|
19
|
|
|
'publish' => 'update', |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* ArticlesController constructor. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->authorizePermissionResource('article'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Display list of articles. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \Yajra\CMS\DataTables\ArticlesDataTable $dataTable |
|
34
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
|
35
|
|
|
*/ |
|
36
|
|
|
public function index(ArticlesDataTable $dataTable) |
|
37
|
|
|
{ |
|
38
|
|
|
return $dataTable->render('administrator.articles.index'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Show articles form. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Yajra\CMS\Entities\Article $article |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
public function create(Article $article) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$article->published = true; |
|
50
|
|
|
$article->setHighestOrderNumber(); |
|
51
|
|
|
$tags = Article::existingTags()->pluck('name'); |
|
52
|
|
|
|
|
53
|
|
|
return view('administrator.articles.create', compact('article', 'tags')); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Store a newly created article. |
|
58
|
|
|
* |
|
59
|
|
|
* @param ArticlesFormRequest $request |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
public function store(ArticlesFormRequest $request) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$article = new Article; |
|
65
|
|
|
$article->fill($request->all()); |
|
66
|
|
|
$article->published = $request->get('published', false); |
|
67
|
|
|
$article->featured = $request->get('featured', false); |
|
68
|
|
|
$article->authenticated = $request->get('authenticated', false); |
|
69
|
|
|
$article->is_page = $request->get('is_page', false); |
|
70
|
|
|
$article->save(); |
|
71
|
|
|
|
|
72
|
|
|
$article->permissions()->sync($request->get('permissions', [])); |
|
73
|
|
|
if ($request->tags) { |
|
|
|
|
|
|
74
|
|
|
$article->tag(explode(',', $request->tags)); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
event(new ArticleWasCreated($article)); |
|
78
|
|
|
|
|
79
|
|
|
flash()->success(trans('cms::article.store.success')); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
return redirect()->route('administrator.articles.index'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Show and edit selected article. |
|
86
|
|
|
* |
|
87
|
|
|
* @param \Yajra\CMS\Entities\Article $article |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
*/ |
|
90
|
|
View Code Duplication |
public function edit(Article $article) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$tags = Article::existingTags()->pluck('name'); |
|
93
|
|
|
$selectedTags = implode(',', $article->tagNames()); |
|
94
|
|
|
|
|
95
|
|
|
return view('administrator.articles.edit', compact('article', 'tags', 'selectedTags')); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Update selected article. |
|
100
|
|
|
* |
|
101
|
|
|
* @param \Yajra\CMS\Entities\Article $article |
|
102
|
|
|
* @param ArticlesFormRequest $request |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
|
View Code Duplication |
public function update(Article $article, ArticlesFormRequest $request) |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$article->fill($request->all()); |
|
108
|
|
|
$article->published = $request->get('published', false); |
|
109
|
|
|
$article->featured = $request->get('featured', false); |
|
110
|
|
|
$article->authenticated = $request->get('authenticated', false); |
|
111
|
|
|
$article->is_page = $request->get('is_page', false); |
|
112
|
|
|
$article->save(); |
|
113
|
|
|
|
|
114
|
|
|
$article->permissions()->sync($request->get('permissions', [])); |
|
115
|
|
|
if ($request->tags) { |
|
|
|
|
|
|
116
|
|
|
$article->retag(explode(',', $request->tags)); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
event(new ArticleWasUpdated($article)); |
|
120
|
|
|
|
|
121
|
|
|
flash()->success(trans('cms::article.update.success')); |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
return redirect()->route('administrator.articles.index'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Remove selected article. |
|
128
|
|
|
* |
|
129
|
|
|
* @param \Yajra\CMS\Entities\Article $article |
|
130
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
131
|
|
|
* @throws \Exception |
|
132
|
|
|
*/ |
|
133
|
|
|
public function destroy(Article $article) |
|
134
|
|
|
{ |
|
135
|
|
|
$article->delete(); |
|
136
|
|
|
|
|
137
|
|
|
return $this->notifySuccess(trans('cms::article.destroy.success')); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Publish/Unpublish a article. |
|
142
|
|
|
* |
|
143
|
|
|
* @param \Yajra\CMS\Entities\Article $article |
|
144
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
145
|
|
|
*/ |
|
146
|
|
|
public function publish(Article $article) |
|
147
|
|
|
{ |
|
148
|
|
|
$article->togglePublishedState(); |
|
149
|
|
|
|
|
150
|
|
|
return $this->notifySuccess(trans( |
|
151
|
|
|
'cms::article.update.publish', [ |
|
152
|
|
|
'task' => $article->published ? 'published' : 'unpublished', |
|
153
|
|
|
]) |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
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.