Passed
Push — master ( 9db407...b7ae74 )
by Anthony
02:58
created

GestionModule::getModuleActiver()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 16
loc 16
rs 8.8571
cc 5
eloc 9
nc 4
nop 1
1
<?php
2
	namespace core\modules;
3
	use core\App;
4
5
	class GestionModule {
6
		private $id_module;
7
		private $url;
8
		private $nom;
9
		private $version;
10
		private $online_version;
11
		private $icone;
12
		private $url_telechargement;
13
		
14
		
15
		//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------//
16
		//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------//
17
		
18
		
19
		//-------------------------- GETTER ----------------------------------------------------------------------------//
20
		public function getIdModule() {
21
			return $this->id_module;
22
		}
23
		public function getUrl() {
24
			return $this->url;
25
		}
26
		public function getNom() {
27
			return $this->nom;
28
		}
29
		public function getVersion() {
30
			return $this->version;
31
		}
32
		public function getOnlineVersion() {
33
			return $this->online_version;
34
		}
35
		public function getIcone() {
36
			return $this->icone;
37
		}
38
		public function getUrlTelechargement() {
39
			return $this->url_telechargement;
40
		}
41
42
		/**
43
		 * récupere la liste des modules activé utilisé pour toutes les pages
44
		 */
45 View Code Duplication
		public function getListeModuleActiver() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
46
			$dbc = App::getDb();
47
48
			$query = $dbc->query("SELECT * FROM module WHERE activer=1 AND installer=1");
49
50
			if ((is_array($query)) && (count($query) > 0)) {
51
				$id_module = [];
52
				$url = [];
53
				$nom = [];
54
				$version = [];
55
				$icone = [];
56
57
				foreach ($query as $obj) {
58
					$id_module[] = $obj->ID_module;
59
					$url[] = $obj->url;
60
					$nom[] = $obj->nom_module;
61
					$version[] = $obj->version;
62
					$icone[] = $obj->icone;
63
				}
64
65
				$this->setListeModuleActiver($id_module, $url, $version, $nom, $icone);
66
			}
67
		}
68
69
		/**
70
		 * recupere la listes des modules ajouter par un autre admin
71
		 * fonction utilisée uniquement dans la config
72
		 */
73 View Code Duplication
		public function getListeModule() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
			$dbc = App::getDb();
75
76
			$query = $dbc->query("SELECT * FROM module WHERE systeme IS NULL");
77
78
			if ((is_array($query)) && (count($query) > 0)) {
79
				$id_module = [];
80
				$url = [];
81
				$nom = [];
82
				$version = [];
83
				$url_telechargement = [];
84
85
				foreach ($query as $obj) {
86
					$id_module[] = $obj->ID_module;
87
					$url[] = $obj->url;
88
					$nom[] = $obj->nom_module;
89
					$version[] = $obj->version;
90
					$url_telechargement[] = $obj->url_telechargement;
91
				}
92
93
				$this->setListeModuleActiver($id_module, $url, $version, $nom, null, $url_telechargement);
94
			}
95
		}
96
97
		/**
98
		 * recupere la listes des modules systeme
99
		 * fonction utilisée uniquement dans la config
100
		 */
101 View Code Duplication
		public function getListeModuleSysteme() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
102
			$dbc = App::getDb();
103
104
			$query = $dbc->query("SELECT * FROM module WHERE systeme = 1");
105
106
			if ((is_array($query)) && (count($query) > 0)) {
107
				$id_module = [];
108
				$url = [];
109
				$nom = [];
110
				$version = [];
111
				$url_telechargement = [];
112
113
				foreach ($query as $obj) {
114
					$id_module[] = $obj->ID_module;
115
					$url[] = $obj->url;
116
					$nom[] = $obj->nom_module;
117
					$version[] = $obj->version;
118
					$url_telechargement[] = $obj->url_telechargement;
119
				}
120
121
				$this->setListeModuleActiver($id_module, $url, $version, $nom, null, $url_telechargement);
122
			}
123
		}
124
125
		/**
126
		 * @param $nom_module
127
		 * @return bool
128
		 * permets de savoir si un module est installé ou non
129
		 */
130
		public static function getModuleInstaller($nom_module) {
131
			$dbc = App::getDb();
132
133
			$query = $dbc->query("SELECT * FROM module WHERE nom_module = ".$dbc->quote($nom_module));
134
135 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...
136
				$installer = 0;
137
138
				foreach ($query as $obj) {
139
					$installer = $obj->installer;
140
				}
141
142
				if ($installer == 1) {
143
					return true;
144
				}
145
				else {
146
					return false;
147
				}
148
			}
149
			else {
150
				return false;
151
			}
152
		}
153
154
		/**
155
		 * @param $nom_module
156
		 * @return boolean|null
157
		 * return true si le module est activer sinon false
158
		 */
159 View Code Duplication
		public static function getModuleActiver($nom_module) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
160
			$dbc = App::getDb();
161
162
			$query = $dbc->query("SELECT activer FROM module WHERE nom_module = ".$dbc->quote($nom_module));
163
164
			if ((is_array($query)) && (count($query) > 0)) {
165
				foreach ($query as $obj) {
166
					if ($obj->activer == 1) {
167
						return true;
168
					}
169
					else {
170
						return false;
171
					}
172
				}
173
			}
174
		}
175
176
		/**
177
		 * @param $nom_module
178
		 * @return boolean|null
179
		 * fonction qui permet de savoir si un module est à jour ou non
180
		 * si a jour renvoi true sinon renvoi false
181
		 */
182 View Code Duplication
		public static function getModuleAJour($nom_module) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
183
			$dbc = App::getDb();
184
185
			$query = $dbc->query("SELECT mettre_jour FROM module WHERE nom_module = ".$dbc->quote($nom_module));
186
187
			if ((is_array($query)) && (count($query) > 0)) {
188
				foreach ($query as $obj) {
189
					if ($obj->mettre_jour == 1) {
190
						return false;
191
					}
192
					else {
193
						return true;
194
					}
195
				}
196
			}
197
		}
198
199
		/**
200
		 * fonction qui se lance à chaquer fois que l'on ouvre l'admin
201
		 * permet de tester si tous les modules présent sur le site sont bien à jour
202
		 */
203
		public function getCheckModuleVersion() {
204
			$dbc = App::getDb();
205
			$today = date("Y-m-d");
206
			$today_o = new \DateTime($today);
207
208
			$query = $dbc->query("SELECT next_check_version, version, url_telechargement, mettre_jour, delete_old_version, ID_module FROM module");
209
210
			if ((is_array($query)) && (count($query) > 0)) {
211
				foreach ($query as $obj) {
212
					if ($obj->next_check_version == null) {
213
						//si pas de version a checker, cela veut dire qu'on vient d'installer le module
214
						//du coup on met le next_check aa la semaine pro
215
						$set_next = true;
216
					}
217
					else if (($obj->next_check_version <= $today) && ($obj->mettre_jour != 1)) {
218
						//avant tout on regarde si on doit delete une vieille version
219
						if ($obj->delete_old_version == 1) {
220
							$import = new ImportModule();
221
							$import->setSupprimerOldModule($obj->ID_module);
222
						}
223
224
						//on recupere le nom du dossier + extention
225
						$explode = explode(".", $obj->url_telechargement);
226
						array_pop($explode);
227
228
						$version_txt = implode(".", $explode)."_version.txt";
229
230
						if (file_get_contents($version_txt) !== "") {
231
232
							//online pour bdd
233
							$version_online_txt = file_get_contents($version_txt);
234
235
							$version_online = floatval($version_online_txt);
236
							$version_site = floatval($obj->version);
237
238
							//la version sur le serveur de telechargement est plus récente, on va donc proposer
239
							//en passant la valeur update a 1 dans la table module pour ce module
240
							// au client de mettre a jour sa version sinon on met la next check a la semaine pro
241
							if ($version_online > $version_site) {
242
								$value = [
243
									"update" => 1,
244
									"online_version" => $version_online_txt,
245
									"id_module" => $obj->ID_module
246
								];
247
248
								//on met la notification admin à 1
249
								$dbc->query("UPDATE notification SET admin=1 WHERE ID_notification=1");
250
251
								$dbc->prepare("UPDATE module SET mettre_jour=:update, online_version=:online_version WHERE ID_module=:id_module", $value);
252
253
								$set_next = true;
254
							}
255
							else {
256
								$set_next = true;
257
							}
258
						}
259
					}
260
261
					if ((isset($set_next)) && ($set_next === true)) {
262
						$value = [
263
							"next_check" => $today_o->add(new \DateInterval("P1W"))->format("Y-m-d"),
264
							"id_module" => $obj->ID_module
265
						];
266
267
						$dbc->prepare("UPDATE module SET next_check_version=:next_check WHERE ID_module=:id_module", $value);
268
					}
269
				}
270
			}
271
		}
272
273
		public function getListeModuleMettreJour() {
274
			$dbc = App::getDb();
275
276
			$query = $dbc->query("SELECT * FROM module WHERE mettre_jour=1");
277
278
			if ((is_array($query)) && (count($query) > 0)) {
279
				$nom_module = [];
280
				$version = [];
281
				$online_version = [];
282
283
				foreach ($query as $obj) {
284
					$nom_module[] = $obj->nom_module;
285
					$version[] = $obj->version;
286
					$online_version[] = $obj->online_version;
287
				}
288
289
				$this->setListeModuleMettreJour($nom_module, $version, $online_version);
290
291
				return true;
292
			}
293
			else {
294
				return false;
295
			}
296
		}
297
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
298
		
299
		
300
		//-------------------------- SETTER ----------------------------------------------------------------------------//
301
		private function setListeModuleActiver($id_module, $url, $version, $nom, $icone = null, $url_telechargement = null) {
302
			$this->id_module = $id_module;
303
			$this->url = $url;
304
			$this->nom = $nom;
305
			$this->version = $version;
306
			$this->icone = $icone;
307
			$this->url_telechargement = $url_telechargement;
308
		}
309
310
		private function setListeModuleMettreJour($nom_module, $version, $online_version) {
311
			$this->nom = $nom_module;
312
			$this->version = $version;
313
			$this->online_version = $online_version;
314
		}
315
316
		/**
317
		 * @param $activer
318
		 * @param $url
319
		 * fonction qui permet d'activer || désactiver un module
320
		 */
321
		public static function setActiverDesactiverModule($activer, $url) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
322
			$dbc = App::getDb();
323
324
			$value = array(
325
				"activer" => $activer,
326
				"url" => $url
327
			);
328
329
			$dbc->prepare("UPDATE module SET activer=:activer WHERE url=:url", $value);
330
		}
331
		//-------------------------- FIN SETTER ----------------------------------------------------------------------------//
332
	}