Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace MA\MelodieAnimationBundle\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use MA\MelodieAnimationBundle\Entity\News;
- use MA\MelodieAnimationBundle\Entity\Message;
- use MA\MelodieAnimationBundle\Entity\Soiree;
- use MA\MelodieAnimationBundle\Form\SoireeType;
- use MA\MelodieAnimationBundle\Form\NewsType;
- use MA\MelodieAnimationBundle\Form\MessageType;
- use MA\MelodieAnimationBundle\Form\ContactType;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Security\Core\SecurityContext;
- class NewsController extends Controller
- {
- // On récupère tous les paramètres en arguments de la méthode
- public function indexAction($page,Request $request)
- {
- $nbPerPage = 3;
- // On ne sait pas combien de pages il y a
- // Mais on sait qu'une page doit être supérieure ou égale à 1
- if ($page < 1) {
- $page=1;
- }
- $listNews = $this->getDoctrine()->getManager()->getRepository('MAMelodieAnimationBundle:News')->getNews($page,$nbPerPage);
- $nbPages = ceil(count($listNews)/$nbPerPage);
- if($nbPages === 0.0){
- $nbPages=1;
- }
- // Si la page n'existe pas, on retourne une 404
- if ($page > $nbPages) {
- throw $this->createNotFoundException("La page ".$page." n'existe pas.");
- }
- // si on nous envoie un message sur le livre d'or :
- $news = new news();
- $formMess=$this->createForm(new NewsType(), $news);
- if($formMess->handleRequest($request)->isValid()){
- $em = $this->getDoctrine()->getManager();
- $em->persist($news);
- $em->flush();
- }
- if ($request->isMethod('POST')) {
- $request->getSession()->getFlashBag()->add('success', 'News ajoutée !');
- }
- return $this->render('MAMelodieAnimationBundle:News:index.html.twig', array(
- 'listNews' => $listNews,'nbPages'=>$nbPages,'page'=>$page,
- 'form'=> $formMess->createView()
- ));
- }
- public function addAction(Request $request)
- {
- $news=new News();
- $news->setTitre("Nouvelle news de test");
- $news->setContenu("Voici une news de test , son contenu est inutile");
- $news->setAuteur("Kevin");
- $em= $this->getDoctrine()->getManager();
- $em->persist($news);
- // Étape 2 : On « flush » tout ce qui a été persisté avant
- $em->flush();
- // Reste de la méthode qu'on avait déjà écrit
- if ($request->isMethod('POST')) {
- $request->getSession()->getFlashBag()->add('notice', 'Annonce bien enregistrée.');
- }
- return $this->render('MAMelodieAnimationBundle:News:add.html.twig');
- }
- public function livreorAction(Request $request,$page)
- {
- $nbPerPage = 10;
- // On ne sait pas combien de pages il y a
- // Mais on sait qu'une page doit être supérieure ou égale à 1
- if ($page < 1) {
- $page=1;
- }
- $em = $this->getDoctrine()->getManager();
- $listMessages = $em->getRepository('MAMelodieAnimationBundle:Message')->getMessages($page,$nbPerPage);
- $nbPages = ceil(count($listMessages)/$nbPerPage);
- if($nbPages === 0.0){
- $nbPages=1;
- }
- // Si la page n'existe pas, on retourne une 404
- if ($page > $nbPages) {
- throw $this->createNotFoundException("La page ".$page." n'existe pas.");
- }
- // si on nous envoie un message sur le livre d'or :
- $message = new message();
- $formMess=$this->createForm(new MessageType(), $message);
- if($formMess->handleRequest($request)->isValid()){
- $em = $this->getDoctrine()->getManager();
- $em->persist($message);
- $em->flush();
- }
- if ($request->isMethod('POST')) {
- $request->getSession()->getFlashBag()->add('notice', 'Message enregistré sur le livre d\'or !');
- }
- return $this->render('MAMelodieAnimationBundle:News:livredor.html.twig', array(
- 'listMessages' => $listMessages,
- 'nbPages'=>$nbPages,
- 'page'=>$page,
- 'form'=> $formMess->createView()
- ));
- }
- // On récupère tous les paramètres en arguments de la méthode
- public function presentationAction()
- {
- return $this->render('MAMelodieAnimationBundle:News:presentation.html.twig');
- }
- public function agendaAction($page,Request $request)
- {
- $nbPerPage = 10;
- // On ne sait pas combien de pages il y a
- // Mais on sait qu'une page doit être supérieure ou égale à 1
- if ($page < 1) {
- $page=1;
- }
- $listSoirees = $this->getDoctrine()->getManager()->getRepository('MAMelodieAnimationBundle:Soiree')->getSoirees($page,$nbPerPage);
- $nbPages = ceil(count($listSoirees)/$nbPerPage);
- if($nbPages === 0.0){
- $nbPages=1;
- }
- // Si la page n'existe pas, on retourne une 404
- if ($page > $nbPages) {
- throw $this->createNotFoundException("La page ".$page." n'existe pas.");
- }
- // si on nous envoie un message sur le livre d'or :
- $Soiree = new soiree();
- $formMess=$this->createForm(new SoireeType(), $Soiree);
- if($formMess->handleRequest($request)->isValid()){
- $em = $this->getDoctrine()->getManager();
- $em->persist($Soiree);
- $em->flush();
- }
- if ($request->isMethod('POST')) {
- $request->getSession()->getFlashBag()->add('success', 'Soirée ajoutée !');
- }
- return $this->render('MAMelodieAnimationBundle:News:agenda.html.twig', array(
- 'listSoirees' => $listSoirees,
- 'nbPages'=>$nbPages,
- 'page'=>$page,
- 'form'=> $formMess->createView()
- ));
- }
- public function servicesAction()
- {
- return $this->render('MAMelodieAnimationBundle:News:services.html.twig');
- }
- public function contactAction(Request $request)
- {
- $form = $this->createForm(new ContactType());
- if ($request->isMethod('POST')) {
- $form->bind($request);
- if ($form->isValid()) {
- $message = \Swift_Message::newInstance()
- ->setSubject($form->get('subject')->getData())
- ->setFrom($form->get('email')->getData())
- ->setTo('webmaster@melodie-animation.fr')
- ->setBody(
- $this->renderView(
- 'MAMelodieAnimationBundle:News:mail.html.twig',
- array(
- 'ip' => $request->getClientIp(),
- 'name' => $form->get('name')->getData(),
- 'message' => $form->get('message')->getData()
- )
- )
- );
- $this->get('mailer')->send($message);
- $request->getSession()->getFlashBag()->add('success', 'Votre message à bien été envoyé !');
- return $this->redirect($this->generateUrl('ma_melodie_animation_contact'));
- }
- }
- return $this->render('MAMelodieAnimationBundle:News:contact.html.twig', array(
- 'form'=> $form->createView()
- ));
- }
- public function photosAction(Request $request,$repertoire)
- {
- $dirname = '/srv/http/MelodieAnim/web/galerie';
- if($repertoire!='')
- {
- $images = array();
- $fulldir=$dirname."/".$repertoire;
- if(is_dir($fulldir))
- {
- $dir = opendir($fulldir);
- while($file = readdir($dir))
- {
- if(is_file($fulldir.'/'.$file))
- {
- $images[] = "http://192.168.1.14/MelodieAnim/web/galerie/".$repertoire.'/'.$file;
- }
- }
- closedir($dir);
- }else
- {
- echo 'ISNOT A DIR';
- }
- return $this->render('MAMelodieAnimationBundle:News:photos.html.twig', array('images' => $images,'repertoire'=> $repertoire));
- }else{
- if($dossier = opendir($dirname))
- {
- $dossiers = array();
- while(false !== ($sousdossier = readdir($dossier)))
- {
- if($sousdossier != '.' && $sousdossier != '..')
- {
- $dossiers[] = $sousdossier;
- }
- }
- closedir($dossier);
- }
- else
- {
- echo 'dossier non existant';
- }
- return $this->render('MAMelodieAnimationBundle:News:photos.html.twig',array('dossiers'=>$dossiers));
- }
- }
- public function videosAction(Request $request,$repertoire)
- {
- $dirname = '/srv/http/MelodieAnim/web/videos';
- if($repertoire!='')
- {
- $videos = array();
- $fulldir=$dirname."/".$repertoire;
- if(is_dir($fulldir))
- {
- $dir = opendir($fulldir);
- while($file = readdir($dir))
- {
- if(is_file($fulldir.'/'.$file))
- {
- $videos[] = "http://192.168.1.14/MelodieAnim/web/videos/".$repertoire.'/'.$file;
- }
- }
- closedir($dir);
- }else
- {
- echo 'ISNOT A DIR';
- }
- return $this->render('MAMelodieAnimationBundle:News:videos.html.twig', array('videos' => $videos,'repertoire'=> $repertoire));
- }else{
- if($dossier = opendir($dirname))
- {
- $dossiers = array();
- while(false !== ($sousdossier = readdir($dossier)))
- {
- if($sousdossier != '.' && $sousdossier != '..')
- {
- $dossiers[] = $sousdossier;
- }
- }
- closedir($dossier);
- }
- else
- {
- echo 'dossier non existant';
- }
- return $this->render('MAMelodieAnimationBundle:News:videos.html.twig',array('dossiers'=>$dossiers));
- }
- }
- public function loginAction()
- {
- // get the error if any (works with forward and redirect -- see below)
- if ($this->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
- $error = $this->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
- } else {
- $error = $this->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
- }
- return $this->render('MAMelodieAnimationBundle:News:login.html.twig', array(
- // last username entered by the user
- 'last_username' => $this->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
- 'error' => $error,
- ));
- }
- public function deletenewsAction($id,Request $request)
- {
- // On vérifie que l'utilisateur dispose bien du rôle ROLE_ADMIN
- if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) {
- // Sinon on déclenche une exception « Accès interdit »
- throw new AccessDeniedException('Accès limité aux admins.');
- }
- $em = $this->getDoctrine()->getManager();
- // On récupère l'composant $id
- $news = $em->getRepository('MAMelodieAnimationBundle:News')->find($id);
- if (null === $news) {
- throw new NotFoundHttpException("La news d'id ".$id." n'existe pas.");
- }
- // On crée un formulaire vide, qui ne contiendra que le champ CSRF
- // Cela permet de protéger la suppression d'composant contre cette faille
- $form = $this->createFormBuilder()->getForm();
- $em->remove($news);
- $em->flush();
- $request->getSession()->getFlashBag()->add('success', 'La news à été supprimée !');
- return $this->redirect($this->generateUrl('ma_melodie_animation_homepage'));
- }
- public function deletemessageAction($id,Request $request)
- {
- // On vérifie que l'utilisateur dispose bien du rôle ROLE_ADMIN
- if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) {
- // Sinon on déclenche une exception « Accès interdit »
- throw new AccessDeniedException('Accès limité aux admins.');
- }
- $em = $this->getDoctrine()->getManager();
- // On récupère l'composant $id
- $message = $em->getRepository('MAMelodieAnimationBundle:Message')->find($id);
- if (null === $message) {
- throw new NotFoundHttpException("La message d'id ".$id." n'existe pas.");
- }
- // On crée un formulaire vide, qui ne contiendra que le champ CSRF
- // Cela permet de protéger la suppression d'composant contre cette faille
- $form = $this->createFormBuilder()->getForm();
- $em->remove($message);
- $em->flush();
- $request->getSession()->getFlashBag()->add('success', 'Le message à été supprimé !');
- return $this->redirect($this->generateUrl('ma_melodie_animation_livredor_index'));
- }
- public function deletesoireeAction($id,Request $request)
- {
- // On vérifie que l'utilisateur dispose bien du rôle ROLE_ADMIN
- if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) {
- // Sinon on déclenche une exception « Accès interdit »
- throw new AccessDeniedException('Accès limité aux admins.');
- }
- $em = $this->getDoctrine()->getManager();
- // On récupère l'composant $id
- $soiree = $em->getRepository('MAMelodieAnimationBundle:Soiree')->find($id);
- if (null === $soiree) {
- throw new NotFoundHttpException("La soirée d'id ".$id." n'existe pas.");
- }
- // On crée un formulaire vide, qui ne contiendra que le champ CSRF
- // Cela permet de protéger la suppression d'composant contre cette faille
- $form = $this->createFormBuilder()->getForm();
- $em->remove($soiree);
- $em->flush();
- $request->getSession()->getFlashBag()->add('success', 'La soirée à été supprimé !');
- return $this->redirect($this->generateUrl('ma_melodie_animation_agenda_index'));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement