diff --git a/src/Controller/CropController.php b/src/Controller/CropController.php index 7d0b369..211b5fa 100755 --- a/src/Controller/CropController.php +++ b/src/Controller/CropController.php @@ -3,20 +3,19 @@ namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\HttpFoundation\Request; class CropController extends AbstractController { // Etape 01 - Téléchargement de l'image public function crop01() { - return $this->render('Crop/crop01.html.twig',[ - 'useheader' => false, - 'usemenu' => false, - 'usesidebar' => false, + return $this->render('Crop/crop01.html.twig', [ + 'useheader' => false, + 'usemenu' => false, + 'usesidebar' => false, ]); } @@ -24,155 +23,162 @@ class CropController extends AbstractController public function crop02(Request $request) { // Récupération de l'image à cropper - $large_image_location = "uploads/avatar/".$this->get('session')->get('uploadavatar'); + $large_image_location = 'uploads/avatar/'.$this->get('session')->get('uploadavatar'); // Récupérer les tailles de l'image - $width = $this->getWidth($large_image_location); - $height = $this->getHeight($large_image_location); + $width = $this->getWidth($large_image_location); + $height = $this->getHeight($large_image_location); - // Définir le pourcentage de réduction de l'image - $max_height=400000; - $max_width=800; - $scale = $max_height/$height; - if(($width*$scale)>$max_width) { - $scale = $max_width/$width; - } - $this->resizeImage($large_image_location,$width,$height,$scale); + // Définir le pourcentage de réduction de l'image + $max_height = 400000; + $max_width = 800; + $scale = $max_height / $height; + if (($width * $scale) > $max_width) { + $scale = $max_width / $width; + } + $this->resizeImage($large_image_location, $width, $height, $scale); // Construction du formulaire $form = $this->createFormBuilder() - ->add('submit',SubmitType::class,array("label" => "Valider","attr" => array("class" => "btn btn-success","onclick" => "reportThumb()"))) - ->add('x',HiddenType::class) - ->add('y',HiddenType::class) - ->add('w',HiddenType::class) - ->add('h',HiddenType::class) + ->add('submit', SubmitType::class, ['label' => 'Valider', 'attr' => ['class' => 'btn btn-success', 'onclick' => 'reportThumb()']]) + ->add('x', HiddenType::class) + ->add('y', HiddenType::class) + ->add('w', HiddenType::class) + ->add('h', HiddenType::class) ->getForm(); // Récupération des data du formulaire $form->handleRequest($request); // Sur validation on généère la miniature croppée - if ($form->get('submit')->isClicked() && $form->isValid()) { + if ($form->isSubmitted() && $form->isValid()) { // Récupération des valeurs du formulaire $data = $form->getData(); - $thumb_image_location = "uploads/avatar/thumb_".$this->get('session')->get('uploadavatar');; - $cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$data["w"],$data["h"],$data["x"],$data["y"],$scale); + $thumb_image_location = 'uploads/avatar/thumb_'.$this->get('session')->get('uploadavatar'); + $cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location, $data['w'], $data['h'], $data['x'], $data['y'], $scale); } return $this->render('Crop/crop02.html.twig', [ - 'useheader' => false, - 'usemenu' => false, - 'usesidebar' => false, - 'form' => $form->createView(), + 'useheader' => false, + 'usemenu' => false, + 'usesidebar' => false, + 'form' => $form->createView(), ]); } - // Calcul de la hauteur - protected function getHeight($image) { - $size = getimagesize($image); - $height = $size[1]; - return $height; - } + // Calcul de la hauteur + protected function getHeight($image) + { + $size = getimagesize($image); + $height = $size[1]; - // Cacul de la largeur - protected function getWidth($image) { - $size = getimagesize($image); - $width = $size[0]; - return $width; - } + return $height; + } - protected function resizeImage($image,$width,$height,$scale) { - list($imagewidth, $imageheight, $imageType) = getimagesize($image); - $imageType = image_type_to_mime_type($imageType); - $newImageWidth = ceil($width * $scale); - $newImageHeight = ceil($height * $scale); - $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); - switch($imageType) { - case "image/gif": - $source=imagecreatefromgif($image); - break; - case "image/pjpeg": - case "image/jpeg": - case "image/jpg": - $source=imagecreatefromjpeg($image); - break; - case "image/png": - case "image/x-png": - $source=imagecreatefrompng($image); - break; - case "image/webp": - $source=imagecreatefromwebp($image); - break; - } - imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height); + // Cacul de la largeur + protected function getWidth($image) + { + $size = getimagesize($image); + $width = $size[0]; - switch($imageType) { - case "image/gif": - imagegif($newImage,$image); - break; - case "image/pjpeg": - case "image/jpeg": - case "image/jpg": - imagejpeg($newImage,$image,90); - break; - case "image/png": - case "image/x-png": - imagepng($newImage,$image); - break; - case "image/webp": - imagewebp($newImage,$image,90); - break; - } + return $width; + } - chmod($image, 0640); - return $image; - } + protected function resizeImage($image, $width, $height, $scale) + { + list($imagewidth, $imageheight, $imageType) = getimagesize($image); + $imageType = image_type_to_mime_type($imageType); + $newImageWidth = ceil($width * $scale); + $newImageHeight = ceil($height * $scale); + $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight); + switch ($imageType) { + case 'image/gif': + $source = imagecreatefromgif($image); + break; + case 'image/pjpeg': + case 'image/jpeg': + case 'image/jpg': + $source = imagecreatefromjpeg($image); + break; + case 'image/png': + case 'image/x-png': + $source = imagecreatefrompng($image); + break; + case 'image/webp': + $source = imagecreatefromwebp($image); + break; + } + imagecopyresampled($newImage, $source, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height); - protected function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){ - list($imagewidth, $imageheight, $imageType) = getimagesize($image); - $imageType = image_type_to_mime_type($imageType); + switch ($imageType) { + case 'image/gif': + imagegif($newImage, $image); + break; + case 'image/pjpeg': + case 'image/jpeg': + case 'image/jpg': + imagejpeg($newImage, $image, 90); + break; + case 'image/png': + case 'image/x-png': + imagepng($newImage, $image); + break; + case 'image/webp': + imagewebp($newImage, $image, 90); + break; + } - $newImageWidth = ceil($width * $scale); - $newImageHeight = ceil($height * $scale); - $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); - switch($imageType) { - case "image/gif": - $source=imagecreatefromgif($image); - break; - case "image/pjpeg": - case "image/jpeg": - case "image/jpg": - $source=imagecreatefromjpeg($image); - break; - case "image/png": - case "image/x-png": - $source=imagecreatefrompng($image); - break; - case "image/webp": - $source=imagecreatefromwebp($image); - break; - } - imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height); - switch($imageType) { - case "image/gif": - imagegif($newImage,$thumb_image_name); - break; - case "image/pjpeg": - case "image/jpeg": - case "image/jpg": - imagejpeg($newImage,$thumb_image_name,90); - break; - case "image/png": - case "image/x-png": - imagepng($newImage,$thumb_image_name); - break; - case "image/webp": - imagewebp($newImage,$thumb_image_name,90); - break; - } - chmod($thumb_image_name, 0640); - return $thumb_image_name; - } + chmod($image, 0640); + return $image; + } + + protected function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale) + { + list($imagewidth, $imageheight, $imageType) = getimagesize($image); + $imageType = image_type_to_mime_type($imageType); + + $newImageWidth = ceil($width * $scale); + $newImageHeight = ceil($height * $scale); + $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight); + switch ($imageType) { + case 'image/gif': + $source = imagecreatefromgif($image); + break; + case 'image/pjpeg': + case 'image/jpeg': + case 'image/jpg': + $source = imagecreatefromjpeg($image); + break; + case 'image/png': + case 'image/x-png': + $source = imagecreatefrompng($image); + break; + case 'image/webp': + $source = imagecreatefromwebp($image); + break; + } + imagecopyresampled($newImage, $source, 0, 0, $start_width, $start_height, $newImageWidth, $newImageHeight, $width, $height); + switch ($imageType) { + case 'image/gif': + imagegif($newImage, $thumb_image_name); + break; + case 'image/pjpeg': + case 'image/jpeg': + case 'image/jpg': + imagejpeg($newImage, $thumb_image_name, 90); + break; + case 'image/png': + case 'image/x-png': + imagepng($newImage, $thumb_image_name); + break; + case 'image/webp': + imagewebp($newImage, $thumb_image_name, 90); + break; + } + chmod($thumb_image_name, 0640); + + return $thumb_image_name; + } } diff --git a/src/Controller/CustomerController.php b/src/Controller/CustomerController.php index 74f990e..0c3f83b 100755 --- a/src/Controller/CustomerController.php +++ b/src/Controller/CustomerController.php @@ -2,179 +2,95 @@ namespace App\Controller; +use App\Entity\Customer; +use App\Form\CustomerType; +use App\Repository\CustomerRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Form\FormError; -use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse; - -use App\Entity\Customer as Entity; -use App\Form\CustomerType as Form; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class CustomerController extends AbstractController { - private $data = "customer"; - private $route = "app_customer"; - private $render = "Customer/"; - private $entity = "App:Customer"; - - private $knpSnappy; - public function __construct(\Knp\Snappy\Pdf $knpSnappy) { $this->knpSnappy = $knpSnappy; } + private EntityManagerInterface $em; + private CustomerRepository $customerRepository; - public function list(Request $request) + public function __construct(EntityManagerInterface $em, CustomerRepository $customerRepository) { - $em = $this->getDoctrine()->getManager(); - $datas = $em->getRepository($this->entity)->findAll(); + $this->em = $em; + $this->customerRepository = $customerRepository; + } - if($request->query->get('fgprint')) { - $render = $this->renderView($this->render.'list.html.twig',[ - $this->data."s" => $datas, - "useheader" => true, - "usesidebar" => true, - "fgprint" => true, - ]); + public function list() + { + $customers = $this->customerRepository->findAll(); - return new PdfResponse( - $this->knpSnappy->getOutputFromHtml($render), - 'clients.pdf' - ); - } - else { - return $this->render($this->render.'list.html.twig',[ - $this->data."s" => $datas, - "useheader" => true, - "usesidebar" => true, - ]); - } + return $this->render('Customer/list.html.twig', [ + 'customers' => $customers, + 'useheader' => true, + 'usesidebar' => true, + ]); } public function submit(Request $request) { - // Initialisation de l'enregistrement - $em = $this->getDoctrine()->getManager(); - $data = new Entity(); - - // Création du formulaire - $form = $this->createForm(Form::class,$data,array("mode"=>"submit")); - - // Récupération des data du formulaire + $customer = new Customer(); + $form = $this->createForm(CustomerType::class, $customer, ['mode' => 'submit']); $form->handleRequest($request); - - // Sur erreur - $this->getErrorForm(null,$form,$request,$data,"submit"); - - // Sur validation - if ($form->get('submit')->isClicked() && $form->isValid()) { - $data = $form->getData(); - $em->persist($data); - $em->flush(); + if ($form->isSubmitted() && $form->isValid()) { + $this->em->persist($customer); + $this->em->flush(); - // Retour à la liste - return $this->redirectToRoute($this->route); + return $this->redirectToRoute('app_customer'); } - - // Affichage du formulaire - return $this->render($this->render.'edit.html.twig', [ - 'useheader' => true, - 'usesidebar' => true, - $this->data => $data, - 'mode' => 'submit', - 'form' => $form->createView() + + return $this->render('Customer/edit.html.twig', [ + 'useheader' => true, + 'usesidebar' => true, + 'customer' => $customer, + 'mode' => 'submit', + 'form' => $form->createView(), ]); - } - - public function update($id,Request $request) + } + + public function update($id, Request $request) { - // Initialisation de l'enregistrement - $em = $this->getDoctrine()->getManager(); - $data=$em->getRepository($this->entity)->find($id); + $customer = $this->customerRepository->find($id); + if (!$customer) { + throw new NotFoundHttpException('La ressource demandée est introuvable.'); + } - // Création du formulaire - $form = $this->createForm(Form::class,$data,array("mode"=>"update")); - - // Récupération des data du formulaire + $form = $this->createForm(CustomerType::class, $customer, ['mode' => 'update']); $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $this->em->flush(); - // Sur erreur - $this->getErrorForm(null,$form,$request,$data,"update"); - - // Sur validation - if ($form->get('submit')->isClicked() && $form->isValid()) { - $data = $form->getData(); - $em->persist($data); - $em->flush(); - - // Retour à la liste - return $this->redirectToRoute($this->route); + return $this->redirectToRoute('app_customer'); } - - // Affichage du formulaire - if($request->query->get('fgprint')) { - $render = $this->renderView($this->render.'edit.html.twig', [ - 'useheader' => true, - 'usesidebar' => true, - $this->data => $data, - 'mode' => 'update', - 'form' => $form->createView(), - "fgprint" => true, - ]); - return new PdfResponse( - $this->knpSnappy->getOutputFromHtml($render), - 'client.pdf' - ); - } - else { - return $this->render($this->render.'edit.html.twig', [ - 'useheader' => true, - 'usesidebar' => true, - $this->data => $data, - 'mode' => 'update', - 'form' => $form->createView() - ]); - } - } + return $this->render('Customer/edit.html.twig', [ + 'useheader' => true, + 'usesidebar' => true, + 'customer' => $customer, + 'mode' => 'update', + 'form' => $form->createView(), + ]); + } - public function delete($id,Request $request) + public function delete($id) { - // Initialisation de l'enregistrement - $em = $this->getDoctrine()->getManager(); - $data=$em->getRepository($this->entity)->find($id); - - // Controle avant suppression - $error=false; - if($error) - return $this->redirectToRoute($this->route."_update",["id"=>$id]); - else { - try { - $em->remove($data); - $em->flush(); - } - catch(\Doctrine\DBAL\DBALException $e) { - // Création du formulaire - $this->get('session')->getFlashBag()->add('error', 'Impossible de supprimer cet enregistrement'); - return $this->redirectToRoute($this->route."_update",["id"=>$id]); - } - - // Retour à la liste - return $this->redirectToRoute($this->route); - } - } - - protected function getErrorForm($id,$form,$request,$data,$mode) { - if ($form->get('submit')->isClicked()&&$mode=="delete") { + $customer = $this->customerRepository->find($id); + if (!$customer) { + throw new NotFoundHttpException('La ressource demandée est introuvable.'); } - if ($form->get('submit')->isClicked() && $mode=="submit") { + try { + $this->em->remove($customer); + $this->em->flush(); + } catch (\Exception $e) { + $this->addflash('error', $e->getMessage()); } - if ($form->get('submit')->isClicked() && !$form->isValid()) { - $this->get('session')->getFlashBag()->clear(); - - $errors = $form->getErrors(); - foreach( $errors as $error ) { - $request->getSession()->getFlashBag()->add("error", $error->getMessage()); - } - } - } + return $this->redirectToRoute('app_customer'); + } }