src/Crud/Controller/DropZoneController.php line 330

Open in your IDE?
  1. <?php
  2. namespace App\Crud\Controller;
  3. use App\Crud\Controller\crudPlainController;
  4. /**
  5.  * GaleriaController controller.
  6.  *
  7.  */
  8. class DropZoneController extends crudPlainController
  9. {
  10.     private $IMAGE_HANDLERS = [
  11.             IMAGETYPE_JPEG => [
  12.                 'load' => 'imagecreatefromjpeg',
  13.                 'save' => 'imagejpeg',
  14.                 'quality' => 100
  15.             ],
  16.             IMAGETYPE_PNG => [
  17.                 'load' => 'imagecreatefrompng',
  18.                 'save' => 'imagepng',
  19.                 'quality' => 0
  20.             ]
  21.         ];
  22. /*    protected $maxPerPage = 20;
  23.     
  24.     protected function indexGetEntitiesFor($params)
  25.     {
  26.         $query = $this->em->getRepository($this->bundleName.'\\Entity\\'.$this->className)->getResultsByFiltros($params, $this->filterData);
  27.         $query->setMaxResults($this->maxPerPage);
  28.         return $query->getQuery()->getResult();
  29.     }
  30.     protected function indexCreateParamsForTwig()
  31.     {
  32.        $params = parent::indexCreateParamsForTwig(); 
  33.        $params['maxPerPage'] = $this->maxPerPage;
  34.        $params['collapsed'] = false;
  35.        return $params;
  36.     }
  37.     public function getMoreFotosAction($routeClassName, $firstResult)
  38.     {
  39.         $this->comunSetInitialValues($routeClassName);
  40.         
  41.         $params = $this->comunParamsForFunctionsInEntity();
  42.         $params['showIdInForm'] = $this->showIdInForm;
  43.         $query = $this->em->getRepository($this->bundleName.'\\Entity\\'.$this->className)->getResultsByFiltros($params, $this->filterData);
  44.         $query->setFirstResult($firstResult);
  45.         $query->setMaxResults($this->maxPerPage);
  46.         $this->entities = $query->getQuery()->getResult();
  47.         $data=array();
  48.         foreach ($this->entities as $entity)
  49.         {
  50.             $urlThumb = $urlFoto = $entity->getFoto()->getUrl();
  51.             if ($entity->getThumb())
  52.             {
  53.                 $urlThumb = $entity->getThumb()->getUrl();
  54.             }
  55.             $data[$entity->getId()]=array('name'=>$entity->getFoto()->getArchivo(), 'size'=>$entity->getFoto()->getSize(), 'urlThumb'=>$urlThumb, 'urlFoto'=>$urlFoto, 'descripcion'=>$entity->__toString());
  56.         }
  57.         $data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
  58.         $data->headers->set('Content-Type', 'application/json');
  59.         return $data;        
  60.     }
  61. */
  62.     public function showGalleryAction()
  63.     {
  64.         $this->comunSetUtilProperties();
  65.         $galeria $this->em->getRepository('WillycasaBundle:Galeria')->findOneBy(array('nombre'=>'COMPTOIR'));
  66.         $params = array();
  67.         $params['controlador'] = 'WillycasaBundle:Galeria:index';
  68.         $params['params'] = array('routeClassName'=>'publicgaleriafoto''filterData'=>array('galeria'=>$galeria->getId()));
  69.         return $this->render('WillycasaBundle:Default:showGallery.html.twig'$params);
  70.     }
  71.     protected function createRedirectOnSuccess($entity)
  72.     {
  73.         $this->session->getFlashBag()->clear();
  74.         $data=array('ok'=>true'id'=>$entity->getId());
  75.         $data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
  76.         $data->headers->set('Content-Type''application/json');
  77.         return $data;        
  78.     } 
  79.     
  80.     protected function createRedirectOnFail($entity)
  81.     {
  82.         $data=array('ok'=>false'error' => $this->getError());  
  83.         $data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
  84.         $data->headers->set('Content-Type''application/json');
  85.         return $data;        
  86.     }   
  87.     protected function updateRedirectOnSuccess($entity)
  88.     {
  89.         $this->session->getFlashBag()->clear();
  90.         $data=array('ok'=>true'id'=>$entity->getId());
  91.         $data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
  92.         $data->headers->set('Content-Type''application/json');
  93.         return $data;        
  94.     } 
  95.     
  96.     protected function updateRedirectOnFail($entity)
  97.     {
  98.         $data=array('ok'=>false'error' => $this->getError());  
  99.         $data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
  100.         $data->headers->set('Content-Type''application/json');
  101.         return $data;        
  102.     }   
  103.       
  104.     public function uploadAction($routeClassName$parentId=-1)
  105.     {
  106.         $this->setInitialValues('all'$routeClassName);
  107.         $file=$this->request->files;
  108.         $filedata=$file->get('file');
  109.         if ($filedata
  110.         {
  111.             $hashKey '';    
  112.             $uploadDir 'uploads';
  113.             $fileName $this->comunUpload($filedata$uploadDir$hashKey);
  114.             if ($fileName)
  115.             {
  116.                 $associations $this->em->getClassMetadata($this->classFullName)->associationMappings;
  117.                 $class $associations['adjunto']['targetEntity'];
  118.                 $adjunto = new $class();
  119.                 $adjunto->setArchivo($filedata->getClientOriginalName());
  120.                 $adjunto->setRuta($uploadDir);
  121.                 $adjunto->setArchivoHash($fileName);
  122.                 $adjunto->setSize($filedata->getClientSize());
  123.                 $this->em->persist($adjunto);
  124.                 $this->em->flush();
  125.                 
  126.                 $entity $this->createEntity();
  127.                 $entity->setFieldValue('adjunto'$adjunto);
  128. //                $entity->setFieldValue('foto', $adjunto);
  129.                 
  130. /*                if ($entity->allowThumb())
  131.                 {
  132.                     $thumb = $this->createThumbOnTheFly($adjunto->getAccesoArchivo(), $adjunto->getRuta(), 'thumb_'.$adjunto->getArchivoHash(), $class, $adjunto->getArchivo());
  133.                     $entity->setFieldValue('thumb', $thumb);
  134.                 }*/
  135.                 $parentEntity $entity->getParentClass();
  136.                 if ($parentEntity)
  137.                 {
  138.                     $parentEntity $this->em->getRepository($parentEntity)->find($parentId);
  139.                     $entity->setParent($parentEntity);
  140.                 }
  141.                 foreach ($this->filterData as $field=>$valor)
  142.                 {
  143.                     if (isset($associations[$field]))
  144.                     {
  145.                         $class $associations[$field]['targetEntity'];
  146.                         $link $this->em->getRepository($class)->find($valor);
  147.                         $set 'set'.ucfirst($field); 
  148.                         $entity->$set($link);
  149.                     }
  150.                 }
  151.                 //$entity->setValues();
  152.                 $this->em->persist($entity);
  153.                 $this->em->flush();
  154.                 $entity->createDoExtraWorkAfter($this->comunParamsForFunctionsInEntity());
  155.             }
  156.         }
  157.         $data = new \Symfony\Component\HttpFoundation\Response($entity->getId());
  158.         return $data;        
  159.     } 
  160.     
  161.     private function createThumbOnTheFly($file$dir$dest$class$originalName)
  162.     {
  163.         try
  164.         {
  165.             $type \exif_imagetype($file);
  166.             if ($type && $this->IMAGE_HANDLERS[$type]) 
  167.             {
  168.                 // load the image with the correct loader
  169.                 $image call_user_func($this->IMAGE_HANDLERS[$type]['load'], $file);
  170.                 if ($image)
  171.                 {
  172.                     // get original image width and height
  173.                     $width imagesx($image);
  174.                     $height imagesy($image);
  175.                     // get width to height ratio
  176.                     $ratio $width $height;
  177.                     $targetWidth 150;   
  178.                     // if is portrait
  179.                     // use ratio to scale height to fit in square
  180.                     if ($width $height
  181.                     {
  182.                         $targetHeight floor($targetWidth $ratio);
  183.                     }
  184.                     // if is landscape
  185.                     // use ratio to scale width to fit in square
  186.                     else
  187.                     {
  188.                         $targetHeight $targetWidth;
  189.                         $targetWidth floor($targetWidth $ratio);
  190.                     }
  191.                     // create duplicate image based on calculated target size
  192.                     $thumb imagecreatetruecolor($targetWidth$targetHeight);
  193.                     // set transparency options for GIFs and PNGs
  194.                     if ($type == IMAGETYPE_PNG)
  195.                     {
  196.                         // make image transparent
  197.                         imagecolortransparent(
  198.                             $thumb,
  199.                             imagecolorallocate($thumb000)
  200.                         );
  201.                         imagealphablending($thumbfalse);
  202.                         imagesavealpha($thumbtrue);
  203.                     }
  204.                     // copy entire source image to duplicate image and resize
  205.                     imagecopyresampled(
  206.                         $thumb,
  207.                         $image,
  208.                         0000,
  209.                         $targetWidth$targetHeight,
  210.                         $width$height
  211.                     );
  212.                     // 3. Save the $thumbnail to disk
  213.                     // - call the correct save method
  214.                     // - set the correct quality level
  215.                     // save the duplicate version of the image to disk
  216.                     call_user_func(
  217.                         $this->IMAGE_HANDLERS[$type]['save'],
  218.                         $thumb,
  219.                         $dir.'/'.$dest,
  220.                         $this->IMAGE_HANDLERS[$type]['quality']
  221.                     );
  222.                     $adjunto = new $class();
  223.                     $adjunto->setArchivo($originalName);
  224.                     $adjunto->setRuta($dir);
  225.                     $adjunto->setArchivoHash($dest);
  226.                     $adjunto->setSize(0);
  227.                     $this->em->persist($adjunto);
  228.                     $this->em->flush();
  229.                     return $adjunto;
  230.                 }
  231.             }
  232.             return null;
  233.         }
  234.         catch (\Exception $exc) {
  235.             return null;
  236.         }
  237.     }
  238.     
  239.     public function deleteFotoAction($routeClassName$id)
  240.     {
  241.         $this->setInitialValues('all'$routeClassName);
  242.         $entity $this->em->getRepository($this->classFullName)->find($id);
  243.         if ($entity)
  244.         {
  245.             $foto $entity->getAdjunto()->getRuta().'/'.$entity->getAdjunto()->getArchivoHash();
  246.             unlink($foto);
  247. /*            if ($entity->getThumb())
  248.             {
  249.                 unlink($entity->getThumb()->getAccesoArchivo());
  250.             }*/
  251.             $this->em->remove($entity);
  252.             $this->em->flush();
  253.         }    
  254.         $data=array('ok'=>true);
  255.         $data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
  256.         $data->headers->set('Content-Type''application/json');
  257.         return $data;        
  258.     }    
  259.     
  260.     public function describirFotoAction($routeClassName$id)
  261.     {
  262.         $this->setInitialValues('all'$routeClassName);
  263.         $entity $this->em->getRepository($this->classFullName)->find($id);
  264.         if ($entity)
  265.         {
  266.             return $this->redirect($this->generateUrl('crud_index', array('routeClassName'=>$routeClassName.'texto''parentRouteClassName'=>$routeClassName'filterData'=>array('foto'=>$id))));
  267.         }    
  268.         return '';        
  269.     }    
  270.     
  271.     public function uploadSingleFileAction($routeClassName$field$parentId=-1)
  272.     {
  273.         $this->setInitialValues('all'$routeClassName);
  274.         $file=$this->request->files;
  275.         $filedata=$file->get('file');
  276.         if ($filedata
  277.         {
  278.             $hashKey '';    
  279.             $uploadDir 'uploads';
  280.             $fileName $this->comunUpload($filedata$uploadDir$hashKey);
  281.             if ($fileName)
  282.             {
  283.                 $associations $this->em->getClassMetadata($this->classFullName)->associationMappings;
  284.                 $class $associations[$field]['targetEntity'];
  285.                 $adjunto = new $class();
  286.                 $adjunto->setArchivo($filedata->getClientOriginalName());
  287.                 $adjunto->setRuta($uploadDir);
  288.                 $adjunto->setArchivoHash($fileName);
  289.                 $adjunto->setSize(filesize($uploadDir.'/'.$fileName));
  290.                 $this->em->persist($adjunto);
  291.                 $this->em->flush();
  292.                 
  293.                 $parentEntity $this->em->getRepository($this->classFullName)->find($parentId);
  294.                 $set 'set'.ucfirst($field);
  295.                 $parentEntity->$set($adjunto);
  296.                 $this->em->persist($parentEntity);
  297.                 $this->em->flush();
  298.                 //$parentEntity->createDoExtraWorkAfter($this->comunParamsForFunctionsInEntity());
  299.             }
  300.         }
  301.         $data = new \Symfony\Component\HttpFoundation\Response($parentEntity->getId());
  302.         return $data;        
  303.     } 
  304.     
  305.     public function deleteSingleFileAction($routeClassName$id$field)
  306.     {
  307.         if ($id!='undefined')
  308.         {         
  309.             $this->setInitialValues('all'$routeClassName);
  310.             $entity $this->em->getRepository($this->classFullName)->find($id);
  311.             if ($entity)
  312.             {
  313.                 $adjunto $entity->getFieldValue($field);
  314.                 $file $adjunto->getRuta().'/'.$adjunto->getArchivoHash();
  315.                 unlink($file);
  316.                 $set 'set'.ucfirst($field);
  317.                 $entity->$set(null);
  318.                 $this->em->flush();
  319.             }    
  320.         }
  321.         $data=array('ok'=>true);
  322.         $data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
  323.         $data->headers->set('Content-Type''application/json');
  324.         return $data;        
  325.     }    
  326.     public function showSingleDropZoneAction($parentId$routeClassName$field$acceptedFiles)
  327.     {
  328.         $this->setInitialValues('all'$routeClassName);
  329.         $entity $this->em->getRepository($this->bundleName.'\\Entity\\'.$this->className)->find($parentId);
  330.         if (!$entity
  331.           throw new \RuntimeException($this->translator->trans(
  332.                                                      'error.instance_not_found',
  333.                                                      array('%className%' => $this->routeClassName.'/'.$this->className,
  334.                                                            '%id%'        => $id),
  335.                                                      'crud'));
  336.         $paramsForTwigShow $this->showCreateParamsForTwig($entity);
  337.         $paramsForTwigShow['texto']='ARRASTRE LA FOTO HACIA EL CUADRO AZUL O HAGA CLICK SOBRE ÉL PARA ADJUNTARLA. ';
  338.         //return $this->render($this->twigShowName, $paramsForTwigShow);        
  339.         
  340.         return $this->render('@crud\crud\dropZone_single.html.twig'$paramsForTwigShow);
  341.     }
  342.     
  343. }