<?php
namespace App\Crud\Controller;
use App\Crud\Controller\crudPlainController;
/**
* GaleriaController controller.
*
*/
class DropZoneController extends crudPlainController
{
private $IMAGE_HANDLERS = [
IMAGETYPE_JPEG => [
'load' => 'imagecreatefromjpeg',
'save' => 'imagejpeg',
'quality' => 100
],
IMAGETYPE_PNG => [
'load' => 'imagecreatefrompng',
'save' => 'imagepng',
'quality' => 0
]
];
/* protected $maxPerPage = 20;
protected function indexGetEntitiesFor($params)
{
$query = $this->em->getRepository($this->bundleName.'\\Entity\\'.$this->className)->getResultsByFiltros($params, $this->filterData);
$query->setMaxResults($this->maxPerPage);
return $query->getQuery()->getResult();
}
protected function indexCreateParamsForTwig()
{
$params = parent::indexCreateParamsForTwig();
$params['maxPerPage'] = $this->maxPerPage;
$params['collapsed'] = false;
return $params;
}
public function getMoreFotosAction($routeClassName, $firstResult)
{
$this->comunSetInitialValues($routeClassName);
$params = $this->comunParamsForFunctionsInEntity();
$params['showIdInForm'] = $this->showIdInForm;
$query = $this->em->getRepository($this->bundleName.'\\Entity\\'.$this->className)->getResultsByFiltros($params, $this->filterData);
$query->setFirstResult($firstResult);
$query->setMaxResults($this->maxPerPage);
$this->entities = $query->getQuery()->getResult();
$data=array();
foreach ($this->entities as $entity)
{
$urlThumb = $urlFoto = $entity->getFoto()->getUrl();
if ($entity->getThumb())
{
$urlThumb = $entity->getThumb()->getUrl();
}
$data[$entity->getId()]=array('name'=>$entity->getFoto()->getArchivo(), 'size'=>$entity->getFoto()->getSize(), 'urlThumb'=>$urlThumb, 'urlFoto'=>$urlFoto, 'descripcion'=>$entity->__toString());
}
$data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
$data->headers->set('Content-Type', 'application/json');
return $data;
}
*/
public function showGalleryAction()
{
$this->comunSetUtilProperties();
$galeria = $this->em->getRepository('WillycasaBundle:Galeria')->findOneBy(array('nombre'=>'COMPTOIR'));
$params = array();
$params['controlador'] = 'WillycasaBundle:Galeria:index';
$params['params'] = array('routeClassName'=>'publicgaleriafoto', 'filterData'=>array('galeria'=>$galeria->getId()));
return $this->render('WillycasaBundle:Default:showGallery.html.twig', $params);
}
protected function createRedirectOnSuccess($entity)
{
$this->session->getFlashBag()->clear();
$data=array('ok'=>true, 'id'=>$entity->getId());
$data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
$data->headers->set('Content-Type', 'application/json');
return $data;
}
protected function createRedirectOnFail($entity)
{
$data=array('ok'=>false, 'error' => $this->getError());
$data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
$data->headers->set('Content-Type', 'application/json');
return $data;
}
protected function updateRedirectOnSuccess($entity)
{
$this->session->getFlashBag()->clear();
$data=array('ok'=>true, 'id'=>$entity->getId());
$data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
$data->headers->set('Content-Type', 'application/json');
return $data;
}
protected function updateRedirectOnFail($entity)
{
$data=array('ok'=>false, 'error' => $this->getError());
$data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
$data->headers->set('Content-Type', 'application/json');
return $data;
}
public function uploadAction($routeClassName, $parentId=-1)
{
$this->setInitialValues('all', $routeClassName);
$file=$this->request->files;
$filedata=$file->get('file');
if ($filedata)
{
$hashKey = '';
$uploadDir = 'uploads';
$fileName = $this->comunUpload($filedata, $uploadDir, $hashKey);
if ($fileName)
{
$associations = $this->em->getClassMetadata($this->classFullName)->associationMappings;
$class = $associations['adjunto']['targetEntity'];
$adjunto = new $class();
$adjunto->setArchivo($filedata->getClientOriginalName());
$adjunto->setRuta($uploadDir);
$adjunto->setArchivoHash($fileName);
$adjunto->setSize($filedata->getClientSize());
$this->em->persist($adjunto);
$this->em->flush();
$entity = $this->createEntity();
$entity->setFieldValue('adjunto', $adjunto);
// $entity->setFieldValue('foto', $adjunto);
/* if ($entity->allowThumb())
{
$thumb = $this->createThumbOnTheFly($adjunto->getAccesoArchivo(), $adjunto->getRuta(), 'thumb_'.$adjunto->getArchivoHash(), $class, $adjunto->getArchivo());
$entity->setFieldValue('thumb', $thumb);
}*/
$parentEntity = $entity->getParentClass();
if ($parentEntity)
{
$parentEntity = $this->em->getRepository($parentEntity)->find($parentId);
$entity->setParent($parentEntity);
}
foreach ($this->filterData as $field=>$valor)
{
if (isset($associations[$field]))
{
$class = $associations[$field]['targetEntity'];
$link = $this->em->getRepository($class)->find($valor);
$set = 'set'.ucfirst($field);
$entity->$set($link);
}
}
//$entity->setValues();
$this->em->persist($entity);
$this->em->flush();
$entity->createDoExtraWorkAfter($this->comunParamsForFunctionsInEntity());
}
}
$data = new \Symfony\Component\HttpFoundation\Response($entity->getId());
return $data;
}
private function createThumbOnTheFly($file, $dir, $dest, $class, $originalName)
{
try
{
$type = \exif_imagetype($file);
if ($type && $this->IMAGE_HANDLERS[$type])
{
// load the image with the correct loader
$image = call_user_func($this->IMAGE_HANDLERS[$type]['load'], $file);
if ($image)
{
// get original image width and height
$width = imagesx($image);
$height = imagesy($image);
// get width to height ratio
$ratio = $width / $height;
$targetWidth = 150;
// if is portrait
// use ratio to scale height to fit in square
if ($width > $height)
{
$targetHeight = floor($targetWidth / $ratio);
}
// if is landscape
// use ratio to scale width to fit in square
else
{
$targetHeight = $targetWidth;
$targetWidth = floor($targetWidth * $ratio);
}
// create duplicate image based on calculated target size
$thumb = imagecreatetruecolor($targetWidth, $targetHeight);
// set transparency options for GIFs and PNGs
if ($type == IMAGETYPE_PNG)
{
// make image transparent
imagecolortransparent(
$thumb,
imagecolorallocate($thumb, 0, 0, 0)
);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
}
// copy entire source image to duplicate image and resize
imagecopyresampled(
$thumb,
$image,
0, 0, 0, 0,
$targetWidth, $targetHeight,
$width, $height
);
// 3. Save the $thumbnail to disk
// - call the correct save method
// - set the correct quality level
// save the duplicate version of the image to disk
call_user_func(
$this->IMAGE_HANDLERS[$type]['save'],
$thumb,
$dir.'/'.$dest,
$this->IMAGE_HANDLERS[$type]['quality']
);
$adjunto = new $class();
$adjunto->setArchivo($originalName);
$adjunto->setRuta($dir);
$adjunto->setArchivoHash($dest);
$adjunto->setSize(0);
$this->em->persist($adjunto);
$this->em->flush();
return $adjunto;
}
}
return null;
}
catch (\Exception $exc) {
return null;
}
}
public function deleteFotoAction($routeClassName, $id)
{
$this->setInitialValues('all', $routeClassName);
$entity = $this->em->getRepository($this->classFullName)->find($id);
if ($entity)
{
$foto = $entity->getAdjunto()->getRuta().'/'.$entity->getAdjunto()->getArchivoHash();
unlink($foto);
/* if ($entity->getThumb())
{
unlink($entity->getThumb()->getAccesoArchivo());
}*/
$this->em->remove($entity);
$this->em->flush();
}
$data=array('ok'=>true);
$data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
$data->headers->set('Content-Type', 'application/json');
return $data;
}
public function describirFotoAction($routeClassName, $id)
{
$this->setInitialValues('all', $routeClassName);
$entity = $this->em->getRepository($this->classFullName)->find($id);
if ($entity)
{
return $this->redirect($this->generateUrl('crud_index', array('routeClassName'=>$routeClassName.'texto', 'parentRouteClassName'=>$routeClassName, 'filterData'=>array('foto'=>$id))));
}
return '';
}
public function uploadSingleFileAction($routeClassName, $field, $parentId=-1)
{
$this->setInitialValues('all', $routeClassName);
$file=$this->request->files;
$filedata=$file->get('file');
if ($filedata)
{
$hashKey = '';
$uploadDir = 'uploads';
$fileName = $this->comunUpload($filedata, $uploadDir, $hashKey);
if ($fileName)
{
$associations = $this->em->getClassMetadata($this->classFullName)->associationMappings;
$class = $associations[$field]['targetEntity'];
$adjunto = new $class();
$adjunto->setArchivo($filedata->getClientOriginalName());
$adjunto->setRuta($uploadDir);
$adjunto->setArchivoHash($fileName);
$adjunto->setSize(filesize($uploadDir.'/'.$fileName));
$this->em->persist($adjunto);
$this->em->flush();
$parentEntity = $this->em->getRepository($this->classFullName)->find($parentId);
$set = 'set'.ucfirst($field);
$parentEntity->$set($adjunto);
$this->em->persist($parentEntity);
$this->em->flush();
//$parentEntity->createDoExtraWorkAfter($this->comunParamsForFunctionsInEntity());
}
}
$data = new \Symfony\Component\HttpFoundation\Response($parentEntity->getId());
return $data;
}
public function deleteSingleFileAction($routeClassName, $id, $field)
{
if ($id!='undefined')
{
$this->setInitialValues('all', $routeClassName);
$entity = $this->em->getRepository($this->classFullName)->find($id);
if ($entity)
{
$adjunto = $entity->getFieldValue($field);
$file = $adjunto->getRuta().'/'.$adjunto->getArchivoHash();
unlink($file);
$set = 'set'.ucfirst($field);
$entity->$set(null);
$this->em->flush();
}
}
$data=array('ok'=>true);
$data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
$data->headers->set('Content-Type', 'application/json');
return $data;
}
public function showSingleDropZoneAction($parentId, $routeClassName, $field, $acceptedFiles)
{
$this->setInitialValues('all', $routeClassName);
$entity = $this->em->getRepository($this->bundleName.'\\Entity\\'.$this->className)->find($parentId);
if (!$entity)
throw new \RuntimeException($this->translator->trans(
'error.instance_not_found',
array('%className%' => $this->routeClassName.'/'.$this->className,
'%id%' => $id),
'crud'));
$paramsForTwigShow = $this->showCreateParamsForTwig($entity);
$paramsForTwigShow['texto']='ARRASTRE LA FOTO HACIA EL CUADRO AZUL O HAGA CLICK SOBRE ÉL PARA ADJUNTARLA. ';
//return $this->render($this->twigShowName, $paramsForTwigShow);
return $this->render('@crud\crud\dropZone_single.html.twig', $paramsForTwigShow);
}
}