1
|
|
|
<?php |
2
|
|
|
namespace App\Controller\Admin; |
3
|
|
|
|
4
|
|
|
use App\Controller\AppController; |
5
|
|
|
use Cake\Routing\Router; |
6
|
|
|
|
7
|
|
|
class PollsController extends AppController |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Display all Polls. |
11
|
|
|
* |
12
|
|
|
* @return \Cake\Network\Response |
13
|
|
|
*/ |
14
|
|
View Code Duplication |
public function index() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$this->loadModel('Polls'); |
17
|
|
|
|
18
|
|
|
$this->paginate = [ |
19
|
|
|
'maxLimit' => 15 |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
$polls = $this->Polls |
|
|
|
|
23
|
|
|
->find() |
24
|
|
|
->contain([ |
25
|
|
|
'Users' => function ($q) { |
26
|
|
|
return $q->find('short'); |
27
|
|
|
}, |
28
|
|
|
'PollsAnswers', |
29
|
|
|
'BlogArticles' |
30
|
|
|
]) |
31
|
|
|
->order([ |
32
|
|
|
'Polls.created' => 'desc' |
33
|
|
|
]); |
34
|
|
|
|
35
|
|
|
$polls = $this->paginate($polls); |
36
|
|
|
$this->set(compact('polls')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a Poll. |
41
|
|
|
* |
42
|
|
|
* @return \Cake\Network\Response|void |
43
|
|
|
*/ |
44
|
|
|
public function add() |
45
|
|
|
{ |
46
|
|
|
$this->loadModel('BlogArticles'); |
47
|
|
|
$this->loadModel('Polls'); |
48
|
|
|
|
49
|
|
|
$poll = $this->Polls->newEntity($this->request->getParsedBody()); |
|
|
|
|
50
|
|
|
$articles = $this->Polls->BlogArticles->find('list'); |
|
|
|
|
51
|
|
|
|
52
|
|
|
if ($this->request->is('post')) { |
53
|
|
|
$article = $this->BlogArticles |
|
|
|
|
54
|
|
|
->find() |
55
|
|
|
->contain([ |
56
|
|
|
'Polls' |
57
|
|
|
]) |
58
|
|
|
->where([ |
59
|
|
|
'BlogArticles.id' => $poll->article_id |
60
|
|
|
]) |
61
|
|
|
->first(); |
62
|
|
|
|
63
|
|
|
//Check if the article has already a poll |
64
|
|
View Code Duplication |
if (!is_null($article->poll)) { |
|
|
|
|
65
|
|
|
$this->Flash->error( |
66
|
|
|
__d( |
67
|
|
|
'admin', |
68
|
|
|
'This article has already a poll, you can edit it <a href="{0}" class="btn btn-sm btn-danger-outline">here</a>.', |
69
|
|
|
Router::url(['_name' => 'polls-edit', 'id' => $article->poll->id, 'slug' => $article->poll->name]) |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
return $this->redirect(['action' => 'index']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$pollAnswers = []; |
77
|
|
|
|
78
|
|
View Code Duplication |
foreach ($poll->answers as $answers => $answer) { |
|
|
|
|
79
|
|
|
$answer = trim($answer); |
80
|
|
|
|
81
|
|
|
if (!empty($answer)) { |
82
|
|
|
$entity = $this->Polls->PollsAnswers->newEntity(); |
|
|
|
|
83
|
|
|
$entity->response = $answer; |
84
|
|
|
array_push($pollAnswers, $entity); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
if (empty($pollAnswers)) { |
|
|
|
|
89
|
|
|
$this->Flash->error(__d('admin', 'You must add at least one answer for this poll.')); |
90
|
|
|
$this->set(compact('poll', 'articles')); |
91
|
|
|
|
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
$poll->user_id = $this->Auth->user('id'); |
95
|
|
|
$poll->polls_answers = $pollAnswers; |
96
|
|
|
$poll->unsetProperty('anwsers'); |
97
|
|
|
|
98
|
|
View Code Duplication |
if ($poll = $this->Polls->save($poll)) { |
|
|
|
|
99
|
|
|
$this->Flash->success(__d('admin', 'Your poll has been created successfully !')); |
100
|
|
|
|
101
|
|
|
return $this->redirect(['action' => 'index']); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->set(compact('poll', 'articles')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Edit a Poll. |
110
|
|
|
* |
111
|
|
|
* @return \Cake\Network\Response |
112
|
|
|
*/ |
113
|
|
|
public function edit() |
114
|
|
|
{ |
115
|
|
|
$this->loadModel('Polls'); |
116
|
|
|
$this->loadModel('BlogArticles'); |
117
|
|
|
|
118
|
|
|
$poll = $this->Polls |
|
|
|
|
119
|
|
|
->find() |
120
|
|
|
->contain([ |
121
|
|
|
'PollsAnswers' |
122
|
|
|
]) |
123
|
|
|
->where([ |
124
|
|
|
'id' => $this->request->getAttribute('params')['id'] |
125
|
|
|
]) |
126
|
|
|
->first(); |
127
|
|
|
|
128
|
|
|
//Check if the poll is found. |
129
|
|
|
if (empty($poll)) { |
130
|
|
|
$this->Flash->error(__d('admin', 'This poll doesn\'t exist or has been deleted.')); |
131
|
|
|
|
132
|
|
|
return $this->redirect(['action' => 'index']); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($this->request->is(['put', 'post'])) { |
136
|
|
|
$this->Polls->patchEntity($poll, $this->request->getParsedBody()); |
|
|
|
|
137
|
|
|
$articles = $this->Polls->BlogArticles->find('list'); |
|
|
|
|
138
|
|
|
|
139
|
|
|
$article = $this->BlogArticles |
|
|
|
|
140
|
|
|
->find() |
141
|
|
|
->contain([ |
142
|
|
|
'Polls' |
143
|
|
|
]) |
144
|
|
|
->where([ |
145
|
|
|
'BlogArticles.id' => $poll->article_id |
146
|
|
|
]) |
147
|
|
|
->first(); |
148
|
|
|
|
149
|
|
|
//Check if the article has already a poll |
150
|
|
|
if (!is_null($article->poll) && $article->poll->id != $this->request->getAttribute('params')['id']) { |
151
|
|
|
$this->Flash->error( |
152
|
|
|
__d( |
153
|
|
|
'admin', |
154
|
|
|
'This article has already a poll, you can edit it <a href="{0}" class="btn btn-sm btn-danger-outline">here</a>.', |
155
|
|
|
Router::url(['_name' => 'polls-edit', 'id' => $article->poll->id, 'slug' => $article->poll->name]) |
156
|
|
|
) |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
return $this->redirect(['action' => 'index']); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$poll->polls_answers = []; |
163
|
|
|
|
164
|
|
View Code Duplication |
foreach ($poll->answers as $answers => $answer) { |
|
|
|
|
165
|
|
|
$answer = trim($answer); |
166
|
|
|
|
167
|
|
|
if (!empty($answer)) { |
168
|
|
|
$entity = $this->Polls->PollsAnswers->newEntity(); |
|
|
|
|
169
|
|
|
$entity->response = $answer; |
170
|
|
|
array_push($poll->polls_answers, $entity); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
View Code Duplication |
if (empty($poll->polls_answers)) { |
|
|
|
|
175
|
|
|
$this->Flash->error(__d('admin', 'You must add at least one answer for this poll.')); |
176
|
|
|
$this->set(compact('poll', 'articles')); |
177
|
|
|
|
178
|
|
|
return; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$poll->user_id = $this->Auth->user('id'); |
182
|
|
|
$poll->unsetProperty('anwsers'); |
183
|
|
|
|
184
|
|
|
if ($poll = $this->Polls->save($poll)) { |
|
|
|
|
185
|
|
|
$this->Flash->success(__d('admin', 'Your poll has been edited successfully !')); |
186
|
|
|
} else { |
187
|
|
|
$this->Flash->error(__d('admin', 'An error occurred while editing the poll.')); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this->redirect(['action' => 'index']); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$articles = $this->Polls->BlogArticles->find('list'); |
|
|
|
|
194
|
|
|
$this->set(compact('poll', 'articles')); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Delete a Poll. |
199
|
|
|
* |
200
|
|
|
* @return \Cake\Network\Response |
201
|
|
|
*/ |
202
|
|
View Code Duplication |
public function delete() |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
$this->loadModel('Polls'); |
205
|
|
|
|
206
|
|
|
$poll = $this->Polls |
|
|
|
|
207
|
|
|
->find() |
208
|
|
|
->where([ |
209
|
|
|
'id' => $this->request->getAttribute('params')['id'] |
210
|
|
|
]) |
211
|
|
|
->first(); |
212
|
|
|
|
213
|
|
|
//Check if the poll is found. |
214
|
|
|
if (empty($poll)) { |
215
|
|
|
$this->Flash->error(__d('admin', 'This poll doesn\'t exist or has been deleted.')); |
216
|
|
|
|
217
|
|
|
return $this->redirect(['action' => 'index']); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if ($this->Polls->delete($poll)) { |
|
|
|
|
221
|
|
|
$this->Flash->success(__d('admin', 'This poll has been deleted successfully !')); |
222
|
|
|
} else { |
223
|
|
|
$this->Flash->error(__d('admin', 'Unable to delete this poll.')); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $this->redirect(['action' => 'index']); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
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.