Advertisement
Garethp

Controller

Jul 7th, 2014
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Garethp\PhotosBundle\Controller;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Garethp\PhotosBundle\Entity\Photo;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8. use Symfony\Component\HttpFoundation\Request;
  9.  
  10. class DefaultController extends Controller
  11. {
  12.     public function indexAction()
  13.     {
  14.         error_reporting(E_ALL);
  15.         ini_set('display_errors', 1);
  16.  
  17.         try
  18.         {
  19.         $repo = $this->getDoctrine()->getRepository('GarethpPhotosBundle:Photo');
  20.         $photos = $repo->createQueryBuilder('p')
  21.             ->addOrderBy('p.timestamp', 'DESC')
  22.             ->addOrderBy('p.id', 'DESC')
  23.             ->getQuery()
  24.             ;
  25.  
  26.         $photos = $photos->getResult();
  27.  
  28.         return $this->render('GarethpPhotosBundle:Default:index.html.twig', array('photos'=>$photos));
  29.  
  30.         }
  31.         catch(Exception $e)
  32.         {
  33.             var_dump($e);
  34.         }
  35.     }
  36.     public function uploadAction(Request $request)
  37.     {
  38.         $photo = new Photo();
  39.         $form = $this->createFormBuilder(null, array('csrf_protection'=>false))
  40.             ->add('file', 'file', array(
  41.                 'multiple'=>true,
  42.                 'attr'=>array(
  43.                     'accept'=>'image/*',
  44.                     'capture'=>'camera',
  45.                     'multiple'=>'multiple'
  46.                 )
  47.             ))
  48.             ->add('upload', 'submit')
  49.             ->getForm();
  50.  
  51.         $form->handleRequest($request);
  52.  
  53.         if($form->isValid())
  54.         {
  55.             $em = $this->getDoctrine()->getManager();
  56.  
  57.             $data = $form->getData();
  58.             $files = $data['file'];
  59.  
  60.             foreach($files as $file)
  61.             {
  62.                 $photo = new Photo();
  63.                 $photo->setFile($file);
  64.                 $em->persist($file);
  65.             }
  66.  
  67. //          $em->persist($photo);
  68.  
  69.             $em->flush();
  70.  
  71.             return $this->redirect($this->generateUrl('garethp_photos_homepage'));
  72.         }
  73.  
  74.         return $this->render('GarethpPhotosBundle:Default:upload.html.twig', array('form'=>$form->createView()));
  75.     }
  76.  
  77.     public function videoAction()
  78.     {
  79.         return $this->render('GarethpPhotosBundle:Default:video.html.twig');
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement