Subversion Repository Public Repository

Nextrek

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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<?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 getComment($id_user, $id_local) {
			$status = array();

			/* ============= SUPPORTO IMMAGINI NEI COMMENTI DROPPATO =============== */

			/*$queryComment = "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 id_user=".$id_user." AND id_locale=".$id_local;*/

            /* =========== FINE SUPPORTO IMMAGINI NEI COMMENTI DROPPATO ============= */

            /*$queryComment = "SELECT c.id AS id_commento, id_locale, id_user, voto, descrizione, data, username, u.avatar as avatar
								FROM commenti as c
                                LEFT JOIN utente as u ON c.id_user=u.id
                                WHERE id_user=".$id_user." AND id_locale=".$id_local;*/

            //echo "<br/>".$queryComment."<br/>";

            $queryComment = "SELECT c.id AS id_commento, id_locale, id_user, voto, descrizione, data, username, u.avatar as avatar
								FROM commenti as c
                                LEFT JOIN utente as u ON c.id_user=u.id
                                WHERE id_user=:id_user AND id_locale=:id_local";

            $comment;
            
			try {

				$stmt = $this->db->prepare($queryComment);
	    		$stmt->bindParam(':id_user', $id_user);
				$stmt->bindParam(':id_local', $id_local);
				$query = $stmt->execute();

	            //$query = $this->db->query($queryComment);

				if (!$query) {
					$status = createErrorMessage(1, "Errore DB");
					$status["comment"] = null;
					return $status;
				}

				//$row = ($query->rowCount()>0) ? $query->fetch() : null;
				$row = ($stmt->rowCount()>0) ? $stmt->fetch() : null;

				if (is_null($row)) {
					$status["return"] = 0;
					$status["comment"] = null;
					return $status;
				}

				//$comment_id = $row["id_commento"];
				
				/* ============= SUPPORTO IMMAGINI NEI COMMENTI DROPPATO =============== */

				/*$imgRow = (!is_null($row["img"])) ? IMAGE_SERVER.IMAGES_PATH.$row["img"] : null;

				if (!isset($comment)) {
					$comment = new Comment();
					$comment->id 			= $row["id_commento"];
					$comment->id_locale 	= $id_local;
					$comment->id_user 		= (!is_null($row["id_user"]) && $row["id_user"]!="") ? $row["id_user"] : -1;
					$comment->user_avatar	= (!is_null($row["avatar"]) && $row["avatar"]!="") ? IMAGE_SERVER.AVATAR_PATH.$row["avatar"] : "";
					$comment->username 		= $row["username"];
					$comment->voto 			= $row["voto"];
					$comment->descrizione 	= $row["descrizione"];
					$comment->data 			= $row["data"];
					$comment->imgs 			= (!is_null($imgRow)) ? array($imgRow) : array();
				} else if (!is_null($imgRow)) {
					array_push($comment->imgs, $imgRow);
				}*/

				/* =========== FINE SUPPORTO IMMAGINI NEI COMMENTI DROPPATO ============= */

				$comment = new Comment();
				$comment->id 			= $row["id_commento"];
				$comment->id_locale 	= $id_local;
				$comment->id_user 		= (!is_null($row["id_user"]) && $row["id_user"]!="") ? $row["id_user"] : -1;
				$comment->user_avatar	= (!is_null($row["avatar"]) && $row["avatar"]!="") ? IMAGE_SERVER.AVATAR_PATH.$row["avatar"] : "";
				$comment->username 		= $row["username"];
				$comment->voto 			= $row["voto"];
				$comment->descrizione 	= $row["descrizione"];
				$comment->data 			= $row["data"];

				$status["return"] = 0;
				$status["comment"] = $comment;
				return $status;

			} catch (PDOException $e) {
				echo $e->getMessage();
				$status = createErrorMessage(1, "Errore DB");
				$status["comment"] = null;
				return $status;
			}

		}


		public function getCommentById($id_comment) {
			$status = array();

            $queryComment = "SELECT c.id AS id_commento, id_locale, id_user, voto, descrizione, data, username, u.avatar as avatar
								FROM commenti as c
                                LEFT JOIN utente as u ON c.id_user=u.id
                                WHERE c.id=:id_commento";

            $comment;
            
			try {

				$stmt = $this->db->prepare($queryComment);
				$stmt->bindParam(':id_commento', $id_comment);
				$query = $stmt->execute();

	            //$query = $this->db->query($queryComment);

				if (!$query) {
					$status = createErrorMessage(1, "Errore DB");
					$status["comment"] = null;
					return $status;
				}

				//$row = ($query->rowCount()>0) ? $query->fetch() : null;
				$row = ($stmt->rowCount()>0) ? $stmt->fetch() : null;

				if (is_null($row)) {
					$status["return"] = 0;
					$status["comment"] = null;
					return $status;
				}

				//$comment_id = $row["id_commento"];
				
				/* ============= SUPPORTO IMMAGINI NEI COMMENTI DROPPATO =============== */

				/*$imgRow = (!is_null($row["img"])) ? IMAGE_SERVER.IMAGES_PATH.$row["img"] : null;

				if (!isset($comment)) {
					$comment = new Comment();
					$comment->id 			= $row["id_commento"];
					$comment->id_locale 	= $id_local;
					$comment->id_user 		= (!is_null($row["id_user"]) && $row["id_user"]!="") ? $row["id_user"] : -1;
					$comment->user_avatar	= (!is_null($row["avatar"]) && $row["avatar"]!="") ? IMAGE_SERVER.AVATAR_PATH.$row["avatar"] : "";
					$comment->username 		= $row["username"];
					$comment->voto 			= $row["voto"];
					$comment->descrizione 	= $row["descrizione"];
					$comment->data 			= $row["data"];
					$comment->imgs 			= (!is_null($imgRow)) ? array($imgRow) : array();
				} else if (!is_null($imgRow)) {
					array_push($comment->imgs, $imgRow);
				}*/

				/* =========== FINE SUPPORTO IMMAGINI NEI COMMENTI DROPPATO ============= */

				$comment = new Comment();
				$comment->id 			= $row["id_commento"];
				$comment->id_locale 	= $row["id_locale"];
				$comment->id_user 		= (!is_null($row["id_user"]) && $row["id_user"]!="") ? $row["id_user"] : -1;
				$comment->user_avatar	= (!is_null($row["avatar"]) && $row["avatar"]!="") ? IMAGE_SERVER.AVATAR_PATH.$row["avatar"] : "";
				$comment->username 		= $row["username"];
				$comment->voto 			= $row["voto"];
				$comment->descrizione 	= $row["descrizione"];
				$comment->data 			= $row["data"];

				$status["return"] = 0;
				$status["comment"] = $comment;
				return $status;

			} catch (PDOException $e) {
				echo $e->getMessage();
				$status = createErrorMessage(1, "Errore DB");
				$status["comment"] = null;
				return $status;
			}

		}

		public function addComment($comment) {
			$status = array();

			$toUpdate = false;

			$previousComment = $this->getComment($comment->id_user, $comment->id_locale);

			if ($previousComment["return"] != 0) { //errore nella getComment()
				return $previousComment;
			} /*else if ($previousComment["return"] == 0 && !is_null($previousComment["comment"])) {
				$status = createErrorMessage(1, "Commento già inserito dall'utente");
				$status["id"] = -1;
				return $status;
			}*/

			$stmt;

			try {

				if ($previousComment["return"] == 0 && !is_null($previousComment["comment"])) { //commento da sovrascrivere
					/*$queryComment = "UPDATE commenti
									SET descrizione='".$comment->descrizione."',voto='".$comment->voto."', data='".$comment->data."'
									WHERE id=".$previousComment["comment"]->id;*/

					$queryComment = "UPDATE commenti
									SET descrizione=:descrizione,voto=:voto, data=:data
									WHERE id=:id";

					$stmt = $this->db->prepare($queryComment);
	    			$stmt->bindParam(':descrizione', $comment->descrizione);
					$stmt->bindParam(':voto', $comment->voto);
					$stmt->bindParam(':data', $comment->data);
					$stmt->bindParam(':id', $previousComment["comment"]->id);

					$toUpdate = true;
				} else {
					/*$queryComment = "INSERT INTO commenti(id_locale, id_user, voto, descrizione, data)
	    				VALUES ('".$comment->id_locale."',
	    						'".$comment->id_user."',
	    						'".$comment->voto."',
	    						'".$comment->descrizione."',
	    						'".$comment->data."')";*/

					$queryComment = "INSERT INTO commenti(id_locale, id_user, voto, descrizione, data)
	    				VALUES (:id_locale, :id_user, :voto, :descrizione, :data)";

					$stmt = $this->db->prepare($queryComment);
					$stmt->bindParam(':id_locale', $comment->id_locale);
					$stmt->bindParam(':id_user', $comment->id_user);
					$stmt->bindParam(':voto', $comment->voto);
	    			$stmt->bindParam(':descrizione', $comment->descrizione);
					$stmt->bindParam(':data', $comment->data);
				}

				//echo "<br/>".$queryComment."<br/>";

				/*$imgs = $comment->imgs;
				$imgsToInsert = array();*/

				$query = $stmt->execute();

				//$this->db->exec($queryComment);
				$id_commento = ($toUpdate) ? $previousComment["comment"]->id : $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;

			/* ============= SUPPORTO IMMAGINI NEI COMMENTI DROPPATO =============== */

			/*$sqlComments = "SELECT id
							FROM commenti
							WHERE id_locale=".$id_local."
							LIMIT ".$start_index.",".$length;*/

			/* =========== FINE SUPPORTO IMMAGINI NEI COMMENTI DROPPATO ============= */

			/*$sqlComments = "SELECT c.id AS id_commento, id_locale, id_user, voto, descrizione, data, username, u.avatar as avatar
								FROM commenti as c
                                LEFT JOIN utente as u ON c.id_user=u.id
                                WHERE c.id_locale=".$id_local."
                                ORDER BY c.data ".$data_order."
                                LIMIT ".$start_index.",".$length;*/

			//echo "<br/>".$sqlComments."<br/>";

            $sqlComments = "SELECT c.id AS id_commento, id_locale, id_user, voto, descrizione, data, username, u.avatar as avatar
								FROM commenti as c
                                LEFT JOIN utente as u ON c.id_user=u.id
                                WHERE c.id_locale=:id_local
                                ORDER BY c.data $data_order
                                LIMIT :start_index, :length";



			try {
				$stmt = $this->db->prepare($sqlComments);
				$stmt->bindParam(':id_local', $id_local);
				$stmt->bindParam(':start_index', intval($start_index), PDO::PARAM_INT);
				$stmt->bindParam(':length', intval($length), PDO::PARAM_INT);
				$query = $stmt->execute();

				//$query = $this->db->query($sqlComments);

				if (!$query || $stmt->rowCount()==0) return $comment_list;

				$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

				/* ============= SUPPORTO IMMAGINI NEI COMMENTI DROPPATO =============== */


				/*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"])) ? IMAGE_SERVER.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($imgRow) : array();

						$comment_list[$comment_id] = $new_comment;
					} else if (!is_null($imgRow)) {
						array_push($comment_list[$comment_id]->imgs, $imgRow);
					}

				} */

				/* =========== FINE SUPPORTO IMMAGINI NEI COMMENTI DROPPATO ============= */

				foreach ($rows as $row) {
					$comment_id = $row["id_commento"];

					$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"];

					array_push($comment_list, $new_comment);
				}

				//return array_values($comment_list);
				return $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;
			$query = "UPDATE commenti SET descrizione=:descrizione WHERE id=:id";
			try {
				$stmt = $this->db->prepare($query);
				$stmt->bindParam(':id', $id_comment);
				$stmt->bindParam(':descrizione', $content);
				$stmt->execute();

				//$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;
			$query = "DELETE FROM commenti WHERE id=:id";
			try {
				$stmt = $this->db->prepare($query);
				$stmt->bindParam(':id', $id_comment);
				$stmt->execute();

				//$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;*/

			$sql = "SELECT img FROM immagini_commenti WHERE id_commento=:id_commento";

			try {
				$stmt = $this->db->prepare($sql);
				$stmt->bindParam(':id_commento', $id_comment);
				$query = $stmt->execute();

				//$query = $this->db->query($sql);

				if (!$query) return null;

				$rows = $stmt->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."'";
			$sql = "DELETE FROM immagini_commenti WHERE id_commento=:id_commento AND img=:img";

			try {
				$stmt = $this->db->prepare($sql);
				$stmt->bindParam(':id_commento', $id_comment);
				$stmt->bindParam(':img', $img);
				$stmt->execute();
				//$this->db->exec($sql);
				return 0;
			} catch (PDOException $e) {
				echo $e->getMessage();
				return 1;
			}
		}

	}

?>

Commits for Nextrek/Android/SmartCharging/endPoints/nightly/repositories/CommentRepository.php

Diff revisions: vs.
Revision Author Commited Message
723 Diff Diff FSallustio picture FSallustio Tue 08 Sep, 2015 15:05:25 +0000

Aggiunto check sull’associazione tra utente e token.

721 Diff Diff FSallustio picture FSallustio Tue 08 Sep, 2015 13:23:54 +0000
716 Diff Diff FSallustio picture FSallustio Tue 08 Sep, 2015 08:29:01 +0000

[DA TESTARE] Utilizzo dei prepared statement nelle PDO, anziché le query dirette da stringa.

706 Diff Diff FSallustio picture FSallustio Mon 07 Sep, 2015 15:50:07 +0000
700 Diff Diff FSallustio picture FSallustio Mon 07 Sep, 2015 14:11:24 +0000
695 Diff Diff FSallustio picture FSallustio Mon 07 Sep, 2015 10:31:36 +0000

Implementata chiamata get_commento.php

685 Diff Diff FSallustio picture FSallustio Fri 04 Sep, 2015 14:52:57 +0000
683 Diff Diff FSallustio picture FSallustio Fri 04 Sep, 2015 14:43:53 +0000
682 Diff Diff FSallustio picture FSallustio Fri 04 Sep, 2015 14:42:15 +0000
639 FSallustio picture FSallustio Tue 01 Sep, 2015 12:16:12 +0000