1
|
|
|
<?php |
2
|
|
|
namespace App\Controller; |
3
|
|
|
|
4
|
|
|
use App\Event\Badges; |
5
|
|
|
use App\Event\Statistics; |
6
|
|
|
use Cake\Core\Configure; |
7
|
|
|
use Cake\Event\Event; |
8
|
|
|
use Cake\Network\Exception\NotFoundException; |
9
|
|
|
use Cake\Routing\Router; |
10
|
|
|
|
11
|
|
|
class BlogController extends AppController |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Initialization hook method. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function initialize() |
20
|
|
|
{ |
21
|
|
|
parent::initialize(); |
22
|
|
|
|
23
|
|
|
$this->loadComponent('RequestHandler'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* BeforeFilter handle. |
28
|
|
|
* |
29
|
|
|
* @param Event $event The beforeFilter event that was fired. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public function beforeFilter(Event $event) |
34
|
|
|
{ |
35
|
|
|
parent::beforeFilter($event); |
36
|
|
|
|
37
|
|
|
$this->Auth->allow(['index', 'category', 'article', 'go', 'archive', 'search']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Display all Articles. |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function index() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$this->loadModel('BlogArticles'); |
48
|
|
|
$this->paginate = [ |
49
|
|
|
'maxLimit' => Configure::read('Blog.article_per_page') |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$articles = $this->BlogArticles |
|
|
|
|
53
|
|
|
->find() |
54
|
|
|
->contain([ |
55
|
|
|
'BlogCategories', |
56
|
|
|
'Users' => function ($q) { |
57
|
|
|
return $q->find('short'); |
58
|
|
|
} |
59
|
|
|
]) |
60
|
|
|
->order([ |
61
|
|
|
'BlogArticles.created' => 'desc' |
62
|
|
|
]) |
63
|
|
|
->where([ |
64
|
|
|
'BlogArticles.is_display' => 1 |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
$articles = $this->paginate($articles); |
68
|
|
|
|
69
|
|
|
$this->set(compact('articles')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Display a specific category with all its articles. |
74
|
|
|
* |
75
|
|
|
* @return \Cake\Network\Response|void |
76
|
|
|
*/ |
77
|
|
|
public function category() |
78
|
|
|
{ |
79
|
|
|
$this->loadModel('BlogCategories'); |
80
|
|
|
|
81
|
|
|
$category = $this->BlogCategories |
|
|
|
|
82
|
|
|
->find() |
83
|
|
|
->where([ |
84
|
|
|
'BlogCategories.id' => $this->request->id |
85
|
|
|
]) |
86
|
|
|
->contain([ |
87
|
|
|
'BlogArticles' |
88
|
|
|
]) |
89
|
|
|
->first(); |
90
|
|
|
|
91
|
|
|
//Check if the category is found. |
92
|
|
|
if (empty($category)) { |
93
|
|
|
$this->Flash->error(__('This category doesn\'t exist or has been deleted.')); |
94
|
|
|
|
95
|
|
|
return $this->redirect(['action' => 'index']); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
//Paginate all Articles. |
99
|
|
|
$this->loadModel('BlogArticles'); |
100
|
|
|
$this->paginate = [ |
101
|
|
|
'maxLimit' => Configure::read('Blog.article_per_page') |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
$articles = $this->BlogArticles |
|
|
|
|
105
|
|
|
->find() |
106
|
|
|
->contain([ |
107
|
|
|
'Users' => function ($q) { |
108
|
|
|
return $q->find('short'); |
109
|
|
|
} |
110
|
|
|
]) |
111
|
|
|
->where([ |
112
|
|
|
'BlogArticles.category_id' => $category->id, |
113
|
|
|
'BlogArticles.is_display' => 1 |
114
|
|
|
]) |
115
|
|
|
->order([ |
116
|
|
|
'BlogArticles.created' => 'desc' |
117
|
|
|
]); |
118
|
|
|
|
119
|
|
|
$articles = $this->paginate($articles); |
120
|
|
|
|
121
|
|
|
$this->set(compact('category', 'articles')); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Display a specific article. |
126
|
|
|
* |
127
|
|
|
* @return \Cake\Network\Response|void |
128
|
|
|
*/ |
129
|
|
|
public function article() |
130
|
|
|
{ |
131
|
|
|
$this->loadModel('BlogArticles'); |
132
|
|
|
|
133
|
|
|
$article = $this->BlogArticles |
|
|
|
|
134
|
|
|
->find() |
135
|
|
|
->where([ |
136
|
|
|
'BlogArticles.id' => $this->request->id, |
137
|
|
|
'BlogArticles.is_display' => 1 |
138
|
|
|
]) |
139
|
|
|
->contain([ |
140
|
|
|
'BlogCategories', |
141
|
|
|
'BlogAttachments', |
142
|
|
|
'Users' => function ($q) { |
143
|
|
|
return $q->find('full'); |
144
|
|
|
}, |
145
|
|
|
'Polls', |
146
|
|
|
'Polls.PollsAnswers', |
147
|
|
|
'Polls.PollsAnswers.Polls' => function ($q) { |
148
|
|
|
return $q->select(['id', 'user_count']); |
149
|
|
|
}, |
150
|
|
|
'Polls.PollsUsers' |
151
|
|
|
]) |
152
|
|
|
->first(); |
153
|
|
|
|
154
|
|
|
//Check if the article is found. |
155
|
|
|
if (is_null($article)) { |
156
|
|
|
$this->Flash->error(__('This article doesn\'t exist or has been deleted.')); |
157
|
|
|
|
158
|
|
|
return $this->redirect(['action' => 'index']); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$this->loadModel('BlogArticlesComments'); |
162
|
|
|
|
163
|
|
|
//A comment has been posted. |
164
|
|
|
if ($this->request->is('post')) { |
165
|
|
|
//Check if the user is connected. |
166
|
|
|
if (!$this->Auth->user()) { |
167
|
|
|
$this->Flash->error(__('You must be connected to post a comment.')); |
168
|
|
|
|
169
|
|
|
return $this->redirect([ |
170
|
|
|
'_name' => 'blog-article', |
171
|
|
|
'slug' => h($article->title), |
172
|
|
|
'id' => $article->id |
173
|
|
|
]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$this->request->data['article_id'] = $article->id; |
177
|
|
|
$this->request->data['user_id'] = $this->Auth->user('id'); |
178
|
|
|
|
179
|
|
|
$newComment = $this->BlogArticlesComments->newEntity($this->request->data, ['validate' => 'create']); |
|
|
|
|
180
|
|
|
|
181
|
|
|
//Attach Event. |
182
|
|
|
$this->BlogArticlesComments->eventManager()->attach(new Badges($this)); |
|
|
|
|
183
|
|
|
|
184
|
|
|
if ($insertComment = $this->BlogArticlesComments->save($newComment)) { |
|
|
|
|
185
|
|
|
$this->eventManager()->attach(new Statistics()); |
|
|
|
|
186
|
|
|
$event = new Event('Model.BlogArticlesComments.new'); |
187
|
|
|
$this->eventManager()->dispatch($event); |
188
|
|
|
|
189
|
|
|
$this->Flash->success(__('Your comment has been posted successfully !')); |
190
|
|
|
//Redirect the user to the last page of the article. |
191
|
|
|
$this->redirect([ |
192
|
|
|
'action' => 'go', |
193
|
|
|
$insertComment->id |
194
|
|
|
]); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$this->loadModel('PollsUsers'); |
199
|
|
|
$hasVoted = $this->PollsUsers |
|
|
|
|
200
|
|
|
->find() |
201
|
|
|
->contain([ |
202
|
|
|
'Polls' => function ($q) { |
203
|
|
|
return $q->select(['id']); |
204
|
|
|
}, |
205
|
|
|
'PollsAnswers' |
206
|
|
|
]) |
207
|
|
|
->where([ |
208
|
|
|
'PollsUsers.user_id' => $this->Auth->user('id'), |
209
|
|
|
'Polls.id' => $article->poll ? $article->poll->id : null |
210
|
|
|
]) |
211
|
|
|
->first(); |
212
|
|
|
|
213
|
|
|
//Paginate all comments related to the article. |
214
|
|
|
$this->paginate = [ |
215
|
|
|
'maxLimit' => Configure::read('Blog.comment_per_page') |
216
|
|
|
]; |
217
|
|
|
|
218
|
|
|
$comments = $this->BlogArticlesComments |
|
|
|
|
219
|
|
|
->find() |
220
|
|
|
->where([ |
221
|
|
|
'BlogArticlesComments.article_id' => $article->id |
222
|
|
|
]) |
223
|
|
|
->contain([ |
224
|
|
|
'Users' => function ($q) { |
225
|
|
|
return $q->find('medium'); |
226
|
|
|
} |
227
|
|
|
]) |
228
|
|
|
->order([ |
229
|
|
|
'BlogArticlesComments.created' => 'asc' |
230
|
|
|
]); |
231
|
|
|
|
232
|
|
|
$comments = $this->paginate($comments); |
233
|
|
|
|
234
|
|
|
//Select the like for the current auth user. |
235
|
|
|
$this->loadModel('BlogArticlesLikes'); |
236
|
|
|
$like = $this->BlogArticlesLikes |
|
|
|
|
237
|
|
|
->find() |
238
|
|
|
->where([ |
239
|
|
|
'user_id' => ($this->Auth->user()) ? $this->Auth->user('id') : null, |
240
|
|
|
'article_id' => $article->id |
241
|
|
|
]) |
242
|
|
|
->first(); |
243
|
|
|
|
244
|
|
|
//Build the newEntity for the comment form. |
245
|
|
|
$formComments = $this->BlogArticlesComments->newEntity(); |
|
|
|
|
246
|
|
|
|
247
|
|
|
//Search related articles |
248
|
|
|
$keywords = preg_split("/([\s,\W])+/", $article->title); |
249
|
|
|
|
250
|
|
|
$query = $this->BlogArticles->find(); |
|
|
|
|
251
|
|
|
$query |
252
|
|
|
->contain([ |
253
|
|
|
'BlogCategories', |
254
|
|
|
]); |
255
|
|
|
|
256
|
|
|
foreach ($keywords as $keyword) { |
257
|
|
|
$query->orWhere(function ($exp, $q) use ($keyword) { |
|
|
|
|
258
|
|
|
return $exp->like('BlogArticles.title', '%' . $keyword . '%'); |
259
|
|
|
}); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$articles = $query->andWhere([ |
263
|
|
|
'BlogArticles.is_display' => 1, |
264
|
|
|
'BlogArticles.id !=' => $article->id |
265
|
|
|
]); |
266
|
|
|
|
267
|
|
|
//Current user. |
268
|
|
|
$this->loadModel('Users'); |
269
|
|
|
$currentUser = $this->Users |
|
|
|
|
270
|
|
|
->find() |
271
|
|
|
->contain([ |
272
|
|
|
'Groups' => function ($q) { |
273
|
|
|
return $q->select(['id', 'is_staff']); |
274
|
|
|
} |
275
|
|
|
]) |
276
|
|
|
->where([ |
277
|
|
|
'Users.id' => $this->Auth->user('id') |
278
|
|
|
]) |
279
|
|
|
->select(['id', 'group_id']) |
280
|
|
|
->first(); |
281
|
|
|
|
282
|
|
|
$this->set(compact('article', 'formComments', 'comments', 'like', 'articles', 'currentUser', 'hasVoted')); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Quote a message. |
287
|
|
|
* |
288
|
|
|
* @param int $articleId Id of the article where is the message to quote. |
289
|
|
|
* @param int $commentId Id of the message to quote. |
290
|
|
|
* |
291
|
|
|
* @throws \Cake\Network\Exception\NotFoundException |
292
|
|
|
* |
293
|
|
|
* @return mixed |
294
|
|
|
*/ |
295
|
|
|
public function quote($articleId = null, $commentId = null) |
296
|
|
|
{ |
297
|
|
|
if (!$this->request->is('ajax')) { |
298
|
|
|
throw new NotFoundException(); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
$this->loadModel('BlogArticlesComments'); |
302
|
|
|
|
303
|
|
|
$comment = $this->BlogArticlesComments |
|
|
|
|
304
|
|
|
->find() |
305
|
|
|
->where([ |
306
|
|
|
'BlogArticlesComments.article_id' => $articleId, |
307
|
|
|
'BlogArticlesComments.id' => $commentId |
308
|
|
|
]) |
309
|
|
|
->contain([ |
310
|
|
|
'Users' => function ($q) { |
311
|
|
|
return $q->find('short'); |
312
|
|
|
} |
313
|
|
|
]) |
314
|
|
|
->first(); |
315
|
|
|
|
316
|
|
|
$json = []; |
317
|
|
|
|
318
|
|
|
if (!is_null($comment)) { |
319
|
|
|
$comment->toArray(); |
320
|
|
|
|
321
|
|
|
$url = Router::url(['action' => 'go', $comment->id]); |
322
|
|
|
$text = __("has said :"); |
323
|
|
|
|
324
|
|
|
//Build the quote. |
325
|
|
|
$json['comment'] = <<<EOT |
326
|
|
|
<div> |
327
|
|
|
<div> |
328
|
|
|
<a href="{$url}"> |
329
|
|
|
<strong>{$comment->user->full_name} {$text}</strong> |
330
|
|
|
</a> |
331
|
|
|
</div> |
332
|
|
|
<blockquote> |
333
|
|
|
$comment->content |
334
|
|
|
</blockquote> |
335
|
|
|
</div><p> </p><p> </p> |
336
|
|
|
EOT; |
337
|
|
|
|
338
|
|
|
$json['error'] = false; |
339
|
|
|
|
340
|
|
|
$this->set(compact('json')); |
341
|
|
|
} else { |
342
|
|
|
$json['comment'] = __("This comment doesn't exist."); |
343
|
|
|
$json['error'] = true; |
344
|
|
|
|
345
|
|
|
$this->set(compact('json')); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
//Send response in JSON. |
349
|
|
|
$this->set('_serialize', 'json'); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Redirect an user to an article, page and comment. |
354
|
|
|
* |
355
|
|
|
* @param int $commentId Id of the comment. |
356
|
|
|
* |
357
|
|
|
* @return \Cake\Network\Response |
358
|
|
|
*/ |
359
|
|
View Code Duplication |
public function go($commentId = null) |
|
|
|
|
360
|
|
|
{ |
361
|
|
|
$this->loadModel('BlogArticlesComments'); |
362
|
|
|
|
363
|
|
|
$comment = $this->BlogArticlesComments |
|
|
|
|
364
|
|
|
->find() |
365
|
|
|
->contain([ |
366
|
|
|
'BlogArticles' |
367
|
|
|
]) |
368
|
|
|
->where([ |
369
|
|
|
'BlogArticlesComments.id' => $commentId |
370
|
|
|
]) |
371
|
|
|
->first(); |
372
|
|
|
|
373
|
|
|
if (is_null($comment)) { |
374
|
|
|
$this->Flash->error(__("This comment doesn't exist or has been deleted.")); |
375
|
|
|
|
376
|
|
|
return $this->redirect(['action' => 'index']); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
$comment->toArray(); |
380
|
|
|
|
381
|
|
|
//Count the number of message before this message. |
382
|
|
|
$messagesBefore = $this->BlogArticlesComments |
|
|
|
|
383
|
|
|
->find() |
384
|
|
|
->where([ |
385
|
|
|
'BlogArticlesComments.article_id' => $comment->article_id, |
386
|
|
|
'BlogArticlesComments.created <' => $comment->created |
387
|
|
|
]) |
388
|
|
|
->count(); |
389
|
|
|
|
390
|
|
|
//Get the number of messages per page. |
391
|
|
|
$messagesPerPage = Configure::read('Blog.comment_per_page'); |
392
|
|
|
|
393
|
|
|
//Calculate the page. |
394
|
|
|
$page = floor($messagesBefore / $messagesPerPage) + 1; |
395
|
|
|
|
396
|
|
|
$page = ($page > 1) ? $page : 1; |
397
|
|
|
|
398
|
|
|
//Redirect the user. |
399
|
|
|
return $this->redirect([ |
400
|
|
|
'_name' => 'blog-article', |
401
|
|
|
'slug' => $comment->blog_article->title, |
402
|
|
|
'id' => $comment->blog_article->id, |
403
|
|
|
'?' => ['page' => $page], |
404
|
|
|
'#' => 'comment-' . $commentId |
405
|
|
|
]); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Get all articles by a date formatted to "m-Y". |
410
|
|
|
* |
411
|
|
|
* @param string $date The date of the archive. |
412
|
|
|
* |
413
|
|
|
* @return void |
414
|
|
|
*/ |
415
|
|
|
public function archive($date = null) |
416
|
|
|
{ |
417
|
|
|
$this->loadModel('BlogArticles'); |
418
|
|
|
|
419
|
|
|
$this->paginate = [ |
420
|
|
|
'maxLimit' => Configure::read('Blog.article_per_page') |
421
|
|
|
]; |
422
|
|
|
|
423
|
|
|
$archives = $this->BlogArticles |
|
|
|
|
424
|
|
|
->find() |
425
|
|
|
->where([ |
426
|
|
|
'DATE_FORMAT(BlogArticles.created,\'%m-%Y\')' => $date, |
427
|
|
|
'BlogArticles.is_display' => 1 |
428
|
|
|
]) |
429
|
|
|
->contain([ |
430
|
|
|
'BlogCategories', |
431
|
|
|
'Users' => function ($q) { |
432
|
|
|
return $q->find('short'); |
433
|
|
|
} |
434
|
|
|
]) |
435
|
|
|
->order([ |
436
|
|
|
'BlogArticles.created' => 'desc' |
437
|
|
|
]); |
438
|
|
|
|
439
|
|
|
$articles = $this->paginate($archives); |
440
|
|
|
|
441
|
|
|
$this->set(compact('articles', 'date')); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Search articles. |
446
|
|
|
* |
447
|
|
|
* @return void |
448
|
|
|
*/ |
449
|
|
|
public function search() |
450
|
|
|
{ |
451
|
|
|
$this->loadModel('BlogArticles'); |
452
|
|
|
|
453
|
|
|
//Check the keyword to search. (For pagination) |
454
|
|
View Code Duplication |
if (!empty($this->request->data['search'])) { |
|
|
|
|
455
|
|
|
$keyword = $this->request->data['search']; |
456
|
|
|
$this->request->session()->write('Search.Blog.Keyword', $keyword); |
457
|
|
|
} else { |
458
|
|
|
if ($this->request->session()->read('Search.Blog.Keyword')) { |
|
|
|
|
459
|
|
|
$keyword = $this->request->session()->read('Search.Blog.Keyword'); |
460
|
|
|
} else { |
461
|
|
|
$keyword = ''; |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
//Pagination |
466
|
|
|
$this->paginate = [ |
467
|
|
|
'maxLimit' => Configure::read('Blog.article_per_page') |
468
|
|
|
]; |
469
|
|
|
|
470
|
|
|
$articles = $this->BlogArticles |
|
|
|
|
471
|
|
|
->find() |
472
|
|
|
->contain([ |
473
|
|
|
'Users' => function ($q) { |
474
|
|
|
return $q->find('short'); |
475
|
|
|
} |
476
|
|
|
]) |
477
|
|
|
->where([ |
478
|
|
|
'BlogArticles.is_display' => 1 |
479
|
|
|
]) |
480
|
|
|
->andWhere(function ($q) use ($keyword) { |
481
|
|
|
return $q |
482
|
|
|
->like('title', "%$keyword%"); |
483
|
|
|
}) |
484
|
|
|
->order([ |
485
|
|
|
'BlogArticles.created' => 'desc' |
486
|
|
|
]); |
487
|
|
|
|
488
|
|
|
$articles = $this->paginate($articles); |
489
|
|
|
|
490
|
|
|
$this->set(compact('articles', 'keyword')); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Like an article. |
495
|
|
|
* |
496
|
|
|
* @param int $articleId Id of the article to like. |
497
|
|
|
* |
498
|
|
|
* @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request. |
499
|
|
|
* |
500
|
|
|
* @return void |
501
|
|
|
*/ |
502
|
|
|
public function articleLike($articleId = null) |
503
|
|
|
{ |
504
|
|
|
if (!$this->request->is('ajax')) { |
505
|
|
|
throw new NotFoundException(); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
//Check if the user hasn't already liked this article. |
509
|
|
|
$this->loadModel('BlogArticlesLikes'); |
510
|
|
|
$checkLike = $this->BlogArticlesLikes |
|
|
|
|
511
|
|
|
->find() |
512
|
|
|
->where([ |
513
|
|
|
'BlogArticlesLikes.user_id' => $this->Auth->user('id'), |
514
|
|
|
'BlogArticlesLikes.article_id' => $articleId |
515
|
|
|
]) |
516
|
|
|
->first(); |
517
|
|
|
|
518
|
|
|
$json = []; |
519
|
|
|
|
520
|
|
View Code Duplication |
if (!is_null($checkLike)) { |
|
|
|
|
521
|
|
|
$json['message'] = __('You already like this article !'); |
522
|
|
|
$json['error'] = true; |
523
|
|
|
|
524
|
|
|
$this->set(compact('json')); |
525
|
|
|
|
526
|
|
|
$this->set('_serialize', 'json'); |
527
|
|
|
|
528
|
|
|
return; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
//Check if the article exist. |
532
|
|
|
$this->loadModel('BlogArticles'); |
533
|
|
|
$checkArticle = $this->BlogArticles |
|
|
|
|
534
|
|
|
->find() |
535
|
|
|
->where([ |
536
|
|
|
'BlogArticles.id' => $articleId, |
537
|
|
|
'BlogArticles.is_display' => 1 |
538
|
|
|
]) |
539
|
|
|
->first(); |
540
|
|
|
|
541
|
|
View Code Duplication |
if (is_null($checkArticle)) { |
|
|
|
|
542
|
|
|
$json['message'] = __("This article doesn't exist !"); |
543
|
|
|
$json['error'] = true; |
544
|
|
|
|
545
|
|
|
$this->set(compact('json')); |
546
|
|
|
$this->set('_serialize', 'json'); |
547
|
|
|
|
548
|
|
|
return; |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
//Prepare data to be saved. |
552
|
|
|
$data = []; |
553
|
|
|
$data['BlogArticlesLikes']['user_id'] = $this->Auth->user('id'); |
554
|
|
|
$data['BlogArticlesLikes']['article_id'] = $articleId; |
555
|
|
|
|
556
|
|
|
$like = $this->BlogArticlesLikes->newEntity($data); |
|
|
|
|
557
|
|
|
|
558
|
|
|
if ($this->BlogArticlesLikes->save($like)) { |
|
|
|
|
559
|
|
|
//Update the Statistics |
560
|
|
|
$this->eventManager()->attach(new Statistics()); |
|
|
|
|
561
|
|
|
$event = new Event('Model.BlogArticlesLikes.new'); |
562
|
|
|
$this->eventManager()->dispatch($event); |
563
|
|
|
|
564
|
|
|
$json['message'] = __('Thanks for {0} this article ! ', "<i class='fa fa-heart text-danger'></i>"); |
565
|
|
|
$json['title'] = __('You {0} this article.', "<i class='fa fa-heart text-danger'></i>"); |
566
|
|
|
$json['url'] = Router::url( |
567
|
|
|
[ |
568
|
|
|
'action' => 'articleUnlike', |
569
|
|
|
$articleId |
570
|
|
|
] |
571
|
|
|
); |
572
|
|
|
$json['error'] = false; |
573
|
|
|
} else { |
574
|
|
|
$json['message'] = __('An error occurred, please try again later.'); |
575
|
|
|
$json['error'] = true; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
$this->set(compact('json')); |
579
|
|
|
|
580
|
|
|
$this->set('_serialize', 'json'); |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* Unlike an article. |
585
|
|
|
* |
586
|
|
|
* @param int|null $articleId Id of the article to like. |
587
|
|
|
* |
588
|
|
|
* @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request. |
589
|
|
|
* |
590
|
|
|
* @return void |
591
|
|
|
*/ |
592
|
|
|
public function articleUnlike($articleId = null) |
593
|
|
|
{ |
594
|
|
|
if (!$this->request->is('ajax')) { |
595
|
|
|
throw new NotFoundException(); |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
//Check if the user like this article. |
599
|
|
|
$this->loadModel('BlogArticlesLikes'); |
600
|
|
|
$like = $this->BlogArticlesLikes |
|
|
|
|
601
|
|
|
->find() |
602
|
|
|
->contain([ |
603
|
|
|
'BlogArticles' |
604
|
|
|
]) |
605
|
|
|
->where([ |
606
|
|
|
'BlogArticlesLikes.user_id' => $this->Auth->user('id'), |
607
|
|
|
'BlogArticlesLikes.article_id' => $articleId, |
608
|
|
|
'BlogArticles.is_display' => 1 |
609
|
|
|
]) |
610
|
|
|
->first(); |
611
|
|
|
|
612
|
|
|
$json = []; |
613
|
|
|
|
614
|
|
View Code Duplication |
if (is_null($like)) { |
|
|
|
|
615
|
|
|
$json['message'] = __("You don't like this article !"); |
616
|
|
|
$json['error'] = true; |
617
|
|
|
|
618
|
|
|
$this->set(compact('json')); |
619
|
|
|
|
620
|
|
|
$this->set('_serialize', 'json'); |
621
|
|
|
|
622
|
|
|
return; |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
if ($this->BlogArticlesLikes->delete($like)) { |
|
|
|
|
626
|
|
|
//Update the Statistics |
627
|
|
|
$this->eventManager()->attach(new Statistics()); |
|
|
|
|
628
|
|
|
$event = new Event('Model.BlogArticlesLikes.new'); |
629
|
|
|
$this->eventManager()->dispatch($event); |
630
|
|
|
|
631
|
|
|
$json['url'] = Router::url([ |
632
|
|
|
'action' => 'articleLike', |
633
|
|
|
$articleId |
634
|
|
|
]); |
635
|
|
|
$json['title'] = __('Like {0}', "<i class='fa fa-heart text-danger'></i>"); |
636
|
|
|
$json['error'] = false; |
637
|
|
|
} else { |
638
|
|
|
$json['message'] = __('An error occurred, please try again later.'); |
639
|
|
|
$json['error'] = true; |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
$this->set(compact('json')); |
643
|
|
|
|
644
|
|
|
$this->set('_serialize', 'json'); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* Delete a comment. |
649
|
|
|
* |
650
|
|
|
* @param int $id Id of the comment to delete. |
651
|
|
|
* |
652
|
|
|
* @return \Cake\Network\Response |
653
|
|
|
*/ |
654
|
|
|
public function deleteComment($id = null) |
655
|
|
|
{ |
656
|
|
|
$this->loadModel('BlogArticlesComments'); |
657
|
|
|
|
658
|
|
|
$comment = $this->BlogArticlesComments |
|
|
|
|
659
|
|
|
->find() |
660
|
|
|
->contain([ |
661
|
|
|
'BlogArticles' |
662
|
|
|
]) |
663
|
|
|
->where([ |
664
|
|
|
'BlogArticlesComments.id' => $id |
665
|
|
|
]) |
666
|
|
|
->first(); |
667
|
|
|
|
668
|
|
|
if (is_null($comment)) { |
669
|
|
|
$this->Flash->error(__("This comment doesn't exist or has been deleted !")); |
670
|
|
|
|
671
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
//Current user. |
675
|
|
|
$this->loadModel('Users'); |
676
|
|
|
$currentUser = $this->Users |
|
|
|
|
677
|
|
|
->find() |
678
|
|
|
->contain([ |
679
|
|
|
'Groups' => function ($q) { |
680
|
|
|
return $q->select(['id', 'is_staff']); |
681
|
|
|
} |
682
|
|
|
]) |
683
|
|
|
->where([ |
684
|
|
|
'Users.id' => $this->Auth->user('id') |
685
|
|
|
]) |
686
|
|
|
->select(['id', 'group_id']) |
687
|
|
|
->first(); |
688
|
|
|
|
689
|
|
|
if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) { |
690
|
|
|
$this->Flash->error(__("You don't have the authorization to delete this comment !")); |
691
|
|
|
|
692
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
if ($this->BlogArticlesComments->delete($comment)) { |
|
|
|
|
696
|
|
|
$this->eventManager()->attach(new Statistics()); |
|
|
|
|
697
|
|
|
$event = new Event('Model.BlogArticlesComments.new'); |
698
|
|
|
$this->eventManager()->dispatch($event); |
699
|
|
|
|
700
|
|
|
$this->Flash->success(__("This comment has been deleted successfully !")); |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
return $this->redirect(['_name' => 'blog-article', 'slug' => $comment->blog_article->title, 'id' => $comment->blog_article->id, '?' => ['page' => $comment->blog_article->last_page]]); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
/** |
707
|
|
|
* Get the form to edit a comment. |
708
|
|
|
* |
709
|
|
|
* @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request. |
710
|
|
|
* |
711
|
|
|
* @return void |
712
|
|
|
*/ |
713
|
|
|
public function getEditComment() |
714
|
|
|
{ |
715
|
|
|
if (!$this->request->is('ajax')) { |
716
|
|
|
throw new NotFoundException(); |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
$this->loadModel('BlogArticlesComments'); |
720
|
|
|
$comment = $this->BlogArticlesComments |
|
|
|
|
721
|
|
|
->find() |
722
|
|
|
->where([ |
723
|
|
|
'BlogArticlesComments.id' => $this->request->data['id'] |
724
|
|
|
]) |
725
|
|
|
->first(); |
726
|
|
|
|
727
|
|
|
$json = [ |
728
|
|
|
'error' => false, |
729
|
|
|
'errorMessage' => '' |
730
|
|
|
]; |
731
|
|
|
|
732
|
|
View Code Duplication |
if (is_null($comment)) { |
|
|
|
|
733
|
|
|
$json['error'] = true; |
734
|
|
|
$json['errorMessage'] = __("This comment doesn't exist or has been deleted !"); |
735
|
|
|
|
736
|
|
|
$this->set(compact('json')); |
737
|
|
|
|
738
|
|
|
return; |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
//Current user. |
742
|
|
|
$this->loadModel('Users'); |
743
|
|
|
$currentUser = $this->Users |
|
|
|
|
744
|
|
|
->find() |
745
|
|
|
->contain([ |
746
|
|
|
'Groups' => function ($q) { |
747
|
|
|
return $q->select(['id', 'is_staff']); |
748
|
|
|
} |
749
|
|
|
]) |
750
|
|
|
->where([ |
751
|
|
|
'Users.id' => $this->Auth->user('id') |
752
|
|
|
]) |
753
|
|
|
->select(['id', 'group_id']) |
754
|
|
|
->first(); |
755
|
|
|
|
756
|
|
|
if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) { |
757
|
|
|
$json['error'] = true; |
758
|
|
|
$json['errorMessage'] = __("You don't have the authorization to edit this comment !"); |
759
|
|
|
|
760
|
|
|
$this->set(compact('json')); |
761
|
|
|
|
762
|
|
|
return; |
763
|
|
|
} |
764
|
|
|
|
765
|
|
|
$this->set(compact('json', 'comment')); |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
/** |
769
|
|
|
* Edit a comment. |
770
|
|
|
* |
771
|
|
|
* @param int $id Id of the comment. |
772
|
|
|
* |
773
|
|
|
* @throws \Cake\Network\Exception\NotFoundException When it's not a POST request. |
774
|
|
|
* |
775
|
|
|
* @return \Cake\Network\Response |
776
|
|
|
*/ |
777
|
|
|
public function editComment($id = null) |
778
|
|
|
{ |
779
|
|
|
if (!$this->request->is('post')) { |
780
|
|
|
throw new NotFoundException(); |
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
$this->loadModel('BlogArticlesComments'); |
784
|
|
|
|
785
|
|
|
$comment = $this->BlogArticlesComments |
|
|
|
|
786
|
|
|
->find() |
787
|
|
|
->contain([ |
788
|
|
|
'BlogArticles' |
789
|
|
|
]) |
790
|
|
|
->where([ |
791
|
|
|
'BlogArticlesComments.id' => $id |
792
|
|
|
]) |
793
|
|
|
->first(); |
794
|
|
|
|
795
|
|
|
if (is_null($comment)) { |
796
|
|
|
$this->Flash->error(__("This comment doesn't exist or has been deleted !")); |
797
|
|
|
|
798
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
//Current user. |
802
|
|
|
$this->loadModel('Users'); |
803
|
|
|
$currentUser = $this->Users |
|
|
|
|
804
|
|
|
->find() |
805
|
|
|
->contain([ |
806
|
|
|
'Groups' => function ($q) { |
807
|
|
|
return $q->select(['id', 'is_staff']); |
808
|
|
|
} |
809
|
|
|
]) |
810
|
|
|
->where([ |
811
|
|
|
'Users.id' => $this->Auth->user('id') |
812
|
|
|
]) |
813
|
|
|
->select(['id', 'group_id']) |
814
|
|
|
->first(); |
815
|
|
|
|
816
|
|
|
if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) { |
817
|
|
|
$this->Flash->error(__("You don't have the authorization to edit this comment !")); |
818
|
|
|
|
819
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
$this->BlogArticlesComments->patchEntity($comment, $this->request->data()); |
|
|
|
|
823
|
|
|
if ($this->BlogArticlesComments->save($comment)) { |
|
|
|
|
824
|
|
|
$this->Flash->success(__("This comment has been edited successfully !")); |
825
|
|
|
} |
826
|
|
|
|
827
|
|
|
return $this->redirect(['action' => 'go', $comment->id]); |
828
|
|
|
} |
829
|
|
|
} |
830
|
|
|
|
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.