Passed
Push — master ( de0e88...de0e88 )
by Anthony
04:58 queued 02:36
created

GestionModule::setActiverDesactiverModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
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
		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
		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
		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) {
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...
131
			$dbc = App::getDb();
132
133
			$query = $dbc->query("SELECT * FROM module WHERE nom_module = ".$dbc->quote($nom_module));
134
135
			if ((is_array($query)) && (count($query) > 0)) {
136
				$installer = 0;
137
138
				foreach ($query as $obj) {
139
					$installer = $obj->installer;
140
				}
141
142
				return $installer;
143
			}
144
			else {
145
				return false;
146
			}
147
		}
148
149
		/**
150
		 * @param $nom_module
151
		 * @return boolean|null
152
		 * return true si le module est activer sinon false
153
		 */
154
		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...
155
			$dbc = App::getDb();
156
157
			$query = $dbc->query("SELECT activer FROM module WHERE nom_module = ".$dbc->quote($nom_module));
158
159
			if ((is_array($query)) && (count($query) > 0)) {
160
				foreach ($query as $obj) {
161
					if ($obj->activer == 1) {
162
						return true;
163
					}
164
					else {
165
						return false;
166
					}
167
				}
168
			}
169
		}
170
171
		/**
172
		 * @param $nom_module
173
		 * @return boolean|null
174
		 * fonction qui permet de savoir si un module est à jour ou non
175
		 * si a jour renvoi true sinon renvoi false
176
		 */
177
		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...
178
			$dbc = App::getDb();
179
180
			$query = $dbc->query("SELECT mettre_jour FROM module WHERE nom_module = ".$dbc->quote($nom_module));
181
182
			if ((is_array($query)) && (count($query) > 0)) {
183
				foreach ($query as $obj) {
184
					if ($obj->mettre_jour == 1) {
185
						return false;
186
					}
187
					else {
188
						return true;
189
					}
190
				}
191
			}
192
		}
193
194
		/**
195
		 * fonction qui se lance à chaquer fois que l'on ouvre l'admin
196
		 * permet de tester si tous les modules présent sur le site sont bien à jour
197
		 */
198
		public function getCheckModuleVersion() {
199
			$dbc = App::getDb();
200
			$today = date("Y-m-d");
201
			$today_o = new \DateTime($today);
202
203
			$query = $dbc->query("SELECT next_check_version, version, url_telechargement, mettre_jour, delete_old_version, ID_module FROM module");
204
205
			if ((is_array($query)) && (count($query) > 0)) {
206
				foreach ($query as $obj) {
207
					if ($obj->next_check_version == null) {
208
						//si pas de version a checker, cela veut dire qu'on vient d'installer le module
209
						//du coup on met le next_check aa la semaine pro
210
						$set_next = true;
211
					}
212
					else if (($obj->next_check_version <= $today) && ($obj->mettre_jour != 1)) {
213
						//avant tout on regarde si on doit delete une vieille version
214
						if ($obj->delete_old_version == 1) {
215
							$import = new ImportModule();
216
							$import->setSupprimerOldModule($obj->ID_module);
217
						}
218
219
						//on recupere le nom du dossier + extention
220
						$explode = explode(".", $obj->url_telechargement);
221
						array_pop($explode);
222
223
						$version_txt = implode(".", $explode)."_version.txt";
224
225
						if (file_get_contents($version_txt) !== "") {
226
227
							//online pour bdd
228
							$version_online_txt = file_get_contents($version_txt);
229
230
							$version_online = floatval($version_online_txt);
231
							$version_site = floatval($obj->version);
232
233
							//la version sur le serveur de telechargement est plus récente, on va donc proposer
234
							//en passant la valeur update a 1 dans la table module pour ce module
235
							// au client de mettre a jour sa version sinon on met la next check a la semaine pro
236
							if ($version_online > $version_site) {
237
								$value = [
238
									"update" => 1,
239
									"online_version" => $version_online_txt,
240
									"id_module" => $obj->ID_module
241
								];
242
243
								//on met la notification admin à 1
244
								$dbc->query("UPDATE notification SET admin=1 WHERE ID_notification=1");
245
246
								$dbc->prepare("UPDATE module SET mettre_jour=:update, online_version=:online_version WHERE ID_module=:id_module", $value);
247
248
								$set_next = true;
249
							}
250
							else {
251
								$set_next = true;
252
							}
253
						}
254
					}
255
256
					if ((isset($set_next)) && ($set_next === true)) {
257
						$value = [
258
							"next_check" => $today_o->add(new \DateInterval("P1W"))->format("Y-m-d"),
259
							"id_module" => $obj->ID_module
260
						];
261
262
						$dbc->prepare("UPDATE module SET next_check_version=:next_check WHERE ID_module=:id_module", $value);
263
					}
264
				}
265
			}
266
		}
267
268
		public function getListeModuleMettreJour() {
269
			$dbc = App::getDb();
270
271
			$query = $dbc->query("SELECT * FROM module WHERE mettre_jour=1");
272
273
			if ((is_array($query)) && (count($query) > 0)) {
274
				$nom_module = [];
275
				$version = [];
276
				$online_version = [];
277
278
				foreach ($query as $obj) {
279
					$nom_module[] = $obj->nom_module;
280
					$version[] = $obj->version;
281
					$online_version[] = $obj->online_version;
282
				}
283
284
				$this->setListeModuleMettreJour($nom_module, $version, $online_version);
285
286
				return true;
287
			}
288
			else {
289
				return false;
290
			}
291
		}
292
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
293
		
294
		
295
		//-------------------------- SETTER ----------------------------------------------------------------------------//
296
		private function setListeModuleActiver($id_module, $url, $version, $nom, $icone = null, $url_telechargement = null) {
297
			$this->id_module = $id_module;
298
			$this->url = $url;
299
			$this->nom = $nom;
300
			$this->version = $version;
301
			$this->icone = $icone;
302
			$this->url_telechargement = $url_telechargement;
303
		}
304
305
		private function setListeModuleMettreJour($nom_module, $version, $online_version) {
306
			$this->nom = $nom_module;
307
			$this->version = $version;
308
			$this->online_version = $online_version;
309
		}
310
311
		/**
312
		 * @param $activer
313
		 * @param $url
314
		 * fonction qui permet d'activer || désactiver un module
315
		 */
316
		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...
317
			$dbc = App::getDb();
318
319
			$value = array(
320
				"activer" => $activer,
321
				"url" => $url
322
			);
323
324
			$dbc->prepare("UPDATE module SET activer=:activer WHERE url=:url", $value);
325
		}
326
		//-------------------------- FIN SETTER ----------------------------------------------------------------------------//
327
	}