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;
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
{
@@ -24,7 +23,7 @@ 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);
@@ -41,7 +40,7 @@ class CropController extends AbstractController
// Construction du formulaire
$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('y', HiddenType::class)
->add('w', HiddenType::class)
@@ -52,12 +51,12 @@ class CropController extends AbstractController
$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', [
@@ -69,67 +68,74 @@ class CropController extends AbstractController
}
// Calcul de la hauteur
protected function getHeight($image) {
protected function getHeight($image)
{
$size = getimagesize($image);
$height = $size[1];
return $height;
}
// Cacul de la largeur
protected function getWidth($image) {
protected function getWidth($image)
{
$size = getimagesize($image);
$width = $size[0];
return $width;
}
protected function resizeImage($image,$width,$height,$scale) {
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":
case 'image/gif':
$source = imagecreatefromgif($image);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
case 'image/pjpeg':
case 'image/jpeg':
case 'image/jpg':
$source = imagecreatefromjpeg($image);
break;
case "image/png":
case "image/x-png":
case 'image/png':
case 'image/x-png':
$source = imagecreatefrompng($image);
break;
case "image/webp":
case 'image/webp':
$source = imagecreatefromwebp($image);
break;
}
imagecopyresampled($newImage, $source, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);
switch ($imageType) {
case "image/gif":
case 'image/gif':
imagegif($newImage, $image);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
case 'image/pjpeg':
case 'image/jpeg':
case 'image/jpg':
imagejpeg($newImage, $image, 90);
break;
case "image/png":
case "image/x-png":
case 'image/png':
case 'image/x-png':
imagepng($newImage, $image);
break;
case "image/webp":
case 'image/webp':
imagewebp($newImage, $image, 90);
break;
}
chmod($image, 0640);
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);
$imageType = image_type_to_mime_type($imageType);
@@ -137,42 +143,42 @@ class CropController extends AbstractController
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
switch ($imageType) {
case "image/gif":
case 'image/gif':
$source = imagecreatefromgif($image);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
case 'image/pjpeg':
case 'image/jpeg':
case 'image/jpg':
$source = imagecreatefromjpeg($image);
break;
case "image/png":
case "image/x-png":
case 'image/png':
case 'image/x-png':
$source = imagecreatefrompng($image);
break;
case "image/webp":
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":
case 'image/gif':
imagegif($newImage, $thumb_image_name);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
case 'image/pjpeg':
case 'image/jpeg':
case 'image/jpg':
imagejpeg($newImage, $thumb_image_name, 90);
break;
case "image/png":
case "image/x-png":
case 'image/png':
case 'image/x-png':
imagepng($newImage, $thumb_image_name);
break;
case "image/webp":
case 'image/webp':
imagewebp($newImage, $thumb_image_name, 90);
break;
}
chmod($thumb_image_name, 0640);
return $thumb_image_name;
}
}

View File

@@ -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 EntityManagerInterface $em;
private CustomerRepository $customerRepository;
private $knpSnappy;
public function __construct(\Knp\Snappy\Pdf $knpSnappy) { $this->knpSnappy = $knpSnappy; }
public function list(Request $request)
public function __construct(EntityManagerInterface $em, CustomerRepository $customerRepository)
{
$em = $this->getDoctrine()->getManager();
$datas = $em->getRepository($this->entity)->findAll();
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'
);
$this->em = $em;
$this->customerRepository = $customerRepository;
}
else {
return $this->render($this->render.'list.html.twig',[
$this->data."s" => $datas,
"useheader" => true,
"usesidebar" => true,
public function list()
{
$customers = $this->customerRepository->findAll();
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);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($customer);
$this->em->flush();
// 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();
// Retour à la liste
return $this->redirectToRoute($this->route);
return $this->redirectToRoute('app_customer');
}
// Affichage du formulaire
return $this->render($this->render.'edit.html.twig', [
return $this->render('Customer/edit.html.twig', [
'useheader' => true,
'usesidebar' => true,
$this->data => $data,
'customer' => $customer,
'mode' => 'submit',
'form' => $form->createView()
'form' => $form->createView(),
]);
}
public function update($id, Request $request)
{
// Initialisation de l'enregistrement
$em = $this->getDoctrine()->getManager();
$data=$em->getRepository($this->entity)->find($id);
// 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);
$customer = $this->customerRepository->find($id);
if (!$customer) {
throw new NotFoundHttpException('La ressource demandée est introuvable.');
}
// Affichage du formulaire
if($request->query->get('fgprint')) {
$render = $this->renderView($this->render.'edit.html.twig', [
$form = $this->createForm(CustomerType::class, $customer, ['mode' => 'update']);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
return $this->redirectToRoute('app_customer');
}
return $this->render('Customer/edit.html.twig', [
'useheader' => true,
'usesidebar' => true,
$this->data => $data,
'customer' => $customer,
'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()
]);
}
}
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);
$customer = $this->customerRepository->find($id);
if (!$customer) {
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 {
$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]);
$this->em->remove($customer);
$this->em->flush();
} catch (\Exception $e) {
$this->addflash('error', $e->getMessage());
}
// Retour à la liste
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());
}
}
return $this->redirectToRoute('app_customer');
}
}