Completed
Push — master ( cb135a...6b08e1 )
by Anthony
02:57
created

Comment::setComment()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 12
nc 4
nop 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
		 * @param $values
48
		 * @return string
49
		 * this function will get the view of list comments and form to write a comment
50
		 */
51
		private function getRender($values) {
52
			ob_start();
53
			foreach ($values as $value) {
54
				require(MODULEROOT."comment/app/views/list-comment.php");
55
			}
56
			require(MODULEROOT."comment/app/views/write-comment.php");
57
			$comments = ob_get_clean();
58
			
59
			return $comments;
60
		}
61
		
62
		/**
63
		 * @param $table
64
		 * @param $id_in_table
65
		 * function wich get all comments of an other module like a article of a blog
66
		 * after all coments was getted it will call getRender to use twig to render them
67
		 */
68
		public function getComments($table, $id_in_table) {
69
			$dbc = App::getDb();
70
			
71
			$query = $dbc->select()->from("_comment_all")->where("table_name", "=", $table, "AND")
72
				->where("ID_in_table", "=", $id_in_table)->get();
73
			
74
			$values = [];
75
			if (count($query) > 0) {
76
				foreach ($query as $obj) {
77
					$values[] = [
78
						"comment" => $obj->comment,
79
						"date" => $obj->date,
80
						"pseudo" => $obj->pseudo,
81
						"id_identite" => $obj->ID_identite,
82
					];
83
				}
84
			}
85
			
86
			$this->table = $table;
87
			$this->id_in_table = $id_in_table;
88
			
89
			return $this->getRender($values);
90
		}
91
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
92
		
93
		
94
		
95
		//-------------------------- SETTER ----------------------------------------------------------------------------//
96
		/**
97
		 * @param $table
98
		 * @param $id_in_table
99
		 * @param $comment
100
		 * @param $pseudo
101
		 * @return bool
102
		 * function to add a comment if pseudo and comment != ""
103
		 */
104
		public function setComment($table, $id_in_table, $comment, $pseudo) {
105
			$dbc = App::getDb();
106
			
107
			if (is_int($pseudo)) {
108
				$member = new Membre($pseudo);
109
				$pseudo = $member->getPseudo();
110
			}
111
			
112
			if (($pseudo != "") && ($comment != "")) {
113
				$dbc->insert("table_name", $table)->insert("ID_in_table", $id_in_table)->insert("date", date("Y-m-d H:i:s"))
114
					->insert("pseudo", $pseudo)->insert("comment", $comment)->into("_comment_all")->set();
115
				
116
				FlashMessage::setFlash("Your comment was correctly added", "success");
117
				return true;
118
			}
119
			
120
			FlashMessage::setFlash("You must enter a pseudo and a comment to publish a comment");
121
			return false;
122
		}
123
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
124
		
125
	}