Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.83 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Msi\CmsBundle\Controller;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  9. use Msi\CmsBundle\Entity\News;
  10. use Msi\CmsBundle\Form\NewsType;
  11. use Symfony\Component\HttpFoundation\File\UploadedFile;
  12. /**
  13.  * News controller.
  14.  *
  15.  * @Route("/news")
  16.  */
  17. class NewsController extends Controller
  18. {
  19.     /**
  20.      * Lists all News entities.
  21.      *
  22.      * @Route("/", name="news")
  23.      * @Template()
  24.      */
  25.     public function indexAction()
  26.     {
  27.         $em = $this->getDoctrine()->getEntityManager();
  28.  
  29.         $entities = $em->getRepository('MsiCmsBundle:News')->findAll();
  30.  
  31.         return array('entities' => $entities);
  32.     }
  33.  
  34.     /**
  35.      * Finds and displays a News entity.
  36.      *
  37.      * @Route("/{id}/show", name="news_show")
  38.      * @Template()
  39.      */
  40.     public function showAction($id)
  41.     {
  42.         $em = $this->getDoctrine()->getEntityManager();
  43.  
  44.         $entity = $em->getRepository('MsiCmsBundle:News')->find($id);
  45.  
  46.         if (!$entity) {
  47.             throw $this->createNotFoundException('Unable to find News entity.');
  48.         }
  49.  
  50.         $deleteForm = $this->createDeleteForm($id);
  51.  
  52.         return array(
  53.             'entity'      => $entity,
  54.             'delete_form' => $deleteForm->createView(),
  55.         );
  56.     }
  57.  
  58.     /**
  59.      * Displays a form to create a new News entity.
  60.      *
  61.      * @Route("/new", name="news_new")
  62.      * @Template()
  63.      */
  64.     public function newAction()
  65.     {
  66.         $entity = new News();
  67.         $form   = $this->createForm(new NewsType(), $entity);
  68.  
  69.         return array(
  70.             'entity' => $entity,
  71.             'form'   => $form->createView()
  72.         );
  73.     }
  74.  
  75.     /**
  76.      * Creates a new News entity.
  77.      *
  78.      * @Route("/create", name="news_create")
  79.      * @Method("post")
  80.      * @Template("MsiCmsBundle:News:new.html.twig")
  81.      */
  82.     public function createAction()
  83.     {
  84.         $entity  = new News();
  85.         $request = $this->getRequest();
  86.         $form    = $this->createForm(new NewsType(), $entity);
  87.  
  88.         if ('POST' === $request->getMethod()) {
  89.             $form->bindRequest($request);
  90.  
  91.             if ($form->isValid()) {
  92.  
  93.                 $files=$request->files->get($form->getName());
  94.                 print_r($files);               
  95.                 $file=$files["attachment"]["file"];
  96.  
  97.                 $file->move(
  98.                     $_SERVER['DOCUMENT_ROOT']."/web/uploads",
  99.                     $file->getOriginalName()
  100.                 );
  101.                 $em = $this->getDoctrine()->getEntityManager();
  102.                 $entity->upload();
  103.                 $em->persist($entity);
  104.                 $em->flush();
  105.  
  106.                 return $this->redirect($this->generateUrl('news'));
  107.                
  108.             }
  109.         }
  110.  
  111.         return array(
  112.             'entity' => $entity,
  113.             'form'   => $form->createView()
  114.         );
  115.     }
  116.  
  117.     /**
  118.      * Displays a form to edit an existing News entity.
  119.      *
  120.      * @Route("/{id}/edit", name="news_edit")
  121.      * @Template()
  122.      */
  123.     public function editAction($id)
  124.     {
  125.         $em = $this->getDoctrine()->getEntityManager();
  126.  
  127.         $entity = $em->getRepository('MsiCmsBundle:News')->find($id);
  128.  
  129.         if (!$entity) {
  130.             throw $this->createNotFoundException('Unable to find News entity.');
  131.         }
  132.  
  133.         $editForm = $this->createForm(new NewsType(), $entity);
  134.         $deleteForm = $this->createDeleteForm($id);
  135.  
  136.         return array(
  137.             'entity'      => $entity,
  138.             'form'   => $editForm->createView(),
  139.             'delete_form' => $deleteForm->createView(),
  140.         );
  141.     }
  142.  
  143.     /**
  144.      * Edits an existing News entity.
  145.      *
  146.      * @Route("/{id}/update", name="news_update")
  147.      * @Method("post")
  148.      * @Template("MsiCmsBundle:News:edit.html.twig")
  149.      */
  150.     public function updateAction($id)
  151.     {
  152.         $em = $this->getDoctrine()->getEntityManager();
  153.  
  154.         $entity = $em->getRepository('MsiCmsBundle:News')->find($id);
  155.  
  156.         if (!$entity) {
  157.             throw $this->createNotFoundException('Unable to find News entity.');
  158.         }
  159.  
  160.         $editForm   = $this->createForm(new NewsType(), $entity);
  161.         $deleteForm = $this->createDeleteForm($id);
  162.  
  163.         $request = $this->getRequest();
  164.  
  165.         if ('POST' === $request->getMethod()) {
  166.             $editForm->bindRequest($request);
  167.  
  168.             if ($editForm->isValid()) {
  169.                 $em = $this->getDoctrine()->getEntityManager();
  170.                 $em = $this->getDoctrine()->getEntityManager();
  171.                 $entity->upload();                
  172.                 $em->persist($entity);
  173.                 $em->flush();
  174.  
  175.                 return $this->redirect($this->generateUrl('news'));
  176.             }
  177.         }
  178.  
  179.         return array(
  180.             'entity'      => $entity,
  181.             'edit_form'   => $editForm->createView(),
  182.             'delete_form' => $deleteForm->createView(),
  183.         );
  184.     }
  185.  
  186.     /**
  187.      * Deletes a News entity.
  188.      *
  189.      * @Route("/{id}/delete", name="news_delete")
  190.      * @Method("post")
  191.      */
  192.     public function deleteAction($id)
  193.     {
  194.         $em = $this->getDoctrine()->getEntityManager();
  195.         $entity = $em->getRepository('MsiCmsBundle:News')->find($id);
  196.  
  197.         if (!$entity) {
  198.             throw $this->createNotFoundException('Unable to find News entity.');
  199.         }
  200.  
  201.         $em->remove($entity);
  202.         $em->flush();
  203.  
  204.         return $this->redirect($this->generateUrl('news'));
  205.     }
  206.  
  207.     private function createDeleteForm($id)
  208.     {
  209.         return $this->createFormBuilder(array('id' => $id))
  210.             ->add('id', 'hidden')
  211.             ->getForm()
  212.         ;
  213.     }
  214.    
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement