Completed
Push — master ( 08510c...96c48f )
by Anthony
02:09
created

AdminComment::getAllComment()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 10
Ratio 50 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 10
loc 20
rs 9.4285
cc 3
eloc 13
nc 2
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A AdminComment::getNbCheckedComment() 0 8 1
1
<?php
2
	namespace modules\comment\admin\controller;
3
	
4
	use core\App;
5
	use modules\comment\app\controller\Comment;
6
	
7
	class AdminComment extends Comment {
8
		public static $router_parameter;
9
		private $values = [];
10
		
11
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
12
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
13
		
14
		
15
		//-------------------------- GETTER ----------------------------------------------------------------------------//
16
		/**
17
		 * @return array
18
		 * function to get values out of the class
19
		 */
20
		public function getValues(){
21
			return ["comment" => $this->values];
22
		}
23
		
24
		/**
25
		 * function that get a list of all table_name wich are in comment module
26
		 */
27
		public function getAllTable() {
28
			$dbc = App::getDb();
29
			
30
			$query = $dbc->select("table_name, ID_in_table")->from("_comment_all")->groupBy("ID_in_table, table_name")->get();
31
			
32
			if ((is_array($query)) && (count($query) > 0)) {
33
				$values = [];
34
				foreach ($query as $obj) {
35
					$values[] = [
36
						"table" => $obj->table_name,
37
						"table_name" => str_replace("_", " ", $obj->table_name),
38
						"id_in_table" => $obj->ID_in_table,
39
						"nb_checked_comment" => $this->getNbCheckedComment($obj->table_name, 1),
40
						"nb_unchecked_comment" => $this->getNbCheckedComment($obj->table_name, 0),
41
					];
42
				}
43
				
44
				$this->setValues($values);
45
			}
46
		}
47
		
48
		/**
49
		 * @param $table_name
50
		 * @param $checked
51
		 * @return int
52
		 * function that get number of checked or unchecked comment for a table_name
53
		 */
54
		private function getNbCheckedComment($table_name, $checked) {
55
			$dbc = App::getDb();
56
			
57
			$query = $dbc->select("ID_comment")->from("_comment_all")->where("table_name", "=", $table_name, "AND")
58
				->where("checked", "=", $checked)->get();
59
			
60
			return count($query);
61
		}
62
		
63
		/**
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($id_in_table) {
69
			$dbc = App::getDb();
70
			
71
			$query = $dbc->select()->from("_comment_all")->where("table_name", "=", self::$router_parameter, "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
						"id_comment" => $obj->ID_comment,
79
						"comment" => $obj->comment,
80
						"checked" => $obj->checked,
81
						"date" => $obj->date,
82
						"pseudo" => $obj->pseudo,
83
						"id_identite" => $obj->ID_identite,
84
					];
85
				}
86
			}
87
			
88
			$this->setValues($values);
89
		}
90
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
91
		
92
		
93
		//-------------------------- SETTER ----------------------------------------------------------------------------//
94
		/**
95
		 * @param $values
96
		 * function to set all values and sort it in future
97
		 */
98
		public function setValues($values) {
99
			$this->values = array_merge($this->values, $values);
100
		}
101
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
102
	}