Passed
Push — master ( abe900...5c6a05 )
by Anthony
02:52
created

Admin::getAllUser()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 52
rs 6.8493
cc 8
eloc 34
nc 16
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
	namespace core\admin;
3
4
	use core\App;
5
	use core\auth\Encrypt;
6
	use core\auth\Membre;
7
	use core\Configuration;
8
	use core\functions\ChaineCaractere;
9
	use core\HTML\flashmessage\FlashMessage;
10
	use core\mail\Mail;
11
12
	class Admin extends Membre {
13
		private $acces_admin;
14
15
16
		//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------//
17
		public function __construct($id_identite) {
18
			$dbc = \core\App::getDb();
19
20
			//on récupere le lvl de l'admin
21
			$query = $dbc->query("SELECT acces_admin FROM identite WHERE ID_identite=".$id_identite);
22
			if ((is_array($query)) && (count($query) > 0)) {
23
				foreach ($query as $obj) {
24
					$this->acces_admin = $obj->acces_admin;
25
				}
26
			}
27
28
			//si on ne passe pas dans le foreach -> on est pas admin donc on deco le compte
29
			if ((!isset($this->acces_admin)) || ($this->acces_admin != 1)) {
30
				FlashMessage::setFlash("Vous n'êtes pas un administrateur, vous ne pouvez pas accéder à cette page");
31
				header("location:".WEBROOT."index.php");
32
			}
33
		}
34
		//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------//
35
36
37
38
		//-------------------------- GETTER ----------------------------------------------------------------------------//
39
		public function getAccesAdmin() {
40
			return $this->acces_admin;
41
		}
42
43
44
		/**
45
		 * Pour récupérer la liste de tous les users afin d'activer un compte ou modifier des trucs dessus
46
		 * si archiver == null on récupère les utilisateurs actifs sur le site sinon on récupere les utilisateurs archives
47
		 */
48
		public function getAllUser($archiver = null) {
49
			$dbc = \core\App::getDb();
50
			$this->setAllUser(null, null, null, null, null, null, null);
51
52
			if ($archiver == null) {
53
				$query = $dbc->query("SELECT * FROM identite WHERE archiver IS NULL AND ID_identite > 1");
54
			}
55
			else {
56
				$query = $dbc->query("SELECT * FROM identite WHERE archiver IS NOT NULL AND ID_identite > 1");
57
			}
58
59
			$config = new Configuration();
60
61
			if ((is_array($query)) && (count($query) > 0)) {
62
				$valide = "";
63
				$id_identite = [];
64
				$nom = [];
65
				$prenom = [];
66
				$pseudo = [];
67
				$mail = [];
68
				$img_profil = [];
69
70
				foreach ($query as $obj) {
71
					$id_identite[] = $obj->ID_identite;
72
					$nom[] = $obj->nom;
73
					$prenom[] = $obj->prenom;
74
					$pseudo[] = $obj->pseudo;
75
					$mail[] = $obj->mail;
76
77
					if ($obj->img_profil == "") {
78
						$img_profil[] = "profil/defaut.png";
79
					}
80
					else {
81
						$img_profil[] = $obj->img_profil;
82
					}
83
84
					if ($config->getValiderInscription() == 1) {
85
						if ($obj->valide == 0) {
86
							$valide[] = "<a href=".ADMWEBROOT."controller/core/admin/comptes/valider_compte?id_identite=$obj->ID_identite>Valider cet utilisateur</a>";
87
						}
88
						else {
89
							$valide[] = "Utilisateur validé";
90
						}
91
					}
92
					else {
93
						$valide = "";
94
					}
95
				}
96
97
				$this->setAllUser($id_identite, $nom, $prenom, $mail, $pseudo, $img_profil, $valide);
98
			}
99
		}
100
101
		/**
102
		 * Fonctio qui premet de setter les différents élément d'un user
103
		 * @param $id_identite
104
		 */
105
		public function getunUser($id_identite) {
106
			$dbc = \core\App::getDb();
107
108
			$query = $dbc->query("SELECT * FROM identite WHERE ID_identite=".$id_identite);
109
110
			if ((is_array($query)) && (count($query) > 0)) {
111
				foreach ($query as $obj) {
112
					$this->id_identite = $obj->ID_identite;
113
					$this->nom = $obj->nom;
114
					$this->prenom = $obj->prenom;
115
					$this->img = $obj->img_profil;
116
					$this->mail = $obj->mail;
117
					$this->valide = $obj->valide;
118
				}
119
			}
120
		}
121
122
		/**
123
		 * fonction qui si égale a 1 alors il y a une notification dans l'admin du site
124
		 * @return mixed
125
		 */
126
		public function getNotification() {
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...
127
			$dbc = App::getDb();
128
129
			$query = $dbc->query("SELECT admin FROM notification");
130
131
			if ((is_array($query)) && (count($query) > 0)) {
132
				foreach ($query as $obj) {
133
					return $obj->admin;
134
				}
135
			}
136
		}
137
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
138
139
140
141
		//-------------------------- SETTER ----------------------------------------------------------------------------//
142
143
		/**
144
		 * @param null|string $valide
145
		 */
146
		private function setAllUser($id_identite, $nom, $prenom, $mail, $pseudo, $img_profil, $valide) {
147
			$this->id_identite = $id_identite;
148
			$this->nom = $nom;
149
			$this->prenom = $prenom;
150
			$this->mail = $mail;
151
			$this->pseudo = $pseudo;
152
			$this->img = $img_profil;
153
			$this->valide = $valide;
154
		}
155
156
		/**
157
		 * Fonction qui permet de valider un compte utilisateur pour qu'il puisse se conecter au site
158
		 * @param $id_identite
159
		 */
160
		public function setValideCompte($id_identite) {
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...
161
			$dbc = \core\App::getDb();
162
163
			$value = array("id_identite" => $id_identite);
164
165
			$dbc->prepare("UPDATE identite SET valide=1 WHERE ID_identite=:id_identite", $value);
166
167
			$this->getunUser($id_identite);
168
		}
169
170
		/**
171
		 * fonction quir genere un mot de passe aleatoire pour le compte spécifié en param
172
		 * @param $id_identite
173
		 */
174
		public function setReinitialiserMdp($id_identite) {
175
			$dbc = \core\App::getDb();
176
177
			$this->getunUser($id_identite);
178
179
			if (($this->mail != "") || ($this->mail != null)) {
180
				$mdp = ChaineCaractere::random(6);
181
				$mdp_encode = Encrypt::setEncryptMdp($mdp, $id_identite);
182
183
				$value = array(
184
					"mdp" => $mdp_encode,
185
					"id_identite" => $id_identite,
186
					"last_change_mdp" => date("Y-m-d")
187
				);
188
189
				FlashMessage::setFlash("Mot de passe réinitialisé avec succès ! L'utilisateur à reçu un E-mail avec son nouveau mot de passe", "success");
190
191
				$dbc->prepare("UPDATE identite SET mdp=:mdp, last_change_mdp=:last_change_mdp WHERE ID_identite=:id_identite", $value);
192
193
				$mail = new Mail($this->mail);
194
				$mail->setEnvoyerMail("Réinitialisation de votre E-mail effectuée", "Votre mot de passe a été réinitialisé");
195
			}
196
			else {
197
				FlashMessage::setFlash("le mot de passe de $this->pseudo ne peu pas être réinitialisé car il ne possède pas d'E-mail");
198
				$this->erreur = true;
199
			}
200
		}
201
202
		/**
203
		 * Supprime le compte en question et enleve l'image de profil aussi
204
		 * @param $id_identite
205
		 */
206
		public function setArchiverCompte($id_identite) {
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...
207
			$dbc = \core\App::getDb();
208
209
			$value = array(
210
				"id_identite" => $id_identite,
211
				"archiver" => 1
212
			);
213
214
			$dbc->prepare("UPDATE identite SET archiver=:archiver WHERE ID_identite=:id_identite", $value);
215
		}
216
217
		/**
218
		 * Supprime le compte en question et enleve l'image de profil aussi
219
		 * @param $id_identite
220
		 */
221
		public function setActiverCompte($id_identite) {
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...
222
			$dbc = \core\App::getDb();
223
224
			$value = array(
225
				"id_identite" => $id_identite,
226
				"archiver" => NULL
227
			);
228
229
			$dbc->prepare("UPDATE identite SET archiver=:archiver WHERE ID_identite=:id_identite", $value);
230
		}
231
232
		/**
233
		 * Supprime le compte en question et enleve l'image de profil aussi
234
		 * @param $id_identite
235
		 */
236
		public function setSupprimerCompte($id_identite) {
237
			$dbc = \core\App::getDb();
238
239
			$oldimg_profil = "";
240
241
			//test si il y a deja une img
242
			$query = $dbc->query("SELECT img_profil FROM identite where ID_identite=$id_identite");
243
244
			if ((is_array($query)) && (count($query) > 0)) {
245
				foreach ($query as $obj) {
246
					$oldimg_profil = $obj->img_profil;
247
				}
248
			}
249
250
251
			if ($oldimg_profil != "") {
252
				$oldimg_profil = explode("/", $oldimg_profil);
253
				if (end($oldimg_profil) != "defaut.png") {
254
					unlink("../../images/profil/".$oldimg_profil[7]);
255
				}
256
			}
257
258
			$value = array(
259
				"id_identite" => $id_identite
260
			);
261
262
			$dbc->prepare("DELETE FROM identite WHERE ID_identite=:id_identite", $value);
263
		}
264
265
		/**
266
		 * permet de dire qu'on a vue une notification dans l'administration du site internet
267
		 */
268
		public static function setNotificationVue() {
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...
269
			$dbc = App::getDb();
270
271
			$value = [
272
				"admin" => 0,
273
				"id" => 1
274
			];
275
276
			$dbc->prepare("UPDATE notification SET admin=:admin WHERE ID_notification=:id", $value);
277
		}
278
		//-------------------------- FIN SETTER ----------------------------------------------------------------------------//
279
280
281
282
	}