1
|
|
|
<?php |
2
|
|
|
namespace core\admin\droitsacces; |
3
|
|
|
|
4
|
|
|
use core\App; |
5
|
|
|
use core\HTML\flashmessage\FlashMessage; |
6
|
|
|
|
7
|
|
|
class GestionDroitAcces extends DroitAcces { |
8
|
|
|
use GetDetailListeAcces; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------// |
13
|
|
|
public function __construct() { |
14
|
|
|
$this->getListeDroitAccesAdmin(); |
15
|
|
|
} |
16
|
|
|
//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------// |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
21
|
|
|
/** |
22
|
|
|
* appellee dans le constructeur pour afficher les listes de droit d'acces |
23
|
|
|
*/ |
24
|
|
|
private function getListeDroitAccesAdmin() { |
25
|
|
|
$dbc = App::getDb(); |
26
|
|
|
|
27
|
|
|
$query = $dbc->select()->from("liste_droit_acces")->get(); |
28
|
|
|
|
29
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
30
|
|
|
$values = []; |
31
|
|
|
foreach ($query as $obj) { |
32
|
|
|
$values[] = [ |
33
|
|
|
"id_liste" => $obj->ID_liste_droit_acces, |
34
|
|
|
"nom_liste" => $obj->nom_liste, |
35
|
|
|
"nb_droit_acces" => $this->getNombreDroitAccesListe($obj->ID_liste_droit_acces), |
36
|
|
|
"nb_droit_acces_page" => $this->getNombrePageListe($obj->ID_liste_droit_acces), |
37
|
|
|
"nb_user" => $this->getNombreUtilisateurListe($obj->ID_liste_droit_acces), |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
App::setValues(["liste_droit_acces" => $values]); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $id_droit_acces |
46
|
|
|
* @return string |
47
|
|
|
* fonction qui renvoi le nom d'un droit d'accès en fonction de son id |
48
|
|
|
*/ |
49
|
|
|
private function getNomDroitAcces($id_droit_acces) { |
50
|
|
|
$dbc = App::getDb(); |
51
|
|
|
|
52
|
|
|
$query = $dbc->select("droit_acces")->from("droit_acces")->where("ID_droit_acces", "=", $id_droit_acces)->get(); |
53
|
|
|
|
54
|
|
|
if (count($query) > 0) { |
55
|
|
|
foreach ($query as $obj) { |
56
|
|
|
return $obj->droit_acces; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return ""; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* fonction qui récupère tous les droits d'accès |
65
|
|
|
*/ |
66
|
|
|
private function getAllDroitAcces() { |
67
|
|
|
$dbc = App::getDb(); |
68
|
|
|
|
69
|
|
|
$query = $dbc->select()->from("droit_acces")->get(); |
70
|
|
|
|
71
|
|
|
if (count($query) > 0) { |
72
|
|
|
foreach ($query as $obj) { |
73
|
|
|
$values[] = [ |
|
|
|
|
74
|
|
|
"id_droit_acces" => $obj->ID_droit_acces, |
75
|
|
|
"droit_acces" => $obj->droit_acces, |
76
|
|
|
"nom_module" => $obj->nom_module |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
App::setValues(["droit_acces" => $values]); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $id_liste |
86
|
|
|
* recupere tous les id des droits dc'acces d'une liste |
87
|
|
|
*/ |
88
|
|
|
public function getDroiAccesListe($id_liste) { |
89
|
|
|
$dbc = App::getDb(); |
90
|
|
|
|
91
|
|
|
$query = $dbc->select()->from("liste_droit_acces, liaison_liste_droit") |
92
|
|
|
->where("liste_droit_acces.ID_liste_droit_acces", "=", $id_liste, "AND") |
93
|
|
|
->where("liste_droit_acces.ID_liste_droit_acces", "=", "liaison_liste_droit.ID_liste_droit_acces", "", true) |
94
|
|
|
->get(); |
95
|
|
|
|
96
|
|
|
if (count($query) > 0) { |
97
|
|
|
foreach ($query as $obj) { |
98
|
|
|
$values[] = $this->getNomDroitAcces($obj->ID_droit_acces); |
|
|
|
|
99
|
|
|
$nom_liste = $obj->nom_liste; |
100
|
|
|
$id_liste = $obj->ID_liste_droit_acces; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
App::setValues(["droit_acces_liste" => $values, "nom_liste" => $nom_liste, "id_liste" => $id_liste]); |
|
|
|
|
104
|
|
|
$this->getAllDroitAcces(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
//-------------------------- FIN GETTER ----------------------------------------------------------------------------// |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
112
|
|
|
public function setGestionDroitAccesListe($id_droit_acces, $id_liste, $activer) { |
113
|
|
|
$dbc = App::getDb(); |
114
|
|
|
|
115
|
|
|
if ($activer == 1) { |
116
|
|
|
$dbc->insert("ID_droit_acces", $id_droit_acces)->insert("ID_liste_droit_acces", $id_liste) |
117
|
|
|
->into("liaison_liste_droit")->set(); |
118
|
|
|
FlashMessage::setFlash("Le droit d'acces a bien été ajouté", "success"); |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$dbc->delete()->from("liaison_liste_droit")->where("ID_liste_droit_acces", "=", $id_liste, "AND") |
123
|
|
|
->where("ID_droit_acces", "=", $id_droit_acces)->del(); |
124
|
|
|
FlashMessage::setFlash("Le droit d'acces a bien été supprimé", "success"); |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
//-------------------------- FIN SETTER ----------------------------------------------------------------------------// |
128
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.