mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,34 +1,37 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CalendarBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\CRUD\Controller\ApiController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
|
||||
class CalendarAPIController extends ApiController
|
||||
{
|
||||
|
||||
|
||||
protected function getContextForSerialization(string $action, Request $request, string $_format, $entity): array
|
||||
{
|
||||
switch($action) {
|
||||
case '_index':
|
||||
switch ($request->getMethod()) {
|
||||
case Request::METHOD_GET:
|
||||
return [ 'groups' => [ 'calendar:read' ] ];
|
||||
}
|
||||
}
|
||||
return parent::getContextForSerialization($action, $request, $_format, $entity);
|
||||
}
|
||||
|
||||
protected function customizeQuery(string $action, Request $request, $qb): void
|
||||
{
|
||||
if ($request->query->has('main_user')) {
|
||||
|
||||
$qb->where('e.mainUser = :main_user')
|
||||
->setParameter('main_user', $request->query->get('main_user'));
|
||||
->setParameter('main_user', $request->query->get('main_user'));
|
||||
}
|
||||
}
|
||||
|
||||
protected function getContextForSerialization(string $action, Request $request, string $_format, $entity): array
|
||||
{
|
||||
switch ($action) {
|
||||
case '_index':
|
||||
switch ($request->getMethod()) {
|
||||
case Request::METHOD_GET:
|
||||
return ['groups' => ['calendar:read']];
|
||||
}
|
||||
}
|
||||
|
||||
return parent::getContextForSerialization($action, $request, $_format, $entity);
|
||||
}
|
||||
}
|
||||
|
@@ -1,43 +1,49 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\CalendarBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Privacy\PrivacyEvent;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Form\Form;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\CalendarBundle\Entity\Calendar;
|
||||
use Chill\CalendarBundle\Form\CalendarType;
|
||||
use Chill\CalendarBundle\Repository\CalendarRepository;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Form;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
class CalendarController extends AbstractController
|
||||
{
|
||||
protected EventDispatcherInterface $eventDispatcher;
|
||||
|
||||
protected AuthorizationHelper $authorizationHelper;
|
||||
|
||||
protected EventDispatcherInterface $eventDispatcher;
|
||||
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
protected SerializerInterface $serializer;
|
||||
|
||||
protected PaginatorFactory $paginator;
|
||||
|
||||
protected SerializerInterface $serializer;
|
||||
|
||||
private CalendarRepository $calendarRepository;
|
||||
|
||||
public function __construct(
|
||||
@@ -56,8 +62,129 @@ class CalendarController extends AbstractController
|
||||
$this->calendarRepository = $calendarRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a calendar item.
|
||||
*
|
||||
* @Route("/{_locale}/calendar/{id}/delete", name="chill_calendar_calendar_delete")
|
||||
*/
|
||||
public function deleteAction(Request $request, int $id)
|
||||
{
|
||||
$view = null;
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
[$user, $accompanyingPeriod] = $this->getEntity($request);
|
||||
|
||||
if ($accompanyingPeriod instanceof AccompanyingPeriod) {
|
||||
$view = '@ChillCalendar/Calendar/confirm_deleteByAccompanyingCourse.html.twig';
|
||||
} elseif ($user instanceof User) {
|
||||
$view = '@ChillCalendar/Calendar/confirm_deleteByUser.html.twig';
|
||||
}
|
||||
|
||||
/* @var $entity Calendar */
|
||||
$entity = $em->getRepository('ChillCalendarBundle:Calendar')->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find Calendar entity.');
|
||||
}
|
||||
|
||||
$form = $this->createDeleteForm($id, $user, $accompanyingPeriod);
|
||||
|
||||
if ($request->getMethod() === Request::METHOD_DELETE) {
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isValid()) {
|
||||
$this->logger->notice('A calendar event has been removed', [
|
||||
'by_user' => $this->getUser()->getUsername(),
|
||||
'calendar_id' => $entity->getId(),
|
||||
]);
|
||||
|
||||
$em->remove($entity);
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')
|
||||
->trans('The calendar item has been successfully removed.'));
|
||||
|
||||
$params = $this->buildParamsToUrl($user, $accompanyingPeriod);
|
||||
|
||||
return $this->redirectToRoute('chill_calendar_calendar_list', $params);
|
||||
}
|
||||
}
|
||||
|
||||
if (null === $view) {
|
||||
throw $this->createNotFoundException('Template not found');
|
||||
}
|
||||
|
||||
return $this->render($view, [
|
||||
'calendar' => $entity,
|
||||
'delete_form' => $form->createView(),
|
||||
'accompanyingCourse' => $accompanyingPeriod,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a calendar item.
|
||||
*
|
||||
* @Route("/{_locale}/calendar/calendar/{id}/edit", name="chill_calendar_calendar_edit")
|
||||
*/
|
||||
public function editAction(int $id, Request $request): Response
|
||||
{
|
||||
$view = null;
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
[$user, $accompanyingPeriod] = $this->getEntity($request);
|
||||
|
||||
if ($accompanyingPeriod instanceof AccompanyingPeriod) {
|
||||
$view = '@ChillCalendar/Calendar/editByAccompanyingCourse.html.twig';
|
||||
} elseif ($user instanceof User) {
|
||||
$view = '@ChillCalendar/Calendar/editByUser.html.twig';
|
||||
}
|
||||
|
||||
$entity = $em->getRepository('ChillCalendarBundle:Calendar')->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find Calendar entity.');
|
||||
}
|
||||
|
||||
$form = $this->createForm(CalendarType::class, $entity, [
|
||||
'accompanyingPeriod' => $accompanyingPeriod,
|
||||
])->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$em->persist($entity);
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')->trans('Success : calendar item updated!'));
|
||||
|
||||
$params = $this->buildParamsToUrl($user, $accompanyingPeriod);
|
||||
|
||||
return $this->redirectToRoute('chill_calendar_calendar_list', $params);
|
||||
}
|
||||
|
||||
if ($form->isSubmitted() and !$form->isValid()) {
|
||||
$this->addFlash('error', $this->get('translator')->trans('This form contains errors'));
|
||||
}
|
||||
|
||||
$deleteForm = $this->createDeleteForm($id, $user, $accompanyingPeriod);
|
||||
|
||||
if (null === $view) {
|
||||
throw $this->createNotFoundException('Template not found');
|
||||
}
|
||||
|
||||
$entity_array = $this->serializer->normalize($entity, 'json', ['groups' => 'read']);
|
||||
|
||||
return $this->render($view, [
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
'delete_form' => $deleteForm->createView(),
|
||||
'accompanyingCourse' => $accompanyingPeriod,
|
||||
'user' => $user,
|
||||
'entity_json' => $entity_array,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all Calendar entities.
|
||||
*
|
||||
* @Route("/{_locale}/calendar/calendar/", name="chill_calendar_calendar_list")
|
||||
*/
|
||||
public function listAction(Request $request): Response
|
||||
@@ -67,14 +194,13 @@ class CalendarController extends AbstractController
|
||||
[$user, $accompanyingPeriod] = $this->getEntity($request);
|
||||
|
||||
if ($user instanceof User) {
|
||||
|
||||
$calendarItems = $this->calendarRepository->findByUser($user);
|
||||
|
||||
$view = '@ChillCalendar/Calendar/listByUser.html.twig';
|
||||
|
||||
return $this->render($view, [
|
||||
'calendarItems' => $calendarItems,
|
||||
'user' => $user
|
||||
'user' => $user,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -93,15 +219,16 @@ class CalendarController extends AbstractController
|
||||
return $this->render($view, [
|
||||
'calendarItems' => $calendarItems,
|
||||
'accompanyingCourse' => $accompanyingPeriod,
|
||||
'paginator' => $paginator
|
||||
'paginator' => $paginator,
|
||||
]);
|
||||
}
|
||||
|
||||
throw new \Exception('Unable to list actions.');
|
||||
throw new Exception('Unable to list actions.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new calendar item
|
||||
* Create a new calendar item.
|
||||
*
|
||||
* @Route("/{_locale}/calendar/calendar/new", name="chill_calendar_calendar_new")
|
||||
*/
|
||||
public function newAction(Request $request): Response
|
||||
@@ -149,7 +276,7 @@ class CalendarController extends AbstractController
|
||||
$this->addFlash('error', $this->get('translator')->trans('This form contains errors'));
|
||||
}
|
||||
|
||||
if ($view === null) {
|
||||
if (null === $view) {
|
||||
throw $this->createNotFoundException('Template not found');
|
||||
}
|
||||
|
||||
@@ -160,12 +287,13 @@ class CalendarController extends AbstractController
|
||||
'accompanyingCourse' => $accompanyingPeriod,
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
'entity_json' => $entity_array
|
||||
'entity_json' => $entity_array,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a calendar item
|
||||
* Show a calendar item.
|
||||
*
|
||||
* @Route("/{_locale}/calendar/calendar/{id}/show", name="chill_calendar_calendar_show")
|
||||
*/
|
||||
public function showAction(Request $request, int $id): Response
|
||||
@@ -177,12 +305,11 @@ class CalendarController extends AbstractController
|
||||
|
||||
if ($accompanyingPeriod instanceof AccompanyingPeriod) {
|
||||
$view = '@ChillCalendar/Calendar/showByAccompanyingCourse.html.twig';
|
||||
}
|
||||
elseif ($user instanceof User) {
|
||||
} elseif ($user instanceof User) {
|
||||
$view = '@ChillCalendar/Calendar/showByUser.html.twig';
|
||||
}
|
||||
|
||||
if ($view === null) {
|
||||
if (null === $view) {
|
||||
throw $this->createNotFoundException('Template not found');
|
||||
}
|
||||
|
||||
@@ -214,7 +341,7 @@ class CalendarController extends AbstractController
|
||||
);
|
||||
|
||||
$durationTime = $entity->getEndDate()->diff($entity->getStartDate());
|
||||
$durationTimeInMinutes = $durationTime->days*1440 + $durationTime->h*60 + $durationTime->i;
|
||||
$durationTimeInMinutes = $durationTime->days * 1440 + $durationTime->h * 60 + $durationTime->i;
|
||||
|
||||
$activityData = [
|
||||
'calendarId' => $id,
|
||||
@@ -230,131 +357,24 @@ class CalendarController extends AbstractController
|
||||
'accompanyingCourse' => $accompanyingPeriod,
|
||||
'entity' => $entity,
|
||||
'user' => $user,
|
||||
'activityData' => $activityData
|
||||
'activityData' => $activityData,
|
||||
//'delete_form' => $deleteForm->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Edit a calendar item
|
||||
* @Route("/{_locale}/calendar/calendar/{id}/edit", name="chill_calendar_calendar_edit")
|
||||
*/
|
||||
public function editAction(int $id, Request $request): Response
|
||||
private function buildParamsToUrl(?User $user, ?AccompanyingPeriod $accompanyingPeriod): array
|
||||
{
|
||||
$view = null;
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$params = [];
|
||||
|
||||
[$user, $accompanyingPeriod] = $this->getEntity($request);
|
||||
|
||||
if ($accompanyingPeriod instanceof AccompanyingPeriod) {
|
||||
$view = '@ChillCalendar/Calendar/editByAccompanyingCourse.html.twig';
|
||||
}
|
||||
elseif ($user instanceof User) {
|
||||
$view = '@ChillCalendar/Calendar/editByUser.html.twig';
|
||||
if (null !== $user) {
|
||||
$params['user_id'] = $user->getId();
|
||||
}
|
||||
|
||||
$entity = $em->getRepository('ChillCalendarBundle:Calendar')->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find Calendar entity.');
|
||||
if (null !== $accompanyingPeriod) {
|
||||
$params['accompanying_period_id'] = $accompanyingPeriod->getId();
|
||||
}
|
||||
|
||||
$form = $this->createForm(CalendarType::class, $entity, [
|
||||
'accompanyingPeriod' => $accompanyingPeriod,
|
||||
])->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$em->persist($entity);
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')->trans('Success : calendar item updated!'));
|
||||
|
||||
$params = $this->buildParamsToUrl($user, $accompanyingPeriod);
|
||||
|
||||
return $this->redirectToRoute('chill_calendar_calendar_list', $params);
|
||||
}
|
||||
|
||||
if ($form->isSubmitted() and !$form->isValid()) {
|
||||
$this->addFlash('error', $this->get('translator')->trans('This form contains errors'));
|
||||
}
|
||||
|
||||
$deleteForm = $this->createDeleteForm($id, $user, $accompanyingPeriod);
|
||||
|
||||
if ($view === null) {
|
||||
throw $this->createNotFoundException('Template not found');
|
||||
}
|
||||
|
||||
$entity_array = $this->serializer->normalize($entity, 'json', ['groups' => 'read']);
|
||||
|
||||
return $this->render($view, [
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
'delete_form' => $deleteForm->createView(),
|
||||
'accompanyingCourse' => $accompanyingPeriod,
|
||||
'user' => $user,
|
||||
'entity_json' => $entity_array
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a calendar item
|
||||
* @Route("/{_locale}/calendar/{id}/delete", name="chill_calendar_calendar_delete")
|
||||
*/
|
||||
public function deleteAction(Request $request, int $id)
|
||||
{
|
||||
$view = null;
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
[$user, $accompanyingPeriod] = $this->getEntity($request);
|
||||
|
||||
if ($accompanyingPeriod instanceof AccompanyingPeriod) {
|
||||
$view = '@ChillCalendar/Calendar/confirm_deleteByAccompanyingCourse.html.twig';
|
||||
}
|
||||
elseif ($user instanceof User) {
|
||||
$view = '@ChillCalendar/Calendar/confirm_deleteByUser.html.twig';
|
||||
}
|
||||
|
||||
/* @var $entity Calendar */
|
||||
$entity = $em->getRepository('ChillCalendarBundle:Calendar')->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find Calendar entity.');
|
||||
}
|
||||
|
||||
$form = $this->createDeleteForm($id, $user, $accompanyingPeriod);
|
||||
|
||||
if ($request->getMethod() === Request::METHOD_DELETE) {
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isValid()) {
|
||||
|
||||
$this->logger->notice("A calendar event has been removed", [
|
||||
'by_user' => $this->getUser()->getUsername(),
|
||||
'calendar_id' => $entity->getId()
|
||||
]);
|
||||
|
||||
$em->remove($entity);
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')
|
||||
->trans("The calendar item has been successfully removed."));
|
||||
|
||||
$params = $this->buildParamsToUrl($user, $accompanyingPeriod);
|
||||
return $this->redirectToRoute('chill_calendar_calendar_list', $params);
|
||||
}
|
||||
}
|
||||
|
||||
if ($view === null) {
|
||||
throw $this->createNotFoundException('Template not found');
|
||||
}
|
||||
|
||||
return $this->render($view, [
|
||||
'calendar' => $entity,
|
||||
'delete_form' => $form->createView(),
|
||||
'accompanyingCourse' => $accompanyingPeriod,
|
||||
]);
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -369,8 +389,7 @@ class CalendarController extends AbstractController
|
||||
->setAction($this->generateUrl('chill_calendar_calendar_delete', $params))
|
||||
->setMethod('DELETE')
|
||||
->add('submit', SubmitType::class, ['label' => 'Delete'])
|
||||
->getForm()
|
||||
;
|
||||
->getForm();
|
||||
}
|
||||
|
||||
private function getEntity(Request $request): array
|
||||
@@ -382,7 +401,7 @@ class CalendarController extends AbstractController
|
||||
$user_id = $request->get('user_id');
|
||||
$user = $em->getRepository(User::class)->find($user_id);
|
||||
|
||||
if ($user === null) {
|
||||
if (null === $user) {
|
||||
throw $this->createNotFoundException('User not found');
|
||||
}
|
||||
|
||||
@@ -392,32 +411,18 @@ class CalendarController extends AbstractController
|
||||
$accompanying_period_id = $request->get('accompanying_period_id');
|
||||
$accompanyingPeriod = $em->getRepository(AccompanyingPeriod::class)->find($accompanying_period_id);
|
||||
|
||||
if ($accompanyingPeriod === null) {
|
||||
if (null === $accompanyingPeriod) {
|
||||
throw $this->createNotFoundException('Accompanying Period not found');
|
||||
}
|
||||
|
||||
// TODO Add permission
|
||||
// $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
|
||||
} else {
|
||||
throw $this->createNotFoundException("Person or Accompanying Period not found");
|
||||
throw $this->createNotFoundException('Person or Accompanying Period not found');
|
||||
}
|
||||
|
||||
return [
|
||||
$user, $accompanyingPeriod
|
||||
$user, $accompanyingPeriod,
|
||||
];
|
||||
}
|
||||
|
||||
private function buildParamsToUrl(?User $user, ?AccompanyingPeriod $accompanyingPeriod): array {
|
||||
$params = [];
|
||||
|
||||
if (null !== $user) {
|
||||
$params['user_id'] = $user->getId();
|
||||
}
|
||||
|
||||
if (null !== $accompanyingPeriod) {
|
||||
$params['accompanying_period_id'] = $accompanyingPeriod->getId();
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CalendarBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\CRUD\Controller\ApiController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Chill\MainBundle\Pagination\PaginatorInterface;
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class CalendarRangeAPIController extends ApiController
|
||||
{
|
||||
|
||||
/**
|
||||
* @Route("/api/1.0/calendar/calendar-range-available.{_format}", name="chill_api_single_calendar_range_available")
|
||||
*/
|
||||
@@ -34,8 +38,7 @@ class CalendarRangeAPIController extends ApiController
|
||||
|
||||
$results = $query->getResult();
|
||||
|
||||
return $this->json(['count' => count($results), 'results' => $results], Response::HTTP_OK, [], [ 'groups' => [ 'read' ]]);
|
||||
return $this->json(['count' => count($results), 'results' => $results], Response::HTTP_OK, [], ['groups' => ['read']]);
|
||||
//TODO use also the paginator, eg return $this->serializeCollection('get', $request, $_format, $paginator, $results);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user