1
|
|
|
<?php |
2
|
|
|
namespace App\Controller\Admin; |
3
|
|
|
|
4
|
|
|
use App\Controller\AppController; |
5
|
|
|
use App\Event\Statistics; |
6
|
|
|
use Cake\Event\Event; |
7
|
|
|
use Cake\I18n\I18n; |
8
|
|
|
|
9
|
|
|
class ArticlesController extends AppController |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Helpers. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
public $helpers = ['I18n']; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Display all articles. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
public function index() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$this->loadModel('BlogArticles'); |
26
|
|
|
|
27
|
|
|
$this->paginate = [ |
28
|
|
|
'maxLimit' => 15 |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
$articles = $this->BlogArticles |
|
|
|
|
32
|
|
|
->find() |
33
|
|
|
->contain([ |
34
|
|
|
'BlogCategories' => function ($q) { |
35
|
|
|
return $q |
36
|
|
|
->select([ |
37
|
|
|
'id', |
38
|
|
|
'title' |
39
|
|
|
]); |
40
|
|
|
}, |
41
|
|
|
'Users' => function ($q) { |
42
|
|
|
return $q->find('short'); |
43
|
|
|
} |
44
|
|
|
]) |
45
|
|
|
->order([ |
46
|
|
|
'BlogArticles.created' => 'desc' |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
$articles = $this->paginate($articles); |
50
|
|
|
$this->set(compact('articles')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add an article. |
55
|
|
|
* |
56
|
|
|
* @return \Cake\Network\Response|void |
57
|
|
|
*/ |
58
|
|
|
public function add() |
59
|
|
|
{ |
60
|
|
|
$this->loadModel('BlogArticles'); |
61
|
|
|
|
62
|
|
|
$this->BlogArticles->locale(I18n::defaultLocale()); |
|
|
|
|
63
|
|
|
$article = $this->BlogArticles->newEntity($this->request->getParsedBody()); |
|
|
|
|
64
|
|
|
|
65
|
|
View Code Duplication |
if ($this->request->is('post')) { |
|
|
|
|
66
|
|
|
$article->user_id = $this->Auth->user('id'); |
67
|
|
|
$article->setTranslations($this->request->getParsedBody()); |
68
|
|
|
|
69
|
|
|
if ($this->BlogArticles->save($article)) { |
|
|
|
|
70
|
|
|
$this->eventManager()->attach(new Statistics()); |
|
|
|
|
71
|
|
|
$event = new Event('Model.BlogArticles.new'); |
72
|
|
|
$this->eventManager()->dispatch($event); |
73
|
|
|
|
74
|
|
|
$this->Flash->success(__d('admin', 'Your article has been created successfully !')); |
75
|
|
|
|
76
|
|
|
return $this->redirect(['action' => 'index']); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$categories = $this->BlogArticles->BlogCategories->find('list'); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$this->set(compact('article', 'categories')); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Edit an Article. |
87
|
|
|
* |
88
|
|
|
* @return \Cake\Network\Response|void |
89
|
|
|
*/ |
90
|
|
|
public function edit() |
91
|
|
|
{ |
92
|
|
|
$this->loadModel('BlogArticles'); |
93
|
|
|
|
94
|
|
|
$this->BlogArticles->locale(I18n::defaultLocale()); |
|
|
|
|
95
|
|
|
$article = $this->BlogArticles |
|
|
|
|
96
|
|
|
->find('translations') |
97
|
|
|
->where([ |
98
|
|
|
'BlogArticles.id' => $this->request->id |
99
|
|
|
]) |
100
|
|
|
->contain([ |
101
|
|
|
'BlogAttachments', |
102
|
|
|
'BlogCategories', |
103
|
|
|
'Users' => function ($q) { |
104
|
|
|
return $q->find('short'); |
105
|
|
|
} |
106
|
|
|
]) |
107
|
|
|
->first(); |
108
|
|
|
|
109
|
|
|
//Check if the article is found. |
110
|
|
|
if (empty($article)) { |
111
|
|
|
$this->Flash->error(__d('admin', 'This article doesn\'t exist or has been deleted.')); |
112
|
|
|
|
113
|
|
|
return $this->redirect(['action' => 'index']); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
if ($this->request->is('put')) { |
|
|
|
|
117
|
|
|
$this->BlogArticles->patchEntity($article, $this->request->getParsedBody()); |
|
|
|
|
118
|
|
|
$article->setTranslations($this->request->getParsedBody()); |
119
|
|
|
|
120
|
|
|
if ($this->BlogArticles->save($article)) { |
|
|
|
|
121
|
|
|
$this->Flash->success(__d('admin', 'This article has been updated successfully !')); |
122
|
|
|
|
123
|
|
|
return $this->redirect(['action' => 'index']); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$categories = $this->BlogArticles->BlogCategories->find('list'); |
|
|
|
|
128
|
|
|
$this->set(compact('article', 'categories')); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Delete an Article and all his comments and likes. |
133
|
|
|
* |
134
|
|
|
* @return \Cake\Network\Response |
135
|
|
|
*/ |
136
|
|
|
public function delete() |
137
|
|
|
{ |
138
|
|
|
$this->loadModel('BlogArticles'); |
139
|
|
|
|
140
|
|
|
$article = $this->BlogArticles |
|
|
|
|
141
|
|
|
->find() |
142
|
|
|
->where([ |
143
|
|
|
'BlogArticles.id' => $this->request->id |
144
|
|
|
]) |
145
|
|
|
->first(); |
146
|
|
|
|
147
|
|
|
//Check if the article is found. |
148
|
|
|
if (empty($article)) { |
149
|
|
|
$this->Flash->error(__d('admin', 'This article doesn\'t exist or has been deleted.')); |
150
|
|
|
|
151
|
|
|
return $this->redirect(['action' => 'index']); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($this->BlogArticles->delete($article)) { |
|
|
|
|
155
|
|
|
$this->eventManager()->attach(new Statistics()); |
|
|
|
|
156
|
|
|
$event = new Event('Model.BlogArticles.new'); |
157
|
|
|
$this->eventManager()->dispatch($event); |
158
|
|
|
|
159
|
|
|
$this->Flash->success(__d('admin', 'This article has been deleted successfully !')); |
160
|
|
|
|
161
|
|
|
return $this->redirect(['action' => 'index']); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->Flash->error(__d('admin', 'Unable to delete this article.')); |
165
|
|
|
|
166
|
|
|
return $this->redirect(['action' => 'index']); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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.