Passed
Push — master ( ddbf02...3bc871 )
by Anthony
03:03
created

Navigation::getSousMenu()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 13
Ratio 61.9 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 21
rs 9.0534
cc 4
eloc 15
nc 2
nop 1
1
<?php
2
	namespace core;
3
	use core\functions\ChaineCaractere;
4
5
	class Navigation {
6
		private $last_ordre;
7
		
8
		
9
		
10
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
11
		/**
12
		 * Navigation constructor.
13
		 * @param null $no_module
14
		 * to init get navigation link (if no module != null) whe only get page it's for admin content gestion pages
15
		 */
16
		public function __construct($no_module = null) {
17
			$dbc = App::getDb();
18
			$navigation = [];
19
			$last_ordre = "";
20
21
			if ($no_module === null) {
22
				$query = $dbc->select()->from("navigation")->orderBy("ordre")->get();
23
			}
24
			else {
25
				$query = $dbc->select()->from("navigation")->where("ID_page", " IS NOT ", "NULL", "", true)->orderBy("ordre")->get();
26
			}
27
28
			if (is_array($query) && (count($query) > 0)) {
29
				foreach ($query as $obj) {
30
					if ($obj->ID_page === null) {
31
						$navigation[] = $this->getLienNavigationModule($obj->ID_module);
32
					}
33
					else {
34
						$navigation[] = $this->getLienNavigationPage($obj->ID_page);
35
					}
36
					$last_ordre = $obj->ordre;
37
				}
38
39
				$this->last_ordre = $last_ordre;
40
				
41
				App::setValues(["navigation" => $navigation]);
42
			}
43
		}
44
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
45
		
46
		
47
		
48
		//-------------------------- GETTER ----------------------------------------------------------------------------//
49
		/**
50
		 * @param $id_page
51
		 * @return array
52
		 * to get the navigation link of pages of the website
53
		 */
54
		private function getLienNavigationPage($id_page) {
55
			$dbc = App::getDb();
56
57
			$query = $dbc->select()->from("navigation")->from("page")->where("page.ID_page", "=", $id_page, "AND")->where("page.affiche", "=", 1, "AND")->where("page.parent", "=", 0, "AND")->where("navigation.ID_page", "=", "page.ID_page", "", true)->get();
58
59
			if (is_array($query) && (count($query) > 0)) {
60
				$nav = [];
61
				foreach ($query as $obj) {
62
					$nav = [
63
						"id" => $obj->ID_page,
64
						"titre" => $obj->titre,
65
						"lien_page" => $this->getLienPage($obj->url),
66
						"url" => $obj->url,
67
						"balise_title" => $obj->balise_title,
68
						"sous_menu" => $this->getSousMenu($id_page),
69
						"type" => "page",
70
						"target" => $obj->target,
71
					];
72
				}
73
				return $nav;
74
			}
75
		}
76
77
		/**
78
		 * @param $id_page
79
		 * @return array
80
		 * to get the sub navigation of a page which have it
81
		 */
82
		private function getSousMenu($id_page) {
83
			$dbc = App::getDb();
84
			$sous_menu = [];
85
86
			$query = $dbc->select()->from("page")->where("parent", "=", $id_page, "AND")->where("affiche", "=", 1)->get();
87
88 View Code Duplication
			if (is_array($query) && (count($query) > 0)) {
0 ignored issues
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...
89
				foreach ($query as $obj) {
90
					$sous_menu[] = [
91
						"id" => $obj->ID_page,
92
						"titre" => $obj->titre,
93
						"lien_page" => $this->getLienPage($obj->url),
94
						"url" => $obj->url,
95
						"balise_title" => $obj->balise_title,
96
						"type" => "page",
97
						"target" => $obj->target,
98
					];
99
				}
100
			}
101
			return $sous_menu;
102
		}
103
104
		/**
105
		 * @param $id_module
106
		 * @return array
107
		 * to get the navigation link of modules of the website
108
		 */
109
		private function getLienNavigationModule($id_module) {
110
			$dbc = App::getDb();
111
112
			$query = $dbc->select()->from("navigation")->from("module")->where("module.ID_module", "=", $id_module, "AND")->where("module.installer", "=", 1, "AND")->where("module.activer", "=", 1, "AND")->where("navigation.ID_module", "=", "module.ID_module", "", true)->get();
113
114 View Code Duplication
			if (is_array($query) && (count($query) > 0)) {
0 ignored issues
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...
115
				foreach ($query as $obj) {
116
					$nav = [
117
						"id" => $obj->ID_module,
118
						"titre" => $obj->nom_module,
119
						"lien_page" => $this->getLienPage($obj->url),
120
						"balise_title" => $obj->nom_module,
121
						"type" => "module",
122
						"target" => $obj->target,
123
					];
124
					return $nav;
125
				}
126
			}
127
		}
128
129
		/*to verify if page exist*/
130
		private function getLienPageExist($id_page) {
131
			$dbc = App::getDb();
132
133
			$query = $dbc->select("ID_page")->from("navigation")->where("ID_page", "=", $id_page)->get();
134
135
			if (count($query) < 1) {
136
				return false;
137
			}
138
139
			return true;
140
		}
141
142
		private function getLienPage($url) {
143
			if (ChaineCaractere::FindInString($url, "http://")) {
144
				return $url;
145
			}
146
			else {
147
				return WEBROOT.$url;
148
			}
149
		}
150
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
151
		
152
		
153
		
154
		//-------------------------- SETTER ----------------------------------------------------------------------------//
155
		/**
156
		 * @param $id
157
		 * @param $value_id
158
		 * to add a link in navigation table
159
		 */
160
		public function setAjoutLien($id, $value_id) {
161
			$dbc = App::getDb();
162
163
			if ($this->getLienPageExist($id) === false) {
164
				$dbc->insert($id, $value_id)->insert("ordre", $this->last_ordre + 1)->into("navigation")->set();
165
			}
166
		}
167
168
		/**
169
		 * @param $id
170
		 * @param $value_id
171
		 * to delete a link in navigation table
172
		 */
173
		public function setSupprimerLien($id, $value_id) {
174
			$dbc = App::getDb();
175
176
			$dbc->delete()->from("navigation")->where($id, "=", $value_id)->del();
177
		}
178
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
179
	}