Skip to content

Instantly share code, notes, and snippets.

@mmsesay
Created April 17, 2020 18:54
Show Gist options
  • Save mmsesay/0ce43a462b9f6b3a29b1e33eea50bea3 to your computer and use it in GitHub Desktop.
Save mmsesay/0ce43a462b9f6b3a29b1e33eea50bea3 to your computer and use it in GitHub Desktop.
<?php
namespace App\Controller;
use App\Entity\Attachment;
use App\Entity\Contract;
use App\GestionDeslogs;
use App\Entity\Event;
use App\Entity\AlertEvent;
use App\Form\EventType;
use App\Form\AlertEventType;
use App\Security\LdapUserProvider;
use App\Repository\EventRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\AttachmentRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Utils\UploaderHelper;
/**
* @Route("/event", name="event.")
*/
class EventController extends BaseController
{
const DETAIL = 'detail';
const ALLCONTRAT = 'allcontrat';
const MAIL = '[email protected]';
const CONTRATHEQUE = 'CONTRATHEQUE';
const CONTRACT = 'contract';
const INFO = 'info';
const ACTIVETAB = 'activeTab';
const EVENT = 'event';
/**
* @Route("/new", name="new_event")
* @param Request $request
* @param UploaderHelper $uploaderHelper
* @return Response
*/
public function new(Request $request, UploaderHelper $uploaderHelper): Response
{
$event = new Event();
$form = $this->createForm(EventType::class, $event);
$form->handleRequest($request);
$contract = $this->entityManager->getRepository(Contract::class)->find($request->get(self::CONTRACT));
if ($form->isSubmitted() && $form->isValid() && $contract) {
$this->entityManager->getConnection()->beginTransaction();
try {
$files = $event->getAttachments()->first();
$event->getAttachments()->clear();
$event->setContract($contract);
$this->entityManager->persist($event);
$this->addAttachments($event, $files, $uploaderHelper);
$this->entityManager->flush();
$this->entityManager->getConnection()->commit();
} catch (Exception $e) {
$this->entityManager->getConnection()->rollBack();
throw $e;
}
// $this->userLogger->addLogg(self::INFO, $this->getRequest(), $this->getUser(),
// sprintf('Creation event:% s for the contract:% s', $event->getEventName(),
// $contract->getContactName()));
$this->addFlash('success', 'The event is successfully registered');
$id = $this->encyptorManager->encryptor(self::ENCRYPT, $contract->getId());
return $this->redirectToRoute(self::DETAIL, [self::SLUG => $id, self::ACTIVETAB => self::EVENEMENT]);
}
return $this->redirectToRoute(self::ALLCONTRAT);
}
public function detail($idEvent)
{
$this->userLogger->addLogg(self::INFO, $this->getRequest(), $this->getUser(), 'Event details display');
return $this->render('admin/detailsEvenement.html.twig', [
self::EVENT => $this->entityManager->getRepository(Event::class)->find($idEvent),
]);
}
public function pj($idPj)
{
$this->userLogger->addLogg(self::INFO, $this->getRequest(), $this->getUser(), 'View attachment');
return $this->render('admin/pj.html.twig', [
'pj' => $this->entityManager->getRepository(Attachment::class)->find($idPj),
]);
}
public function addAttachment($event, $files, $uploaderHelper)
{
$formats = [
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/pdf', 'application/x-pdf', 'image/jpeg', 'image/png',
'video/mpeg', 'audio/mpeg', 'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'application/vnd.oasis.opendocument.text', 'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.oasis.opendocument.presentation',
'application/vnd.oasis.opendocument.spreadsheet'
];
foreach ($files as $file) {
if (!in_array($file->getMimeType(), $formats) || $file->getSize() > 20000000) {
continue;
}
$uploadedFile = $file;
$fileName = $uploaderHelper->uploadDocument($uploadedFile);
$attachment = new Attachment();
$attachment->setEvent($event)
->setAttachmentName($file->getClientOriginalName())
->setTypepiece($file->getMimeType())
->setFile($fileName);
$this->entityManager->persist($attachment);
}
}
/**
* @Route("/new_alert", name="new_alert")
* @param Request $request
* @param EventRepository $eventRepository
* @return Response
* @throws \Exception
*/
public function alertEvenement(Request $request, EventRepository $eventRepository): Response
{
$alertEvent = new AlertEvent();
$form2 = $this->createForm(AlertEventType::class, $alertEvent);
$form2->handleRequest($request);
$contract = $this->entityManager->getRepository(Contract::class)->find($request->get(self::CONTRACT));
$id = $contract->getId();
// $id = $this->encyptorManager->encryptor(self::ENCRYPT, $contract->getId());
$event = $eventRepository->findBy(['contract' => $contract]);
if ($form2->isSubmitted() && $form2->isValid()) {
$alertEvent->setContrat($contract);
$alertEvent->setEvenement($eventRepository->find($request->get('event')));
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($alertEvent);
$entityManager->flush();
// $this->userLogger->addLogg(self::INFO, $this->getRequest(), $this->getUser(), 'Add alert ' . $alertEvent->getPattern() . ' planned for ' . $alertEvent->getDateAlertEvent(new \DateTime)->format('d/m/Y') . '.');
$this->addFlash('success', 'the alert is successfully registered');
return $this->redirectToRoute(self::DETAIL, [self::SLUG => $id, self::ACTIVETAB => self::EVENT]);
}
return $this->redirectToRoute(self::DETAIL, [self::SLUG => $id, self::ACTIVETAB => self::EVENT, 'even' => $event]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment