1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Comments; |
4
|
|
|
|
5
|
|
|
use \Anax\Comments\Filter; |
6
|
|
|
|
7
|
|
|
class Comments |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
private $data = array(); |
11
|
|
|
|
12
|
2 |
|
public function init($db) |
13
|
|
|
{ |
14
|
2 |
|
$comment = new Comment(); |
15
|
2 |
|
$comment->setDb($db); |
16
|
|
|
|
17
|
2 |
|
$this->data = array(); |
18
|
|
|
|
19
|
2 |
|
$allComments = $comment->findAll(); |
20
|
2 |
|
foreach ($allComments as $comment) { |
21
|
|
|
$input = array( |
22
|
2 |
|
'id' => $comment->id, |
23
|
2 |
|
'article' => $comment->article, |
24
|
2 |
|
'author' => $comment->author, |
25
|
2 |
|
'email' => $comment->email, |
26
|
2 |
|
'comment' => $comment->comment); |
27
|
|
|
|
28
|
2 |
|
array_push($this->data, $input); |
29
|
2 |
|
} |
30
|
2 |
|
} |
31
|
|
|
|
32
|
2 |
|
public function getComment($id, $db) |
33
|
|
|
{ |
34
|
2 |
|
$comment = new Comment(); |
35
|
2 |
|
$comment->setDb($db); |
36
|
2 |
|
$content = $comment->find("id", $id); |
37
|
|
|
|
38
|
2 |
|
return $content; |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
public function getAllComments() |
42
|
|
|
{ |
43
|
3 |
|
return $this->data; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
public function addComment($vars, $db, $user) |
47
|
|
|
{ |
48
|
|
|
$input = array( |
49
|
2 |
|
'id' => ($this->commentCount() + 1), |
50
|
2 |
|
'article' => $vars['article'], |
51
|
2 |
|
'author' => $user["name"], |
52
|
2 |
|
'email' => $user["email"], |
53
|
2 |
|
'comment' => $vars['comment']); |
54
|
|
|
|
55
|
2 |
|
array_push($this->data, $input); |
56
|
|
|
|
57
|
2 |
|
$comment = new Comment(); |
58
|
2 |
|
$comment->setDb($db); |
59
|
2 |
|
$comment->article = $input['article']; |
60
|
2 |
|
$comment->author = $user["name"]; |
61
|
2 |
|
$comment->email = $user["email"]; |
62
|
2 |
|
$comment->comment = $input['comment']; |
63
|
2 |
|
$comment->save(); |
64
|
2 |
|
} |
65
|
|
|
|
66
|
2 |
|
public function deleteComment($id, $db) |
67
|
|
|
{ |
68
|
2 |
|
$comment = new Comment(); |
69
|
2 |
|
$comment->setDb($db); |
70
|
2 |
|
$comment = $comment->find("id", $id); |
71
|
2 |
|
$comment->delete(); |
|
|
|
|
72
|
2 |
|
} |
73
|
|
|
|
74
|
2 |
|
public function editComment($id, $text, $db) |
75
|
|
|
{ |
76
|
2 |
|
$comment = new Comment(); |
77
|
2 |
|
$comment->setDb($db); |
78
|
2 |
|
$old = $comment->find("id", $id); |
79
|
2 |
|
$comment->article = $old->article; |
80
|
2 |
|
$comment->author = $old->author; |
81
|
2 |
|
$comment->email = $old->email; |
82
|
2 |
|
$comment->comment = $text; |
83
|
2 |
|
$comment->save(); |
84
|
2 |
|
} |
85
|
|
|
|
86
|
3 |
|
public function commentCount() |
87
|
|
|
{ |
88
|
3 |
|
return count($this->data); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
public function commentSection($post, $del, $edt, $session) |
92
|
|
|
{ |
93
|
2 |
|
$comments = $this->getAllComments(); |
94
|
|
|
|
95
|
2 |
|
$htmlSection = ""; |
96
|
|
|
|
97
|
2 |
|
$filter = new Filter(); |
98
|
|
|
|
99
|
2 |
|
$user = $session->get("user"); |
100
|
|
|
|
101
|
2 |
|
foreach ($comments as $comment) { |
102
|
2 |
|
$author = $comment['author']; |
103
|
2 |
|
$content = $filter->bbcode2html($comment['comment']); |
104
|
2 |
|
$emailHash = md5(strtolower(trim($comment['email']))); |
105
|
2 |
|
if ($user["name"] == $author) { |
106
|
2 |
|
$delete = "<p><a href='" . $del . "?id=" . $comment['id'] . "'>Ta bort</a></p>"; |
107
|
2 |
|
$edit = "<p><a href='" . $edt . "?id=" . $comment['id'] . "'>Redigera</a></p>"; |
108
|
2 |
|
} elseif ($user["role"] == "admin") { |
109
|
|
|
$delete = "<p><a href='" . $del . "?id=" . $comment['id'] . "'>Ta bort</a></p>"; |
110
|
|
|
$edit = "<p><a href='" . $edt . "?id=" . $comment['id'] . "'>Redigera</a></p>"; |
111
|
|
|
} else { |
112
|
2 |
|
$delete = ""; |
113
|
2 |
|
$edit = ""; |
114
|
|
|
} |
115
|
|
|
$htmlSection .= |
116
|
|
|
<<<EOD |
117
|
|
|
<div class="comment"> |
118
|
|
|
<img class="avatar" |
119
|
2 |
|
src="https://www.gravatar.com/avatar/{$emailHash}?s=100&d=http%3A%2F%2Fi.imgur.com%2FCrOKsOd.png" |
120
|
|
|
alt="gravatar"> |
121
|
|
|
<address class="vcard author"> |
122
|
2 |
|
Av <em>{$author}</em> |
123
|
|
|
</address> |
124
|
|
|
|
125
|
|
|
<div class="entry-content"> |
126
|
2 |
|
{$content} |
127
|
|
|
</div> |
128
|
|
|
<div class="comment-actions"> |
129
|
2 |
|
{$edit} |
130
|
2 |
|
{$delete} |
131
|
|
|
</div> |
132
|
|
|
</article> |
133
|
2 |
|
</div> |
134
|
2 |
|
EOD; |
135
|
2 |
|
} |
136
|
|
|
|
137
|
|
|
$htmlSection .= |
138
|
|
|
<<<EOD |
139
|
|
|
<div class="leave-comment"> |
140
|
|
|
<h3>Skriv en kommentar</h3> |
141
|
2 |
|
<form action="{$post}" method="post"> |
142
|
|
|
<input type="hidden" name="author" value="Anonymous"> |
143
|
|
|
<input type="hidden" name="article" value="1"> |
144
|
|
|
<div class="compose-comment"> |
145
|
|
|
<textarea class="comment-text" name="comment" required="required"></textarea> |
146
|
|
|
</div> |
147
|
|
|
<input class="comment-post" name="submit" type="submit" value="Posta"> |
148
|
|
|
</form> |
149
|
2 |
|
</div> |
150
|
2 |
|
EOD; |
151
|
2 |
|
echo $htmlSection; |
152
|
2 |
|
} |
153
|
|
|
} |
154
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.