1
|
|
|
<?php |
2
|
|
|
namespace modules\comment\app\controller; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use core\App; |
6
|
|
|
use core\auth\Membre; |
7
|
|
|
use core\HTML\flashmessage\FlashMessage; |
8
|
|
|
|
9
|
|
|
class Comment { |
10
|
|
|
private $required_connection; |
11
|
|
|
private $check_comment; |
12
|
|
|
private $table; |
13
|
|
|
private $id_in_table; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
18
|
|
|
public function __construct() { |
19
|
|
|
$dbc = App::getDb(); |
20
|
|
|
|
21
|
|
|
$query = $dbc->select()->from("_comment_configuration")->where("ID_configuration", "=", 1)->get(); |
22
|
|
|
|
23
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
24
|
|
|
foreach ($query as $obj) { |
25
|
|
|
$this->required_connection = $obj->required_connection; |
26
|
|
|
$this->check_comment = $obj->check_comment_publish; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
else { |
30
|
|
|
$this->required_connection = 0; |
31
|
|
|
$this->check_comment = 0; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
39
|
|
|
public function getRequiredConnection() { |
40
|
|
|
return $this->required_connection; |
41
|
|
|
} |
42
|
|
|
public function getCheckComment() { |
43
|
|
|
return $this->check_comment; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function getSuccessMessagePublish() { |
47
|
|
|
if ($this->check_comment == 1) { |
48
|
|
|
FlashMessage::setFlash("Your comment was correctly added, it will be displayed on website when an admin validate it", "success"); |
49
|
|
|
} |
50
|
|
|
else { |
51
|
|
|
FlashMessage::setFlash("Your comment was correctly added", "success"); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $values |
57
|
|
|
* @return string |
58
|
|
|
* this function will get the view of list comments and form to write a comment |
59
|
|
|
*/ |
60
|
|
|
private function getRender($values) { |
61
|
|
|
ob_start(); |
62
|
|
|
foreach ($values as $value) { |
63
|
|
|
require(MODULEROOT."comment/app/views/list-comment.php"); |
64
|
|
|
} |
65
|
|
|
require(MODULEROOT."comment/app/views/write-comment.php"); |
66
|
|
|
$comments = ob_get_clean(); |
67
|
|
|
|
68
|
|
|
return $comments; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $table |
73
|
|
|
* @param $id_in_table |
74
|
|
|
* function wich get all comments of an other module like a article of a blog |
75
|
|
|
* after all coments was getted it will call getRender to use twig to render them |
76
|
|
|
*/ |
77
|
|
|
public function getComments($table, $id_in_table) { |
78
|
|
|
$dbc = App::getDb(); |
79
|
|
|
|
80
|
|
|
$query = $dbc->select()->from("_comment_all")->where("table_name", "=", $table, "AND") |
81
|
|
|
->where("ID_in_table", "=", $id_in_table)->get(); |
82
|
|
|
|
83
|
|
|
$values = []; |
84
|
|
|
if (count($query) > 0) { |
85
|
|
|
foreach ($query as $obj) { |
86
|
|
|
$values[] = [ |
87
|
|
|
"comment" => $obj->comment, |
88
|
|
|
"date" => $obj->date, |
89
|
|
|
"pseudo" => $obj->pseudo, |
90
|
|
|
"id_identite" => $obj->ID_identite, |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->table = $table; |
96
|
|
|
$this->id_in_table = $id_in_table; |
97
|
|
|
|
98
|
|
|
return $this->getRender($values); |
99
|
|
|
} |
100
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
|
104
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
105
|
|
|
/** |
106
|
|
|
* @param $table |
107
|
|
|
* @param $id_in_table |
108
|
|
|
* @param $comment |
109
|
|
|
* @param $pseudo |
110
|
|
|
* @return bool |
111
|
|
|
* function to add a comment if pseudo and comment != "" |
112
|
|
|
*/ |
113
|
|
|
public function setComment($table, $id_in_table, $comment, $pseudo) { |
114
|
|
|
$dbc = App::getDb(); |
115
|
|
|
|
116
|
|
|
if (is_int($pseudo)) { |
117
|
|
|
$member = new Membre($pseudo); |
118
|
|
|
$pseudo = $member->getPseudo(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (($pseudo != "") && ($comment != "")) { |
122
|
|
|
$dbc->insert("table_name", $table)->insert("ID_in_table", $id_in_table)->insert("date", date("Y-m-d H:i:s")) |
123
|
|
|
->insert("pseudo", $pseudo)->insert("comment", $comment)->into("_comment_all")->set(); |
124
|
|
|
|
125
|
|
|
$this->getSuccessMessagePublish(); |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
FlashMessage::setFlash("You must enter a pseudo and a comment to publish a comment"); |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
133
|
|
|
|
134
|
|
|
} |