<?php
namespace App\Controller;
use App\Entity\Holidays;
use App\Entity\SettingsOffice;
use App\Entity\User;
use App\Form\HolidaysType;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class HolidaysController extends AbstractController
{
private $translator;
public function __construct(TranslatorInterface $translator) {
$this->translator = $translator;
}
/**
* @Route("/holidays/add", name="holiday_form")
*/
public function createHolidaysAction(Request $request) {
$holiday = new Holidays();
$form = $this->createHolidaysCreateForm($holiday);
return $this->render('holidays/add-holidays.html.twig', array('form' => $form->createView()));
}
private function createHolidaysCreateForm(Holidays $entity)
{
$form = $this->createForm(HolidaysType::class, $entity, array(
'action' => $this->generateUrl('holidays_create'),
'method' => 'POST'
));
return $form;
}
/**
* @Route("/holidays/create", name="holidays_create")
*/
public function createAction(Request $request, LoggerInterface $logger)
{
$holiday = new Holidays();
$form = $this->createHolidaysCreateForm($holiday);
$form->handleRequest($request);
if($form->isValid())
{
$em = $this->getDoctrine()->getManager();
/* Obtengo usuario logueado */
$user_logueado = $this->get('security.token_storage')->getToken()->getUser();
$user_id = $user_logueado->getId();
$holiday->setCreatedId($user_id);
$holiday->setUpdatedId($user_id);
/* Gestión de eventos en Log */
$user_lastname = $user_logueado->getLastname();
$user_name = $user_logueado->getName();
$user_email = $user_logueado->getEmail();
$user_rol = $user_logueado->getRoles();
$event_url = $request->getPathInfo();
$event_complete = $user_name.' '.$user_lastname.' - '.$user_email.' - '.$user_rol[0].' | '.$event_url;
try
{
$office = $form->get('office')->getData();
$holiday->setOffice($office->getId());
$em->persist($holiday);
$em->flush();
$event = 'The holiday has been created. ';
$successMessage = $this->translator->trans($event);
$this->addFlash('mensajeholidays', $successMessage);
$logger->info($event_complete.' | '.$event);
} catch (\Exception $e){
$event = 'An error occurred: '.$e->getMessage();
/* Para el log */
$logger->error($event_complete.' | '.$event);
/* Para el usuario */
$errorMessage = $this->translator->trans($event);
$this->addFlash('mensajeholidayserror', $errorMessage);
}
/* Fin Gestión de eventos en Log */
//return $this->render('holidays/add-holidays.html.twig', array('form' => $form->createView()));
return $this->redirectToRoute('holidays_index');
}else{
$errorMessage = $this->translator->trans('Error, some fields are empty');
$this->addFlash('mensajeholidayserror', $errorMessage);
}
//return $this->render('holidays/add-holidays.html.twig', array('form' => $form->createView()));
return $this->redirectToRoute('holidays_index');
}
/**
* @Route("/holidays/list", name="holidays_index")
*/
public function listAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$holidays = $em->getRepository(Holidays::class)->findAll();
$data = array();
foreach($holidays as $holiday) {
$offices = $em->getRepository(SettingsOffice::class)->findOneById($holiday->getOffice());
$holiday->setOffice($offices->getOffice());
$data[] = array(
'id' => $holiday->getId(),
'date' => $holiday->getDate(),
'office' => $holiday->getOffice(),
'color' => $holiday->getColor(),
'title' => $holiday->getTitle(),
);
//$destination = $em->getRepository(Destination::class)->findOneById($idea->getDestinationId());
//$supplierIdeaServicesControl = $em->getRepository(SupplierIdeaServicesControl::class)->findOneByIdeaId($idea->getId());
// if($idea->getDestinationId() == '0' OR is_null($idea->getDestinationId())){
// $data[] = array(
// 'id' => $idea->getId(),
// 'idc' => $supplierIdeaServicesControl->getId(),
// 'name' => $idea->getName(),
// 'destination' => 'Sin Destino',
// 'observation' => $idea->getObservation()
// );
// }else{
// $data[] = array(
// 'id' => $idea->getId(),
// 'idc' => $supplierIdeaServicesControl->getId(),
// 'name' => $idea->getName(),
// 'destination' => $destination->getTitle(),
// 'observation' => $idea->getObservation()
// );
// }
}
return $this->render('holidays/list-holidays.html.twig', array(
'holidays' => $data
));
}
/**
* @Route("/holidays/deleted/{id}", name="holidays_deleted")
*
*/
// public function deleteAction($id, Request $request, LoggerInterface $logger)
public function deleteAction($id, Request $request, LoggerInterface $logger)
{
$em = $this->getDoctrine()->getManager();
// $var = $em->getRepository(ProposalDocument::class);
// $delete = $var->findOneById($id);
$holidays = $em->getRepository(Holidays::class)->findOneById($id);
if (!empty($holidays))
{
try{
$em->remove($holidays);
$em->flush();
}
catch (\Exception $e){
$event = 'An error occurred: '.$e->getMessage();
/* Para el log */
$logger->error($event);
/* Para el usuario */
$errorMessage = $this->translator->trans($event);
$this->addFlash('mensajeholidayserror', $errorMessage);
}
}
$event = 'The Item has been deleted.';
$successMessage = $this->translator->trans($event);
$this->addFlash('mensajeholidays', $successMessage);
/* Fin Gestión de eventos en Log */
return $this->redirectToRoute('holidays_index');
}
/**
* @Route("/holidays/edit/{id}", name="holidays_edit")
*/
public function editHolidaysAction($id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$holidays = $em->getRepository(Holidays::class)->findOneById($id);
$form = $this->createEditHolidaysForm($holidays, $id);
return $this->render('holidays/edit-holidays.html.twig',
array(
'id' =>$id,
'holidays' => $holidays,
'form' => $form->createView()
)
);
}
private function createEditHolidaysForm(Holidays $entity, $id)
{
$form = $this->createForm(HolidaysType::class, $entity,
array(
'action' => $this->generateUrl('holidays_update',
array(
'id' => $id
)
), 'method' => 'PUT'));
return $form;
}
/**
* @Route("/holidays/update/{id}", name="holidays_update")
*/
public function updateHolidaysAction($id, Request $request, LoggerInterface $logger)
{
$em = $this->getDoctrine()->getManager();
$holiday = $em->getRepository(Holidays::class)->findOneById($id);
$form = $this->createEditHolidaysForm($holiday, $id);
$form->handleRequest($request);
if($form->isValid())
{
$em = $this->getDoctrine()->getManager();
/* Obtengo usuario logueado */
$user_logueado = $this->get('security.token_storage')->getToken()->getUser();
$user_id = $user_logueado->getId();
// $holiday->setCreatedId($user_id);
$holiday->setUpdatedId($user_id);
/* Gestión de eventos en Log */
$user_lastname = $user_logueado->getLastname();
$user_name = $user_logueado->getName();
$user_email = $user_logueado->getEmail();
$user_rol = $user_logueado->getRoles();
$event_url = $request->getPathInfo();
$event_complete = $user_name.' '.$user_lastname.' - '.$user_email.' - '.$user_rol[0].' | '.$event_url;
try
{
$office = $form->get('office')->getData();
$holiday->setOffice($office->getId());
$em->persist($holiday);
$em->flush();
$event = 'The holiday has been updated. ';
$successMessage = $this->translator->trans($event);
$this->addFlash('mensajeholidays', $successMessage);
$logger->info($event_complete.' | '.$event);
} catch (\Exception $e){
$event = 'An error occurred: '.$e->getMessage();
/* Para el log */
$logger->error($event_complete.' | '.$event);
/* Para el usuario */
$errorMessage = $this->translator->trans($event);
$this->addFlash('mensajeholidayserror', $errorMessage);
return $this->render('holidays/add-holidays.html.twig', array('form' => $form->createView()));
}
/* Fin Gestión de eventos en Log */
}else{
$errorMessage = $this->translator->trans('Error, some fields are empty');
$this->addFlash('mensajeholidayserror', $errorMessage);
}
return $this->redirectToRoute('holidays_index');
}
/**
* @Route("/holidays/events", name="get_holidays_select")
*/
public function holidaysSelectAction(Request $request) {
$em = $this->getDoctrine()->getManager();
/* Obtengo usuario logueado */
/**
* @var User $user_logueado
*/
// Se enviará la lista de días festivos correspondientes a la oficina del usuario que está logueado
$user_logueado = $this->get('security.token_storage')->getToken()->getUser();
$userOffice = $user_logueado->getOffice();
$holidays = $em->getRepository(Holidays::class)->findBy(
array(
'office' => $userOffice
)
)
;
$datos = array();
if (!empty($holidays)){
foreach($holidays as $holiday){
$offices = $em->getRepository(SettingsOffice::class)->findOneById($holiday->getOffice());
$holiday->setOffice($offices->getOffice());
$colorHay = $holiday->getColor();
if (empty($colorHay)){
$color = "#BDBDBD";
}else{
$color = $holiday->getColor();
}
$datos[] = array(
"id" => $holiday->getId(),
"title" => $holiday->getTitle(),
"tooltip" => $holiday->getTitle().", Office: ".$holiday->getOffice(),
"start" => $holiday->getDate(),
"end" => $holiday->getDate(),
"color" => $color,
"url" => "#"
);
}
}
else
{
$datos = [];
}
$return = array(
'holidays' => $datos,
);
$response = new JsonResponse($return);
return $response;
}
}