|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* class for commenting |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Loom\Comment; |
|
6
|
|
|
|
|
7
|
|
|
class CommentController extends \Phpmvc\Comment\CommentController |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* View all comments. |
|
12
|
|
|
* |
|
13
|
|
|
* @return void |
|
14
|
|
|
*/ |
|
15
|
|
|
public function viewAction() |
|
16
|
|
|
{ |
|
17
|
|
|
$comments = new \Loom\Comment\CommentsInSession(); |
|
18
|
|
|
$comments->setDI($this->di); |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
$all = $comments->findAll(); |
|
21
|
|
|
$this->views->add('comment/comments', [ |
|
|
|
|
|
|
22
|
|
|
'comments' => $all, |
|
23
|
|
|
]); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Add a comment. |
|
29
|
|
|
* |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
View Code Duplication |
public function addAction() |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
$isPosted = $this->request->getPost('doCreate'); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
if (!$isPosted) { |
|
37
|
|
|
$this->response->redirect($this->request->getPost('redirect')); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$comment = [ |
|
41
|
|
|
'content' => $this->request->getPost('content'), |
|
42
|
|
|
'name' => $this->request->getPost('name'), |
|
43
|
|
|
'web' => $this->request->getPost('web'), |
|
44
|
|
|
'mail' => $this->request->getPost('mail'), |
|
45
|
|
|
'timestamp' => time(), |
|
46
|
|
|
'ip' => $this->request->getServer('REMOTE_ADDR'), |
|
47
|
|
|
'comment-flow' => $this->request->getPost('comment-flow'), |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
$comments = new \Phpmvc\Comment\CommentsInSession(); |
|
51
|
|
|
$comments->setDI($this->di); |
|
52
|
|
|
|
|
53
|
|
|
$comments->add($comment); |
|
54
|
|
|
|
|
55
|
|
|
$this->response->redirect($this->request->getPost('redirect')); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Edit a comment. |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public function editAction() |
|
64
|
|
|
{ |
|
65
|
|
|
$comments = new \Phpmvc\Comment\CommentsInSession(); |
|
66
|
|
|
$comments->setDI($this->di); |
|
67
|
|
|
$id = $this->request->getGet('id'); |
|
68
|
|
|
$theComments = $comments->findAll(); |
|
69
|
|
|
$this->validate->check($id, ['int', 'range' => [0, count($theComments)]]) |
|
|
|
|
|
|
70
|
|
|
or die("Wrong index. Comment does not exist!"); |
|
71
|
|
|
$comment = $theComments[$id]; |
|
72
|
|
|
$this->views->add('comment/edit', [ |
|
73
|
|
|
'mail' => $comment['mail'], |
|
74
|
|
|
'web' => $comment['web'], |
|
75
|
|
|
'name' => $comment['name'], |
|
76
|
|
|
'content' => $comment['content'], |
|
77
|
|
|
'output' => null, |
|
78
|
|
|
'id' => $id, |
|
79
|
|
|
'commentFlow' => $comment['comment-flow'], |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Update a comment. |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
public function updateAction() |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$isPosted = $this->request->getPost('doUpdate'); |
|
91
|
|
|
|
|
92
|
|
|
if (!$isPosted) { |
|
93
|
|
|
$this->response->redirect($this->request->getPost('redirect')); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$comment = [ |
|
97
|
|
|
'content' => $this->request->getPost('content'), |
|
98
|
|
|
'name' => $this->request->getPost('name'), |
|
99
|
|
|
'web' => $this->request->getPost('web'), |
|
100
|
|
|
'mail' => $this->request->getPost('mail'), |
|
101
|
|
|
'timestamp' => time(), |
|
102
|
|
|
'ip' => $this->request->getServer('REMOTE_ADDR'), |
|
103
|
|
|
'comment-flow' => $this->request->getPost('comment-flow'), |
|
104
|
|
|
]; |
|
105
|
|
|
$id = $this->request->getPost('id'); |
|
106
|
|
|
|
|
107
|
|
|
$comments = new \Loom\Comment\CommentsInSession(); |
|
108
|
|
|
$comments->setDI($this->di); |
|
109
|
|
|
|
|
110
|
|
|
$comments->update($comment, $id); |
|
111
|
|
|
|
|
112
|
|
|
$this->response->redirect($this->request->getPost('redirect')); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Delete a comment. |
|
117
|
|
|
* |
|
118
|
|
|
* @return void |
|
119
|
|
|
*/ |
|
120
|
|
|
public function deleteAction() |
|
121
|
|
|
{ |
|
122
|
|
|
$id = $this->request->getGet('id'); |
|
123
|
|
|
$comments = new \Loom\Comment\CommentsInSession(); |
|
124
|
|
|
$comments->setDI($this->di); |
|
125
|
|
|
|
|
126
|
|
|
$comments->delete($id); |
|
127
|
|
|
// TODO: Lägg till metod i CRequestBasic med referer url. Check if HTTP_REFERER exists also and escape also. |
|
128
|
|
|
$this->response->redirect($_SERVER['HTTP_REFERER']); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|