Passed
Push — master ( 416fc4...96bf24 )
by Anthony
03:07
created

Navigation::getLienPage()   A

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 $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
61
			$query = $dbc->select()
62
				->from("navigation")
63
				->from("page")
64
				->where("page.ID_page", "=", $id_page, "AND")
65
				->where("page.affiche", "=", 1, "AND")
66
				->where("page.parent", "=", 0, "AND")
67
				->where("navigation.ID_page", "=", "page.ID_page", "", true)
68
				->get();
69
70 View Code Duplication
			if (is_array($query) && (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...
71
				foreach ($query as $obj) {
72
					return [$obj->ID_page, $obj->titre, $this->getLienPage($obj->url), $obj->balise_title, "page", $obj->target, $this->getSousMenu($id_page)];
73
				}
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()
87
				->from("page")
88
				->where("parent", "=", $id_page, "AND")
89
				->where("affiche", "=", 1)
90
				->get();
91
92 View Code Duplication
			if (is_array($query) && (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...
93
				foreach ($query as $obj) {
94
					$sous_menu[] = [$obj->ID_page, $obj->titre, $this->getLienPage($obj->url), $obj->balise_title, "page", $obj->target];
95
				}
96
			}
97
98
			return $sous_menu;
99
		}
100
101
		/**
102
		 * @param $id_module
103
		 * @return array
104
		 * to get the navigation link of modules of the website
105
		 */
106
		private function getLienNavigationModule($id_module) {
107
			$dbc = App::getDb();
108
109
			$query = $dbc->select()
110
				->from("navigation")
111
				->from("module")
112
				->where("module.ID_module", "=", $id_module, "AND")
113
				->where("module.installer", "=", 1, "AND")
114
				->where("module.activer", "=", 1, "AND")
115
				->where("navigation.ID_module", "=", "module.ID_module", "", true)
116
				->get();
117
118
			if (is_array($query) && (count($query) > 0)) {
119
				foreach ($query as $obj) {
120
					return [$obj->ID_module, $obj->nom_module, $this->getLienPage($obj->url), $obj->nom_module, "module"];
121
				}
122
			}
123
		}
124
125
		/*to verify if page exist*/
126
		private function getLienPageExist($id_page) {
127
			$dbc = App::getDb();
128
129
			$query = $dbc->select("ID_page")->from("navigation")->where("ID_page", "=", $id_page)->get();
130
131
			if (count($query) < 1) {
132
				return false;
133
			}
134
135
			return true;
136
		}
137
138
		private function getLienPage($url) {
139
			if (ChaineCaractere::FindInString($url, "http://")) {
140
				return $url;
141
			}
142
			else {
143
				return WEBROOT.$url;
144
			}
145
		}
146
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
147
		
148
		
149
		
150
		//-------------------------- SETTER ----------------------------------------------------------------------------//
151
		private function setNavigation($navigation) {
152
			$this->navigation = $navigation;
153
		}
154
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
	}