Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.89 KB | None | 0 0
  1. <?php
  2.  
  3. namespace MA\MelodieAnimationBundle\Controller;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use MA\MelodieAnimationBundle\Entity\News;
  9. use MA\MelodieAnimationBundle\Entity\Message;
  10. use MA\MelodieAnimationBundle\Entity\Soiree;
  11. use MA\MelodieAnimationBundle\Form\SoireeType;
  12. use MA\MelodieAnimationBundle\Form\NewsType;
  13. use MA\MelodieAnimationBundle\Form\MessageType;
  14. use MA\MelodieAnimationBundle\Form\ContactType;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Security\Core\SecurityContext;
  17.  
  18. class NewsController extends Controller
  19. {
  20.     // On récupère tous les paramètres en arguments de la méthode
  21.     public function indexAction($page,Request $request)
  22.     {
  23.  
  24.         $nbPerPage = 3;
  25.         // On ne sait pas combien de pages il y a
  26.         // Mais on sait qu'une page doit être supérieure ou égale à 1
  27.         if ($page < 1) {
  28.           $page=1;
  29.         }
  30.  
  31.         $listNews = $this->getDoctrine()->getManager()->getRepository('MAMelodieAnimationBundle:News')->getNews($page,$nbPerPage);
  32.  
  33.         $nbPages = ceil(count($listNews)/$nbPerPage);
  34.         if($nbPages === 0.0){
  35.             $nbPages=1;
  36.         }
  37.        
  38.         // Si la page n'existe pas, on retourne une 404
  39.         if ($page > $nbPages) {
  40.           throw $this->createNotFoundException("La page ".$page." n'existe pas.");
  41.         }
  42.  
  43.         // si on nous envoie un message sur le livre d'or :
  44.         $news = new news();
  45.     $formMess=$this->createForm(new NewsType(), $news);
  46.  
  47.     if($formMess->handleRequest($request)->isValid()){
  48.       $em = $this->getDoctrine()->getManager();
  49.       $em->persist($news);
  50.       $em->flush();
  51.     }
  52.      if ($request->isMethod('POST')) {
  53.           $request->getSession()->getFlashBag()->add('success', 'News ajoutée !');
  54.         }
  55.  
  56.         return $this->render('MAMelodieAnimationBundle:News:index.html.twig', array(
  57.       'listNews' => $listNews,'nbPages'=>$nbPages,'page'=>$page,
  58.     'form'=> $formMess->createView()
  59.     ));
  60.     }
  61.  
  62.     public function addAction(Request $request)
  63.     {
  64.         $news=new News();
  65.         $news->setTitre("Nouvelle news de test");
  66.         $news->setContenu("Voici une news de test , son contenu est inutile");
  67.         $news->setAuteur("Kevin");
  68.  
  69.         $em= $this->getDoctrine()->getManager();
  70.  
  71.         $em->persist($news);
  72.  
  73.             // Étape 2 : On « flush » tout ce qui a été persisté avant
  74.         $em->flush();
  75.  
  76.         // Reste de la méthode qu'on avait déjà écrit
  77.         if ($request->isMethod('POST')) {
  78.           $request->getSession()->getFlashBag()->add('notice', 'Annonce bien enregistrée.');
  79.         }
  80.  
  81.         return $this->render('MAMelodieAnimationBundle:News:add.html.twig');
  82.     }
  83.  
  84.     public function livreorAction(Request $request,$page)
  85.     {
  86.  
  87.         $nbPerPage = 10;
  88.         // On ne sait pas combien de pages il y a
  89.         // Mais on sait qu'une page doit être supérieure ou égale à 1
  90.         if ($page < 1) {
  91.           $page=1;
  92.         }
  93.  
  94.          $em = $this->getDoctrine()->getManager();
  95.    
  96.  
  97.  
  98.      $listMessages = $em->getRepository('MAMelodieAnimationBundle:Message')->getMessages($page,$nbPerPage);  
  99.        
  100.          $nbPages = ceil(count($listMessages)/$nbPerPage);
  101.                 if($nbPages === 0.0){
  102.                     $nbPages=1;
  103.                 }
  104.        
  105.  
  106.        
  107.         // Si la page n'existe pas, on retourne une 404
  108.         if ($page > $nbPages) {
  109.           throw $this->createNotFoundException("La page ".$page." n'existe pas.");
  110.         }
  111.  
  112.         // si on nous envoie un message sur le livre d'or :
  113.         $message = new message();
  114.     $formMess=$this->createForm(new MessageType(), $message);
  115.  
  116.     if($formMess->handleRequest($request)->isValid()){
  117.       $em = $this->getDoctrine()->getManager();
  118.       $em->persist($message);
  119.       $em->flush();
  120.     }
  121.      if ($request->isMethod('POST')) {
  122.           $request->getSession()->getFlashBag()->add('notice', 'Message enregistré sur le livre d\'or !');
  123.         }
  124.  
  125.  
  126.  
  127.  
  128.  
  129.         return $this->render('MAMelodieAnimationBundle:News:livredor.html.twig', array(
  130.       'listMessages' => $listMessages,
  131.       'nbPages'=>$nbPages,
  132.       'page'=>$page,
  133.         'form'=> $formMess->createView()
  134.       ));
  135.     }
  136.  
  137.         // On récupère tous les paramètres en arguments de la méthode
  138.     public function presentationAction()
  139.     {
  140.  
  141.         return $this->render('MAMelodieAnimationBundle:News:presentation.html.twig');
  142.     }
  143.  
  144.     public function agendaAction($page,Request $request)
  145.     {
  146.  
  147.         $nbPerPage = 10;
  148.         // On ne sait pas combien de pages il y a
  149.         // Mais on sait qu'une page doit être supérieure ou égale à 1
  150.         if ($page < 1) {
  151.           $page=1;
  152.         }
  153.  
  154.         $listSoirees = $this->getDoctrine()->getManager()->getRepository('MAMelodieAnimationBundle:Soiree')->getSoirees($page,$nbPerPage);
  155.  
  156.         $nbPages = ceil(count($listSoirees)/$nbPerPage);
  157.         if($nbPages === 0.0){
  158.             $nbPages=1;
  159.         }
  160.        
  161.         // Si la page n'existe pas, on retourne une 404
  162.         if ($page > $nbPages) {
  163.           throw $this->createNotFoundException("La page ".$page." n'existe pas.");
  164.         }
  165.  
  166.  
  167.         // si on nous envoie un message sur le livre d'or :
  168.         $Soiree = new soiree();
  169.     $formMess=$this->createForm(new SoireeType(), $Soiree);
  170.  
  171.     if($formMess->handleRequest($request)->isValid()){
  172.       $em = $this->getDoctrine()->getManager();
  173.       $em->persist($Soiree);
  174.       $em->flush();
  175.     }
  176.      if ($request->isMethod('POST')) {
  177.           $request->getSession()->getFlashBag()->add('success', 'Soirée ajoutée !');
  178.         }
  179.  
  180.  
  181.         return $this->render('MAMelodieAnimationBundle:News:agenda.html.twig', array(
  182.       'listSoirees' => $listSoirees,
  183.       'nbPages'=>$nbPages,
  184.       'page'=>$page,
  185.     'form'=> $formMess->createView()
  186.     ));
  187.     }
  188.  
  189.  
  190.     public function servicesAction()
  191.     {
  192.  
  193.         return $this->render('MAMelodieAnimationBundle:News:services.html.twig');
  194.     }
  195.  
  196.     public function contactAction(Request $request)
  197.     {
  198.         $form = $this->createForm(new ContactType());
  199.  
  200.         if ($request->isMethod('POST')) {
  201.             $form->bind($request);
  202.  
  203.             if ($form->isValid()) {
  204.                 $message = \Swift_Message::newInstance()
  205.                     ->setSubject($form->get('subject')->getData())
  206.                     ->setFrom($form->get('email')->getData())
  207.                     ->setTo('webmaster@melodie-animation.fr')
  208.                     ->setBody(
  209.                         $this->renderView(
  210.                             'MAMelodieAnimationBundle:News:mail.html.twig',
  211.                             array(
  212.                                 'ip' => $request->getClientIp(),
  213.                                 'name' => $form->get('name')->getData(),
  214.                                 'message' => $form->get('message')->getData()
  215.                             )
  216.                         )
  217.                     );
  218.  
  219.                 $this->get('mailer')->send($message);
  220.  
  221.                 $request->getSession()->getFlashBag()->add('success', 'Votre message à bien été envoyé !');
  222.  
  223.                 return $this->redirect($this->generateUrl('ma_melodie_animation_contact'));
  224.             }
  225.         }
  226.         return $this->render('MAMelodieAnimationBundle:News:contact.html.twig', array(
  227.             'form'=> $form->createView()
  228.           ));
  229.     }
  230.  
  231.     public function photosAction(Request $request,$repertoire)
  232.     {
  233.  
  234.          $dirname = '/srv/http/MelodieAnim/web/galerie';
  235.         if($repertoire!='')
  236.         {
  237.             $images = array();
  238.             $fulldir=$dirname."/".$repertoire;
  239.             if(is_dir($fulldir))
  240.             {
  241.                 $dir = opendir($fulldir);
  242.              
  243.                 while($file = readdir($dir))
  244.                 {
  245.                     if(is_file($fulldir.'/'.$file))
  246.                     {
  247.                         $images[] = "http://192.168.1.14/MelodieAnim/web/galerie/".$repertoire.'/'.$file;
  248.                     }  
  249.                 }
  250.      
  251.                 closedir($dir);
  252.                  
  253.             }else
  254.             {
  255.                 echo 'ISNOT A DIR';
  256.             }
  257.  
  258.  
  259.             return $this->render('MAMelodieAnimationBundle:News:photos.html.twig', array('images' => $images,'repertoire'=> $repertoire));
  260.         }else{
  261.             if($dossier = opendir($dirname))
  262.             {  
  263.                 $dossiers = array();
  264.                 while(false !== ($sousdossier = readdir($dossier)))
  265.                        {
  266.                             if($sousdossier != '.' && $sousdossier != '..')
  267.                             {
  268.                                 $dossiers[] = $sousdossier;
  269.                                
  270.                             }
  271.                        }
  272.                 closedir($dossier);
  273.             }
  274.             else
  275.             {
  276.              echo 'dossier non existant';
  277.             }
  278.             return $this->render('MAMelodieAnimationBundle:News:photos.html.twig',array('dossiers'=>$dossiers));
  279.         }
  280.     }
  281.  
  282.     public function videosAction(Request $request,$repertoire)
  283.     {
  284.  
  285.          $dirname = '/srv/http/MelodieAnim/web/videos';
  286.         if($repertoire!='')
  287.         {
  288.             $videos = array();
  289.             $fulldir=$dirname."/".$repertoire;
  290.             if(is_dir($fulldir))
  291.             {
  292.                 $dir = opendir($fulldir);
  293.              
  294.                 while($file = readdir($dir))
  295.                 {
  296.                     if(is_file($fulldir.'/'.$file))
  297.                     {
  298.                    
  299.                         $videos[] = "http://192.168.1.14/MelodieAnim/web/videos/".$repertoire.'/'.$file;
  300.                     }  
  301.                 }
  302.      
  303.                 closedir($dir);
  304.                  
  305.             }else
  306.             {
  307.                 echo 'ISNOT A DIR';
  308.             }
  309.  
  310.  
  311.             return $this->render('MAMelodieAnimationBundle:News:videos.html.twig', array('videos' => $videos,'repertoire'=> $repertoire));
  312.         }else{
  313.             if($dossier = opendir($dirname))
  314.             {  
  315.                 $dossiers = array();
  316.                 while(false !== ($sousdossier = readdir($dossier)))
  317.                        {
  318.                             if($sousdossier != '.' && $sousdossier != '..')
  319.                             {
  320.                                 $dossiers[] = $sousdossier;
  321.                                
  322.                             }
  323.                        }
  324.                 closedir($dossier);
  325.             }
  326.             else
  327.             {
  328.              echo 'dossier non existant';
  329.             }
  330.             return $this->render('MAMelodieAnimationBundle:News:videos.html.twig',array('dossiers'=>$dossiers));
  331.         }
  332.     }
  333.  
  334.     public function loginAction()
  335.     {
  336.        // get the error if any (works with forward and redirect -- see below)
  337.           if ($this->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
  338.           $error = $this->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
  339.       } else {
  340.           $error = $this->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
  341.       }
  342.  
  343.  
  344.       return $this->render('MAMelodieAnimationBundle:News:login.html.twig', array(
  345.           // last username entered by the user
  346.           'last_username' => $this->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
  347.           'error' => $error,
  348.       ));
  349.     }
  350.  
  351.  
  352.    public function deletenewsAction($id,Request $request)
  353.   {
  354.     // On vérifie que l'utilisateur dispose bien du rôle ROLE_ADMIN
  355.     if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) {
  356.       // Sinon on déclenche une exception « Accès interdit »
  357.       throw new AccessDeniedException('Accès limité aux admins.');
  358.     }
  359.     $em = $this->getDoctrine()->getManager();
  360.  
  361.     // On récupère l'composant $id
  362.     $news = $em->getRepository('MAMelodieAnimationBundle:News')->find($id);
  363.  
  364.     if (null === $news) {
  365.       throw new NotFoundHttpException("La news d'id ".$id." n'existe pas.");
  366.     }
  367.  
  368.     // On crée un formulaire vide, qui ne contiendra que le champ CSRF
  369.     // Cela permet de protéger la suppression d'composant contre cette faille
  370.     $form = $this->createFormBuilder()->getForm();
  371.  
  372.       $em->remove($news);
  373.       $em->flush();
  374.     $request->getSession()->getFlashBag()->add('success', 'La news à été supprimée !');
  375.  
  376.     return $this->redirect($this->generateUrl('ma_melodie_animation_homepage'));
  377.    
  378.   }
  379.  
  380.   public function deletemessageAction($id,Request $request)
  381.   {
  382.     // On vérifie que l'utilisateur dispose bien du rôle ROLE_ADMIN
  383.     if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) {
  384.       // Sinon on déclenche une exception « Accès interdit »
  385.       throw new AccessDeniedException('Accès limité aux admins.');
  386.     }
  387.     $em = $this->getDoctrine()->getManager();
  388.  
  389.     // On récupère l'composant $id
  390.     $message = $em->getRepository('MAMelodieAnimationBundle:Message')->find($id);
  391.  
  392.     if (null === $message) {
  393.       throw new NotFoundHttpException("La message d'id ".$id." n'existe pas.");
  394.     }
  395.  
  396.     // On crée un formulaire vide, qui ne contiendra que le champ CSRF
  397.     // Cela permet de protéger la suppression d'composant contre cette faille
  398.     $form = $this->createFormBuilder()->getForm();
  399.  
  400.       $em->remove($message);
  401.       $em->flush();
  402.  
  403.     $request->getSession()->getFlashBag()->add('success', 'Le message à été supprimé !');
  404.     return $this->redirect($this->generateUrl('ma_melodie_animation_livredor_index'));
  405.    
  406.   }
  407.  
  408.   public function deletesoireeAction($id,Request $request)
  409.   {
  410.     // On vérifie que l'utilisateur dispose bien du rôle ROLE_ADMIN
  411.     if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) {
  412.       // Sinon on déclenche une exception « Accès interdit »
  413.       throw new AccessDeniedException('Accès limité aux admins.');
  414.     }
  415.     $em = $this->getDoctrine()->getManager();
  416.  
  417.     // On récupère l'composant $id
  418.     $soiree = $em->getRepository('MAMelodieAnimationBundle:Soiree')->find($id);
  419.  
  420.     if (null === $soiree) {
  421.       throw new NotFoundHttpException("La soirée d'id ".$id." n'existe pas.");
  422.     }
  423.  
  424.     // On crée un formulaire vide, qui ne contiendra que le champ CSRF
  425.     // Cela permet de protéger la suppression d'composant contre cette faille
  426.     $form = $this->createFormBuilder()->getForm();
  427.  
  428.       $em->remove($soiree);
  429.       $em->flush();
  430.  
  431.     $request->getSession()->getFlashBag()->add('success', 'La soirée à été supprimé !');
  432.     return $this->redirect($this->generateUrl('ma_melodie_animation_agenda_index'));
  433.    
  434.   }
  435.  
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement