1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| <?php
namespace SymfonyCapsule\CapsuleBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use SymfonyCapsule\CapsuleBundle\Entity\Adherent;
use SymfonyCapsule\CapsuleBundle\Form\AdherentType;
use SymfonyCapsule\CapsuleBundle\Entity\Capsule;
use SymfonyCapsule\CapsuleBundle\Form\CapsuleType;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller {
public function indexAction() {
//Acceuil
return $this->render('SymfonyCapsuleCapsuleBundle:Default:index.html.twig');
}
public function inscriptionAction() {
$em = $this->getDoctrine()->getEntityManager();
$a = new Adherent();
$dateAdherent = new \DateTime();
$a->setDateAdherent($dateAdherent);
$form = $this->createForm(new AdherentType(), $a);
$request = $this->getRequest();
if ($request->isMethod('POST')) {
$form->bindRequest($request);
if ($form->isValid()) {
$a = $form->getData();
$em->persist($a);
$em->flush();
return $this->redirect($this->generateUrl("symfony_capsule_capsule_index"));
}
}
return $this->render('SymfonyCapsuleCapsuleBundle:Default:inscription.html.twig', array(
'form' => $form->createView(),
));
}
public function voirCapsulesAction() {
//affichage de toutes les capsules
$em = $this->getDoctrine()->getEntityManager();
$capsule = $em->getRepository("SymfonyCapsuleCapsuleBundle:Capsule")->findAll();
return $this->render('SymfonyCapsuleCapsuleBundle:Default:voirCapsules.html.twig', array(
'capsules' => $capsule,
));
}
public function detailCapsuleAction(Capsule $capsule) {
//detail de la capsule selectionné
return $this->render('SymfonyCapsuleCapsuleBundle:Default:detailCapsule.html.twig', array(
'capsule' => $capsule,
));
}
public function modifierProfilAction() {
// persistance de l'utilisateur
$em = $this->getDoctrine()->getEntityManager();
// récupération de l'utilisateur
$adherent = $this->get('security.context')->getToken()->getUser();
// création d'un objet formulaire suivant un modèle type et préchargé avec les données de notre utilisateur
$editForm = $this->createForm(new AdherentType(), $adherent);
// récupération des données du formulaire (entre autres)
$request = $this->getRequest();
//si la methode est un post alors
if ($request->isMethod('POST')) {
// hydratation de l'utilisateur avec TOUTES les données issues du formulaire
$editForm->bindRequest($request);
//si le formulaire est valide alors
if ($editForm->isValid()) {
//on persist l'envoi du formulaire et on flush vers la base de donnée
$em->persist($adherent);
$em->flush();
//redirection vers l'index
return $this->redirect($this->generateUrl("symfony_capsule_capsule_profil"));
}
}
return $this->render('SymfonyCapsuleCapsuleBundle:Default:modifierProfil.html.twig', array(
'form' => $editForm->createView(),
));
}
public function ajouterCapsuleAction() {
$objUser = $this->get('security.context')->getToken()->getUser();
//On recupère la classe EntityManager
$em = $this->getDoctrine()->getEntityManager();
//nouvel objet de capsule
$a = new Capsule();
$a->setIdAdherent($objUser);
//creation du formulaire CapsuleType
$form = $this->createForm(new CapsuleType(), $a);
//on recupère la fonction getRequest de la classe Controller
$request = $this->getRequest();
//si la methode est un post alors
if ($request->isMethod('POST')) {
$form->bindRequest($request);
//si le formulaire est valide alors
if ($form->isValid()) {
$maison = $em->getRepository('SymfonyCapsuleCapsuleBundle:Maison')->find($a->getIdMaison());
$a->setIdMaison($maison);
/*
* affiche les valeurs des variables
* $a = $form->getData();
* var_dump($form->getData());
* exit;
*/
//on persist l'envoi du formulaire et on flush vers la base de donnée
$em->persist($a);
$em->flush();
//redirection vers l'index
return $this->redirect($this->generateUrl("symfony_capsule_capsule_index"));
}
}
return $this->render('SymfonyCapsuleCapsuleBundle:Default:ajouterCapsule.html.twig', array(
'form' => $form->createView(),
));
}
public function loginAction() {
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('SymfonyCapsuleCapsuleBundle:Default:login.html.twig', array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
public function profilAction() {
$utilisateur = $this->get('security.context')->getToken()->getUser();
return $this->render('SymfonyCapsuleCapsuleBundle:Default:profil.html.twig', array(
'utilisateur' => $utilisateur,
));
}
public function modifierCapsulesAction(Capsule $capsule){
//On recupère la classe EntityManager
$em = $this->getDoctrine()->getEntityManager();
//creation du formulaire CapsuleType
$form = $this->createForm(new CapsuleType(), $capsule);
//on recupère la fonction getRequest de la classe Controller
$request = $this->getRequest();
//si la methode est un post alors
if ($request->isMethod('POST')) {
$form->bindRequest($request);
//si le formulaire est valide alors
if ($form->isValid()) {
$a = $form->getData();
//on persist l'envoi du formulaire et on flush(met a jour) vers la base de donnée
$em->persist($a);
$em->flush();
//redirection vers mes capsules
return $this->redirect($this->generateUrl("symfony_capsule_capsule_voir_Mes_Capsules", array(
'id' => $a->getIdCaps(),
)));
}
}
return $this->render('SymfonyCapsuleCapsuleBundle:Default:modifierCapsules.html.twig', array(
'id' => $capsule->getIdCaps(),
'form' => $form->createView(),
));
}
public function voirMesCapsulesAction(){
//affichage de toutes les capsules de l'utilisateur
$utilisateur = $this->get('security.context')->getToken()->getUser();
$em = $this->getDoctrine()->getEntityManager();
$capsule = $em->getRepository("SymfonyCapsuleCapsuleBundle:Capsule")->findByIdAdherent($utilisateur);
return $this->render('SymfonyCapsuleCapsuleBundle:Default:voirMesCapsules.html.twig', array(
'capsules' => $capsule,
));
}
} |
Partager