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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
<?php
require_once('CommentRepositoryInterface.php');
require_once(dirname(__FILE__).'/../classes/Comment.php');
require_once(dirname(__FILE__)."/../constants.php");
require_once(dirname(__FILE__)."/../utilities.php");
class CommentRepository implements CommentRepositoryInterface {
protected $db;
public function __construct($db) {
$this->db = $db;
}
public function addComment($comment) {
$status = array();
$queryComment = "INSERT INTO commenti(id_locale, id_user, voto, descrizione, data)
VALUES ('".$comment->id_locale."',
'".$comment->id_user."',
'".$comment->voto."',
'".$comment->descrizione."',
'".$comment->data."')";
//echo "<br/>".$queryComment."<br/>";
$imgs = $comment->imgs;
$imgsToInsert = array();
try {
$this->db->exec($queryComment);
$id_commento = $this->db->lastInsertId();
/*foreach($imgs as $img) {
$imgsToInsert[] = '("'.$id_commento.'", "'.$img.'")';
}
$queryImgs = "INSERT INTO immagini_commenti(id_commento, img) VALUES" . implode(',', $imgsToInsert);
$this->db->exec($queryImgs);*/
$status["id"] = $id_commento;
$status["return"] = 0;
return $status;
} catch (PDOException $e) {
echo $e->getMessage();
$status = createErrorMessage(2, "Errore DB");
$status["id"] = -1;
//$status["return"] = 1;
return $status;
}
}
public function countComments($id_local) {
$countQuery = "SELECT count(*) as count FROM commenti WHERE id_locale=".$id_local;
try {
$result = $this->db->query($countQuery);
return (!$result || $result->rowCount()==0) ? 0 : $result->fetchColumn();
} catch (PDOException $e) {
echo $e->getMessage();
return null;
}
}
public function getComments($id_local, $start_index, $length, $data_order) {
/* query SQL da eseguire
SELECT id_commento, id_locale, id_user, voto, descrizione, data, img
FROM commenti, immagini_commenti
WHERE commenti.id=immagini_commenti.id_commento AND id_locale=$id_local
LIMIT $start_index, $length
*/
$comment_id_list = array();
$comment_list = array();
$queryComments;
$sqlComments = "SELECT id
FROM commenti
WHERE id_locale=".$id_local."
LIMIT ".$start_index.",".$length;
//echo "<br/>".$sqlComments."<br/>";
try {
$query = $this->db->query($sqlComments);
if (!$query || $query->rowCount()==0) return $comment_list;
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
array_push($comment_id_list, $row["id"]);
}
$sqlImgs = "SELECT c.id AS id_commento, id_locale, id_user, voto, descrizione, data, img, username, u.avatar as avatar
FROM commenti as c
LEFT JOIN immagini_commenti as i ON c.id=i.id_commento
LEFT JOIN utente as u ON c.id_user=u.id
WHERE c.id IN (".implode(",", $comment_id_list).")
ORDER BY c.data ".$data_order;
//echo "<br/>".$sqlImgs."<br/>";
$query = $this->db->query($sqlImgs);
if (!$query) return null;
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$comment_id = $row["id_commento"];
$imgRow = (!is_null($row["img"])) ? IMAGES_PATH.$row["img"] : null;
if (!array_key_exists($comment_id, $comment_list)) {
$new_comment = new Comment();
$new_comment->id = $comment_id;
$new_comment->id_locale = $id_local;
$new_comment->id_user = (!is_null($row["id_user"]) && $row["id_user"]!="") ? $row["id_user"] : -1;
$new_comment->user_avatar = (!is_null($row["avatar"]) && $row["avatar"]!="") ? IMAGE_SERVER.AVATAR_PATH.$row["avatar"] : "";
$new_comment->username = $row["username"];
$new_comment->voto = $row["voto"];
$new_comment->descrizione = $row["descrizione"];
$new_comment->data = $row["data"];
$new_comment->imgs = (!is_null($imgRow)) ? array(IMAGE_SERVER.IMAGES_PATH.$imgRow) : array();
$comment_list[$comment_id] = $new_comment;
} else if (!is_null($imgRow)) {
array_push($comment_list[$comment_id]->imgs, $imgRow);
}
}
return array_values($comment_list);
} catch (PDOException $e) {
echo $e->getMessage();
return null;
}
}
public function editComment($id_comment, $content) {
$query = "UPDATE commenti SET descrizione='".$content."' WHERE id=".$id_comment;
try {
$this->db->exec($query);
$status["return"] = 0;
$status["content"] = $content;
return $status;
} catch (PDOException $e) {
echo $e->getMessage();
$status = createErrorMessage(1, "Errore DB");
return $status;
}
}
public function removeComment($id_comment) {
$query = "DELETE FROM commenti WHERE id=".$id_comment;
try {
$this->db->exec($query);
$status["return"] = 0;
return $status;
} catch (PDOException $e) {
echo $e->getMessage();
$status = createErrorMessage(1, "Errore DB");
return $status;
}
}
public function getImages($id_comment) {
$comment = new Comment();
$query;
$sql = "SELECT img
FROM immagini_commenti
WHERE id_commento=".$id_comment;
try {
$query = $this->db->query($sql);
if (!$query) return null;
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
$comment->imgs = array();
foreach ($rows as $row) {
array_push($comment->imgs, IMAGE_SERVER.IMAGES_PATH.$row["img"]);
}
return $comment;
} catch (PDOException $e) {
echo $e->getMessage();
return null;
}
}
/*public function addImage($id_comment, $img) {
$sql = "INSERT INTO immagini_commenti(id_commento, img)
VALUES (".$id_comment.",'".$img."')";
try {
$this->db->exec($sql);
return 0;
} catch (PDOException $e) {
echo $e->getMessage();
return 1;
}
}*/
public function addImages($id_comment, $imgs) {
try {
foreach($imgs as $img) {
$imgsToInsert[] = '("'.$id_comment.'", "'.$img.'")';
}
$queryImgs = "INSERT INTO immagini_commenti(id_commento, img) VALUES" . implode(',', $imgsToInsert);
//echo "<br/>".$queryImgs."<br/>";
$this->db->exec($queryImgs);
return 0;
} catch (PDOException $e) {
echo $e->getMessage();
return 1;
}
}
public function deleteImage($id_comment, $img) {
$sql = "DELETE FROM immagini_commenti WHERE id_commento=".$id_comment." AND img='".$img."'";
try {
$this->db->exec($sql);
return 0;
} catch (PDOException $e) {
echo $e->getMessage();
return 1;
}
}
}
?>
|