RelationFaction::setSupprimerRelation()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 3
eloc 11
nc 2
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 RelationFaction extends Faction {
9
		//-------------------------- GETTER ----------------------------------------------------------------------------//
10
		/**
11
		 * renvoi la liste des relations d'une faction
12
		 */
13
		public function getListeRelation() {
14
			$dbc = App::getDb();
15
			
16
			$query = $dbc->select()->from("_bataille_faction_relation")
17
				->from("_bataille_faction")
18
				->where("_bataille_faction_relation.ID_faction", "=", $this->id_faction, "AND")
19
				->where("_bataille_faction_relation.ID_autre_faction", "=", "_bataille_faction.ID_faction", "", true)
20
				->get();
21
			
22
			$relations = [];
23
			$id_faction = [];
24
			if (count($query) > 0) {
25
				foreach ($query as $obj) {
26
					$relations[] = [
27
						"id_relation" => $obj->ID_faction_relation,
28
						"relation" => $obj->relation,
29
						"id_autre_faction" => $obj->ID_autre_faction,
30
						"nom_autre_faction" => $obj->nom_faction
31
					];
32
					
33
					$id_faction[] = $obj->ID_autre_faction;
34
				}
35
			}
36
			
37
			Bataille::setValues(["relations" => $relations]);
38
			
39
			return $id_faction;
40
		}
41
		
42
		/**
43
		 * @param $relation
44
		 * @return array
45
		 */
46
		public function getIdFactionRelation($relation) {
47
			$dbc = App::getDb();
48
			
49
			$query = $dbc->select("ID_autre_faction")->from("_bataille_faction_relation")
50
				->where("relation", "=", $relation, "AND")
51
				->where("ID_faction", "=", $this->id_faction)
52
				->get();
53
			
54
			$relations = [];
55
			if (count($query) > 0) {
56
				foreach ($query as $obj) {
57
					$relations[] = $obj->ID_autre_faction;
58
				}
59
			}
60
			
61
			return $relations;
62
		}
63
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
64
		
65
		
66
		//-------------------------- SETTER ----------------------------------------------------------------------------//
67
		/**
68
		 * @param $nom_faction
69
		 * @param $relation
70
		 * @return bool
71
		 * fonction qui permet d'ajouter une relation
72
		 */
73
		public function setAjouterRelation($nom_faction, $relation) {
74
			$dbc = App::getDb();
75
			$permissions_membre = $this->getPermissionsMembre($this->id_faction);
76
			
77
			if ($permissions_membre == "chef" || in_array("GERER_RELATIONS", $permissions_membre)) {
78
				if ($this->getFactionExist($nom_faction) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
79
					FlashMessage::setFlash("Cette faction n'existe pas, vérifiez que vous avez correctement écrit son nom");
80
					return false;
81
				}
82
				if (in_array($this->id_autre_faction, $this->getListeRelation())) {
83
					FlashMessage::setFlash("Vous avez déjà une relation avec cette faction");
84
					return false;
85
				}
86
				if ($this->id_autre_faction == $this->id_faction) {
87
					FlashMessage::setFlash("Vous ne pouvez pas avoir de relations avec votre propre faction");
88
					return false;
89
				}
90
				
91
				$dbc->insert("relation", $relation)
92
					->insert("ID_faction", $this->id_faction)
93
					->insert("ID_autre_faction", $this->id_autre_faction)
94
					->into("_bataille_faction_relation")->set();
95
				
96
				FlashMessage::setFlash("La relation a été ajoutée avec succès", "success");
97
				return true;
98
			}
99
			
100
			FlashMessage::setFlash("Vous n'avez pas l'autorisation de gérer les relations de votre faction");
101
			return false;
102
		}
103
		
104
		/**
105
		 * @param $id_relation
106
		 * @return bool
107
		 * fonction qui permet de supprimer une relation
108
		 */
109 View Code Duplication
		public function setSupprimerRelation($id_relation) {
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...
110
			$dbc = App::getDb();
111
			$permissions_membre = $this->getPermissionsMembre($this->id_faction);
112
			
113
			if ($permissions_membre == "chef" || in_array("GERER_RELATIONS", $permissions_membre)) {
114
				$dbc->delete()->from("_bataille_faction_relation")
115
					->where("ID_faction_relation", "=", $id_relation, "AND")
116
					->where("ID_faction", "=", $this->id_faction)->del();
117
				
118
				FlashMessage::setFlash("La relation a été supprimée avec succès".$id_relation, "success");
119
				return true;
120
			}
121
			
122
			FlashMessage::setFlash("Vous n'avez pas l'autorisation de gérer les relations de votre faction");
123
			return false;
124
		}
125
		
126
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
127
	}