AdminComment::setValues()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
	namespace modules\comment\admin\controller;
3
	
4
	use core\App;
5
	use core\HTML\flashmessage\FlashMessage;
6
	use modules\comment\app\controller\Comment;
7
	
8
	class AdminComment extends Comment {
9
		public static $router_parameter;
10
		private $values = [];
11
		
12
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
13
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
14
		
15
		
16
		//-------------------------- GETTER ----------------------------------------------------------------------------//
17
		/**
18
		 * @return array
19
		 * function to get values out of the class
20
		 */
21
		public function getValues(){
22
			return ["comment" => $this->values];
23
		}
24
		
25
		/**
26
		 * function that get a list of all table_name wich are in comment module
27
		 */
28
		public function getAllTable() {
29
			$dbc = App::getDb();
30
			
31
			$query = $dbc->select("table_name, ID_in_table")->from("_comment_all")->groupBy("ID_in_table, table_name")->get();
32
			
33
			if ((is_array($query)) && (count($query) > 0)) {
34
				$values = [];
35
				foreach ($query as $obj) {
36
					$values[] = [
37
						"table" => $obj->table_name,
38
						"table_name" => str_replace("_", " ", $obj->table_name),
39
						"id_in_table" => $obj->ID_in_table,
40
						"nb_checked_comment" => $this->getNbCheckedComment($obj->table_name, 1),
41
						"nb_unchecked_comment" => $this->getNbCheckedComment($obj->table_name, 0),
42
					];
43
				}
44
				
45
				$this->setValues($values);
46
			}
47
		}
48
		
49
		/**
50
		 * @param $table_name
51
		 * @param $checked
52
		 * @return int
53
		 * function that get number of checked or unchecked comment for a table_name
54
		 */
55
		private function getNbCheckedComment($table_name, $checked) {
56
			$dbc = App::getDb();
57
			
58
			$query = $dbc->select("ID_comment")->from("_comment_all")->where("table_name", "=", $table_name, "AND")
59
				->where("checked", "=", $checked)->get();
60
			
61
			return count($query);
62
		}
63
		
64
		/**
65
		 * @param $id_in_table
66
		 * function wich get all comments of an other module like a article of a blog
67
		 * after all coments was getted it will call getRender to use twig to render them
68
		 */
69
		public function getComments($id_in_table) {
70
			$dbc = App::getDb();
71
			
72
			$query = $dbc->select()->from("_comment_all")->where("table_name", "=", self::$router_parameter, "AND")
73
				->where("ID_in_table", "=", $id_in_table)->get();
74
			
75
			$values = [];
76
			if (count($query) > 0) {
77
				foreach ($query as $obj) {
78
					$values[] = [
79
						"id_comment" => $obj->ID_comment,
80
						"comment" => $obj->comment,
81
						"checked" => $obj->checked,
82
						"date" => $obj->date,
83
						"pseudo" => $obj->pseudo,
84
						"id_identite" => $obj->ID_identite,
85
					];
86
				}
87
			}
88
			
89
			$this->setValues($values);
90
		}
91
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
92
		
93
		
94
		//-------------------------- SETTER ----------------------------------------------------------------------------//
95
		/**
96
		 * @param $values
97
		 * function to set all values and sort it in future
98
		 */
99
		public function setValues($values) {
100
			$this->values = array_merge($this->values, $values);
101
		}
102
		
103
		/**
104
		 * @param $id_comment
105
		 * @param $checked
106
		 * function that it is used to change if a comment is changed or not
107
		 */
108
		public function setChangeCheck($id_comment, $checked) {
109
			$dbc = App::getDb();
110
			
111
			$dbc->update("checked", $checked)->from("_comment_all")->where("ID_comment", "=", $id_comment)->set();
112
			FlashMessage::setFlash("The status of the comment was correctly changed", "success");
113
		}
114
		
115
		/**
116
		 * @param $id_comment
117
		 * @param $table
118
		 * function that is used to delete a comment
119
		 */
120
		public function setDeleteComment($id_comment, $table) {
121
			$dbc = App::getDb();
122
			$explode = explode("/", $table);
123
			$table = end($explode);
124
			
125
			$dbc->delete()->from("_comment_all")->where("ID_comment", "=", $id_comment, "AND")
126
				->where("table_name", "=", $table)->del();
127
			
128
			FlashMessage::setFlash("Comment was correctly deleted", "success");
129
		}
130
		
131
		/**
132
		 * @param $parameter
133
		 * @param $checked
134
		 * function that checck or uncheck a parameter in configuration
135
		 */
136
		public function setChangeConfiguration($parameter, $checked) {
137
			$dbc = App::getDb();
138
			
139
			$dbc->update($parameter, $checked)->from("_comment_configuration")->where("ID_configuration", "=", 1)->set();
140
		
141
			FlashMessage::setFlash("The configuration was correctly updated", "success");
142
		}
143
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
144
	}