Comment   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 19
c 5
b 0
f 1
lcom 2
cbo 0
dl 0
loc 142
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A getRequiredConnection() 0 3 1
A getCheckComment() 0 3 1
A getSuccessMessagePublish() 0 8 2
A getCheckPublishComment() 0 7 2
A getRender() 0 10 2
A getComments() 0 23 3
A setComment() 0 20 4
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
		/**
47
		 * function to display message after comment creation in bdd
48
		 */
49
		private function getSuccessMessagePublish() {
50
			if ($this->check_comment == 1) {
51
				FlashMessage::setFlash("Your comment was correctly added, it will be displayed on website when an admin validate it", "success");
52
			}
53
			else {
54
				FlashMessage::setFlash("Your comment was correctly added", "success");
55
			}
56
		}
57
		
58
		/**
59
		 * @return int
60
		 * if check_comment unable return 1 to automaticly check a comment
61
		 */
62
		private function getCheckPublishComment() {
63
			if ($this->check_comment == 0) {
64
				return 1;
65
			}
66
			
67
			return 0;
68
		}
69
		
70
		/**
71
		 * @param $values
72
		 * @return string
73
		 * this function will get the view of list comments and form to write a comment
74
		 */
75
		private function getRender($values) {
76
			ob_start();
77
			foreach ($values as $value) {
78
				require(MODULEROOT."comment/app/views/list-comment.php");
79
			}
80
			require(MODULEROOT."comment/app/views/write-comment.php");
81
			$comments = ob_get_clean();
82
			
83
			return $comments;
84
		}
85
		
86
		/**
87
		 * @param $table
88
		 * @param $id_in_table
89
		 * function wich get all comments of an other module like a article of a blog
90
		 * after all coments was getted it will call getRender to use twig to render them
91
		 */
92
		public function getComments($table, $id_in_table) {
93
			$dbc = App::getDb();
94
			
95
			$query = $dbc->select()->from("_comment_all")->where("table_name", "=", $table, "AND")
96
				->where("ID_in_table", "=", $id_in_table, "AND")->where("checked", "=", 1)->get();
97
			
98
			$values = [];
99
			if (count($query) > 0) {
100
				foreach ($query as $obj) {
101
					$values[] = [
102
						"comment" => $obj->comment,
103
						"date" => $obj->date,
104
						"pseudo" => $obj->pseudo,
105
						"id_identite" => $obj->ID_identite,
106
					];
107
				}
108
			}
109
			
110
			$this->table = $table;
111
			$this->id_in_table = $id_in_table;
112
			
113
			return $this->getRender($values);
114
		}
115
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
116
		
117
		
118
		
119
		//-------------------------- SETTER ----------------------------------------------------------------------------//
120
		/**
121
		 * @param $table
122
		 * @param $id_in_table
123
		 * @param $comment
124
		 * @param $pseudo
125
		 * @return bool
126
		 * function to add a comment if pseudo and comment != ""
127
		 */
128
		public function setComment($table, $id_in_table, $comment, $pseudo) {
129
			$dbc = App::getDb();
130
			$id_identite = null;
131
			
132
			if (is_numeric($pseudo)) {
133
				$member = new Membre($pseudo);
134
				$pseudo = $member->getPseudo();
135
				$id_identite = $member->getIdidentite();
136
			}
137
			
138
			if (($pseudo != "") && ($comment != "")) {
139
				$dbc->insert("table_name", $table)->insert("ID_in_table", $id_in_table)->insert("date", date("Y-m-d H:i:s"))
140
					->insert("pseudo", $pseudo)->insert("comment", $comment)->into("_comment_all")->insert("checked", $this->getCheckPublishComment())
141
					->insert("ID_identite", $id_identite)->set();
142
				$this->getSuccessMessagePublish();
143
				return true;
144
			}
145
			FlashMessage::setFlash("You must enter a pseudo and a comment to publish a comment");
146
			return false;
147
		}
148
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
149
		
150
	}