src/Controller/LotAdminController.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Mpdf\Mpdf;
  4. use App\Entity\Lot;
  5. use App\Entity\Prospect;
  6. use App\Entity\Historique;
  7. use App\Entity\PieceJointe;
  8. use Mpdf\Output\Destination;
  9. use App\Service\Notification;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Symfony\Component\Mailer\Mailer;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Sonata\AdminBundle\Controller\CRUDController;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Symfony\Component\DependencyInjection\ContainerInterface;
  19. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  20. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  21. class LotAdminController extends CRUDController
  22. {
  23.     protected $em;
  24.     public function __construct(EntityManagerInterface $em)
  25.     {
  26.         $this->em $em;
  27.     }
  28.     public function preCreate(Request $requestobject $object): ?Response {
  29.         if($object->getProgrammeParent()) {
  30.             $object->setLivraison($object->getProgrammeParent()->getLivraison());
  31.             foreach ($object->getProgrammeParent()->getPjs() as $pj) {
  32.                 $clonedPj = clone $pj;
  33.                 $clonedPj->setLot($object);
  34.                 $clonedPj->setPieceJointeMere($pj);
  35.                 $clonedPj->setProgramme(null);
  36.                 $object->addPj($clonedPj);
  37.             }
  38.         }
  39.         foreach (Lot::INTITULES_PREDEFINIS as $description) {
  40.             $pj = new PieceJointe();
  41.             $pj->setDescriptionCourte($description);
  42.             $pj->setLot($object);
  43.             $object->addPj($pj);
  44.         }  
  45.         return null;
  46.     }
  47.     public function editAction(Request $request): Response
  48.     {
  49.         $originalProgrammeParentId $this->admin->getSubject()->getProgrammeParent()->getId();
  50.         $response parent::editAction($request);
  51.         $lotObject $this->admin->getSubject();
  52.         if ($response instanceof RedirectResponse && $response->isRedirect() && $originalProgrammeParentId != $lotObject->getProgrammeParent()->getId()) {
  53.             return $this->redirectToRoute('admin_app_programme_lot_edit', array(
  54.                 'id' => $lotObject->getProgrammeParent()->getId(),
  55.                 'childId' => $request->get('childId')
  56.             ));
  57.         }
  58.         return $response;
  59.     }
  60.     /**
  61.      * @param $id
  62.      */
  63.     public function cloneAction($id): Response
  64.     {
  65.         $object $this->admin->getSubject();
  66.         if (!$object) {
  67.             throw new NotFoundHttpException(sprintf('Impossible de trouver le lot associé à l\'id : %s'$id));
  68.         }
  69.         $clonedObject = clone $object;
  70.         $clonedObject->setReference($object->getReference() . ' (Copie)');
  71.         $this->admin->create($clonedObject);
  72.         $this->addFlash('sonata_flash_success''Le lot a bien été dupliqué.');
  73.         return new RedirectResponse(
  74.             $this->admin->generateUrl('edit', ['id' => $clonedObject->getId()])
  75.         );
  76.     }
  77. }