Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1. <?php
  2.  
  3. namespace PimBundle\Controller\Admin;
  4.  
  5. use PimBundle\Entity\Image;
  6. use PimBundle\Form\Admin\UploadImageType;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10.  
  11. /**
  12.  * Class ImageController
  13.  * @package PimBundle\Controller\Admin
  14.  */
  15. class ImageController extends Controller
  16. {
  17.     /**
  18.      * @param Request $request
  19.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  20.      */
  21.     public function indexAction(Request $request)
  22.     {
  23.         $em = $this->getDoctrine()->getManager();
  24.         $images = $em->getRepository('PimBundle:Image')->getAll();
  25.  
  26.         $image = new Image();
  27.         $form = $this->createForm(new UploadImageType(), $image, array(
  28.             'action' => $this->generateUrl('pim_admin_image_index'),
  29.             'method' => 'POST',
  30.         ));
  31.         $form->handleRequest($request);
  32.  
  33.         if ($form->isValid()) {
  34.             $em->persist($image);
  35.             $em->flush();
  36.  
  37.             return $this->redirectToRoute('pim_admin_image_index');
  38.         }
  39.  
  40.         return $this->render('PimBundle:Admin:imagesList.html.twig', array(
  41.             'images' => $images,
  42.             'form' => $form->createView(),
  43.         ));
  44.     }
  45.  
  46.     public function listAction()
  47.     {
  48.         $em = $this->getDoctrine()->getManager();
  49.         $images = $em->getRepository('PimBundle:Image')->getAll();
  50.         $response = array();
  51.  
  52.         foreach ($images as $image) {
  53.             $response[] = array(
  54.                 'title' => (
  55.                 $image->getTitle() ?
  56.                     'no title (' . date_format($image->getCreatedAt(), 'H:i M j') . ')' :
  57.                     $image->getTitle()
  58.                 ),
  59.                 'value' => $image->getSrc(),
  60.             );
  61.         }
  62.  
  63.         return new JsonResponse($response);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement