Passed
Push — master ( 1faf28...95951e )
by Anthony
02:22
created

Navigation::__construct()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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