1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KI\CoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
6
|
|
|
|
7
|
|
|
// Fonctions génériques |
8
|
|
|
abstract class BaseController extends CoreController |
9
|
|
|
{ |
10
|
|
|
protected $class; |
11
|
|
|
protected $bundle; |
12
|
|
|
protected $className; |
13
|
|
|
/** |
14
|
|
|
* @var \Doctrine\ORM\EntityManager |
15
|
|
|
*/ |
16
|
|
|
protected $manager; |
17
|
|
|
/** |
18
|
|
|
* @var \Doctrine\ORM\EntityRepository |
19
|
|
|
*/ |
20
|
|
|
protected $repository; |
21
|
|
|
protected $form; |
22
|
|
|
protected $save; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Initialise le controleur de base |
26
|
|
|
* @param string $class Le nom de la classe sur laquelle se baser |
27
|
|
|
* @param string $bundle Le nom du bundle dans lequel se trouve cette classe |
28
|
|
|
*/ |
29
|
|
|
public function initialize($class, $bundle) |
30
|
|
|
{ |
31
|
|
|
$this->class = 'KI\\'.$bundle.'Bundle\Entity\\'.$class; |
32
|
|
|
$this->bundle = $bundle; |
33
|
|
|
$this->className = $class; |
34
|
|
|
|
35
|
|
|
$this->manager = $this->getDoctrine()->getManager(); |
36
|
|
|
$this->repository = $this->manager->getRepository('KI'.$bundle.'Bundle:'.$class); |
37
|
|
|
$this->form = 'KI\\'.$bundle.'Bundle\Form\\'.$class.'Type'; |
38
|
|
|
|
39
|
|
|
parent::setUser(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Permet de changer le type d'objet sur lequel s'appuie le controleur |
44
|
|
|
* @param string $class Le nom de la nouvelle classe. Si laissé à null, |
45
|
|
|
* revient à la classe précédent(celle-ci est sauvegardée à chaque changement) |
46
|
|
|
*/ |
47
|
|
|
protected function switchClass($class = null) |
48
|
|
|
{ |
49
|
|
|
// On garde en mémoire la classe précédente |
50
|
|
|
if ($class === null) { |
51
|
|
|
$class = $this->save; |
52
|
|
|
} else { |
53
|
|
|
$this->save = $this->className; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// À priori, une sous ressource garde le même namespace |
57
|
|
|
$this->initialize($class, $this->bundle); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Recherche une entité selon son slug |
62
|
|
|
* @param string $slug |
63
|
|
|
* @return mixed |
64
|
|
|
* @throws NotFoundHttpException Si l'entité n'est pas trouvée |
65
|
|
|
*/ |
66
|
|
|
protected function findBySlug($slug) |
67
|
|
|
{ |
68
|
|
|
if (!method_exists($this->class, 'setSlug')) { |
69
|
|
|
$item = $this->repository->findOneById($slug); |
70
|
|
|
} else { |
71
|
|
|
if ($this->className == 'User') { |
72
|
|
|
$item = $this->repository->findOneByUsername($slug); |
73
|
|
|
} else { |
74
|
|
|
$item = $this->repository->findOneBySlug($slug); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
if (!$item instanceof $this->class) { |
78
|
|
|
throw new NotFoundHttpException('Objet '.$this->className.' non trouvé'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $item; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sert à checker si l'user actuel est membre du club au nom duquel il poste |
86
|
|
|
* @param string $club |
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
|
|
protected function isClubMember($club = null) |
90
|
|
|
{ |
91
|
|
|
if ($this->is('ADMISSIBLE')) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// On vérifie que la requete est valide. |
96
|
|
|
// Si aucun club n'est précisé, c'est qu'on publie à son nom |
97
|
|
|
// (par exemple message perso) donc ok |
98
|
|
|
$request = $this->get('request_stack')->getCurrentRequest()->request; |
99
|
|
|
if (!$request->has('authorClub') && $club === null) { |
100
|
|
|
return $this->is('USER'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$repo = $this->manager->getRepository('KIUserBundle:Club'); |
104
|
|
|
$club = $repo->findOneBySlug($request->has('authorClub') ? $request->get('authorClub') : $club); |
105
|
|
|
|
106
|
|
|
if (!$club) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// On vérifie que l'utilisateur fait bien partie du club |
111
|
|
|
return $this->get('ki_user.service.permission')->isClubMember($this->user, $club); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Sert à checker si l'user courant est membre du foyer actuel |
116
|
|
|
* @return boolean |
117
|
|
|
*/ |
118
|
|
|
protected function isFoyerMember() |
119
|
|
|
{ |
120
|
|
|
return $this->isClubMember('foyer') |
121
|
|
|
&& $this->user->getPromo() == $this->getConfig('promos.assos'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|