Navigation::getLienPage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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