Passed
Push — master ( a47f7f...7f5395 )
by Anthony
02:41
created

Faction::getFactionExist()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	use core\HTML\flashmessage\FlashMessage;
7
	
8
	class Faction extends PermissionsFaction {
9
		protected $id_faction;
10
		protected $id_autre_faction;
11
		
12
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
13
		public function __construct() {
14
			
15
		}
16
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
17
		
18
		
19
		//-------------------------- GETTER ----------------------------------------------------------------------------//
20
		public function getIdFaction(){
21
		    return $this->id_faction;
22
		}
23
		
24
		/**
25
		 * @param $id_faction
26
		 * @return bool
27
		 * permet de tester si le joueur est dans la faction affichée
28
		 */
29
		private function getTestFactionPlayer($id_faction) {
30
			$dbc = App::getDb();
31
			$id_ma_faction = 0;
32
			
33
			$query = $dbc->select("ID_faction")->from("_bataille_infos_player")->where("ID_identite", "=", Bataille::getIdIdentite())->get();
34
			
35
			foreach ($query as $obj) {
36
				$id_ma_faction = $obj->ID_faction;
37
			}
38
			
39
			if ($id_ma_faction == $id_faction) {
40
				Bataille::setValues([
41
					"ma_faction" => true,
42
					"id_identite_player" => Bataille::getIdIdentite()
43
				]);
44
				return true;
45
			}
46
			
47
			return false;
48
		}
49
		
50
		/**
51
		 * @return mixed
52
		 * fonction qui renvoi l'ID de la faction du joueur
53
		 */
54
		public function getFactionPlayer($id_identite = null) {
55
			$dbc = App::getDb();
56
			
57
			if ($id_identite === null) {
58
				$id_identite = Bataille::getIdIdentite();
59
			}
60
			
61
			$query = $dbc->select("ID_faction")->from("_bataille_infos_player")
62
				->where("ID_identite", "=", $id_identite, "AND")
63
				->where("ID_faction", ">", 0)
64
				->get();
65
			
66
			if (count($query) > 0) {
67
				foreach ($query as $obj) {
68
					$this->id_faction = $obj->ID_faction;
69
					$this->getInfosFaction();
70
				}
71
				
72
				return true;
73
			}
74
			
75
			return false;
76
		}
77
		
78
		/**
79
		 * @param null $id_faction
80
		 * fonction qui récupère les infos de la faction
81
		 */
82
		public function getInfosFaction($id_faction = null) {
83
			$dbc = App::getDb();
84
			
85
			if ($id_faction === null) {
86
				$id_faction = $this->id_faction;
87
			}
88
			
89
			$this->getTestFactionPlayer($id_faction);
90
			
91
			$query = $dbc->select("identite.pseudo")
92
				->select("_bataille_faction.ID_faction")
93
				->select("_bataille_faction.nom_faction")
94
				->select("_bataille_faction.points_faction")
95
				->select("_bataille_faction.img_profil")
96
				->select("_bataille_faction.description")
97
				->from("_bataille_faction")
98
				->from("identite")
99
				->where("_bataille_faction.ID_faction", "=", $id_faction, "AND")
100
				->where("_bataille_faction.ID_identite", "=", "identite.ID_identite", "", true)
101
				->get();
102
			
103
			if ((count($query) == 1)) {
104
				foreach ($query as $obj) {
105
					Bataille::setValues(["faction" => [
106
						"id_faction" => $obj->ID_faction,
107
						"nom" => $obj->nom_faction,
108
						"points_faction" => $obj->points_faction,
109
						"description" => $obj->description,
110
						"url_img" => $obj->img_profil,
111
						"pseudo_chef" => $obj->pseudo
112
					]]);
113
				}
114
			}
115
		}
116
		
117
		/**
118
		 * fonction qui récupère les membres d'un faction
119
		 */
120
		public function getMembreFaction() {
121
			$dbc = App::getDb();
122
			
123
			$query = $dbc->select()
124
				->from("_bataille_infos_player")
125
				->from("identite")
126
				->where("_bataille_infos_player.ID_faction", "=", $this->id_faction, "AND")
127
				->where("_bataille_infos_player.ID_identite", "=", "identite.ID_identite", "", true)
128
				->orderBy("_bataille_infos_player.points", "DESC")
129
				->get();
130
			
131
			$membre = [];
132
			foreach ($query as $obj) {
133
				$membre[] = [
134
					"id_identite" => $obj->ID_identite,
135
					"pseudo" => $obj->pseudo,
136
					"points" => $obj->points,
137
					"rang_faction" => $obj->rang_faction,
138
					"chef" => $this->getTestChefFaction($obj->ID_identite, $this->id_faction),
139
					"permissions" => $this->getMembrePermissions($obj->ID_identite, $this->id_faction)
140
				];
141
			}
142
			
143
			Bataille::setValues(["membres_faction" => $membre]);
144
		}
145
		
146
		/**
147
		 * @param $nom_faction
148
		 * @return bool
149
		 * ajout d'une fonction pour tester si une faction existe ou non renvoi true si elle existe
150
		 */
151
		protected function getFactionExist($nom_faction) {
152
			$dbc = App::getDb();
153
			
154
			$query = $dbc->select("ID_faction")->from("_bataille_faction")->where("nom_faction", "=", $nom_faction)->get();
155
			
156
			if (count($query) > 0) {
157
				foreach ($query as $obj) {
158
					$this->id_autre_faction = $obj->ID_faction;
159
				}
160
				
161
				return true;
162
			}
163
			
164
			return false;
165
		}
166
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
167
		
168
		
169
		//-------------------------- SETTER ----------------------------------------------------------------------------//
170
		/**
171
		 * @param $id_identite
172
		 * @return bool
173
		 * fonction qui permet de renvoyer un membre d'un faction
174
		 */
175
		public function setRenvoyerMembre($id_identite) {
176
			$dbc = App::getDb();
177
			$permissions_membre = $this->getPermissionsMembre($this->id_faction);
178
			
179
			if ($permissions_membre == "chef" || in_array("RENVOYER_MEMBRE", $permissions_membre)) {
180
				$dbc->update("ID_faction", 0)
181
					->update("rang_faction", "")
182
					->from("_bataille_infos_player")
183
					->where("ID_identite", "=", $id_identite, "AND")
184
					->where("ID_faction", "=", $this->id_faction, "", true)
185
					->set();
186
				
187
				$this->setSupprilerAllPermissions($id_identite);
188
				
189
				FlashMessage::setFlash("Le membre a bien été renvoyé de la faction", "success");
190
				return true;
191
			}
192
			
193
			FlashMessage::setFlash("Vous n'avez pas l'autorisation de renvoyer un membre");
194
			return false;
195
		}
196
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
197
	}