Completed
Push — master ( eb5763...da62cd )
by Anthony
02:23
created

AdminComment::getAllTable()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 12
nc 3
nop 0
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
		private $values = [];
9
		
10
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
11
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
12
		
13
		
14
		//-------------------------- GETTER ----------------------------------------------------------------------------//
15
		/**
16
		 * @return array
17
		 * function to get values out of the class
18
		 */
19
		public function getValues(){
20
			return ["comment" => $this->values];
21
		}
22
		
23
		/**
24
		 * function that get a list of all table_name wich are in comment module
25
		 */
26
		public function getAllTable() {
27
			$dbc = App::getDb();
28
			
29
			$query = $dbc->select("table_name, ID_in_table")->from("_comment_all")->groupBy("ID_in_table, table_name")->get();
30
			
31
			if ((is_array($query)) && (count($query) > 0)) {
32
				$values = [];
33
				foreach ($query as $obj) {
34
					$values[] = [
35
						"table_name" => str_replace("_", " ", $obj->table_name),
36
						"id_in_table" => $obj->ID_in_table,
37
						"nb_checked_comment" => $this->getNbCheckedComment($obj->table_name, 1),
38
						"nb_unchecked_comment" => $this->getNbCheckedComment($obj->table_name, 0),
39
					];
40
				}
41
				
42
				$this->setValues($values);
43
			}
44
		}
45
		
46
		/**
47
		 * @param $table
48
		 * @param $id_in_table
49
		 * function to get all comments of a table with an id
50
		 */
51
		public function getAllComment($table, $id_in_table) {
52
			$dbc = App::getDb();
53
			
54
			$query = $dbc->select()->from("_comment_all")->where("table_name", "=", $table, "AND")
55
				->where("ID_in_table", "=", $id_in_table)->orderBy("checked", "DESC")->get();
56
			
57
			$values = [];
58 View Code Duplication
			if (count($query) > 0) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
				foreach ($query as $obj) {
60
					$values[] = [
61
						"comment" => $obj->comment,
62
						"date" => $obj->date,
63
						"pseudo" => $obj->pseudo,
64
						"id_identite" => $obj->ID_identite,
65
					];
66
				}
67
			}
68
			
69
			$this->setValues($values);
70
		}
71
		
72
		/**
73
		 * @param $table_name
74
		 * @param $checked
75
		 * @return int
76
		 * function that get number of checked or unchecked comment for a table_name
77
		 */
78
		private function getNbCheckedComment($table_name, $checked) {
79
			$dbc = App::getDb();
80
			
81
			$query = $dbc->select("ID_comment")->from("_comment_all")->where("table_name", "=", $table_name, "AND")
82
				->where("checked", "=", $checked)->get();
83
			
84
			return count($query);
85
		}
86
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
87
		
88
		
89
		//-------------------------- SETTER ----------------------------------------------------------------------------//
90
		/**
91
		 * @param $values
92
		 * function to set all values and sort it in future
93
		 */
94
		public function setValues($values) {
95
			$this->values = array_merge($this->values, $values);
96
		}
97
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
98
	}