Completed
Push — master ( 5fd26f...eee259 )
by Anthony
02:08
created

Comment::setComment()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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