Passed
Push — master ( dd3b74...86e6da )
by Anthony
04:19
created

Navigation::getLienNavigationModule()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 14
Ratio 73.68 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 19
rs 9.2
cc 4
eloc 13
nc 3
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
			$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();
57
			if (is_array($query) && (count($query) > 0)) {
58
				$nav = [];
59
				foreach ($query as $obj) {
60
					$nav = [
61
						"id" => $obj->ID_page,
62
						"titre" => $obj->titre,
63
						"lien_page" => $this->getLienPage($obj->url),
64
						"url" => $obj->url,
65
						"balise_title" => $obj->balise_title,
66
						"sous_menu" => $this->getSousMenu($id_page),
67
						"type" => "page",
68
						"target" => $obj->target,
69
					];
70
				}
71
				return $nav;
72
			}
73
		}
74
75
		/**
76
		 * @param $id_page
77
		 * @return array
78
		 * to get the sub navigation of a page which have it
79
		 */
80
		private function getSousMenu($id_page) {
81
			$dbc = App::getDb();
82
			$sous_menu = [];
83
84
			$query = $dbc->select()->from("page")->where("parent", "=", $id_page, "AND")->where("affiche", "=", 1)->get();
85
86 View Code Duplication
			if (is_array($query) && (count($query) > 0)) {
87
				foreach ($query as $obj) {
88
					$sous_menu[] = [
89
						"id" => $obj->ID_page,
90
						"titre" => $obj->titre,
91
						"lien_page" => $this->getLienPage($obj->url),
92
						"url" => $obj->url,
93
						"balise_title" => $obj->balise_title,
94
						"type" => "page",
95
						"target" => $obj->target,
96
					];
97
				}
98
			}
99
			return $sous_menu;
100
		}
101
102
		/**
103
		 * @param $id_module
104
		 * @return array
105
		 * to get the navigation link of modules of the website
106
		 */
107
		private function getLienNavigationModule($id_module) {
108
			$dbc = App::getDb();
109
110
			$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();
111
112 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...
113
				foreach ($query as $obj) {
114
					$nav = [
115
						"id" => $obj->ID_module,
116
						"titre" => $obj->nom_module,
117
						"lien_page" => $this->getLienPage($obj->url),
118
						"balise_title" => $obj->nom_module,
119
						"type" => "module",
120
						"target" => $obj->target,
121
					];
122
					return $nav;
123
				}
124
			}
125
		}
126
127
		/*to verify if page exist*/
128
		private function getLienPageExist($id_page) {
129
			$dbc = App::getDb();
130
131
			$query = $dbc->select("ID_page")->from("navigation")->where("ID_page", "=", $id_page)->get();
132
133
			if (count($query) < 1) {
134
				return false;
135
			}
136
137
			return true;
138
		}
139
140
		private function getLienPage($url) {
141
			if (ChaineCaractere::FindInString($url, "http://")) {
142
				return $url;
143
			}
144
			else {
145
				return WEBROOT.$url;
146
			}
147
		}
148
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
149
		
150
		
151
		
152
		//-------------------------- SETTER ----------------------------------------------------------------------------//
153
		/**
154
		 * @param $id
155
		 * @param $value_id
156
		 * to add a link in navigation table
157
		 */
158
		public function setAjoutLien($id, $value_id) {
159
			$dbc = App::getDb();
160
161
			if ($this->getLienPageExist($id) === false) {
162
				$dbc->insert($id, $value_id)->insert("ordre", $this->last_ordre+1)->into("navigation")->set();
163
			}
164
		}
165
166
		/**
167
		 * @param $id
168
		 * @param $value_id
169
		 * to delete a link in navigation table
170
		 */
171
		public function setSupprimerLien($id, $value_id) {
172
			$dbc = App::getDb();
173
174
			$dbc->delete()->from("navigation")->where($id, "=", $value_id)->del();
175
		}
176
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
177
	}