This commit is contained in:
2025-09-30 22:59:52 +02:00
parent b0894e0267
commit 29e5daad9a
2 changed files with 198 additions and 276 deletions

View File

@@ -3,10 +3,9 @@
namespace App\Controller; namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 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\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
class CropController extends AbstractController class CropController extends AbstractController
{ {
@@ -24,7 +23,7 @@ class CropController extends AbstractController
public function crop02(Request $request) public function crop02(Request $request)
{ {
// Récupération de l'image à cropper // 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 // Récupérer les tailles de l'image
$width = $this->getWidth($large_image_location); $width = $this->getWidth($large_image_location);
@@ -41,7 +40,7 @@ class CropController extends AbstractController
// Construction du formulaire // Construction du formulaire
$form = $this->createFormBuilder() $form = $this->createFormBuilder()
->add('submit',SubmitType::class,array("label" => "Valider","attr" => array("class" => "btn btn-success","onclick" => "reportThumb()"))) ->add('submit', SubmitType::class, ['label' => 'Valider', 'attr' => ['class' => 'btn btn-success', 'onclick' => 'reportThumb()']])
->add('x', HiddenType::class) ->add('x', HiddenType::class)
->add('y', HiddenType::class) ->add('y', HiddenType::class)
->add('w', HiddenType::class) ->add('w', HiddenType::class)
@@ -52,12 +51,12 @@ class CropController extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
// Sur validation on généère la miniature croppée // 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 // Récupération des valeurs du formulaire
$data = $form->getData(); $data = $form->getData();
$thumb_image_location = "uploads/avatar/thumb_".$this->get('session')->get('uploadavatar');; $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); $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', [ return $this->render('Crop/crop02.html.twig', [
@@ -69,67 +68,74 @@ class CropController extends AbstractController
} }
// Calcul de la hauteur // Calcul de la hauteur
protected function getHeight($image) { protected function getHeight($image)
{
$size = getimagesize($image); $size = getimagesize($image);
$height = $size[1]; $height = $size[1];
return $height; return $height;
} }
// Cacul de la largeur // Cacul de la largeur
protected function getWidth($image) { protected function getWidth($image)
{
$size = getimagesize($image); $size = getimagesize($image);
$width = $size[0]; $width = $size[0];
return $width; return $width;
} }
protected function resizeImage($image,$width,$height,$scale) { protected function resizeImage($image, $width, $height, $scale)
{
list($imagewidth, $imageheight, $imageType) = getimagesize($image); list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType); $imageType = image_type_to_mime_type($imageType);
$newImageWidth = ceil($width * $scale); $newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale); $newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight); $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
switch ($imageType) { switch ($imageType) {
case "image/gif": case 'image/gif':
$source = imagecreatefromgif($image); $source = imagecreatefromgif($image);
break; break;
case "image/pjpeg": case 'image/pjpeg':
case "image/jpeg": case 'image/jpeg':
case "image/jpg": case 'image/jpg':
$source = imagecreatefromjpeg($image); $source = imagecreatefromjpeg($image);
break; break;
case "image/png": case 'image/png':
case "image/x-png": case 'image/x-png':
$source = imagecreatefrompng($image); $source = imagecreatefrompng($image);
break; break;
case "image/webp": case 'image/webp':
$source = imagecreatefromwebp($image); $source = imagecreatefromwebp($image);
break; break;
} }
imagecopyresampled($newImage, $source, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height); imagecopyresampled($newImage, $source, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);
switch ($imageType) { switch ($imageType) {
case "image/gif": case 'image/gif':
imagegif($newImage, $image); imagegif($newImage, $image);
break; break;
case "image/pjpeg": case 'image/pjpeg':
case "image/jpeg": case 'image/jpeg':
case "image/jpg": case 'image/jpg':
imagejpeg($newImage, $image, 90); imagejpeg($newImage, $image, 90);
break; break;
case "image/png": case 'image/png':
case "image/x-png": case 'image/x-png':
imagepng($newImage, $image); imagepng($newImage, $image);
break; break;
case "image/webp": case 'image/webp':
imagewebp($newImage, $image, 90); imagewebp($newImage, $image, 90);
break; break;
} }
chmod($image, 0640); chmod($image, 0640);
return $image; return $image;
} }
protected function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){ protected function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale)
{
list($imagewidth, $imageheight, $imageType) = getimagesize($image); list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType); $imageType = image_type_to_mime_type($imageType);
@@ -137,42 +143,42 @@ class CropController extends AbstractController
$newImageHeight = ceil($height * $scale); $newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight); $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
switch ($imageType) { switch ($imageType) {
case "image/gif": case 'image/gif':
$source = imagecreatefromgif($image); $source = imagecreatefromgif($image);
break; break;
case "image/pjpeg": case 'image/pjpeg':
case "image/jpeg": case 'image/jpeg':
case "image/jpg": case 'image/jpg':
$source = imagecreatefromjpeg($image); $source = imagecreatefromjpeg($image);
break; break;
case "image/png": case 'image/png':
case "image/x-png": case 'image/x-png':
$source = imagecreatefrompng($image); $source = imagecreatefrompng($image);
break; break;
case "image/webp": case 'image/webp':
$source = imagecreatefromwebp($image); $source = imagecreatefromwebp($image);
break; break;
} }
imagecopyresampled($newImage, $source, 0, 0, $start_width, $start_height, $newImageWidth, $newImageHeight, $width, $height); imagecopyresampled($newImage, $source, 0, 0, $start_width, $start_height, $newImageWidth, $newImageHeight, $width, $height);
switch ($imageType) { switch ($imageType) {
case "image/gif": case 'image/gif':
imagegif($newImage, $thumb_image_name); imagegif($newImage, $thumb_image_name);
break; break;
case "image/pjpeg": case 'image/pjpeg':
case "image/jpeg": case 'image/jpeg':
case "image/jpg": case 'image/jpg':
imagejpeg($newImage, $thumb_image_name, 90); imagejpeg($newImage, $thumb_image_name, 90);
break; break;
case "image/png": case 'image/png':
case "image/x-png": case 'image/x-png':
imagepng($newImage, $thumb_image_name); imagepng($newImage, $thumb_image_name);
break; break;
case "image/webp": case 'image/webp':
imagewebp($newImage, $thumb_image_name, 90); imagewebp($newImage, $thumb_image_name, 90);
break; break;
} }
chmod($thumb_image_name, 0640); chmod($thumb_image_name, 0640);
return $thumb_image_name; return $thumb_image_name;
} }
} }

View File

@@ -2,179 +2,95 @@
namespace App\Controller; 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\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Form\FormError;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use App\Entity\Customer as Entity;
use App\Form\CustomerType as Form;
class CustomerController extends AbstractController class CustomerController extends AbstractController
{ {
private $data = "customer"; private EntityManagerInterface $em;
private $route = "app_customer"; private CustomerRepository $customerRepository;
private $render = "Customer/";
private $entity = "App:Customer";
private $knpSnappy; public function __construct(EntityManagerInterface $em, CustomerRepository $customerRepository)
public function __construct(\Knp\Snappy\Pdf $knpSnappy) { $this->knpSnappy = $knpSnappy; }
public function list(Request $request)
{ {
$em = $this->getDoctrine()->getManager(); $this->em = $em;
$datas = $em->getRepository($this->entity)->findAll(); $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,
]);
return new PdfResponse(
$this->knpSnappy->getOutputFromHtml($render),
'clients.pdf'
);
} }
else {
return $this->render($this->render.'list.html.twig',[ public function list()
$this->data."s" => $datas, {
"useheader" => true, $customers = $this->customerRepository->findAll();
"usesidebar" => true,
return $this->render('Customer/list.html.twig', [
'customers' => $customers,
'useheader' => true,
'usesidebar' => true,
]); ]);
} }
}
public function submit(Request $request) public function submit(Request $request)
{ {
// Initialisation de l'enregistrement $customer = new Customer();
$em = $this->getDoctrine()->getManager(); $form = $this->createForm(CustomerType::class, $customer, ['mode' => 'submit']);
$data = new Entity();
// Création du formulaire
$form = $this->createForm(Form::class,$data,array("mode"=>"submit"));
// Récupération des data du formulaire
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($customer);
$this->em->flush();
// Sur erreur return $this->redirectToRoute('app_customer');
$this->getErrorForm(null,$form,$request,$data,"submit");
// 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);
} }
// Affichage du formulaire return $this->render('Customer/edit.html.twig', [
return $this->render($this->render.'edit.html.twig', [
'useheader' => true, 'useheader' => true,
'usesidebar' => true, 'usesidebar' => true,
$this->data => $data, 'customer' => $customer,
'mode' => 'submit', 'mode' => 'submit',
'form' => $form->createView() 'form' => $form->createView(),
]); ]);
} }
public function update($id, Request $request) public function update($id, Request $request)
{ {
// Initialisation de l'enregistrement $customer = $this->customerRepository->find($id);
$em = $this->getDoctrine()->getManager(); if (!$customer) {
$data=$em->getRepository($this->entity)->find($id); 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->handleRequest($request);
// 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);
} }
// Affichage du formulaire $form = $this->createForm(CustomerType::class, $customer, ['mode' => 'update']);
if($request->query->get('fgprint')) { $form->handleRequest($request);
$render = $this->renderView($this->render.'edit.html.twig', [ if ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
return $this->redirectToRoute('app_customer');
}
return $this->render('Customer/edit.html.twig', [
'useheader' => true, 'useheader' => true,
'usesidebar' => true, 'usesidebar' => true,
$this->data => $data, 'customer' => $customer,
'mode' => 'update', 'mode' => 'update',
'form' => $form->createView(), '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()
]); ]);
} }
}
public function delete($id,Request $request) public function delete($id)
{ {
// Initialisation de l'enregistrement $customer = $this->customerRepository->find($id);
$em = $this->getDoctrine()->getManager(); if (!$customer) {
$data=$em->getRepository($this->entity)->find($id); throw new NotFoundHttpException('La ressource demandée est introuvable.');
}
// Controle avant suppression
$error=false;
if($error)
return $this->redirectToRoute($this->route."_update",["id"=>$id]);
else {
try { try {
$em->remove($data); $this->em->remove($customer);
$em->flush(); $this->em->flush();
} } catch (\Exception $e) {
catch(\Doctrine\DBAL\DBALException $e) { $this->addflash('error', $e->getMessage());
// 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('app_customer');
return $this->redirectToRoute($this->route);
}
}
protected function getErrorForm($id,$form,$request,$data,$mode) {
if ($form->get('submit')->isClicked()&&$mode=="delete") {
}
if ($form->get('submit')->isClicked() && $mode=="submit") {
}
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());
}
}
} }
} }