-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCommentsController.php
More file actions
114 lines (96 loc) · 2.86 KB
/
CommentsController.php
File metadata and controls
114 lines (96 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
CakePHP Feedback Plugin
Copyright (C) 2012-3827 dr. Hannibal Lecter / lecterror
<http://lecterror.com/>
Multi-licensed under:
MPL <http://www.mozilla.org/MPL/MPL-1.1.html>
LGPL <http://www.gnu.org/licenses/lgpl.html>
GPL <http://www.gnu.org/licenses/gpl.html>
*/
App::uses('FeedbackAppController', 'Feedback.Controller');
/**
* Comments Controller
*
* @property CommentsComponent $Comments
* @property AuthComponent $Auth
*/
class CommentsController extends FeedbackAppController
{
public $components = array('Feedback.Comments');
public function add($foreign_model = null, $foreign_id = null)
{
if (empty($foreign_model) ||
empty($foreign_id) ||
!$this->request->is('post')
)
{
return $this->redirect('/');
}
App::uses($foreign_model, 'Model');
$Model = ClassRegistry::init($foreign_model);
if (!($Model instanceof Model))
{
return $this->redirect('/');
}
if ($Model->hasAny(array($Model->primaryKey => $foreign_id)) == false)
{
return $this->redirect('/');
}
if (!isset($this->request->data['Comment']['foreign_model']) ||
!isset($this->request->data['Comment']['foreign_id']) ||
$this->request->data['Comment']['foreign_model'] != $foreign_model ||
$this->request->data['Comment']['foreign_id'] != $foreign_id)
{
return $this->redirect('/');
}
$user_id = null;
if (isset($this->Auth))
{
$user_id = $this->Auth->user('id');
}
$this->request->data['Comment']['foreign_model'] = $Model->name;
$this->request->data['Comment']['foreign_id'] = $foreign_id;
$this->request->data['Comment']['user_id'] = $user_id;
$this->request->data['Comment']['author_ip'] = $this->request->clientIp();
$this->Comment->create();
if (!$this->Comment->save($this->request->data))
{
$this->set('validation_errors', $this->Comment->validationErrors);
return;
}
if ($this->request->data['Comment']['remember_info'])
{
$this->Comments->saveInfo();
}
else
{
$this->Comments->forgetInfo();
}
$this->redirect($this->request->referer() . '#comment-' . $this->Comment->id);
}
/**
* delete method
*
* @throws NotFoundException
* @throws MethodNotAllowedException
* @param string $id
* @return void
*/
public function delete($id = null) {
$redirect = $this->request->referer() . '#comment';
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Comment->id = $id;
if (!$this->Comment->exists()) {
throw new NotFoundException(__d('feedback','Invalid request.'));
}
if ($this->Comment->delete()) {
$this->Flash->success(__d('feedback','Record deleted.'));
$this->redirect($redirect );
}
$this->Flash->error(__d('feedback','Failed, record was not deleted.'));
$this->redirect($redirect);
}
}