Passed
Push — master ( d47a8b...3f1f77 )
by Anthony
02:59
created

Navigation::getLienNavigationPage()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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