Merge branch 'master' into 616_rapid-action

This commit is contained in:
2023-05-24 11:21:34 +02:00
2652 changed files with 82793 additions and 26335 deletions

View File

@@ -1,18 +1,26 @@
<?php
/**
declare(strict_types=1);
/*
* 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;
use Chill\CalendarBundle\RemoteCalendar\DependencyInjection\RemoteCalendarCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class ChillCalendarBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new RemoteCalendarCompilerPass());
}
}

View File

@@ -0,0 +1,84 @@
<?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);
/*
* 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\Command;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MachineTokenStorage;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use TheNetworg\OAuth2\Client\Provider\Azure;
class AzureGrantAdminConsentAndAcquireToken extends Command
{
private Azure $azure;
private ClientRegistry $clientRegistry;
private MachineTokenStorage $machineTokenStorage;
public function __construct(Azure $azure, ClientRegistry $clientRegistry, MachineTokenStorage $machineTokenStorage)
{
parent::__construct('chill:calendar:msgraph-grant-admin-consent');
$this->azure = $azure;
$this->clientRegistry = $clientRegistry;
$this->machineTokenStorage = $machineTokenStorage;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var FormatterHelper $formatter */
$formatter = $this->getHelper('formatter');
$this->azure->scope = ['https://graph.microsoft.com/.default'];
$authorizationUrl = explode('?', $this->azure->getAuthorizationUrl(['prompt' => 'admin_consent']));
// replace the first part by the admin consent authorization url
$authorizationUrl[0] = strtr('https://login.microsoftonline.com/{tenant}/adminconsent', ['{tenant}' => $this->azure->tenant]);
$output->writeln('Go to the url');
$output->writeln(implode('?', $authorizationUrl));
$output->writeln('Authenticate as admin, and grant admin consent');
// not necessary ?
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Access granted ?');
if (!$helper->ask($input, $output, $question)) {
$messages = ['No problem, we will wait for you', 'Grant access and come back here'];
$output->writeln($formatter->formatBlock($messages, 'warning'));
return 0;
}
$token = $this->machineTokenStorage->getToken();
$messages = ['Token acquired!', 'We could acquire a machine token successfully'];
$output->writeln($formatter->formatBlock($messages, 'success'));
$output->writeln('Token information:');
$output->writeln($token->getToken());
$output->writeln('Expires at: ' . $token->getExpires());
$output->writeln('To inspect the token content, go to https://jwt.ms/#access_token=' . urlencode($token->getToken()));
return 0;
}
}

View File

@@ -0,0 +1,171 @@
<?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);
/*
* 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\Command;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\EventsOnUserSubscriptionCreator;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MapCalendarToUser;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MSGraphUserRepository;
use DateInterval;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class MapAndSubscribeUserCalendarCommand extends Command
{
private EntityManagerInterface $em;
private EventsOnUserSubscriptionCreator $eventsOnUserSubscriptionCreator;
private LoggerInterface $logger;
private MapCalendarToUser $mapCalendarToUser;
private MSGraphUserRepository $userRepository;
public function __construct(
EntityManagerInterface $em,
EventsOnUserSubscriptionCreator $eventsOnUserSubscriptionCreator,
LoggerInterface $logger,
MapCalendarToUser $mapCalendarToUser,
MSGraphUserRepository $userRepository
) {
parent::__construct('chill:calendar:msgraph-user-map-subscribe');
$this->em = $em;
$this->eventsOnUserSubscriptionCreator = $eventsOnUserSubscriptionCreator;
$this->logger = $logger;
$this->mapCalendarToUser = $mapCalendarToUser;
$this->userRepository = $userRepository;
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$this->logger->info(self::class . ' execute command');
$limit = 50;
$offset = 0;
/** @var DateInterval $interval the interval before the end of the expiration */
$interval = new DateInterval('P1D');
$expiration = (new DateTimeImmutable('now'))->add(new DateInterval($input->getOption('subscription-duration')));
$total = $this->userRepository->countByMostOldSubscriptionOrWithoutSubscriptionOrData($interval);
$created = 0;
$renewed = 0;
$this->logger->info(self::class . ' the number of user to get - renew', [
'total' => $total,
'expiration' => $expiration->format(DateTimeImmutable::ATOM),
]);
while ($offset < $total) {
$users = $this->userRepository->findByMostOldSubscriptionOrWithoutSubscriptionOrData(
$interval,
$limit,
$offset
);
foreach ($users as $user) {
if (!$this->mapCalendarToUser->hasUserId($user)) {
$this->mapCalendarToUser->writeMetadata($user);
}
if ($this->mapCalendarToUser->hasUserId($user)) {
// we first try to renew an existing subscription, if any.
// if not, or if it fails, we try to create a new one
if ($this->mapCalendarToUser->hasActiveSubscription($user)) {
$this->logger->debug(self::class . ' renew a subscription for', [
'userId' => $user->getId(),
'username' => $user->getUsernameCanonical(),
]);
['secret' => $secret, 'id' => $id, 'expiration' => $expirationTs]
= $this->eventsOnUserSubscriptionCreator->renewSubscriptionForUser($user, $expiration);
$this->mapCalendarToUser->writeSubscriptionMetadata($user, $expirationTs, $id, $secret);
if (0 !== $expirationTs) {
++$renewed;
} else {
$this->logger->warning(self::class . ' could not renew subscription for a user', [
'userId' => $user->getId(),
'username' => $user->getUsernameCanonical(),
]);
}
}
if (!$this->mapCalendarToUser->hasActiveSubscription($user)) {
$this->logger->debug(self::class . ' create a subscription for', [
'userId' => $user->getId(),
'username' => $user->getUsernameCanonical(),
]);
['secret' => $secret, 'id' => $id, 'expiration' => $expirationTs]
= $this->eventsOnUserSubscriptionCreator->createSubscriptionForUser($user, $expiration);
$this->mapCalendarToUser->writeSubscriptionMetadata($user, $expirationTs, $id, $secret);
if (0 !== $expirationTs) {
++$created;
} else {
$this->logger->warning(self::class . ' could not create subscription for a user', [
'userId' => $user->getId(),
'username' => $user->getUsernameCanonical(),
]);
}
}
}
++$offset;
}
$this->em->flush();
$this->em->clear();
}
$this->logger->warning(self::class . ' process executed', [
'created' => $created,
'renewed' => $renewed,
]);
return 0;
}
protected function configure()
{
parent::configure();
$this
->setDescription('MSGraph: collect user metadata and create subscription on events for users')
->addOption(
'renew-before-end-interval',
'r',
InputOption::VALUE_OPTIONAL,
'delay before renewing subscription',
'P1D'
)
->addOption(
'subscription-duration',
's',
InputOption::VALUE_OPTIONAL,
'duration for the subscription',
'PT4230M'
);
}
}

View File

@@ -0,0 +1,48 @@
<?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);
/*
* 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\Command;
use Chill\CalendarBundle\Service\ShortMessageNotification\BulkCalendarShortMessageSender;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SendShortMessageOnEligibleCalendar extends Command
{
private BulkCalendarShortMessageSender $messageSender;
public function __construct(BulkCalendarShortMessageSender $messageSender)
{
parent::__construct();
$this->messageSender = $messageSender;
}
public function getName()
{
return 'chill:calendar:send-short-messages';
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->messageSender->sendBulkMessageToEligibleCalendars();
return 0;
}
}

View File

@@ -0,0 +1,207 @@
<?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);
/*
* 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\Command;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Service\ShortMessageNotification\ShortMessageForCalendarBuilderInterface;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Phonenumber\PhoneNumberHelperInterface;
use Chill\MainBundle\Repository\UserRepositoryInterface;
use Chill\MainBundle\Service\ShortMessage\ShortMessageTransporterInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\PersonRepository;
use DateInterval;
use DateTimeImmutable;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use UnexpectedValueException;
use function count;
class SendTestShortMessageOnCalendarCommand extends Command
{
private ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder;
private PersonRepository $personRepository;
private PhoneNumberHelperInterface $phoneNumberHelper;
private PhoneNumberUtil $phoneNumberUtil;
private ShortMessageTransporterInterface $transporter;
private UserRepositoryInterface $userRepository;
public function __construct(
PersonRepository $personRepository,
PhoneNumberUtil $phoneNumberUtil,
PhoneNumberHelperInterface $phoneNumberHelper,
ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder,
ShortMessageTransporterInterface $transporter,
UserRepositoryInterface $userRepository
) {
parent::__construct();
$this->personRepository = $personRepository;
$this->phoneNumberUtil = $phoneNumberUtil;
$this->phoneNumberHelper = $phoneNumberHelper;
$this->messageForCalendarBuilder = $messageForCalendarBuilder;
$this->transporter = $transporter;
$this->userRepository = $userRepository;
}
public function getName()
{
return 'chill:calendar:test-send-short-message';
}
protected function configure()
{
$this->setDescription('Test sending a SMS for a dummy calendar appointment');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$calendar = new Calendar();
$calendar->setSendSMS(true);
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
// start date
$question = new Question('When will start the appointment ? (default: "1 hour") ', '1 hour');
$startDate = new DateTimeImmutable($helper->ask($input, $output, $question));
if (false === $startDate) {
throw new UnexpectedValueException('could not create a date with this date and time');
}
$calendar->setStartDate($startDate);
// end date
$question = new Question('How long will last the appointment ? (default: "PT30M") ', 'PT30M');
$interval = new DateInterval($helper->ask($input, $output, $question));
if (false === $interval) {
throw new UnexpectedValueException('could not create the interval');
}
$calendar->setEndDate($calendar->getStartDate()->add($interval));
// a person
$question = new Question('Who will participate ? Give an id for a person. ');
$question
->setValidator(function ($answer): Person {
if (!is_numeric($answer)) {
throw new UnexpectedValueException('the answer must be numeric');
}
if (0 >= (int) $answer) {
throw new UnexpectedValueException('the answer must be greater than zero');
}
$person = $this->personRepository->find((int) $answer);
if (null === $person) {
throw new UnexpectedValueException('The person is not found');
}
return $person;
});
$person = $helper->ask($input, $output, $question);
$calendar->addPerson($person);
// a main user
$question = new Question('Who will be the main user ? Give an id for a user. ');
$question
->setValidator(function ($answer): User {
if (!is_numeric($answer)) {
throw new UnexpectedValueException('the answer must be numeric');
}
if (0 >= (int) $answer) {
throw new UnexpectedValueException('the answer must be greater than zero');
}
$user = $this->userRepository->find((int) $answer);
if (null === $user) {
throw new UnexpectedValueException('The user is not found');
}
return $user;
});
$user = $helper->ask($input, $output, $question);
$calendar->setMainUser($user);
// phonenumber
$phonenumberFormatted = null !== $person->getMobilenumber() ?
$this->phoneNumberUtil->format($person->getMobilenumber(), PhoneNumberFormat::E164) : '';
$question = new Question(
sprintf('To which number are we going to send this fake message ? (default to: %s)', $phonenumberFormatted),
$phonenumberFormatted
);
$question->setNormalizer(function ($answer): PhoneNumber {
if (null === $answer) {
throw new UnexpectedValueException('The person is not found');
}
$phone = $this->phoneNumberUtil->parse($answer, 'BE');
if (!$this->phoneNumberUtil->isPossibleNumberForType($phone, PhoneNumberType::MOBILE)) {
throw new UnexpectedValueException('Phone number si not a mobile');
}
return $phone;
});
$phone = $helper->ask($input, $output, $question);
$question = new ConfirmationQuestion('really send the message to the phone ?');
$reallySend = (bool) $helper->ask($input, $output, $question);
$messages = $this->messageForCalendarBuilder->buildMessageForCalendar($calendar);
if (0 === count($messages)) {
$output->writeln('no message to send to this user');
}
foreach ($messages as $key => $message) {
$output->writeln("The short message for SMS {$key} will be: ");
$output->writeln($message->getContent());
$message->setPhoneNumber($phone);
if ($reallySend) {
$this->transporter->send($message);
}
}
return 0;
}
}

View File

@@ -1,14 +1,14 @@
<?php
/**
declare(strict_types=1);
/*
* 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 Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

View File

@@ -1,21 +1,83 @@
<?php
/**
declare(strict_types=1);
/*
* 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\CalendarBundle\Repository\CalendarRepository;
use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Serializer\Model\Collection;
use DateTimeImmutable;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route;
class CalendarAPIController extends ApiController
{
private CalendarRepository $calendarRepository;
public function __construct(CalendarRepository $calendarRepository)
{
$this->calendarRepository = $calendarRepository;
}
/**
* @Route("/api/1.0/calendar/calendar/by-user/{id}.{_format}",
* name="chill_api_single_calendar_list_by-user",
* requirements={"_format": "json"}
* )
*/
public function listByUser(User $user, Request $request, string $_format): JsonResponse
{
$this->denyAccessUnlessGranted('ROLE_USER');
if (!$request->query->has('dateFrom')) {
throw new BadRequestHttpException('You must provide a dateFrom parameter');
}
if (false === $dateFrom = DateTimeImmutable::createFromFormat(
DateTimeImmutable::ATOM,
$request->query->get('dateFrom')
)) {
throw new BadRequestHttpException('dateFrom not parsable');
}
if (!$request->query->has('dateTo')) {
throw new BadRequestHttpException('You must provide a dateTo parameter');
}
if (false === $dateTo = DateTimeImmutable::createFromFormat(
DateTimeImmutable::ATOM,
$request->query->get('dateTo')
)) {
throw new BadRequestHttpException('dateTo not parsable');
}
$total = $this->calendarRepository->countByUser($user, $dateFrom, $dateTo);
$paginator = $this->getPaginatorFactory()->create($total);
$ranges = $this->calendarRepository->findByUser(
$user,
$dateFrom,
$dateTo,
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()
);
$collection = new Collection($ranges, $paginator);
return $this->json($collection, Response::HTTP_OK, [], ['groups' => ['calendar:light']]);
}
protected function customizeQuery(string $action, Request $request, $qb): void
{
if ($request->query->has('main_user')) {

View File

@@ -1,65 +1,104 @@
<?php
/**
declare(strict_types=1);
/*
* 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\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Form\CalendarType;
use Chill\CalendarBundle\Repository\CalendarRepository;
use Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface;
use Chill\CalendarBundle\Repository\CalendarACLAwareRepositoryInterface;
use Chill\CalendarBundle\Security\Voter\CalendarVoter;
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\MainBundle\Repository\UserRepositoryInterface;
use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
use Chill\MainBundle\Templating\Listing\FilterOrderHelperFactoryInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use DateTimeImmutable;
use Exception;
use http\Exception\UnexpectedValueException;
use Psr\Log\LoggerInterface;
use RuntimeException;
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\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class CalendarController extends AbstractController
{
protected AuthorizationHelper $authorizationHelper;
private AccompanyingPeriodRepository $accompanyingPeriodRepository;
protected EventDispatcherInterface $eventDispatcher;
private CalendarACLAwareRepositoryInterface $calendarACLAwareRepository;
protected LoggerInterface $logger;
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
protected PaginatorFactory $paginator;
private FilterOrderHelperFactoryInterface $filterOrderHelperFactory;
protected SerializerInterface $serializer;
private LoggerInterface $logger;
private CalendarRepository $calendarRepository;
private PaginatorFactory $paginator;
private PersonRepository $personRepository;
private RemoteCalendarConnectorInterface $remoteCalendarConnector;
private SerializerInterface $serializer;
private TranslatableStringHelperInterface $translatableStringHelper;
private TranslatorInterface $translator;
private UserRepositoryInterface $userRepository;
public function __construct(
EventDispatcherInterface $eventDispatcher,
AuthorizationHelper $authorizationHelper,
CalendarACLAwareRepositoryInterface $calendarACLAwareRepository,
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
FilterOrderHelperFactoryInterface $filterOrderHelperFactory,
LoggerInterface $logger,
SerializerInterface $serializer,
PaginatorFactory $paginator,
CalendarRepository $calendarRepository
RemoteCalendarConnectorInterface $remoteCalendarConnector,
SerializerInterface $serializer,
TranslatableStringHelperInterface $translatableStringHelper,
PersonRepository $personRepository,
AccompanyingPeriodRepository $accompanyingPeriodRepository,
UserRepositoryInterface $userRepository,
TranslatorInterface $translator
) {
$this->eventDispatcher = $eventDispatcher;
$this->authorizationHelper = $authorizationHelper;
$this->calendarACLAwareRepository = $calendarACLAwareRepository;
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
$this->filterOrderHelperFactory = $filterOrderHelperFactory;
$this->logger = $logger;
$this->serializer = $serializer;
$this->paginator = $paginator;
$this->calendarRepository = $calendarRepository;
$this->remoteCalendarConnector = $remoteCalendarConnector;
$this->serializer = $serializer;
$this->translatableStringHelper = $translatableStringHelper;
$this->personRepository = $personRepository;
$this->accompanyingPeriodRepository = $accompanyingPeriodRepository;
$this->userRepository = $userRepository;
$this->translator = $translator;
}
/**
@@ -67,27 +106,23 @@ class CalendarController extends AbstractController
*
* @Route("/{_locale}/calendar/{id}/delete", name="chill_calendar_calendar_delete")
*/
public function deleteAction(Request $request, int $id)
public function deleteAction(Request $request, Calendar $entity)
{
$view = null;
$em = $this->getDoctrine()->getManager();
[$user, $accompanyingPeriod] = $this->getEntity($request);
[$person, $accompanyingPeriod] = [$entity->getPerson(), $entity->getAccompanyingPeriod()];
if ($accompanyingPeriod instanceof AccompanyingPeriod) {
$view = '@ChillCalendar/Calendar/confirm_deleteByAccompanyingCourse.html.twig';
} elseif ($user instanceof User) {
$view = '@ChillCalendar/Calendar/confirm_deleteByUser.html.twig';
$redirectRoute = $this->generateUrl('chill_calendar_calendar_list_by_period', ['id' => $accompanyingPeriod->getId()]);
} elseif ($person instanceof Person) {
$view = '@ChillCalendar/Calendar/confirm_deleteByPerson.html.twig';
$redirectRoute = $this->generateUrl('chill_calendar_calendar_list_by_person', ['id' => $person->getId()]);
} else {
throw new RuntimeException('nor person or accompanying period');
}
/** @var Calendar $entity */
$entity = $em->getRepository(\Chill\CalendarBundle\Entity\Calendar::class)->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Calendar entity.');
}
$form = $this->createDeleteForm($id, $user, $accompanyingPeriod);
$form = $this->createDeleteForm($entity);
if ($request->getMethod() === Request::METHOD_DELETE) {
$form->handleRequest($request);
@@ -101,23 +136,18 @@ class CalendarController extends AbstractController
$em->remove($entity);
$em->flush();
$this->addFlash('success', $this->get('translator')
$this->addFlash('success', $this->translator
->trans('The calendar item has been successfully removed.'));
$params = $this->buildParamsToUrl($user, $accompanyingPeriod);
return $this->redirectToRoute('chill_calendar_calendar_list', $params);
return new RedirectResponse($redirectRoute);
}
}
if (null === $view) {
throw $this->createNotFoundException('Template not found');
}
return $this->render($view, [
'calendar' => $entity,
'delete_form' => $form->createView(),
'accompanyingCourse' => $accompanyingPeriod,
'person' => $person,
]);
}
@@ -126,48 +156,67 @@ class CalendarController extends AbstractController
*
* @Route("/{_locale}/calendar/calendar/{id}/edit", name="chill_calendar_calendar_edit")
*/
public function editAction(int $id, Request $request): Response
public function editAction(Calendar $entity, Request $request): Response
{
$view = null;
$this->denyAccessUnlessGranted(CalendarVoter::EDIT, $entity);
if (!$this->remoteCalendarConnector->isReady()) {
return $this->remoteCalendarConnector->getMakeReadyResponse($request->getUri());
}
$em = $this->getDoctrine()->getManager();
[$user, $accompanyingPeriod] = $this->getEntity($request);
[$person, $accompanyingPeriod] = [$entity->getPerson(), $entity->getAccompanyingPeriod()];
if ($accompanyingPeriod instanceof AccompanyingPeriod) {
$view = '@ChillCalendar/Calendar/editByAccompanyingCourse.html.twig';
} elseif ($user instanceof User) {
$view = '@ChillCalendar/Calendar/editByUser.html.twig';
$redirectRoute = $this->generateUrl('chill_calendar_calendar_list_by_period', ['id' => $accompanyingPeriod->getId()]);
} elseif ($person instanceof Person) {
$view = '@ChillCalendar/Calendar/editByPerson.html.twig';
$redirectRoute = $this->generateUrl('chill_calendar_calendar_list_by_person', ['id' => $person->getId()]);
} else {
throw new RuntimeException('no person nor accompanying period');
}
$entity = $em->getRepository(\Chill\CalendarBundle\Entity\Calendar::class)->find($id);
$form = $this->createForm(CalendarType::class, $entity)
->add('save', SubmitType::class);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Calendar entity.');
$form->add('save_and_upload_doc', SubmitType::class);
$templates = $this->docGeneratorTemplateRepository->findByEntity(Calendar::class);
foreach ($templates as $template) {
$form->add('save_and_generate_doc_' . $template->getId(), SubmitType::class, [
'label' => $this->translatableStringHelper->localize($template->getName()),
]);
}
$form = $this->createForm(CalendarType::class, $entity, [
'accompanyingPeriod' => $accompanyingPeriod,
])->handleRequest($request);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($entity);
$em->flush();
$this->addFlash('success', $this->get('translator')->trans('Success : calendar item updated!'));
$this->addFlash('success', $this->translator->trans('Success : calendar item updated!'));
$params = $this->buildParamsToUrl($user, $accompanyingPeriod);
if ($form->get('save_and_upload_doc')->isClicked()) {
return $this->redirectToRoute('chill_calendar_calendardoc_new', ['id' => $entity->getId()]);
}
return $this->redirectToRoute('chill_calendar_calendar_list', $params);
foreach ($templates as $template) {
if ($form->get('save_and_generate_doc_' . $template->getId())->isClicked()) {
return $this->redirectToRoute('chill_docgenerator_generate_from_template', [
'entityClassName' => Calendar::class,
'entityId' => $entity->getId(),
'template' => $template->getId(),
'returnPath' => $request->getRequestUri(),
]);
}
}
return new RedirectResponse($redirectRoute);
}
if ($form->isSubmitted() && !$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');
$this->addFlash('error', $this->translator->trans('This form contains errors'));
}
$entity_array = $this->serializer->normalize($entity, 'json', ['groups' => 'read']);
@@ -175,55 +224,101 @@ class CalendarController extends AbstractController
return $this->render($view, [
'entity' => $entity,
'form' => $form->createView(),
'delete_form' => $deleteForm->createView(),
'accompanyingCourse' => $accompanyingPeriod,
'user' => $user,
'accompanyingCourse' => $entity->getAccompanyingPeriod(),
'person' => $entity->getPerson(),
'entity_json' => $entity_array,
'templates' => $templates,
]);
}
/**
* Lists all Calendar entities.
*
* @Route("/{_locale}/calendar/calendar/", name="chill_calendar_calendar_list")
* @Route("/{_locale}/calendar/calendar/by-period/{id}", name="chill_calendar_calendar_list_by_period")
*/
public function listAction(Request $request): Response
public function listActionByCourse(AccompanyingPeriod $accompanyingPeriod): Response
{
$view = null;
$this->denyAccessUnlessGranted(CalendarVoter::SEE, $accompanyingPeriod);
[$user, $accompanyingPeriod] = $this->getEntity($request);
$filterOrder = $this->buildListFilterOrder();
['from' => $from, 'to' => $to] = $filterOrder->getDateRangeData('startDate');
if ($user instanceof User) {
$calendarItems = $this->calendarRepository->findByUser($user);
$total = $this->calendarACLAwareRepository
->countByAccompanyingPeriod($accompanyingPeriod, $from, $to);
$paginator = $this->paginator->create($total);
$calendarItems = $this->calendarACLAwareRepository->findByAccompanyingPeriod(
$accompanyingPeriod,
$from,
$to,
['startDate' => 'DESC'],
$paginator->getCurrentPageFirstItemNumber(),
$paginator->getItemsPerPage()
);
$view = '@ChillCalendar/Calendar/listByUser.html.twig';
return $this->render('@ChillCalendar/Calendar/listByAccompanyingCourse.html.twig', [
'calendarItems' => $calendarItems,
'accompanyingCourse' => $accompanyingPeriod,
'paginator' => $paginator,
'filterOrder' => $filterOrder,
'nbIgnored' => $this->calendarACLAwareRepository->countIgnoredByAccompanyingPeriod($accompanyingPeriod, $from, $to),
'templates' => $this->docGeneratorTemplateRepository->findByEntity(Calendar::class),
]);
}
return $this->render($view, [
'calendarItems' => $calendarItems,
'user' => $user,
]);
/**
* Lists all Calendar entities on a person.
*
* @Route("/{_locale}/calendar/calendar/by-person/{id}", name="chill_calendar_calendar_list_by_person")
*/
public function listActionByPerson(Person $person): Response
{
$this->denyAccessUnlessGranted(CalendarVoter::SEE, $person);
$filterOrder = $this->buildListFilterOrder();
['from' => $from, 'to' => $to] = $filterOrder->getDateRangeData('startDate');
$total = $this->calendarACLAwareRepository
->countByPerson($person, $from, $to);
$paginator = $this->paginator->create($total);
$calendarItems = $this->calendarACLAwareRepository->findByPerson(
$person,
$from,
$to,
['startDate' => 'DESC'],
$paginator->getCurrentPageFirstItemNumber(),
$paginator->getItemsPerPage()
);
return $this->render('@ChillCalendar/Calendar/listByPerson.html.twig', [
'calendarItems' => $calendarItems,
'person' => $person,
'paginator' => $paginator,
'filterOrder' => $filterOrder,
'nbIgnored' => $this->calendarACLAwareRepository->countIgnoredByPerson($person, $from, $to),
'templates' => $this->docGeneratorTemplateRepository->findByEntity(Calendar::class),
]);
}
/**
* @Route("/{_locale}/calendar/calendar/my", name="chill_calendar_calendar_list_my")
*/
public function myCalendar(Request $request): Response
{
$this->denyAccessUnlessGranted('ROLE_USER');
if (!$this->remoteCalendarConnector->isReady()) {
return $this->remoteCalendarConnector->getMakeReadyResponse($request->getUri());
}
if ($accompanyingPeriod instanceof AccompanyingPeriod) {
$total = $this->calendarRepository->countByAccompanyingPeriod($accompanyingPeriod);
$paginator = $this->paginator->create($total);
$calendarItems = $this->calendarRepository->findBy(
['accompanyingPeriod' => $accompanyingPeriod],
['startDate' => 'DESC'],
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()
);
$view = '@ChillCalendar/Calendar/listByAccompanyingCourse.html.twig';
return $this->render($view, [
'calendarItems' => $calendarItems,
'accompanyingCourse' => $accompanyingPeriod,
'paginator' => $paginator,
]);
if (!$this->getUser() instanceof User) {
throw new UnauthorizedHttpException('you are not an user');
}
throw new Exception('Unable to list actions.');
$view = '@ChillCalendar/Calendar/listByUser.html.twig';
return $this->render($view, [
'user' => $this->getUser(),
]);
}
/**
@@ -233,47 +328,81 @@ class CalendarController extends AbstractController
*/
public function newAction(Request $request): Response
{
if (!$this->remoteCalendarConnector->isReady()) {
return $this->remoteCalendarConnector->getMakeReadyResponse($request->getUri());
}
$view = null;
$em = $this->getDoctrine()->getManager();
[$user, $accompanyingPeriod] = $this->getEntity($request);
[$person, $accompanyingPeriod] = $this->getEntity($request);
$entity = new Calendar();
$redirectRoute = '';
if ($accompanyingPeriod instanceof AccompanyingPeriod) {
$view = '@ChillCalendar/Calendar/newByAccompanyingCourse.html.twig';
}
// elseif ($user instanceof User) {
// $view = '@ChillCalendar/Calendar/newUser.html.twig';
// }
$entity = new Calendar();
$entity->setUser($this->getUser());
$entity->setStatus($entity::STATUS_VALID);
// if ($user instanceof User) {
// $entity->setPerson($user);
// }
if ($accompanyingPeriod instanceof AccompanyingPeriod) {
$entity->setAccompanyingPeriod($accompanyingPeriod);
$redirectRoute = $this->generateUrl('chill_calendar_calendar_list_by_period', ['id' => $accompanyingPeriod->getId()]);
} elseif (null !== $person) {
$view = '@ChillCalendar/Calendar/newByPerson.html.twig';
$entity->setPerson($person)->addPerson($person);
$redirectRoute = $this->generateUrl('chill_calendar_calendar_list_by_person', ['id' => $person->getId()]);
}
$form = $this->createForm(CalendarType::class, $entity, [
'accompanyingPeriod' => $accompanyingPeriod,
])->handleRequest($request);
if ($request->query->has('mainUser')) {
$entity->setMainUser($this->userRepository->find($request->query->getInt('mainUser')));
}
$this->denyAccessUnlessGranted(CalendarVoter::CREATE, $entity);
$form = $this->createForm(CalendarType::class, $entity)
->add('save', SubmitType::class);
$templates = $this->docGeneratorTemplateRepository->findByEntity(Calendar::class);
$form->add('save_and_upload_doc', SubmitType::class);
foreach ($templates as $template) {
$form->add('save_and_generate_doc_' . $template->getId(), SubmitType::class, [
'label' => $this->translatableStringHelper->localize($template->getName()),
]);
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($entity);
$em->flush();
$this->addFlash('success', $this->get('translator')->trans('Success : calendar item created!'));
$this->addFlash('success', $this->translator->trans('Success : calendar item created!'));
$params = $this->buildParamsToUrl($user, $accompanyingPeriod);
if ($form->get('save_and_upload_doc')->isClicked()) {
return $this->redirectToRoute('chill_calendar_calendardoc_new', [
'id' => $entity->getId()
]);
}
return $this->redirectToRoute('chill_calendar_calendar_list', $params);
foreach ($templates as $template) {
if ($form->get('save_and_generate_doc_' . $template->getId())->isClicked()) {
return $this->redirectToRoute('chill_docgenerator_generate_from_template', [
'entityClassName' => Calendar::class,
'entityId' => $entity->getId(),
'template' => $template->getId(),
'returnPath' => $this->generateUrl('chill_calendar_calendar_edit', ['id' => $entity->getId()]),
]);
}
}
if ('' !== $redirectRoute) {
return new RedirectResponse($redirectRoute);
}
throw new UnexpectedValueException('No person id or accompanying period id was given');
}
if ($form->isSubmitted() && !$form->isValid()) {
$this->addFlash('error', $this->get('translator')->trans('This form contains errors'));
$this->addFlash('error', $this->translator->trans('This form contains errors'));
}
if (null === $view) {
@@ -283,11 +412,13 @@ class CalendarController extends AbstractController
$entity_array = $this->serializer->normalize($entity, 'json', ['groups' => 'read']);
return $this->render($view, [
'user' => $user,
'context' => $entity->getContext(),
'person' => $person,
'accompanyingCourse' => $accompanyingPeriod,
'entity' => $entity,
'form' => $form->createView(),
'entity_json' => $entity_array,
'templates' => $templates,
]);
}
@@ -298,6 +429,7 @@ class CalendarController extends AbstractController
*/
public function showAction(Request $request, int $id): Response
{
throw new Exception('not implemented');
$view = null;
$em = $this->getDoctrine()->getManager();
@@ -349,7 +481,7 @@ class CalendarController extends AbstractController
'professionalsId' => $professionalsId,
'date' => $entity->getStartDate()->format('Y-m-d'),
'durationTime' => $durationTimeInMinutes,
'location' => $entity->getLocation()->getId(),
'location' => $entity->getLocation() ? $entity->getLocation()->getId() : null,
'comment' => $entity->getComment()->getComment(),
];
@@ -362,67 +494,108 @@ class CalendarController extends AbstractController
]);
}
private function buildParamsToUrl(?User $user, ?AccompanyingPeriod $accompanyingPeriod): array
/**
* @Route("/{_locale}/calendar/calendar/{id}/to-activity", name="chill_calendar_calendar_to_activity")
*/
public function toActivity(Request $request, Calendar $calendar): RedirectResponse
{
$params = [];
$this->denyAccessUnlessGranted(CalendarVoter::SEE, $calendar);
if (null !== $user) {
$params['user_id'] = $user->getId();
$personsId = array_map(
static fn (Person $p): int => $p->getId(),
$calendar->getPersons()->toArray()
);
$professionalsId = array_map(
static fn (ThirdParty $thirdParty): ?int => $thirdParty->getId(),
$calendar->getProfessionals()->toArray()
);
$usersId = array_map(
static fn (User $user): ?int => $user->getId(),
array_merge($calendar->getUsers()->toArray(), [$calendar->getMainUser()])
);
$durationTime = $calendar->getEndDate()->diff($calendar->getStartDate());
$durationTimeInMinutes = $durationTime->days * 1440 + $durationTime->h * 60 + $durationTime->i;
$activityData = [
'calendarId' => $calendar->getId(),
'personsId' => $personsId,
'professionalsId' => $professionalsId,
'usersId' => $usersId,
'date' => $calendar->getStartDate()->format('Y-m-d'),
'durationTime' => $durationTimeInMinutes,
'location' => $calendar->getLocation() ? $calendar->getLocation()->getId() : null,
'comment' => $calendar->getComment()->getComment(),
];
$routeParams = [
'activityData' => $activityData,
'returnPath' => $request->query->get('returnPath', null),
];
if ($calendar->getContext() === 'accompanying_period') {
$routeParams['accompanying_period_id'] = $calendar->getAccompanyingPeriod()->getId();
} elseif ($calendar->getContext() === 'person') {
$routeParams['person_id'] = $calendar->getPerson()->getId();
} else {
throw new RuntimeException('context not found for this calendar');
}
if (null !== $accompanyingPeriod) {
$params['accompanying_period_id'] = $accompanyingPeriod->getId();
}
return $this->redirectToRoute('chill_activity_activity_new', $routeParams);
}
return $params;
private function buildListFilterOrder(): FilterOrderHelper
{
$filterOrder = $this->filterOrderHelperFactory->create(self::class);
$filterOrder->addDateRange('startDate', null, new DateTimeImmutable('3 days ago'), null);
return $filterOrder->build();
}
/**
* Creates a form to delete a Calendar entity by id.
*/
private function createDeleteForm(int $id, ?User $user, ?AccompanyingPeriod $accompanyingPeriod): FormInterface
private function createDeleteForm(Calendar $calendar): FormInterface
{
$params = $this->buildParamsToUrl($user, $accompanyingPeriod);
$params['id'] = $id;
return $this->createFormBuilder()
->setAction($this->generateUrl('chill_calendar_calendar_delete', $params))
->setAction($this->generateUrl('chill_calendar_calendar_delete', ['id' => $calendar->getId()]))
->setMethod('DELETE')
->add('submit', SubmitType::class, ['label' => 'Delete'])
->getForm();
}
/**
* @return array{0: ?Person, 1: ?AccompanyingPeriod}
*/
private function getEntity(Request $request): array
{
$em = $this->getDoctrine()->getManager();
$user = $accompanyingPeriod = null;
$person = $accompanyingPeriod = null;
if ($request->query->has('user_id')) {
$user_id = $request->get('user_id');
$user = $em->getRepository(User::class)->find($user_id);
if ($request->query->has('person_id')) {
$person = $this->personRepository->find($request->query->getInt('person_id'));
if (null === $user) {
throw $this->createNotFoundException('User not found');
if (null === $person) {
throw $this->createNotFoundException('Person not found');
}
// TODO Add permission
// $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $user);
$this->denyAccessUnlessGranted(PersonVoter::SEE, $person);
} elseif ($request->query->has('accompanying_period_id')) {
$accompanying_period_id = $request->get('accompanying_period_id');
$accompanyingPeriod = $em->getRepository(AccompanyingPeriod::class)->find($accompanying_period_id);
$accompanyingPeriod = $this->accompanyingPeriodRepository->find($request->query->getInt('accompanying_period_id'));
if (null === $accompanyingPeriod) {
throw $this->createNotFoundException('Accompanying Period not found');
}
// TODO Add permission
// $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
$this->denyAccessUnlessGranted(AccompanyingPeriodVoter::SEE, $accompanyingPeriod);
} else {
throw $this->createNotFoundException('Person or Accompanying Period not found');
}
return [
$user, $accompanyingPeriod,
$person, $accompanyingPeriod,
];
}
}

View File

@@ -0,0 +1,240 @@
<?php
declare(strict_types=1);
/*
* 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\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\CalendarDoc;
use Chill\CalendarBundle\Form\CalendarDocCreateType;
use Chill\CalendarBundle\Form\CalendarDocEditType;
use Chill\CalendarBundle\Security\Voter\CalendarDocVoter;
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Templating\EngineInterface;
use UnexpectedValueException;
class CalendarDocController
{
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private EngineInterface $engine;
private EntityManagerInterface $entityManager;
private FormFactoryInterface $formFactory;
private Security $security;
private SerializerInterface $serializer;
private UrlGeneratorInterface $urlGenerator;
public function __construct(
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
EngineInterface $engine,
EntityManagerInterface $entityManager,
FormFactoryInterface $formFactory,
Security $security,
UrlGeneratorInterface $urlGenerator
) {
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
$this->engine = $engine;
$this->entityManager = $entityManager;
$this->formFactory = $formFactory;
$this->security = $security;
$this->urlGenerator = $urlGenerator;
}
/**
* @Route("/{_locale}/calendar/calendar-doc/{id}/new", name="chill_calendar_calendardoc_new")
*/
public function create(Calendar $calendar, Request $request): Response
{
$calendarDoc = (new CalendarDoc($calendar, null))->setCalendar($calendar);
if (!$this->security->isGranted(CalendarDocVoter::EDIT, $calendarDoc)) {
throw new AccessDeniedHttpException();
}
// set variables
switch ($calendarDoc->getCalendar()->getContext()) {
case 'accompanying_period':
$view = '@ChillCalendar/CalendarDoc/new_accompanying_period.html.twig';
$returnRoute = 'chill_calendar_calendar_list_by_period';
$returnParams = ['id' => $calendarDoc->getCalendar()->getAccompanyingPeriod()->getId()];
break;
case 'person':
$view = '@ChillCalendar/CalendarDoc/new_person.html.twig';
$returnRoute = 'chill_calendar_calendar_list_by_person';
$returnParams = ['id' => $calendarDoc->getCalendar()->getPerson()->getId()];
break;
default:
throw new UnexpectedValueException('Unsupported context');
}
$calendarDocDTO = new CalendarDoc\CalendarDocCreateDTO();
$form = $this->formFactory->create(CalendarDocCreateType::class, $calendarDocDTO);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$calendarDoc->createFromDTO($calendarDocDTO);
$this->entityManager->persist($calendarDoc);
$this->entityManager->flush();
if ($request->query->has('returnPath')) {
return new RedirectResponse($request->query->get('returnPath'));
}
return new RedirectResponse(
$this->urlGenerator->generate($returnRoute, $returnParams)
);
}
return new Response(
$this->engine->render(
$view,
['calendar_doc' => $calendarDoc, 'form' => $form->createView()]
)
);
}
/**
* @Route("/{_locale}/calendar/calendar-doc/{id}/delete", name="chill_calendar_calendardoc_delete")
*/
public function delete(CalendarDoc $calendarDoc, Request $request): Response
{
if (!$this->security->isGranted(CalendarDocVoter::EDIT, $calendarDoc)) {
throw new AccessDeniedHttpException('Not authorized to delete document');
}
switch ($calendarDoc->getCalendar()->getContext()) {
case 'accompanying_period':
$view = '@ChillCalendar/CalendarDoc/delete_accompanying_period.html.twig';
$returnRoute = 'chill_calendar_calendar_list_by_period';
$returnParams = ['id' => $calendarDoc->getCalendar()->getAccompanyingPeriod()->getId()];
break;
case 'person':
$view = '@ChillCalendar/CalendarDoc/delete_person.html.twig';
$returnRoute = 'chill_calendar_calendar_list_by_person';
$returnParams = ['id' => $calendarDoc->getCalendar()->getPerson()->getId()];
break;
default:
throw new \LogicException(sprintf("This context '%s' is not supported", $calendarDoc->getCalendar()->getContext()));
}
$form = $this->formFactory->createBuilder()
->add('submit', SubmitType::class, [
'label' => 'Delete',
])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted()) {
$this->entityManager->remove($calendarDoc);
$this->entityManager->flush();
if ($request->query->has('returnPath')) {
return new RedirectResponse($request->query->get('returnPath'));
}
return new RedirectResponse(
$this->urlGenerator->generate($returnRoute, $returnParams)
);
}
return new Response(
$this->engine->render(
$view,
[
'calendar_doc' => $calendarDoc,
'form' => $form->createView(),
]
)
);
}
/**
* @Route("/{_locale}/calendar/calendar-doc/{id}/edit", name="chill_calendar_calendardoc_edit")
*/
public function edit(CalendarDoc $calendarDoc, Request $request): Response
{
if (!$this->security->isGranted(CalendarDocVoter::EDIT, $calendarDoc)) {
throw new AccessDeniedHttpException();
}
// set variables
switch ($calendarDoc->getCalendar()->getContext()) {
case 'accompanying_period':
$view = '@ChillCalendar/CalendarDoc/edit_accompanying_period.html.twig';
$returnRoute = 'chill_calendar_calendar_list_by_period';
$returnParams = ['id' => $calendarDoc->getCalendar()->getAccompanyingPeriod()->getId()];
break;
case 'person':
$view = '@ChillCalendar/CalendarDoc/edit_person.html.twig';
$returnRoute = 'chill_calendar_calendar_list_by_person';
$returnParams = ['id' => $calendarDoc->getCalendar()->getPerson()->getId()];
break;
default:
throw new UnexpectedValueException('Unsupported context');
}
$calendarDocEditDTO = new CalendarDoc\CalendarDocEditDTO($calendarDoc);
$form = $this->formFactory->create(CalendarDocEditType::class, $calendarDocEditDTO);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$calendarDoc->editFromDTO($calendarDocEditDTO);
$this->entityManager->flush();
if ($request->query->has('returnPath')) {
return new RedirectResponse($request->query->get('returnPath'));
}
return new RedirectResponse(
$this->urlGenerator->generate($returnRoute, $returnParams)
);
}
return new Response(
$this->engine->render(
$view,
['calendar_doc' => $calendarDoc, 'form' => $form->createView()]
)
);
}
}

View File

@@ -1,48 +1,82 @@
<?php
/**
declare(strict_types=1);
/*
* 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\CalendarBundle\Repository\CalendarRangeRepository;
use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Serializer\Model\Collection;
use DateTimeImmutable;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use function count;
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")
*/
public function availableRanges(Request $request, string $_format): JsonResponse
private CalendarRangeRepository $calendarRangeRepository;
public function __construct(CalendarRangeRepository $calendarRangeRepository)
{
$em = $this->getDoctrine()->getManager();
$this->calendarRangeRepository = $calendarRangeRepository;
}
$sql = 'SELECT c FROM ChillCalendarBundle:CalendarRange c
WHERE NOT EXISTS (SELECT cal.id FROM ChillCalendarBundle:Calendar cal WHERE cal.calendarRange = c.id)';
/**
* @Route("/api/1.0/calendar/calendar-range-available/{id}.{_format}",
* name="chill_api_single_calendar_range_available",
* requirements={"_format": "json"}
* )
*/
public function availableRanges(User $user, Request $request, string $_format): JsonResponse
{
//return new JsonResponse(['ok' => true], 200, [], false);
$this->denyAccessUnlessGranted('ROLE_USER');
if ($request->query->has('user')) {
$user = $request->query->get('user');
$sql = $sql . ' AND c.user = :user';
$query = $em->createQuery($sql)
->setParameter('user', $user);
} else {
$query = $em->createQuery($sql);
if (!$request->query->has('dateFrom')) {
throw new BadRequestHttpException('You must provide a dateFrom parameter');
}
$results = $query->getResult();
if (false === $dateFrom = DateTimeImmutable::createFromFormat(
DateTimeImmutable::ATOM,
$request->query->get('dateFrom')
)) {
throw new BadRequestHttpException('dateFrom not parsable');
}
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);
if (!$request->query->has('dateTo')) {
throw new BadRequestHttpException('You must provide a dateTo parameter');
}
if (false === $dateTo = DateTimeImmutable::createFromFormat(
DateTimeImmutable::ATOM,
$request->query->get('dateTo')
)) {
throw new BadRequestHttpException('dateTo not parsable');
}
$total = $this->calendarRangeRepository->countByAvailableRangesForUser($user, $dateFrom, $dateTo);
$paginator = $this->getPaginatorFactory()->create($total);
$ranges = $this->calendarRangeRepository->findByAvailableRangesForUser(
$user,
$dateFrom,
$dateTo,
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()
);
$collection = new Collection($ranges, $paginator);
return $this->json($collection, Response::HTTP_OK, [], ['groups' => ['read']]);
}
}

View File

@@ -1,14 +1,14 @@
<?php
/**
declare(strict_types=1);
/*
* 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\CRUD\Controller\CRUDController;

View File

@@ -0,0 +1,83 @@
<?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);
/*
* 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\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\Invite;
use Chill\CalendarBundle\Messenger\Message\InviteUpdateMessage;
use Chill\CalendarBundle\Security\Voter\InviteVoter;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use function in_array;
class InviteApiController
{
private EntityManagerInterface $entityManager;
private MessageBusInterface $messageBus;
private Security $security;
public function __construct(EntityManagerInterface $entityManager, MessageBusInterface $messageBus, Security $security)
{
$this->entityManager = $entityManager;
$this->messageBus = $messageBus;
$this->security = $security;
}
/**
* Give an answer to a calendar invite.
*
* @Route("/api/1.0/calendar/calendar/{id}/answer/{answer}.json", methods={"post"})
*/
public function answer(Calendar $calendar, string $answer): Response
{
$user = $this->security->getUser();
if (!$user instanceof User) {
throw new AccessDeniedHttpException('not a regular user');
}
if (null === $invite = $calendar->getInviteForUser($user)) {
throw new AccessDeniedHttpException('not invited to this calendar');
}
if (!$this->security->isGranted(InviteVoter::ANSWER, $invite)) {
throw new AccessDeniedHttpException('not allowed to answer on this invitation');
}
if (!in_array($answer, Invite::STATUSES, true)) {
throw new BadRequestHttpException('answer not valid');
}
$invite->setStatus($answer);
$this->entityManager->flush();
$this->messageBus->dispatch(new InviteUpdateMessage($invite, $this->security->getUser()));
return new JsonResponse(null, Response::HTTP_ACCEPTED, [], false);
}
}

View File

@@ -0,0 +1,76 @@
<?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);
/*
* 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\CalendarBundle\RemoteCalendar\Connector\MSGraph\OnBehalfOfUserTokenStorage;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use TheNetworg\OAuth2\Client\Provider\Azure;
use TheNetworg\OAuth2\Client\Token\AccessToken;
class RemoteCalendarConnectAzureController
{
private ClientRegistry $clientRegistry;
private OnBehalfOfUserTokenStorage $MSGraphTokenStorage;
public function __construct(
ClientRegistry $clientRegistry,
OnBehalfOfUserTokenStorage $MSGraphTokenStorage
) {
$this->clientRegistry = $clientRegistry;
$this->MSGraphTokenStorage = $MSGraphTokenStorage;
}
/**
* @Route("/{_locale}/connect/azure", name="chill_calendar_remote_connect_azure")
*/
public function connectAzure(Request $request): Response
{
$request->getSession()->set('azure_return_path', $request->query->get('returnPath', '/'));
return $this->clientRegistry
->getClient('azure') // key used in config/packages/knpu_oauth2_client.yaml
->redirect(['https://graph.microsoft.com/.default', 'offline_access'], []);
}
/**
* @Route("/connect/azure/check", name="chill_calendar_remote_connect_azure_check")
*/
public function connectAzureCheck(Request $request): Response
{
/** @var Azure $client */
$client = $this->clientRegistry->getClient('azure');
try {
/** @var AccessToken $token */
$token = $client->getAccessToken([]);
$this->MSGraphTokenStorage->setToken($token);
} catch (IdentityProviderException $e) {
throw $e;
}
return new RedirectResponse($request->getSession()->remove('azure_return_path', '/'));
}
}

View File

@@ -0,0 +1,61 @@
<?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);
/*
* 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\CalendarBundle\Messenger\Message\MSGraphChangeNotificationMessage;
use JsonException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use const JSON_THROW_ON_ERROR;
class RemoteCalendarMSGraphSyncController
{
private MessageBusInterface $messageBus;
public function __construct(MessageBusInterface $messageBus)
{
$this->messageBus = $messageBus;
}
/**
* @Route("/public/incoming-hook/calendar/msgraph/events/{userId}", name="chill_calendar_remote_msgraph_incoming_webhook_events",
* methods={"POST"})
*/
public function webhookCalendarReceiver(int $userId, Request $request): Response
{
if ($request->query->has('validationToken')) {
return new Response($request->query->get('validationToken'), Response::HTTP_OK, [
'content-type' => 'text/plain',
]);
}
try {
$body = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new BadRequestHttpException('could not decode json', $e);
}
$this->messageBus->dispatch(new MSGraphChangeNotificationMessage($body, $userId));
return new Response('', Response::HTTP_ACCEPTED);
}
}

View File

@@ -0,0 +1,114 @@
<?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);
/*
* 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\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Serializer\Model\Collection;
use DateTimeImmutable;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
use function count;
/**
* Contains method to get events (Calendar) from remote calendar.
*/
class RemoteCalendarProxyController
{
private PaginatorFactory $paginatorFactory;
private RemoteCalendarConnectorInterface $remoteCalendarConnector;
private SerializerInterface $serializer;
public function __construct(PaginatorFactory $paginatorFactory, RemoteCalendarConnectorInterface $remoteCalendarConnector, SerializerInterface $serializer)
{
$this->paginatorFactory = $paginatorFactory;
$this->remoteCalendarConnector = $remoteCalendarConnector;
$this->serializer = $serializer;
}
/**
* @Route("api/1.0/calendar/proxy/calendar/by-user/{id}/events")
*/
public function listEventForCalendar(User $user, Request $request): Response
{
if (!$request->query->has('dateFrom')) {
throw new BadRequestHttpException('You must provide a dateFrom parameter');
}
if (false === $dateFrom = DateTimeImmutable::createFromFormat(
DateTimeImmutable::ATOM,
$request->query->get('dateFrom')
)) {
throw new BadRequestHttpException('dateFrom not parsable');
}
if (!$request->query->has('dateTo')) {
throw new BadRequestHttpException('You must provide a dateTo parameter');
}
if (false === $dateTo = DateTimeImmutable::createFromFormat(
DateTimeImmutable::ATOM,
$request->query->get('dateTo')
)) {
throw new BadRequestHttpException('dateTo not parsable');
}
$total = $this->remoteCalendarConnector->countEventsForUser($user, $dateFrom, $dateTo);
$paginator = $this->paginatorFactory->create($total);
if (0 === $total) {
return new JsonResponse(
$this->serializer->serialize(new Collection([], $paginator), 'json'),
JsonResponse::HTTP_OK,
[],
true
);
}
$events = $this->remoteCalendarConnector->listEventsForUser(
$user,
$dateFrom,
$dateTo,
$paginator->getCurrentPageFirstItemNumber(),
$paginator->getItemsPerPage()
);
// in some case, we cannot paginate: we have to fetch all the items at once. We must avoid
// further requests by forcing the number of items returned.
if (count($events) > $paginator->getItemsPerPage()) {
$paginator->setItemsPerPage(count($events));
}
$collection = new Collection($events, $paginator);
return new JsonResponse(
$this->serializer->serialize($collection, 'json', ['groups' => ['read']]),
JsonResponse::HTTP_OK,
[],
true
);
}
}

View File

@@ -1,23 +1,29 @@
<?php
/**
declare(strict_types=1);
/*
* 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\DataFixtures\ORM;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Entity\Country;
use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\LocationType;
use Chill\MainBundle\Entity\PostalCode;
use Chill\MainBundle\Repository\UserRepository;
use DateTimeImmutable;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use libphonenumber\PhoneNumberUtil;
class LoadCalendarRange extends Fixture implements FixtureGroupInterface, OrderedFixtureInterface
{
@@ -49,6 +55,24 @@ class LoadCalendarRange extends Fixture implements FixtureGroupInterface, Ordere
$users = $this->userRepository->findAll();
$location = (new Location())
->setAddress($address = new Address())
->setName('Centre A')
->setEmail('centreA@test.chill.social')
->setLocationType($type = new LocationType())
->setPhonenumber1(PhoneNumberUtil::getInstance()->parse('+3287653812'));
$type->setTitle(['fr' => 'Service']);
$address->setStreet('Rue des Épaules')->setStreetNumber('14')
->setPostcode($postCode = new PostalCode());
$postCode->setCode('4145')->setName('Houte-Si-Plout')->setCountry(
($country = new Country())->setName(['fr' => 'Pays'])->setCountryCode('ZZ')
);
$manager->persist($country);
$manager->persist($postCode);
$manager->persist($address);
$manager->persist($type);
$manager->persist($location);
$days = [
'2021-08-23',
'2021-08-24',
@@ -58,6 +82,8 @@ class LoadCalendarRange extends Fixture implements FixtureGroupInterface, Ordere
'2021-08-31',
'2021-09-01',
'2021-09-02',
(new DateTimeImmutable('tomorrow'))->format('Y-m-d'),
(new DateTimeImmutable('today'))->format('Y-m-d'),
];
$hours = [
@@ -76,7 +102,8 @@ class LoadCalendarRange extends Fixture implements FixtureGroupInterface, Ordere
$calendarRange = (new CalendarRange())
->setUser($u)
->setStartDate($startEvent)
->setEndDate($endEvent);
->setEndDate($endEvent)
->setLocation($location);
$manager->persist($calendarRange);
}

View File

@@ -1,14 +1,14 @@
<?php
/**
declare(strict_types=1);
/*
* 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\DataFixtures\ORM;
use Chill\CalendarBundle\Entity\CancelReason;

View File

@@ -1,17 +1,19 @@
<?php
/**
declare(strict_types=1);
/*
* 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\DataFixtures\ORM;
use Chill\CalendarBundle\Entity\Invite;
use Chill\MainBundle\DataFixtures\ORM\LoadUsers;
use Chill\MainBundle\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Doctrine\Persistence\ObjectManager;
@@ -33,14 +35,21 @@ class LoadInvite extends Fixture implements FixtureGroupInterface
public function load(ObjectManager $manager): void
{
$arr = [
['name' => ['fr' => 'Rendez-vous décliné']],
['name' => ['fr' => 'Rendez-vous accepté']],
[
'name' => ['fr' => 'Rendez-vous décliné'],
'status' => Invite::DECLINED,
],
[
'name' => ['fr' => 'Rendez-vous accepté'],
'status' => Invite::ACCEPTED,
],
];
foreach ($arr as $a) {
echo 'Creating calendar invite : ' . $a['name']['fr'] . "\n";
$invite = (new Invite())
->setStatus($a['name']);
->setStatus($a['status'])
->setUser($this->getRandomUser());
$manager->persist($invite);
$reference = 'Invite_' . $a['name']['fr'];
$this->addReference($reference, $invite);
@@ -49,4 +58,11 @@ class LoadInvite extends Fixture implements FixtureGroupInterface
$manager->flush();
}
private function getRandomUser(): User
{
$userRef = array_rand(LoadUsers::$refs);
return $this->getReference($userRef);
}
}

View File

@@ -1,16 +1,17 @@
<?php
/**
declare(strict_types=1);
/*
* 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\DependencyInjection;
use Chill\CalendarBundle\Security\Voter\CalendarVoter;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
@@ -32,19 +33,30 @@ class ChillCalendarExtension extends Extension implements PrependExtensionInterf
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
$loader->load('services/exports.yaml');
$loader->load('services/controller.yml');
$loader->load('services/fixtures.yml');
$loader->load('services/form.yml');
$loader->load('services/event.yml');
$loader->load('services/remote_calendar.yaml');
$container->setParameter('chill_calendar', $config);
if ($config['short_messages']['enabled']) {
$container->setParameter('chill_calendar.short_messages', $config['short_messages']);
} else {
$container->setParameter('chill_calendar.short_messages', null);
}
}
public function prepend(ContainerBuilder $container)
{
$this->preprendRoutes($container);
$this->prependCruds($container);
$this->prependRoleHierarchy($container);
}
protected function prependCruds(ContainerBuilder $container)
private function prependCruds(ContainerBuilder $container)
{
$container->prependExtensionConfig('chill_main', [
'cruds' => [
@@ -120,7 +132,18 @@ class ChillCalendarExtension extends Extension implements PrependExtensionInterf
]);
}
protected function preprendRoutes(ContainerBuilder $container)
private function prependRoleHierarchy(ContainerBuilder $container): void
{
$container->prependExtensionConfig('security', [
'role_hierarchy' => [
CalendarVoter::CREATE => [CalendarVoter::SEE],
CalendarVoter::EDIT => [CalendarVoter::SEE],
CalendarVoter::DELETE => [CalendarVoter::SEE],
],
]);
}
private function preprendRoutes(ContainerBuilder $container)
{
$container->prependExtensionConfig('chill_main', [
'routing' => [

View File

@@ -1,14 +1,14 @@
<?php
/**
declare(strict_types=1);
/*
* 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\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@@ -24,11 +24,24 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('chill_calendar');
$rootNode = $treeBuilder->getRootNode('chill_calendar');
$rootNode = $treeBuilder->getRootNode();
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$rootNode
->children()
->arrayNode('short_messages')
->canBeDisabled()
->children()->end()
->end() // end for short_messages
->arrayNode('remote_calendars_sync')->canBeEnabled()
->children()
->arrayNode('microsoft_graph')->canBeEnabled()
->children()
->end() // end of machine_access_token
->end() // end of microsoft_graph children
->end() // end of array microsoft_graph
->end() // end of children's remote_calendars_sync
->end() // end of array remote_calendars_sync
->end();
return $treeBuilder;
}

View File

@@ -1,54 +1,101 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Entity;
use Chill\ActivityBundle\Entity\Activity;
use Chill\CalendarBundle\Repository\CalendarRepository;
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
use Chill\MainBundle\Doctrine\Model\TrackCreationTrait;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateTrait;
use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable;
use Chill\MainBundle\Entity\Embeddable\PrivateCommentEmbeddable;
use Chill\MainBundle\Entity\HasCentersInterface;
use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use DateInterval;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\ReadableCollection;
use Doctrine\ORM\Mapping as ORM;
use LogicException;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use function in_array;
/**
* @ORM\Table(name="chill_calendar.calendar")
* @ORM\Entity(repositoryClass=CalendarRepository::class)
* @ORM\Table(
* name="chill_calendar.calendar",
* uniqueConstraints={@ORM\UniqueConstraint(name="idx_calendar_remote", columns={"remoteId"}, options={"where": "remoteId <> ''"})}
* )
* @ORM\Entity
* @Serializer\DiscriminatorMap(typeProperty="type", mapping={
* "chill_calendar_calendar": Calendar::class
* })
*/
class Calendar
class Calendar implements TrackCreationInterface, TrackUpdateInterface, HasCentersInterface
{
use RemoteCalendarTrait;
use TrackCreationTrait;
use TrackUpdateTrait;
public const SMS_CANCEL_PENDING = 'sms_cancel_pending';
public const SMS_PENDING = 'sms_pending';
public const SMS_SENT = 'sms_sent';
public const STATUS_CANCELED = 'canceled';
/**
* @deprecated
*/
public const STATUS_MOVED = 'moved';
public const STATUS_VALID = 'valid';
/**
* @ORM\ManyToOne(targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod")
* @Groups({"read"})
* a list of invite which have been added during this session.
*
* @var array|Invite[]
*/
private AccompanyingPeriod $accompanyingPeriod;
public array $newInvites = [];
/**
* a list of invite which have been removed during this session.
*
* @var array|Invite[]
*/
public array $oldInvites = [];
public ?CalendarRange $previousCalendarRange = null;
public ?User $previousMainUser = null;
/**
* @ORM\ManyToOne(targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod", inversedBy="calendars")
* @Serializer\Groups({"calendar:read", "read"})
*/
private ?AccompanyingPeriod $accompanyingPeriod = null;
/**
* @ORM\ManyToOne(targetEntity="Chill\ActivityBundle\Entity\Activity")
@@ -56,7 +103,8 @@ class Calendar
private ?Activity $activity = null;
/**
* @ORM\ManyToOne(targetEntity="CalendarRange", inversedBy="calendars")
* @ORM\OneToOne(targetEntity="CalendarRange", inversedBy="calendar")
* @Serializer\Groups({"calendar:read", "read"})
*/
private ?CalendarRange $calendarRange = null;
@@ -67,13 +115,25 @@ class Calendar
/**
* @ORM\Embedded(class=CommentEmbeddable::class, columnPrefix="comment_")
* @Serializer\Groups({"calendar:read"})
* @Serializer\Groups({"calendar:read", "read", "docgen:read"})
*/
private CommentEmbeddable $comment;
/**
* @ORM\Column(type="datetimetz_immutable")
* @Serializer\Groups({"calendar:read"})
* @ORM\Column(type="integer", nullable=false, options={"default": 0})
*/
private int $dateTimeVersion = 0;
/**
* @var Collection<CalendarDoc::class>
* @ORM\OneToMany(targetEntity=CalendarDoc::class, mappedBy="calendar", orphanRemoval=true)
*/
private Collection $documents;
/**
* @ORM\Column(type="datetime_immutable", nullable=false)
* @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"})
* @Assert\NotNull(message="calendar.An end date is required")
*/
private ?DateTimeImmutable $endDate = null;
@@ -81,38 +141,49 @@ class Calendar
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Serializer\Groups({"calendar:read"})
* @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"})
*/
private ?int $id;
private ?int $id = null;
/**
* @ORM\ManyToMany(
* targetEntity="Invite",
* cascade={"persist", "remove", "merge", "detach"})
* @ORM\OneToMany(
* targetEntity=Invite::class,
* mappedBy="calendar",
* orphanRemoval=true,
* cascade={"persist", "remove", "merge", "detach"}
* )
* @ORM\JoinTable(name="chill_calendar.calendar_to_invites")
* @Groups({"read"})
* @Serializer\Groups({"read", "docgen:read"})
*/
private Collection $invites;
/**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Location")
* @groups({"read"})
* @Serializer\Groups({"read", "docgen:read"})
* @Assert\NotNull(message="calendar.A location is required")
*/
private ?Location $location = null;
/**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User")
* @Serializer\Groups({"calendar:read"})
* @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"})
* @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"})
* @Assert\NotNull(message="calendar.A main user is mandatory")
*/
private ?User $mainUser;
private ?User $mainUser = null;
/**
* @ORM\ManyToMany(
* targetEntity="Chill\PersonBundle\Entity\Person",
* cascade={"persist", "remove", "merge", "detach"})
* @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\JoinColumn(nullable=true)
*/
private ?Person $person = null;
/**
* @ORM\ManyToMany(targetEntity="Chill\PersonBundle\Entity\Person", inversedBy="calendars")
* @ORM\JoinTable(name="chill_calendar.calendar_to_persons")
* @Groups({"read"})
* @Serializer\Groups({"calendar:read"})
* @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"})
* @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"})
* @Assert\Count(min=1, minMessage="calendar.At least {{ limit }} person is required.")
*/
private Collection $persons;
@@ -123,69 +194,102 @@ class Calendar
private PrivateCommentEmbeddable $privateComment;
/**
* @ORM\ManyToMany(
* targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty",
* cascade={"persist", "remove", "merge", "detach"})
* @ORM\ManyToMany(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty")
* @ORM\JoinTable(name="chill_calendar.calendar_to_thirdparties")
* @Groups({"read"})
* @Serializer\Groups({"calendar:read"})
* @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"})
* @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"})
*/
private Collection $professionals;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"docgen:read"})
*/
private ?bool $sendSMS;
private ?bool $sendSMS = false;
/**
* @ORM\Column(type="datetimetz_immutable")
* @Serializer\Groups({"calendar:read"})
* @ORM\Column(type="text", nullable=false, options={"default": Calendar::SMS_PENDING})
*/
private string $smsStatus = self::SMS_PENDING;
/**
* @ORM\Column(type="datetime_immutable", nullable=false)
* @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"})
* @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"})
* @Assert\NotNull(message="calendar.A start date is required")
*/
private ?DateTimeImmutable $startDate = null;
/**
* @ORM\Column(type="string", length=255)
* @ORM\Column(type="string", length=255, nullable=false, options={"default": "valid"})
* @Serializer\Groups({"calendar:read", "read", "calendar:light"})
* @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"})
*/
private ?string $status = null;
private string $status = self::STATUS_VALID;
/**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User")
* @Groups({"read"})
* @Serializer\Groups({"calendar:read"})
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"docgen:read"})
*/
private ?User $user = null;
private ?bool $urgent = false;
public function __construct()
{
$this->comment = new CommentEmbeddable();
$this->documents = new ArrayCollection();
$this->privateComment = new PrivateCommentEmbeddable();
$this->persons = new ArrayCollection();
$this->professionals = new ArrayCollection();
$this->invites = new ArrayCollection();
}
public function addInvite(?Invite $invite): self
/**
* @internal use @{CalendarDoc::__construct} instead
*/
public function addDocument(CalendarDoc $calendarDoc): self
{
if (null !== $invite) {
$this->invites[] = $invite;
if ($this->documents->contains($calendarDoc)) {
$this->documents[] = $calendarDoc;
}
return $this;
}
public function addPerson(?Person $person): self
/**
* @internal Use {@link (Calendar::addUser)} instead
*/
public function addInvite(Invite $invite): self
{
if (null !== $person) {
$this->persons[] = $person;
if ($invite->getCalendar() instanceof Calendar && $invite->getCalendar() !== $this) {
throw new LogicException('Not allowed to move an invitation to another Calendar');
}
$this->invites[] = $invite;
$this->newInvites[] = $invite;
$invite->setCalendar($this);
return $this;
}
public function addProfessional(?ThirdParty $professional): self
public function addPerson(Person $person): self
{
if (null !== $professional) {
$this->professionals[] = $professional;
$this->persons[] = $person;
return $this;
}
public function addProfessional(ThirdParty $professional): self
{
$this->professionals[] = $professional;
return $this;
}
public function addUser(User $user): self
{
if (!$this->getUsers()->contains($user) && $this->getMainUser() !== $user) {
$this->addInvite((new Invite())->setUser($user));
}
return $this;
@@ -211,11 +315,66 @@ class Calendar
return $this->cancelReason;
}
public function getCenters(): ?iterable
{
switch ($this->getContext()) {
case 'person':
return [$this->getPerson()->getCenter()];
case 'accompanying_period':
return $this->getAccompanyingPeriod()->getCenters();
default:
throw new LogicException('context not supported: ' . $this->getContext());
}
}
public function getComment(): CommentEmbeddable
{
return $this->comment;
}
/**
* @return 'person'|'accompanying_period'|null
*/
public function getContext(): ?string
{
if ($this->getAccompanyingPeriod() !== null) {
return 'accompanying_period';
}
if ($this->getPerson() !== null) {
return 'person';
}
return null;
}
/**
* Each time the date and time is update, this version is incremented.
*/
public function getDateTimeVersion(): int
{
return $this->dateTimeVersion;
}
public function getDocuments(): Collection
{
return $this->documents;
}
/**
* @Serializer\Groups({"docgen:read"})
*/
public function getDuration(): ?DateInterval
{
if ($this->getStartDate() === null || $this->getEndDate() === null) {
return null;
}
return $this->getStartDate()->diff($this->getEndDate());
}
public function getEndDate(): ?DateTimeImmutable
{
return $this->endDate;
@@ -226,6 +385,21 @@ class Calendar
return $this->id;
}
public function getInviteForUser(User $user): ?Invite
{
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('user', $user));
$matchings = $this->invites
->matching($criteria);
if (1 === $matchings->count()) {
return $matchings->first();
}
return null;
}
/**
* @return Collection|Invite[]
*/
@@ -244,6 +418,11 @@ class Calendar
return $this->mainUser;
}
public function getPerson(): ?Person
{
return $this->person;
}
/**
* @return Collection|Person[]
*/
@@ -304,6 +483,11 @@ class Calendar
return $this->sendSMS;
}
public function getSmsStatus(): string
{
return $this->smsStatus;
}
public function getStartDate(): ?DateTimeImmutable
{
return $this->startDate;
@@ -319,14 +503,40 @@ class Calendar
return $this->getProfessionals();
}
public function getUser(): ?User
public function getUrgent(): ?bool
{
return $this->user;
return $this->urgent;
}
public function getusers(): Collection
/**
* @return ReadableCollection<(int|string), User>
* @Serializer\Groups({"calendar:read", "read"})
*/
public function getUsers(): ReadableCollection
{
return $this->getInvites(); //TODO get users of the invite
return $this->getInvites()->map(static fn (Invite $i) => $i->getUser());
}
public function hasCalendarRange(): bool
{
return null !== $this->calendarRange;
}
public function hasLocation(): bool
{
return null !== $this->getLocation();
}
/**
* return true if the user is invited.
*/
public function isInvited(User $user): bool
{
if ($this->getMainUser() === $user) {
return false;
}
return $this->getUsers()->contains($user);
}
public static function loadValidatorMetadata(ClassMetadata $metadata): void
@@ -343,9 +553,27 @@ class Calendar
]));
}
/**
* @internal use @{CalendarDoc::setCalendar} with null instead
*/
public function removeDocument(CalendarDoc $calendarDoc): self
{
if ($calendarDoc->getCalendar() !== $this) {
throw new LogicException('cannot remove document of another calendar');
}
return $this;
}
/**
* @internal Use {@link (Calendar::removeUser)} instead
*/
public function removeInvite(Invite $invite): self
{
$this->invites->removeElement($invite);
if ($this->invites->removeElement($invite)) {
$invite->setCalendar(null);
$this->oldInvites[] = $invite;
}
return $this;
}
@@ -364,6 +592,20 @@ class Calendar
return $this;
}
public function removeUser(User $user): self
{
if (!$this->getUsers()->contains($user)) {
return $this;
}
$invite = $this->invites
->filter(static fn (Invite $invite) => $invite->getUser() === $user)
->first();
$this->removeInvite($invite);
return $this;
}
public function setAccompanyingPeriod(?AccompanyingPeriod $accompanyingPeriod): self
{
$this->accompanyingPeriod = $accompanyingPeriod;
@@ -380,8 +622,20 @@ class Calendar
public function setCalendarRange(?CalendarRange $calendarRange): self
{
if ($this->calendarRange !== $calendarRange) {
$this->previousCalendarRange = $this->calendarRange;
if (null !== $this->previousCalendarRange) {
$this->previousCalendarRange->setCalendar(null);
}
}
$this->calendarRange = $calendarRange;
if ($this->calendarRange instanceof CalendarRange) {
$this->calendarRange->setCalendar($this);
}
return $this;
}
@@ -401,6 +655,10 @@ class Calendar
public function setEndDate(DateTimeImmutable $endDate): self
{
if (null === $this->endDate || $this->endDate->getTimestamp() !== $endDate->getTimestamp()) {
$this->increaseaDatetimeVersion();
}
$this->endDate = $endDate;
return $this;
@@ -415,7 +673,19 @@ class Calendar
public function setMainUser(?User $mainUser): self
{
if ($this->mainUser !== $mainUser) {
$this->previousMainUser = $this->mainUser;
}
$this->mainUser = $mainUser;
$this->removeUser($mainUser);
return $this;
}
public function setPerson(?Person $person): Calendar
{
$this->person = $person;
return $this;
}
@@ -434,8 +704,19 @@ class Calendar
return $this;
}
public function setSmsStatus(string $smsStatus): self
{
$this->smsStatus = $smsStatus;
return $this;
}
public function setStartDate(DateTimeImmutable $startDate): self
{
if (null === $this->startDate || $this->startDate->getTimestamp() !== $startDate->getTimestamp()) {
$this->increaseaDatetimeVersion();
}
$this->startDate = $startDate;
return $this;
@@ -445,13 +726,22 @@ class Calendar
{
$this->status = $status;
if (self::STATUS_CANCELED === $status && $this->getSmsStatus() === self::SMS_SENT) {
$this->setSmsStatus(self::SMS_CANCEL_PENDING);
}
return $this;
}
public function setUser(?User $user): self
public function setUrgent(bool $urgent): self
{
$this->user = $user;
$this->urgent = $urgent;
return $this;
}
private function increaseaDatetimeVersion(): void
{
++$this->dateTimeVersion;
}
}

View File

@@ -0,0 +1,153 @@
<?php
declare(strict_types=1);
/*
* 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\Entity;
use Chill\CalendarBundle\Entity\CalendarDoc\CalendarDocCreateDTO;
use Chill\CalendarBundle\Entity\CalendarDoc\CalendarDocEditDTO;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
use Chill\MainBundle\Doctrine\Model\TrackCreationTrait;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateTrait;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(
* name="chill_calendar.calendar_doc",
* )
*/
class CalendarDoc implements TrackCreationInterface, TrackUpdateInterface
{
use TrackCreationTrait;
use TrackUpdateTrait;
/**
* @ORM\ManyToOne(targetEntity=Calendar::class, inversedBy="documents")
* @ORM\JoinColumn(nullable=false)
*/
private Calendar $calendar;
/**
* @ORM\Column(type="integer", nullable=false, options={"default": 0})
*/
private int $datetimeVersion = 0;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @ORM\ManyToOne(targetEntity=StoredObject::class, cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private ?StoredObject $storedObject;
/**
* @ORM\Column(type="boolean", nullable=false, options={"default": false})
*/
private bool $trackDateTimeVersion = false;
public function __construct(Calendar $calendar, ?StoredObject $storedObject)
{
$this->setCalendar($calendar);
$this->storedObject = $storedObject;
$this->datetimeVersion = $calendar->getDateTimeVersion();
}
public function createFromDTO(CalendarDocCreateDTO $calendarDocCreateDTO): void
{
$this->storedObject = $calendarDocCreateDTO->doc;
$this->storedObject->setTitle($calendarDocCreateDTO->title);
}
public function editFromDTO(CalendarDocEditDTO $calendarDocEditDTO): void
{
if (null !== $calendarDocEditDTO->doc) {
$calendarDocEditDTO->doc->setTitle($this->getStoredObject()->getTitle());
$this->setStoredObject($calendarDocEditDTO->doc);
}
$this->getStoredObject()->setTitle($calendarDocEditDTO->title);
}
public function getCalendar(): Calendar
{
return $this->calendar;
}
public function getDatetimeVersion(): int
{
return $this->datetimeVersion;
}
public function getId(): ?int
{
return $this->id;
}
public function getStoredObject(): StoredObject
{
return $this->storedObject;
}
public function isTrackDateTimeVersion(): bool
{
return $this->trackDateTimeVersion;
}
/**
* @internal use @see{Calendar::removeDocument} instead
*
* @param Calendar $calendar
*/
public function setCalendar(?Calendar $calendar): CalendarDoc
{
if (null === $calendar) {
$this->calendar->removeDocument($this);
} else {
$calendar->addDocument($this);
}
$this->calendar = $calendar;
$this->datetimeVersion = $calendar->getDateTimeVersion();
return $this;
}
public function setDatetimeVersion(int $datetimeVersion): CalendarDoc
{
$this->datetimeVersion = $datetimeVersion;
return $this;
}
public function setStoredObject(StoredObject $storedObject): CalendarDoc
{
$this->storedObject = $storedObject;
return $this;
}
public function setTrackDateTimeVersion(bool $trackDateTimeVersion): CalendarDoc
{
$this->trackDateTimeVersion = $trackDateTimeVersion;
return $this;
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
/*
* 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\Entity\CalendarDoc;
use Chill\DocStoreBundle\Entity\StoredObject;
use Symfony\Component\Validator\Constraints as Assert;
class CalendarDocCreateDTO
{
/**
* @Assert\NotNull
* @Assert\Valid
*/
public ?StoredObject $doc = null;
/**
* @Assert\NotBlank
* @Assert\NotNull
*/
public ?string $title = '';
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
/*
* 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\Entity\CalendarDoc;
use Chill\CalendarBundle\Entity\CalendarDoc;
use Chill\DocStoreBundle\Entity\StoredObject;
use Symfony\Component\Validator\Constraints as Assert;
class CalendarDocEditDTO
{
/**
* @Assert\Valid
*/
public ?StoredObject $doc = null;
/**
* @Assert\NotBlank
* @Assert\NotNull
*/
public ?string $title = '';
public function __construct(CalendarDoc $calendarDoc)
{
$this->title = $calendarDoc->getStoredObject()->getTitle();
}
}

View File

@@ -1,39 +1,51 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Entity;
use Chill\CalendarBundle\Repository\CalendarRangeRepository;
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
use Chill\MainBundle\Doctrine\Model\TrackCreationTrait;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateTrait;
use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Table(name="chill_calendar.calendar_range")
* @ORM\Entity(repositoryClass=CalendarRangeRepository::class)
* @ORM\Table(
* name="chill_calendar.calendar_range",
* uniqueConstraints={@ORM\UniqueConstraint(name="idx_calendar_range_remote", columns={"remoteId"}, options={"where": "remoteId <> ''"})}
* )
* @ORM\Entity
*/
class CalendarRange
class CalendarRange implements TrackCreationInterface, TrackUpdateInterface
{
/**
* @ORM\OneToMany(targetEntity=Calendar::class,
* mappedBy="calendarRange")
*/
private Collection $calendars;
use RemoteCalendarTrait;
use TrackCreationTrait;
use TrackUpdateTrait;
/**
* @ORM\Column(type="datetimetz_immutable")
* @groups({"read", "write"})
* @ORM\OneToOne(targetEntity=Calendar::class, mappedBy="calendarRange")
*/
private ?Calendar $calendar = null;
/**
* @ORM\Column(type="datetime_immutable", nullable=false)
* @Groups({"read", "write", "calendar:read"})
* @Assert\NotNull
*/
private ?DateTimeImmutable $endDate = null;
@@ -41,27 +53,35 @@ class CalendarRange
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @groups({"read"})
* @Groups({"read"})
*/
private $id;
/**
* @ORM\Column(type="datetimetz_immutable")
* @groups({"read", "write"})
* @ORM\ManyToOne(targetEntity=Location::class)
* @ORM\JoinColumn(nullable=false)
* @Groups({"read", "write", "calendar:read"})
* @Assert\NotNull
*/
private ?Location $location = null;
/**
* @ORM\Column(type="datetime_immutable", nullable=false)
* @groups({"read", "write", "calendar:read"})
* @Assert\NotNull
*/
private ?DateTimeImmutable $startDate = null;
/**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User")
* @groups({"read", "write"})
* @Groups({"read", "write", "calendar:read"})
* @Assert\NotNull
*/
private ?User $user = null;
//TODO Lieu
public function __construct()
public function getCalendar(): ?Calendar
{
$this->calendars = new ArrayCollection();
return $this->calendar;
}
public function getEndDate(): ?DateTimeImmutable
@@ -74,6 +94,11 @@ class CalendarRange
return $this->id;
}
public function getLocation(): ?Location
{
return $this->location;
}
public function getStartDate(): ?DateTimeImmutable
{
return $this->startDate;
@@ -84,6 +109,14 @@ class CalendarRange
return $this->user;
}
/**
* @internal use {@link (Calendar::setCalendarRange)} instead
*/
public function setCalendar(?Calendar $calendar): void
{
$this->calendar = $calendar;
}
public function setEndDate(DateTimeImmutable $endDate): self
{
$this->endDate = $endDate;
@@ -91,6 +124,13 @@ class CalendarRange
return $this;
}
public function setLocation(?Location $location): self
{
$this->location = $location;
return $this;
}
public function setStartDate(DateTimeImmutable $startDate): self
{
$this->startDate = $startDate;

View File

@@ -1,14 +1,14 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Entity;
use Chill\CalendarBundle\Repository\CancelReasonRepository;

View File

@@ -1,49 +1,100 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Entity;
use Chill\CalendarBundle\Repository\InviteRepository;
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
use Chill\MainBundle\Doctrine\Model\TrackCreationTrait;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateTrait;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use LogicException;
use Symfony\Component\Serializer\Annotation as Serializer;
/**
* @ORM\Table(name="chill_calendar.invite")
* @ORM\Entity(repositoryClass=InviteRepository::class)
* An invitation for another user to a Calendar.
*
* The event/calendar in the user may have a different id than the mainUser. We add then fields to store the
* remote id of this event in the remote calendar.
*
* @ORM\Table(
* name="chill_calendar.invite",
* uniqueConstraints={@ORM\UniqueConstraint(name="idx_calendar_invite_remote", columns={"remoteId"}, options={"where": "remoteId <> ''"})}
* )
* @ORM\Entity
*/
class Invite
class Invite implements TrackUpdateInterface, TrackCreationInterface
{
use RemoteCalendarTrait;
use TrackCreationTrait;
use TrackUpdateTrait;
public const ACCEPTED = 'accepted';
public const DECLINED = 'declined';
public const PENDING = 'pending';
/**
* all statuses in one const.
*/
public const STATUSES = [
self::ACCEPTED,
self::DECLINED,
self::PENDING,
self::TENTATIVELY_ACCEPTED,
];
public const TENTATIVELY_ACCEPTED = 'tentative';
/**
* @ORM\ManyToOne(targetEntity=Calendar::class, inversedBy="invites")
*/
private ?Calendar $calendar = null;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Serializer\Groups(groups={"calendar:read", "read"})
*/
private $id;
private ?int $id = null;
/**
* @ORM\Column(type="json")
* @ORM\Column(type="text", nullable=false, options={"default": "pending"})
* @Serializer\Groups(groups={"calendar:read", "read", "docgen:read"})
*/
private array $status = [];
private string $status = self::PENDING;
/**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User")
* @ORM\JoinColumn(nullable=false)
* @Serializer\Groups(groups={"calendar:read", "read", "docgen:read"})
*/
private User $user;
private ?User $user = null;
public function getCalendar(): ?Calendar
{
return $this->calendar;
}
public function getId(): ?int
{
return $this->id;
}
public function getStatus(): ?array
public function getStatus(): string
{
return $this->status;
}
@@ -53,7 +104,15 @@ class Invite
return $this->user;
}
public function setStatus(array $status): self
/**
* @internal use Calendar::addInvite instead
*/
public function setCalendar(?Calendar $calendar): void
{
$this->calendar = $calendar;
}
public function setStatus(string $status): self
{
$this->status = $status;
@@ -62,6 +121,10 @@ class Invite
public function setUser(?User $user): self
{
if ($user instanceof User && $this->user instanceof User && $user !== $this->user) {
throw new LogicException('Not allowed to associate an invite to a different user');
}
$this->user = $user;
return $this;

View File

@@ -0,0 +1,71 @@
<?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);
/*
* 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\Entity;
use Doctrine\ORM\Mapping as ORM;
trait RemoteCalendarTrait
{
/**
* If true, the changes won't be enqueued to remote.
*
* This is required to prevent update loop: a persist trigger an event creation on remote,
* which in turn change remoteId and, in turn, trigger an update change, ...
*/
public bool $preventEnqueueChanges = false;
/**
* @ORM\Column(type="json", options={"default": "[]"}, nullable=false)
*/
private array $remoteAttributes = [];
/**
* @ORM\Column(type="text", options={"default": ""}, nullable=false)
*/
private string $remoteId = '';
public function addRemoteAttributes(array $remoteAttributes): self
{
$this->remoteAttributes = array_merge($this->remoteAttributes, $remoteAttributes);
return $this;
}
public function getRemoteAttributes(): array
{
return $this->remoteAttributes;
}
public function getRemoteId(): string
{
return $this->remoteId;
}
public function hasRemoteId(): bool
{
return '' !== $this->remoteId;
}
public function setRemoteId(string $remoteId): self
{
$this->remoteId = $remoteId;
return $this;
}
}

View File

@@ -1,14 +1,14 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Event;
use Chill\ActivityBundle\Entity\Activity;

View File

@@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Templating\Entity\UserRender;
use Closure;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
final class AgentAggregator implements AggregatorInterface
{
private UserRender $userRender;
private UserRepository $userRepository;
public function __construct(
UserRepository $userRepository,
UserRender $userRender
) {
$this->userRepository = $userRepository;
$this->userRender = $userRender;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('caluser', $qb->getAllAliases(), true)) {
$qb->join('cal.mainUser', 'caluser');
}
$qb->addSelect('caluser.id AS agent_aggregator');
$qb->addGroupBy('agent_aggregator');
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
// no form
}
public function getLabels($key, array $values, $data): Closure
{
return function ($value): string {
if ('_header' === $value) {
return 'Agent';
}
if (null === $value || '' === $value) {
return '';
}
$r = $this->userRepository->find($value);
return $this->userRender->renderString($r, []);
};
}
public function getQueryKeys($data): array
{
return ['agent_aggregator'];
}
public function getTitle(): string
{
return 'Group calendars by agent';
}
}

View File

@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations;
use Chill\CalendarBundle\Repository\CancelReasonRepository;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Closure;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
class CancelReasonAggregator implements AggregatorInterface
{
private CancelReasonRepository $cancelReasonRepository;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
CancelReasonRepository $cancelReasonRepository,
TranslatableStringHelper $translatableStringHelper
) {
$this->cancelReasonRepository = $cancelReasonRepository;
$this->translatableStringHelper = $translatableStringHelper;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
// TODO: still needs to take into account calendars without a cancel reason somehow
if (!in_array('calcancel', $qb->getAllAliases(), true)) {
$qb->join('cal.cancelReason', 'calcancel');
}
$qb->addSelect('IDENTITY(cal.cancelReason) as cancel_reason_aggregator');
$qb->addGroupBy('cancel_reason_aggregator');
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
// no form
}
public function getLabels($key, array $values, $data): Closure
{
return function ($value): string {
if ('_header' === $value) {
return 'Cancel reason';
}
if (null === $value || '' === $value) {
return '';
}
$j = $this->cancelReasonRepository->find($value);
return $this->translatableStringHelper->localize(
$j->getName()
);
};
}
public function getQueryKeys($data): array
{
return ['cancel_reason_aggregator'];
}
public function getTitle(): string
{
return 'Group calendars by cancel reason';
}
}

View File

@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Repository\UserJobRepository;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Closure;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
final class JobAggregator implements AggregatorInterface
{
private UserJobRepository $jobRepository;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
UserJobRepository $jobRepository,
TranslatableStringHelper $translatableStringHelper
) {
$this->jobRepository = $jobRepository;
$this->translatableStringHelper = $translatableStringHelper;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('caluser', $qb->getAllAliases(), true)) {
$qb->join('cal.mainUser', 'caluser');
}
$qb->addSelect('IDENTITY(caluser.userJob) as job_aggregator');
$qb->addGroupBy('job_aggregator');
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
// no form
}
public function getLabels($key, array $values, $data): Closure
{
return function ($value): string {
if ('_header' === $value) {
return 'Job';
}
if (null === $value || '' === $value) {
return '';
}
$j = $this->jobRepository->find($value);
return $this->translatableStringHelper->localize(
$j->getLabel()
);
};
}
public function getQueryKeys($data): array
{
return ['job_aggregator'];
}
public function getTitle(): string
{
return 'Group calendars by agent job';
}
}

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Repository\LocationRepository;
use Closure;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
final class LocationAggregator implements AggregatorInterface
{
private LocationRepository $locationRepository;
public function __construct(
LocationRepository $locationRepository
) {
$this->locationRepository = $locationRepository;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('calloc', $qb->getAllAliases(), true)) {
$qb->join('cal.location', 'calloc');
}
$qb->addSelect('IDENTITY(cal.location) as location_aggregator');
$qb->addGroupBy('location_aggregator');
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
// no form
}
public function getLabels($key, array $values, $data): Closure
{
return function ($value): string {
if ('_header' === $value) {
return 'Location';
}
if (null === $value || '' === $value) {
return '';
}
$l = $this->locationRepository->find($value);
return $l->getName();
};
}
public function getQueryKeys($data): array
{
return ['location_aggregator'];
}
public function getTitle(): string
{
return 'Group calendars by location';
}
}

View File

@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Repository\LocationTypeRepository;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Closure;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
final class LocationTypeAggregator implements AggregatorInterface
{
private LocationTypeRepository $locationTypeRepository;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
LocationTypeRepository $locationTypeRepository,
TranslatableStringHelper $translatableStringHelper
) {
$this->locationTypeRepository = $locationTypeRepository;
$this->translatableStringHelper = $translatableStringHelper;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('calloc', $qb->getAllAliases(), true)) {
$qb->join('cal.location', 'calloc');
}
$qb->addSelect('IDENTITY(calloc.locationType) as location_type_aggregator');
$qb->addGroupBy('location_type_aggregator');
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
// no form
}
public function getLabels($key, array $values, $data): Closure
{
return function ($value): string {
if ('_header' === $value) {
return 'Location type';
}
if (null === $value || '' === $value) {
return '';
}
if (null === $j = $this->locationTypeRepository->find($value)) {
return '';
}
return $this->translatableStringHelper->localize(
$j->getTitle()
);
};
}
public function getQueryKeys($data): array
{
return ['location_type_aggregator'];
}
public function getTitle(): string
{
return 'Group calendars by location type';
}
}

View File

@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Export\AggregatorInterface;
use Closure;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
class MonthYearAggregator implements AggregatorInterface
{
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$qb->addSelect("to_char(cal.startDate, 'MM-YYYY') AS month_year_aggregator");
// $qb->addSelect("extract(month from age(cal.startDate, cal.endDate)) AS month_aggregator");
$qb->addGroupBy('month_year_aggregator');
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
// No form needed
}
public function getLabels($key, array $values, $data): Closure
{
return static function ($value): string {
if ('_header' === $value) {
return 'by month and year';
}
return $value;
};
}
public function getQueryKeys($data): array
{
return ['month_year_aggregator'];
}
public function getTitle(): string
{
return 'Group calendars by month and year';
}
}

View File

@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Repository\ScopeRepository;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Closure;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
final class ScopeAggregator implements AggregatorInterface
{
private ScopeRepository $scopeRepository;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
ScopeRepository $scopeRepository,
TranslatableStringHelper $translatableStringHelper
) {
$this->scopeRepository = $scopeRepository;
$this->translatableStringHelper = $translatableStringHelper;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('caluser', $qb->getAllAliases(), true)) {
$qb->join('cal.mainUser', 'caluser');
}
$qb->addSelect('IDENTITY(caluser.mainScope) as scope_aggregator');
$qb->addGroupBy('scope_aggregator');
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
// no form
}
public function getLabels($key, array $values, $data): Closure
{
return function ($value): string {
if ('_header' === $value) {
return 'Scope';
}
if (null === $value || '' === $value) {
return '';
}
$s = $this->scopeRepository->find($value);
return $this->translatableStringHelper->localize(
$s->getName()
);
};
}
public function getQueryKeys($data): array
{
return ['scope_aggregator'];
}
public function getTitle(): string
{
return 'Group calendars by agent scope';
}
}

View File

@@ -0,0 +1,89 @@
<?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);
/*
* 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\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Export\AggregatorInterface;
use Closure;
use Doctrine\ORM\QueryBuilder;
use LogicException;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class UrgencyAggregator implements AggregatorInterface
{
private TranslatorInterface $translator;
public function __construct(
TranslatorInterface $translator
) {
$this->translator = $translator;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$qb->addSelect('cal.urgent AS urgency_aggregator');
$qb->addGroupBy('urgency_aggregator');
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
// no form
}
public function getLabels($key, array $values, $data): Closure
{
return function ($value): string {
if ('_header' === $value) {
return 'Urgency';
}
switch ($value) {
case true:
return $this->translator->trans('is urgent');
case false:
return $this->translator->trans('is not urgent');
default:
throw new LogicException(sprintf('The value %s is not valid', $value));
}
};
}
public function getQueryKeys($data): array
{
return ['urgency_aggregator'];
}
public function getTitle(): string
{
return 'Group calendars by urgency';
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
/*
* 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\Export;
/**
* This class declare constants used for the export framework.
*/
abstract class Declarations
{
public const CALENDAR_TYPE = 'calendar';
}

View File

@@ -0,0 +1,113 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Export;
use Chill\CalendarBundle\Export\Declarations;
use Chill\CalendarBundle\Repository\CalendarRepository;
use Chill\MainBundle\Export\ExportInterface;
use Chill\MainBundle\Export\FormatterInterface;
use Chill\MainBundle\Export\GroupedExportInterface;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Closure;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Exception\LogicException;
class CountCalendars implements ExportInterface, GroupedExportInterface
{
private CalendarRepository $calendarRepository;
public function __construct(CalendarRepository $calendarRepository)
{
$this->calendarRepository = $calendarRepository;
}
public function buildForm(FormBuilderInterface $builder)
{
// No form necessary
}
public function getAllowedFormattersTypes(): array
{
return [FormatterInterface::TYPE_TABULAR];
}
public function getDescription(): string
{
return 'Count calendars by various parameters.';
}
public function getGroup(): string
{
return 'Exports of calendar';
}
public function getLabels($key, array $values, $data)
{
if ('export_result' !== $key) {
throw new LogicException("the key {$key} is not used by this export");
}
$labels = array_combine($values, $values);
$labels['_header'] = $this->getTitle();
return static fn ($value) => $labels[$value];
}
public function getQueryKeys($data): array
{
return ['export_result'];
}
public function getResult($query, $data)
{
return $query->getQuery()->getResult(AbstractQuery::HYDRATE_SCALAR);
}
public function getTitle(): string
{
return 'Count calendars';
}
public function getType(): string
{
return Declarations::CALENDAR_TYPE;
}
/**
* Initiate the query.
*/
public function initiateQuery(array $requiredModifiers, array $acl, array $data = []): QueryBuilder
{
$centers = array_map(static fn ($el) => $el['center'], $acl);
$qb = $this->calendarRepository->createQueryBuilder('cal');
$qb->select('COUNT(cal.id) AS export_result');
return $qb;
}
public function requiredRole(): string
{
// which role should we give here?
return PersonVoter::STATS;
}
public function supportsModifiers(): array
{
return [
Declarations::CALENDAR_TYPE,
];
}
}

View File

@@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Export;
use Chill\CalendarBundle\Export\Declarations;
use Chill\CalendarBundle\Repository\CalendarRepository;
use Chill\MainBundle\Export\ExportInterface;
use Chill\MainBundle\Export\FormatterInterface;
use Chill\MainBundle\Export\GroupedExportInterface;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use LogicException;
use Symfony\Component\Form\FormBuilderInterface;
class StatCalendarAvgDuration implements ExportInterface, GroupedExportInterface
{
private CalendarRepository $calendarRepository;
public function __construct(
CalendarRepository $calendarRepository
) {
$this->calendarRepository = $calendarRepository;
}
public function buildForm(FormBuilderInterface $builder): void
{
// no form needed
}
public function getAllowedFormattersTypes(): array
{
return [FormatterInterface::TYPE_TABULAR];
}
public function getDescription(): string
{
return 'Get the average of calendar duration according to various filters';
}
public function getGroup(): string
{
return 'Exports of calendar';
}
public function getLabels($key, array $values, $data)
{
if ('export_result' !== $key) {
throw new LogicException("the key {$key} is not used by this export");
}
$labels = array_combine($values, $values);
$labels['_header'] = $this->getTitle();
return static fn ($value) => $labels[$value];
}
public function getQueryKeys($data): array
{
return ['export_result'];
}
public function getResult($query, $data)
{
return $query->getQuery()->getResult(Query::HYDRATE_SCALAR);
}
public function getTitle(): string
{
return 'Average calendar duration';
}
public function getType(): string
{
return Declarations::CALENDAR_TYPE;
}
public function initiateQuery(array $requiredModifiers, array $acl, array $data = []): QueryBuilder
{
$qb = $this->calendarRepository->createQueryBuilder('cal');
$qb
->select('AVG(cal.endDate - cal.startDate) AS export_result');
return $qb;
}
public function requiredRole(): string
{
return AccompanyingPeriodVoter::STATS;
}
public function supportsModifiers(): array
{
return [
Declarations::CALENDAR_TYPE,
];
}
}

View File

@@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Export;
use Chill\CalendarBundle\Export\Declarations;
use Chill\CalendarBundle\Repository\CalendarRepository;
use Chill\MainBundle\Export\ExportInterface;
use Chill\MainBundle\Export\FormatterInterface;
use Chill\MainBundle\Export\GroupedExportInterface;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use LogicException;
use Symfony\Component\Form\FormBuilderInterface;
class StatCalendarSumDuration implements ExportInterface, GroupedExportInterface
{
private CalendarRepository $calendarRepository;
public function __construct(
CalendarRepository $calendarRepository
) {
$this->calendarRepository = $calendarRepository;
}
public function buildForm(FormBuilderInterface $builder): void
{
// no form needed
}
public function getAllowedFormattersTypes(): array
{
return [FormatterInterface::TYPE_TABULAR];
}
public function getDescription(): string
{
return 'Get the sum of calendar durations according to various filters';
}
public function getGroup(): string
{
return 'Exports of calendar';
}
public function getLabels($key, array $values, $data)
{
if ('export_result' !== $key) {
throw new LogicException("the key {$key} is not used by this export");
}
$labels = array_combine($values, $values);
$labels['_header'] = $this->getTitle();
return static fn ($value) => $labels[$value];
}
public function getQueryKeys($data): array
{
return ['export_result'];
}
public function getResult($query, $data)
{
return $query->getQuery()->getResult(Query::HYDRATE_SCALAR);
}
public function getTitle(): string
{
return 'Sum of calendar durations';
}
public function getType(): string
{
return Declarations::CALENDAR_TYPE;
}
public function initiateQuery(array $requiredModifiers, array $acl, array $data = []): QueryBuilder
{
$qb = $this->calendarRepository->createQueryBuilder('cal');
$qb
->select('SUM(cal.endDate - cal.startDate) AS export_result');
return $qb;
}
public function requiredRole(): string
{
return AccompanyingPeriodVoter::STATS;
}
public function supportsModifiers(): array
{
return [
Declarations::CALENDAR_TYPE,
];
}
}

View File

@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Filter;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\Entity\UserRender;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
class AgentFilter implements FilterInterface
{
private UserRender $userRender;
public function __construct(UserRender $userRender)
{
$this->userRender = $userRender;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('cal.mainUser', ':agents');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('agents', $data['accepted_agents']);
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('accepted_agents', EntityType::class, [
'class' => User::class,
'choice_label' => fn (User $u) => $this->userRender->renderString($u, []),
'multiple' => true,
'expanded' => true,
]);
}
public function describeAction($data, $format = 'string'): array
{
$users = [];
foreach ($data['accepted_agents'] as $r) {
$users[] = $r;
}
return [
'Filtered by agent: only %agents%', [
'%agents' => implode(', ', $users),
], ];
}
public function getTitle(): string
{
return 'Filter calendars by agent';
}
}

View File

@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Filter;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
class BetweenDatesFilter implements FilterInterface
{
private RollingDateConverterInterface $rollingDateConverter;
public function __construct(RollingDateConverterInterface $rollingDateConverter)
{
$this->rollingDateConverter = $rollingDateConverter;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$clause = $qb->expr()->andX(
$qb->expr()->gte('cal.startDate', ':dateFrom'),
$qb->expr()->lte('cal.endDate', ':dateTo')
);
$qb->andWhere($clause);
$qb->setParameter(
'dateFrom',
$this->rollingDateConverter->convert($data['date_from'])
);
// modify dateTo so that entire day is also taken into account up until the beginning of the next day.
$qb->setParameter(
'dateTo',
$this->rollingDateConverter->convert($data['date_to'])->modify('+1 day')
);
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
$builder
->add('date_from', PickRollingDateType::class, [
'data' => new RollingDate(RollingDate::T_YEAR_PREVIOUS_START),
])
->add('date_to', PickRollingDateType::class, [
'data' => new RollingDate(RollingDate::T_TODAY),
]);
}
public function describeAction($data, $format = 'string'): array
{
return ['Filtered by calendars between %dateFrom% and %dateTo%', [
'%dateFrom%' => $this->rollingDateConverter->convert($data['date_from'])->format('d-m-Y'),
'%dateTo%' => $this->rollingDateConverter->convert($data['date_to'])->format('d-m-Y'),
]];
}
public function getTitle(): string
{
return 'Filter calendars between certain dates';
}
}

View File

@@ -0,0 +1,99 @@
<?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);
/*
* 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\Export\Filter;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Export\FilterInterface;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class CalendarRangeFilter implements FilterInterface
{
private const CHOICES = [
'Not made within a calendar range' => true,
'Made within a calendar range' => false,
];
private const DEFAULT_CHOICE = false;
private TranslatorInterface $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
if (null !== $data['hasCalendarRange']) {
$qb->andWhere($qb->expr()->isNotNull('cal.calendarRange'));
} else {
$qb->andWhere($qb->expr()->isNull('cal.calendarRange'));
}
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('hasCalendarRange', ChoiceType::class, [
'choices' => self::CHOICES,
'label' => 'has calendar range',
'multiple' => false,
'expanded' => true,
'empty_data' => self::DEFAULT_CHOICE,
'data' => self::DEFAULT_CHOICE,
]);
}
public function describeAction($data, $format = 'string'): array
{
$choice = '';
foreach (self::CHOICES as $k => $v) {
if ($v === $data['hasCalendarRange']) {
$choice = $k;
} else {
$choice = 'Not made within a calendar range';
}
}
return [
'Filtered by calendar range: only %calendarRange%', [
'%calendarRange%' => $this->translator->trans($choice),
],
];
}
public function getTitle(): string
{
return 'Filter by calendar range';
}
}

View File

@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Filter;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Entity\UserJob;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function in_array;
class JobFilter implements FilterInterface
{
protected TranslatorInterface $translator;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper
) {
$this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('caluser', $qb->getAllAliases(), true)) {
$qb->join('cal.mainUser', 'caluser');
}
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('caluser.userJob', ':job');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('job', $data['job']);
}
public function applyOn(): string
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('job', EntityType::class, [
'class' => UserJob::class,
'choice_label' => fn (UserJob $j) => $this->translatableStringHelper->localize(
$j->getLabel()
),
'multiple' => true,
'expanded' => true,
]);
}
public function describeAction($data, $format = 'string'): array
{
$userJobs = [];
foreach ($data['job'] as $j) {
$userJobs[] = $this->translatableStringHelper->localize(
$j->getLabel()
);
}
return ['Filtered by agent job: only %jobs%', [
'%jobs%' => implode(', ', $userJobs),
]];
}
public function getTitle(): string
{
return 'Filter calendars by agent job';
}
}

View File

@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
/*
* 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\Export\Filter;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function in_array;
class ScopeFilter implements FilterInterface
{
protected TranslatorInterface $translator;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper
) {
$this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('caluser', $qb->getAllAliases(), true)) {
$qb->join('cal.mainUser', 'caluser');
}
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('caluser.mainScope', ':scope');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('scope', $data['scope']);
}
public function applyOn()
{
return Declarations::CALENDAR_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('scope', EntityType::class, [
'class' => Scope::class,
'choice_label' => fn (Scope $s) => $this->translatableStringHelper->localize(
$s->getName()
),
'multiple' => true,
'expanded' => true,
]);
}
public function describeAction($data, $format = 'string')
{
$scopes = [];
foreach ($data['scope'] as $s) {
$scopes[] = $this->translatableStringHelper->localize(
$s->getName()
);
}
return ['Filtered by agent scope: only %scopes%', [
'%scopes%' => implode(', ', $scopes),
]];
}
public function getTitle()
{
return 'Filter calendars by agent scope';
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/*
* 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\Form;
use Chill\CalendarBundle\Entity\CalendarDoc\CalendarDocCreateDTO;
use Chill\DocStoreBundle\Form\StoredObjectType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CalendarDocCreateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'label' => 'chill_calendar.Document title',
'required' => true,
])
->add('doc', StoredObjectType::class, [
'label' => 'chill_calendar.Document object',
'required' => true,
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => CalendarDocCreateDTO::class,
]);
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
/*
* 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\Form;
use Chill\CalendarBundle\Entity\CalendarDoc\CalendarDocEditDTO;
use Chill\DocStoreBundle\Form\StoredObjectType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CalendarDocEditType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'label' => 'chill_calendar.Document title',
'required' => true,
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => CalendarDocEditDTO::class,
]);
}
}

View File

@@ -1,29 +1,27 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Form;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\CalendarBundle\Entity\CancelReason;
use Chill\CalendarBundle\Entity\Invite;
use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\User;
use Chill\CalendarBundle\Form\DataTransformer\IdToCalendarRangeDataTransformer;
use Chill\MainBundle\Form\DataTransformer\IdToLocationDataTransformer;
use Chill\MainBundle\Form\DataTransformer\IdToUserDataTransformer;
use Chill\MainBundle\Form\DataTransformer\IdToUsersDataTransformer;
use Chill\MainBundle\Form\Type\CommentType;
use Chill\MainBundle\Form\Type\PrivateCommentType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\PersonBundle\Form\DataTransformer\PersonsToIdDataTransformer;
use Chill\ThirdPartyBundle\Form\DataTransformer\ThirdPartiesToIdDataTransformer;
use DateTimeImmutable;
use Doctrine\Persistence\ObjectManager;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
@@ -34,16 +32,32 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class CalendarType extends AbstractType
{
protected ObjectManager $om;
private IdToCalendarRangeDataTransformer $calendarRangeDataTransformer;
protected TranslatableStringHelper $translatableStringHelper;
private IdToLocationDataTransformer $idToLocationDataTransformer;
private IdToUserDataTransformer $idToUserDataTransformer;
private IdToUsersDataTransformer $idToUsersDataTransformer;
private ThirdPartiesToIdDataTransformer $partiesToIdDataTransformer;
private PersonsToIdDataTransformer $personsToIdDataTransformer;
public function __construct(
TranslatableStringHelper $translatableStringHelper,
ObjectManager $om
PersonsToIdDataTransformer $personsToIdDataTransformer,
IdToUserDataTransformer $idToUserDataTransformer,
IdToUsersDataTransformer $idToUsersDataTransformer,
IdToLocationDataTransformer $idToLocationDataTransformer,
ThirdPartiesToIdDataTransformer $partiesToIdDataTransformer,
IdToCalendarRangeDataTransformer $idToCalendarRangeDataTransformer
) {
$this->translatableStringHelper = $translatableStringHelper;
$this->om = $om;
$this->personsToIdDataTransformer = $personsToIdDataTransformer;
$this->idToUserDataTransformer = $idToUserDataTransformer;
$this->idToUsersDataTransformer = $idToUsersDataTransformer;
$this->idToLocationDataTransformer = $idToLocationDataTransformer;
$this->partiesToIdDataTransformer = $partiesToIdDataTransformer;
$this->calendarRangeDataTransformer = $idToCalendarRangeDataTransformer;
}
public function buildForm(FormBuilderInterface $builder, array $options)
@@ -64,7 +78,6 @@ class CalendarType extends AbstractType
// },
// ])
->add('sendSMS', ChoiceType::class, [
'required' => false,
'choices' => [
'Oui' => true,
'Non' => false,
@@ -73,36 +86,26 @@ class CalendarType extends AbstractType
]);
$builder->add('mainUser', HiddenType::class);
$builder->get('mainUser')
->addModelTransformer(new CallbackTransformer(
static function (?User $user): int {
if (null !== $user) {
$res = $user->getId();
} else {
$res = -1; //TODO cannot be null in any ways...
}
return $res;
},
function (?int $userId): User {
return $this->om->getRepository(user::class)->findOneBy(['id' => (int) $userId]);
}
));
$builder->get('mainUser')->addModelTransformer($this->idToUserDataTransformer);
$builder->add('startDate', HiddenType::class);
$builder->get('startDate')
->addModelTransformer(new CallbackTransformer(
static function (?DateTimeImmutable $dateTimeImmutable): string {
if (null !== $dateTimeImmutable) {
$res = date_format($dateTimeImmutable, 'Y-m-d H:i:s');
$res = date_format($dateTimeImmutable, DateTimeImmutable::ATOM);
} else {
$res = '';
}
return $res;
},
static function (?string $dateAsString): DateTimeImmutable {
return new DateTimeImmutable($dateAsString);
static function (?string $dateAsString): ?DateTimeImmutable {
if ('' === $dateAsString || null === $dateAsString) {
return null;
}
return DateTimeImmutable::createFromFormat(DateTimeImmutable::ATOM, $dateAsString);
}
));
@@ -111,115 +114,41 @@ class CalendarType extends AbstractType
->addModelTransformer(new CallbackTransformer(
static function (?DateTimeImmutable $dateTimeImmutable): string {
if (null !== $dateTimeImmutable) {
$res = date_format($dateTimeImmutable, 'Y-m-d H:i:s');
$res = date_format($dateTimeImmutable, DateTimeImmutable::ATOM);
} else {
$res = '';
}
return $res;
},
static function (?string $dateAsString): DateTimeImmutable {
return new DateTimeImmutable($dateAsString);
static function (?string $dateAsString): ?DateTimeImmutable {
if ('' === $dateAsString || null === $dateAsString) {
return null;
}
return DateTimeImmutable::createFromFormat(DateTimeImmutable::ATOM, $dateAsString);
}
));
$builder->add('persons', HiddenType::class);
$builder->get('persons')
->addModelTransformer(new CallbackTransformer(
static function (iterable $personsAsIterable): string {
$personIds = [];
foreach ($personsAsIterable as $value) {
$personIds[] = $value->getId();
}
return implode(',', $personIds);
},
function (?string $personsAsString): array {
return array_map(
fn (string $id): ?Person => $this->om->getRepository(Person::class)->findOneBy(['id' => (int) $id]),
explode(',', $personsAsString)
);
}
));
->addModelTransformer($this->personsToIdDataTransformer);
$builder->add('professionals', HiddenType::class);
$builder->get('professionals')
->addModelTransformer(new CallbackTransformer(
static function (iterable $thirdpartyAsIterable): string {
$thirdpartyIds = [];
->addModelTransformer($this->partiesToIdDataTransformer);
foreach ($thirdpartyAsIterable as $value) {
$thirdpartyIds[] = $value->getId();
}
return implode(',', $thirdpartyIds);
},
function (?string $thirdpartyAsString): array {
return array_map(
fn (string $id): ?ThirdParty => $this->om->getRepository(ThirdParty::class)->findOneBy(['id' => (int) $id]),
explode(',', $thirdpartyAsString)
);
}
));
$builder->add('users', HiddenType::class);
$builder->get('users')
->addModelTransformer($this->idToUsersDataTransformer);
$builder->add('calendarRange', HiddenType::class);
$builder->get('calendarRange')
->addModelTransformer(new CallbackTransformer(
static function (?CalendarRange $calendarRange): ?int {
if (null !== $calendarRange) {
$res = $calendarRange->getId();
} else {
$res = -1;
}
return $res;
},
function (?string $calendarRangeId): ?CalendarRange {
if (null !== $calendarRangeId) {
$res = $this->om->getRepository(CalendarRange::class)->findOneBy(['id' => (int) $calendarRangeId]);
} else {
$res = null;
}
return $res;
}
));
->addModelTransformer($this->calendarRangeDataTransformer);
$builder->add('location', HiddenType::class)
->get('location')
->addModelTransformer(new CallbackTransformer(
static function (?Location $location): string {
if (null === $location) {
return '';
}
return $location->getId();
},
function (?string $id): ?Location {
return $this->om->getRepository(Location::class)->findOneBy(['id' => (int) $id]);
}
));
$builder->add('invites', HiddenType::class);
$builder->get('invites')
->addModelTransformer(new CallbackTransformer(
static function (iterable $usersAsIterable): string {
$userIds = [];
foreach ($usersAsIterable as $value) {
$userIds[] = $value->getId();
}
return implode(',', $userIds);
},
function (?string $usersAsString): array {
return array_map(
fn (string $id): ?Invite => $this->om->getRepository(Invite::class)->findOneBy(['id' => (int) $id]),
explode(',', $usersAsString)
);
}
));
->addModelTransformer($this->idToLocationDataTransformer);
}
public function configureOptions(OptionsResolver $resolver)
@@ -227,14 +156,11 @@ class CalendarType extends AbstractType
$resolver->setDefaults([
'data_class' => Calendar::class,
]);
$resolver
->setRequired(['accompanyingPeriod'])
->setAllowedTypes('accompanyingPeriod', [\Chill\PersonBundle\Entity\AccompanyingPeriod::class, 'null']);
}
public function getBlockPrefix()
{
return 'chill_calendarbundle_calendar';
// as the js share some hardcoded items from activity, we have to rewrite block prefix
return 'chill_activitybundle_activity';
}
}

View File

@@ -1,14 +1,14 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Form;
use Chill\CalendarBundle\Entity\CancelReason;

View File

@@ -0,0 +1,30 @@
<?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);
/*
* 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\Form\DataTransformer;
use Chill\CalendarBundle\Repository\CalendarRangeRepository;
use Chill\MainBundle\Form\DataTransformer\IdToEntityDataTransformer;
class IdToCalendarRangeDataTransformer extends IdToEntityDataTransformer
{
public function __construct(CalendarRangeRepository $repository)
{
parent::__construct($repository, false);
}
}

View File

@@ -1,54 +1,47 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Menu;
use Chill\CalendarBundle\Security\Voter\CalendarVoter;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Knp\Menu\MenuItem;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class AccompanyingCourseMenuBuilder implements LocalMenuBuilderInterface
{
protected AuthorizationHelper $authorizationHelper;
protected TokenStorageInterface $tokenStorage;
protected TranslatorInterface $translator;
private Security $security;
public function __construct(
TokenStorageInterface $tokenStorage,
AuthorizationHelper $authorizationHelper,
Security $security,
TranslatorInterface $translator
) {
$this->security = $security;
$this->translator = $translator;
$this->authorizationHelper = $authorizationHelper;
$this->tokenStorage = $tokenStorage;
}
public function buildMenu($menuId, MenuItem $menu, array $parameters)
{
$period = $parameters['accompanyingCourse'];
if (AccompanyingPeriod::STEP_DRAFT !== $period->getStep()) {
/*
if ($this->security->isGranted(CalendarVoter::SEE, $period)) {
$menu->addChild($this->translator->trans('Calendar'), [
'route' => 'chill_calendar_calendar_list',
'route' => 'chill_calendar_calendar_list_by_period',
'routeParameters' => [
'accompanying_period_id' => $period->getId(),
'id' => $period->getId(),
], ])
->setExtras(['order' => 35]);
*/
}
}

View File

@@ -1,14 +1,14 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Menu;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
@@ -39,7 +39,6 @@ class AdminMenuBuilder implements LocalMenuBuilderInterface
->setAttribute('class', 'list-group-item-header')
->setExtras([
'order' => 6000,
'icons' => ['calendar'],
]);
$menu->addChild('Cancel reason', [

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/*
* 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\Menu;
use Chill\CalendarBundle\Security\Voter\CalendarVoter;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
use Knp\Menu\MenuItem;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class PersonMenuBuilder implements LocalMenuBuilderInterface
{
protected TranslatorInterface $translator;
private Security $security;
public function __construct(
Security $security,
TranslatorInterface $translator
) {
$this->security = $security;
$this->translator = $translator;
}
public function buildMenu($menuId, MenuItem $menu, array $parameters)
{
$person = $parameters['person'];
if ($this->security->isGranted(CalendarVoter::SEE, $person)) {
$menu->addChild($this->translator->trans('Calendar'), [
'route' => 'chill_calendar_calendar_list_by_person',
'routeParameters' => [
'id' => $person->getId(),
], ])
->setExtras(['order' => 198]);
}
}
public static function getMenuIds(): array
{
return ['person'];
}
}

View File

@@ -1,64 +1,40 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Menu;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
use Chill\TaskBundle\Templating\UI\CountNotificationTask;
use Knp\Menu\MenuItem;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class UserMenuBuilder implements LocalMenuBuilderInterface
{
/**
* @var AuthorizationCheckerInterface
*/
public $authorizationChecker;
public TranslatorInterface $translator;
/**
* @var CountNotificationTask
*/
public $counter;
/**
* @var TokenStorageInterface
*/
public $tokenStorage;
/**
* @var TranslatorInterface
*/
public $translator;
private Security $security;
public function __construct(
CountNotificationTask $counter,
TokenStorageInterface $tokenStorage,
TranslatorInterface $translator,
AuthorizationCheckerInterface $authorizationChecker
Security $security,
TranslatorInterface $translator
) {
$this->counter = $counter;
$this->tokenStorage = $tokenStorage;
$this->security = $security;
$this->translator = $translator;
$this->authorizationChecker = $authorizationChecker;
}
public function buildMenu($menuId, MenuItem $menu, array $parameters)
{
$user = $this->tokenStorage->getToken()->getUser();
if ($this->authorizationChecker->isGranted('ROLE_USER')) {
if ($this->security->isGranted('ROLE_USER')) {
$menu->addChild('My calendar list', [
'route' => 'chill_calendar_calendar_list',
'route' => 'chill_calendar_calendar_list_my',
])
->setExtras([
'order' => 9,

View File

@@ -0,0 +1,80 @@
<?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);
/*
* 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\Messenger\Doctrine;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Messenger\Message\CalendarMessage;
use Chill\CalendarBundle\Messenger\Message\CalendarRemovedMessage;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Event\PostRemoveEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Security;
class CalendarEntityListener
{
private MessageBusInterface $messageBus;
private Security $security;
public function __construct(MessageBusInterface $messageBus, Security $security)
{
$this->messageBus = $messageBus;
$this->security = $security;
}
public function postPersist(Calendar $calendar, PostPersistEventArgs $args): void
{
if (!$calendar->preventEnqueueChanges) {
$this->messageBus->dispatch(
new CalendarMessage(
$calendar,
CalendarMessage::CALENDAR_PERSIST,
$this->security->getUser()
)
);
}
}
public function postRemove(Calendar $calendar, PostRemoveEventArgs $args): void
{
if (!$calendar->preventEnqueueChanges) {
$this->messageBus->dispatch(
new CalendarRemovedMessage(
$calendar,
$this->security->getUser()
)
);
}
}
public function postUpdate(Calendar $calendar, PostUpdateEventArgs $args): void
{
if (!$calendar->preventEnqueueChanges) {
$this->messageBus->dispatch(
new CalendarMessage(
$calendar,
CalendarMessage::CALENDAR_UPDATE,
$this->security->getUser()
)
);
}
}
}

View File

@@ -0,0 +1,80 @@
<?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);
/*
* 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\Messenger\Doctrine;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\CalendarBundle\Messenger\Message\CalendarRangeMessage;
use Chill\CalendarBundle\Messenger\Message\CalendarRangeRemovedMessage;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Event\PostRemoveEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Security;
class CalendarRangeEntityListener
{
private MessageBusInterface $messageBus;
private Security $security;
public function __construct(MessageBusInterface $messageBus, Security $security)
{
$this->messageBus = $messageBus;
$this->security = $security;
}
public function postPersist(CalendarRange $calendarRange, PostPersistEventArgs $eventArgs): void
{
if (!$calendarRange->preventEnqueueChanges) {
$this->messageBus->dispatch(
new CalendarRangeMessage(
$calendarRange,
CalendarRangeMessage::CALENDAR_RANGE_PERSIST,
$this->security->getUser()
)
);
}
}
public function postRemove(CalendarRange $calendarRange, PostRemoveEventArgs $eventArgs): void
{
if (!$calendarRange->preventEnqueueChanges) {
$this->messageBus->dispatch(
new CalendarRangeRemovedMessage(
$calendarRange,
$this->security->getUser()
)
);
}
}
public function postUpdate(CalendarRange $calendarRange, PostUpdateEventArgs $eventArgs): void
{
if (!$calendarRange->preventEnqueueChanges) {
$this->messageBus->dispatch(
new CalendarRangeMessage(
$calendarRange,
CalendarRangeMessage::CALENDAR_RANGE_UPDATE,
$this->security->getUser()
)
);
}
}
}

View File

@@ -0,0 +1,52 @@
<?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);
/*
* 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\Messenger\Handler;
use Chill\CalendarBundle\Messenger\Message\CalendarRangeRemovedMessage;
use Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface;
use Chill\MainBundle\Repository\UserRepository;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
/**
* Remove a calendar range when it is removed from local calendar.
*
* @AsMessageHandler
*/
class CalendarRangeRemoveToRemoteHandler implements MessageHandlerInterface
{
private RemoteCalendarConnectorInterface $remoteCalendarConnector;
private UserRepository $userRepository;
public function __construct(RemoteCalendarConnectorInterface $remoteCalendarConnector, UserRepository $userRepository)
{
$this->remoteCalendarConnector = $remoteCalendarConnector;
$this->userRepository = $userRepository;
}
public function __invoke(CalendarRangeRemovedMessage $calendarRangeRemovedMessage)
{
$this->remoteCalendarConnector->removeCalendarRange(
$calendarRangeRemovedMessage->getRemoteId(),
$calendarRangeRemovedMessage->getRemoteAttributes(),
$this->userRepository->find($calendarRangeRemovedMessage->getCalendarRangeUserId())
);
}
}

View File

@@ -0,0 +1,64 @@
<?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);
/*
* 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\Messenger\Handler;
use Chill\CalendarBundle\Messenger\Message\CalendarRangeMessage;
use Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface;
use Chill\CalendarBundle\Repository\CalendarRangeRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
/**
* Write calendar range creation / update to the remote calendar.
*
* @AsMessageHandler
*/
class CalendarRangeToRemoteHandler implements MessageHandlerInterface
{
private CalendarRangeRepository $calendarRangeRepository;
private EntityManagerInterface $entityManager;
private RemoteCalendarConnectorInterface $remoteCalendarConnector;
public function __construct(
CalendarRangeRepository $calendarRangeRepository,
RemoteCalendarConnectorInterface $remoteCalendarConnector,
EntityManagerInterface $entityManager
) {
$this->calendarRangeRepository = $calendarRangeRepository;
$this->remoteCalendarConnector = $remoteCalendarConnector;
$this->entityManager = $entityManager;
}
public function __invoke(CalendarRangeMessage $calendarRangeMessage): void
{
$range = $this->calendarRangeRepository->find($calendarRangeMessage->getCalendarRangeId());
if (null === $range) {
return;
}
$this->remoteCalendarConnector->syncCalendarRange($range);
$range->preventEnqueueChanges = true;
$this->entityManager->flush();
}
}

View File

@@ -0,0 +1,62 @@
<?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);
/*
* 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\Messenger\Handler;
use Chill\CalendarBundle\Messenger\Message\CalendarRemovedMessage;
use Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface;
use Chill\CalendarBundle\Repository\CalendarRangeRepository;
use Chill\MainBundle\Repository\UserRepositoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
/**
* Handle the deletion of calendar.
*
* @AsMessageHandler
*/
class CalendarRemoveHandler implements MessageHandlerInterface
{
private CalendarRangeRepository $calendarRangeRepository;
private RemoteCalendarConnectorInterface $remoteCalendarConnector;
private UserRepositoryInterface $userRepository;
public function __construct(RemoteCalendarConnectorInterface $remoteCalendarConnector, CalendarRangeRepository $calendarRangeRepository, UserRepositoryInterface $userRepository)
{
$this->remoteCalendarConnector = $remoteCalendarConnector;
$this->calendarRangeRepository = $calendarRangeRepository;
$this->userRepository = $userRepository;
}
public function __invoke(CalendarRemovedMessage $message)
{
if (null !== $message->getAssociatedCalendarRangeId()) {
$associatedRange = $this->calendarRangeRepository->find($message->getAssociatedCalendarRangeId());
} else {
$associatedRange = null;
}
$this->remoteCalendarConnector->removeCalendar(
$message->getRemoteId(),
$message->getRemoteAttributes(),
$this->userRepository->find($message->getCalendarUserId()),
$associatedRange
);
}
}

View File

@@ -0,0 +1,119 @@
<?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);
/*
* 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\Messenger\Handler;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\CalendarBundle\Entity\Invite;
use Chill\CalendarBundle\Messenger\Message\CalendarMessage;
use Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface;
use Chill\CalendarBundle\Repository\CalendarRangeRepository;
use Chill\CalendarBundle\Repository\CalendarRepository;
use Chill\CalendarBundle\Repository\InviteRepository;
use Chill\MainBundle\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
/**
* Write calendar creation / update to the remote calendar.
*
* @AsMessageHandler
*/
class CalendarToRemoteHandler implements MessageHandlerInterface
{
private RemoteCalendarConnectorInterface $calendarConnector;
private CalendarRangeRepository $calendarRangeRepository;
private CalendarRepository $calendarRepository;
private EntityManagerInterface $entityManager;
private InviteRepository $inviteRepository;
private UserRepository $userRepository;
public function __construct(
CalendarRangeRepository $calendarRangeRepository,
CalendarRepository $calendarRepository,
EntityManagerInterface $entityManager,
InviteRepository $inviteRepository,
RemoteCalendarConnectorInterface $calendarConnector,
UserRepository $userRepository
) {
$this->calendarConnector = $calendarConnector;
$this->calendarRepository = $calendarRepository;
$this->calendarRangeRepository = $calendarRangeRepository;
$this->entityManager = $entityManager;
$this->userRepository = $userRepository;
$this->inviteRepository = $inviteRepository;
}
public function __invoke(CalendarMessage $calendarMessage)
{
$calendar = $this->calendarRepository->find($calendarMessage->getCalendarId());
if (null === $calendar) {
return;
}
if (null !== $calendarMessage->getPreviousCalendarRangeId()) {
$previousCalendarRange = $this->calendarRangeRepository
->find($calendarMessage->getPreviousCalendarRangeId());
} else {
$previousCalendarRange = null;
}
if (null !== $calendarMessage->getPreviousMainUserId()) {
$previousMainUser = $this->userRepository
->find($calendarMessage->getPreviousMainUserId());
} else {
$previousMainUser = null;
}
$newInvites = array_filter(
array_map(
fn ($id) => $this->inviteRepository->find($id),
$calendarMessage->getNewInvitesIds(),
),
static fn (?Invite $invite) => null !== $invite
);
$this->calendarConnector->syncCalendar(
$calendar,
$calendarMessage->getAction(),
$previousCalendarRange,
$previousMainUser,
$calendarMessage->getOldInvites(),
$newInvites
);
$calendar->preventEnqueueChanges = true;
if ($calendar->hasCalendarRange()) {
$calendar->getCalendarRange()->preventEnqueueChanges = true;
}
if ($previousCalendarRange instanceof CalendarRange) {
$previousCalendarRange->preventEnqueueChanges = true;
}
$this->entityManager->flush();
}
}

View File

@@ -0,0 +1,57 @@
<?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);
/*
* 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\Messenger\Handler;
use Chill\CalendarBundle\Messenger\Message\InviteUpdateMessage;
use Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface;
use Chill\CalendarBundle\Repository\InviteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
/**
* Sync the local invitation to the remote calendar.
*
* @AsMessageHandler
*/
class InviteUpdateHandler implements MessageHandlerInterface
{
private EntityManagerInterface $em;
private InviteRepository $inviteRepository;
private RemoteCalendarConnectorInterface $remoteCalendarConnector;
public function __construct(EntityManagerInterface $em, InviteRepository $inviteRepository, RemoteCalendarConnectorInterface $remoteCalendarConnector)
{
$this->em = $em;
$this->inviteRepository = $inviteRepository;
$this->remoteCalendarConnector = $remoteCalendarConnector;
}
public function __invoke(InviteUpdateMessage $inviteUpdateMessage): void
{
if (null === $invite = $this->inviteRepository->find($inviteUpdateMessage->getInviteId())) {
return;
}
$this->remoteCalendarConnector->syncInvite($invite);
$this->em->flush();
}
}

View File

@@ -0,0 +1,110 @@
<?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);
/*
* 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\Messenger\Handler;
use Chill\CalendarBundle\Messenger\Message\MSGraphChangeNotificationMessage;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MapCalendarToUser;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\RemoteToLocalSync\CalendarRangeSyncer;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\RemoteToLocalSync\CalendarSyncer;
use Chill\CalendarBundle\Repository\CalendarRangeRepository;
use Chill\CalendarBundle\Repository\CalendarRepository;
use Chill\MainBundle\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
/**
* Handle notification of changes made by users directly on Outlook calendar.
*
* @AsMessageHandler
*/
class MSGraphChangeNotificationHandler implements MessageHandlerInterface
{
private CalendarRangeRepository $calendarRangeRepository;
private CalendarRangeSyncer $calendarRangeSyncer;
private CalendarRepository $calendarRepository;
private CalendarSyncer $calendarSyncer;
private EntityManagerInterface $em;
private LoggerInterface $logger;
private MapCalendarToUser $mapCalendarToUser;
private UserRepository $userRepository;
public function __construct(
CalendarRangeRepository $calendarRangeRepository,
CalendarRangeSyncer $calendarRangeSyncer,
CalendarRepository $calendarRepository,
CalendarSyncer $calendarSyncer,
EntityManagerInterface $em,
LoggerInterface $logger,
MapCalendarToUser $mapCalendarToUser,
UserRepository $userRepository
) {
$this->calendarRangeRepository = $calendarRangeRepository;
$this->calendarRangeSyncer = $calendarRangeSyncer;
$this->calendarRepository = $calendarRepository;
$this->calendarSyncer = $calendarSyncer;
$this->em = $em;
$this->logger = $logger;
$this->mapCalendarToUser = $mapCalendarToUser;
$this->userRepository = $userRepository;
}
public function __invoke(MSGraphChangeNotificationMessage $changeNotificationMessage): void
{
$user = $this->userRepository->find($changeNotificationMessage->getUserId());
if (null === $user) {
$this->logger->warning(self::class . ' notification concern non-existent user, skipping');
return;
}
foreach ($changeNotificationMessage->getContent()['value'] as $notification) {
$secret = $this->mapCalendarToUser->getSubscriptionSecret($user);
if ($secret !== ($notification['clientState'] ?? -1)) {
$this->logger->warning(self::class . ' could not validate secret, skipping');
continue;
}
$remoteId = $notification['resourceData']['id'];
// is this a calendar range ?
if (null !== $calendarRange = $this->calendarRangeRepository->findOneBy(['remoteId' => $remoteId])) {
$this->calendarRangeSyncer->handleCalendarRangeSync($calendarRange, $notification, $user);
$this->em->flush();
} elseif (null !== $calendar = $this->calendarRepository->findOneBy(['remoteId' => $remoteId])) {
$this->calendarSyncer->handleCalendarSync($calendar, $notification, $user);
$this->em->flush();
} else {
$this->logger->info(self::class . ' id not found in any calendar nor calendar range');
}
}
$this->em->flush();
}
}

View File

@@ -0,0 +1,109 @@
<?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);
/*
* 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\Messenger\Message;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\Invite;
use Chill\MainBundle\Entity\User;
class CalendarMessage
{
public const CALENDAR_PERSIST = 'CHILL_CALENDAR_CALENDAR_PERSIST';
public const CALENDAR_UPDATE = 'CHILL_CALENDAR_CALENDAR_UPDATE';
private string $action;
private int $byUserId;
private int $calendarId;
private array $newInvitesIds = [];
/**
* @var array<array{inviteId: int, userId: int, userEmail: int, userLabel: string}>
*/
private array $oldInvites = [];
private ?int $previousCalendarRangeId = null;
private ?int $previousMainUserId = null;
public function __construct(
Calendar $calendar,
string $action,
User $byUser
) {
$this->calendarId = $calendar->getId();
$this->byUserId = $byUser->getId();
$this->action = $action;
$this->previousCalendarRangeId = null !== $calendar->previousCalendarRange ?
$calendar->previousCalendarRange->getId() : null;
$this->previousMainUserId = null !== $calendar->previousMainUser ?
$calendar->previousMainUser->getId() : null;
$this->newInvitesIds = array_map(static fn (Invite $i) => $i->getId(), $calendar->newInvites);
$this->oldInvites = array_map(static fn (Invite $i) => [
'inviteId' => $i->getId(),
'userId' => $i->getUser()->getId(),
'userEmail' => $i->getUser()->getEmail(),
'userLabel' => $i->getUser()->getLabel(),
], $calendar->oldInvites);
}
public function getAction(): string
{
return $this->action;
}
public function getByUserId(): ?int
{
return $this->byUserId;
}
public function getCalendarId(): ?int
{
return $this->calendarId;
}
/**
* @return array|int[]|null[]
*/
public function getNewInvitesIds(): array
{
return $this->newInvitesIds;
}
/**
* @return array<array{inviteId: int, userId: int, userEmail: int, userLabel: string}>
*/
public function getOldInvites(): array
{
return $this->oldInvites;
}
public function getPreviousCalendarRangeId(): ?int
{
return $this->previousCalendarRangeId;
}
public function getPreviousMainUserId(): ?int
{
return $this->previousMainUserId;
}
}

View File

@@ -0,0 +1,60 @@
<?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);
/*
* 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\Messenger\Message;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\MainBundle\Entity\User;
class CalendarRangeMessage
{
public const CALENDAR_RANGE_PERSIST = 'CHILL_CALENDAR_CALENDAR_RANGE_PERSIST';
public const CALENDAR_RANGE_UPDATE = 'CHILL_CALENDAR_CALENDAR_RANGE_UPDATE';
private string $action;
private ?int $byUserId = null;
private int $calendarRangeId;
public function __construct(CalendarRange $calendarRange, string $action, ?User $byUser)
{
$this->action = $action;
$this->calendarRangeId = $calendarRange->getId();
if (null !== $byUser) {
$this->byUserId = $byUser->getId();
}
}
public function getAction(): string
{
return $this->action;
}
public function getByUserId(): ?int
{
return $this->byUserId;
}
public function getCalendarRangeId(): ?int
{
return $this->calendarRangeId;
}
}

View File

@@ -0,0 +1,64 @@
<?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);
/*
* 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\Messenger\Message;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\MainBundle\Entity\User;
class CalendarRangeRemovedMessage
{
private ?int $byUserId = null;
private int $calendarRangeUserId;
private array $remoteAttributes;
private string $remoteId;
public function __construct(CalendarRange $calendarRange, ?User $byUser)
{
$this->remoteId = $calendarRange->getRemoteId();
$this->remoteAttributes = $calendarRange->getRemoteAttributes();
$this->calendarRangeUserId = $calendarRange->getUser()->getId();
if (null !== $byUser) {
$this->byUserId = $byUser->getId();
}
}
public function getByUserId(): ?int
{
return $this->byUserId;
}
public function getCalendarRangeUserId(): ?int
{
return $this->calendarRangeUserId;
}
public function getRemoteAttributes(): array
{
return $this->remoteAttributes;
}
public function getRemoteId(): string
{
return $this->remoteId;
}
}

View File

@@ -0,0 +1,75 @@
<?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);
/*
* 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\Messenger\Message;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\MainBundle\Entity\User;
class CalendarRemovedMessage
{
private ?int $associatedCalendarRangeId = null;
private ?int $byUserId = null;
private int $calendarUserId;
private array $remoteAttributes;
private string $remoteId;
public function __construct(Calendar $calendar, ?User $byUser)
{
$this->remoteId = $calendar->getRemoteId();
$this->remoteAttributes = $calendar->getRemoteAttributes();
$this->calendarUserId = $calendar->getMainUser()->getId();
if ($calendar->hasCalendarRange()) {
$this->associatedCalendarRangeId = $calendar->getCalendarRange()->getId();
}
if (null !== $byUser) {
$this->byUserId = $byUser->getId();
}
}
public function getAssociatedCalendarRangeId(): ?int
{
return $this->associatedCalendarRangeId;
}
public function getByUserId(): ?int
{
return $this->byUserId;
}
public function getCalendarUserId(): ?int
{
return $this->calendarUserId;
}
public function getRemoteAttributes(): array
{
return $this->remoteAttributes;
}
public function getRemoteId(): string
{
return $this->remoteId;
}
}

View File

@@ -0,0 +1,45 @@
<?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);
/*
* 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\Messenger\Message;
use Chill\CalendarBundle\Entity\Invite;
use Chill\MainBundle\Entity\User;
class InviteUpdateMessage
{
private int $byUserId;
private int $inviteId;
public function __construct(Invite $invite, User $byUser)
{
$this->inviteId = $invite->getId();
$this->byUserId = $byUser->getId();
}
public function getByUserId(): int
{
return $this->byUserId;
}
public function getInviteId(): int
{
return $this->inviteId;
}
}

View File

@@ -0,0 +1,42 @@
<?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);
/*
* 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\Messenger\Message;
class MSGraphChangeNotificationMessage
{
private array $content;
private int $userId;
public function __construct(array $content, int $userId)
{
$this->content = $content;
$this->userId = $userId;
}
public function getContent(): array
{
return $this->content;
}
public function getUserId(): int
{
return $this->userId;
}
}

View File

@@ -0,0 +1,48 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Templating\Entity\AddressRender;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
class AddressConverter
{
private AddressRender $addressRender;
private TranslatableStringHelperInterface $translatableStringHelper;
public function __construct(AddressRender $addressRender, TranslatableStringHelperInterface $translatableStringHelper)
{
$this->addressRender = $addressRender;
$this->translatableStringHelper = $translatableStringHelper;
}
public function addressToRemote(Address $address): array
{
return [
'city' => $address->getPostcode()->getName(),
'postalCode' => $address->getPostcode()->getCode(),
'countryOrRegion' => $this->translatableStringHelper->localize($address->getPostcode()->getCountry()->getName()),
'street' => $address->isNoAddress() ? '' :
implode(', ', $this->addressRender->renderLines($address, false, false)),
'state' => '',
];
}
}

View File

@@ -0,0 +1,140 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use LogicException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
/**
* Create a subscription for a user.
*/
class EventsOnUserSubscriptionCreator
{
private LoggerInterface $logger;
private MachineHttpClient $machineHttpClient;
private MapCalendarToUser $mapCalendarToUser;
private UrlGeneratorInterface $urlGenerator;
public function __construct(
LoggerInterface $logger,
MachineHttpClient $machineHttpClient,
MapCalendarToUser $mapCalendarToUser,
UrlGeneratorInterface $urlGenerator
) {
$this->logger = $logger;
$this->machineHttpClient = $machineHttpClient;
$this->mapCalendarToUser = $mapCalendarToUser;
$this->urlGenerator = $urlGenerator;
}
/**
* @throws ClientExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*
* @return array<secret: string, id: string, expiration: int>
*/
public function createSubscriptionForUser(User $user, DateTimeImmutable $expiration): array
{
if (null === $userId = $this->mapCalendarToUser->getUserId($user)) {
throw new LogicException('no user id');
}
$subscription = [
'changeType' => 'deleted,updated',
'notificationUrl' => $this->urlGenerator->generate(
'chill_calendar_remote_msgraph_incoming_webhook_events',
['userId' => $user->getId()],
UrlGeneratorInterface::ABSOLUTE_URL
),
'resource' => "/users/{$userId}/calendar/events",
'clientState' => $secret = base64_encode(openssl_random_pseudo_bytes(92, $cstrong)),
'expirationDateTime' => $expiration->format(DateTimeImmutable::ATOM),
];
try {
$subs = $this->machineHttpClient->request(
'POST',
'/v1.0/subscriptions',
[
'json' => $subscription,
]
)->toArray();
} catch (ClientExceptionInterface $e) {
$this->logger->error('could not create subscription for user events', [
'body' => $e->getResponse()->getContent(false),
]);
return ['secret' => '', 'id' => '', 'expiration' => 0];
}
return ['secret' => $secret, 'id' => $subs['id'], 'expiration' => $expiration->getTimestamp()];
}
/**
* @throws ClientExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*
* @return array<secret: string, id: string, expiration: int>
*/
public function renewSubscriptionForUser(User $user, DateTimeImmutable $expiration): array
{
if (null === $userId = $this->mapCalendarToUser->getUserId($user)) {
throw new LogicException('no user id');
}
if (null === $subscriptionId = $this->mapCalendarToUser->getActiveSubscriptionId($user)) {
throw new LogicException('no user id');
}
$subscription = [
'expirationDateTime' => $expiration->format(DateTimeImmutable::ATOM),
];
try {
$subs = $this->machineHttpClient->request(
'PATCH',
"/v1.0/subscriptions/{$subscriptionId}",
[
'json' => $subscription,
]
)->toArray();
} catch (ClientExceptionInterface $e) {
$this->logger->error('could not patch subscription for user events', [
'body' => $e->getResponse()->getContent(false),
]);
return ['secret' => '', 'id' => '', 'expiration' => 0];
}
return ['secret' => $subs['clientState'], 'id' => $subs['id'], 'expiration' => $expiration->getTimestamp()];
}
}

View File

@@ -0,0 +1,53 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph;
use Chill\MainBundle\Entity\Location;
class LocationConverter
{
private AddressConverter $addressConverter;
public function __construct(AddressConverter $addressConverter)
{
$this->addressConverter = $addressConverter;
}
public function locationToRemote(Location $location): array
{
$results = [];
if ($location->hasAddress()) {
$results['address'] = $this->addressConverter->addressToRemote($location->getAddress());
if ($location->getAddress()->hasAddressReference() && $location->getAddress()->getAddressReference()->hasPoint()) {
$results['coordinates'] = [
'latitude' => $location->getAddress()->getAddressReference()->getPoint()->getLat(),
'longitude' => $location->getAddress()->getAddressReference()->getPoint()->getLon(),
];
}
}
if (null !== $location->getName()) {
$results['displayName'] = $location->getName();
}
return $results;
}
}

View File

@@ -0,0 +1,84 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph;
use Chill\MainBundle\Entity\User;
use DateInterval;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
use function strtr;
/**
* Contains classes and methods for fetching users with some calendar metadatas.
*/
class MSGraphUserRepository
{
private const MOST_OLD_SUBSCRIPTION_OR_ANY_MS_GRAPH = <<<'SQL'
select
{select}
from users u
where
NOT attributes ?? 'msgraph'
OR NOT attributes->'msgraph' ?? 'subscription_events_expiration'
OR (attributes->'msgraph' ?? 'subscription_events_expiration' AND (attributes->'msgraph'->>'subscription_events_expiration')::int < EXTRACT(EPOCH FROM (NOW() + :interval::interval)))
LIMIT :limit OFFSET :offset
;
SQL;
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function countByMostOldSubscriptionOrWithoutSubscriptionOrData(DateInterval $interval): int
{
$rsm = new ResultSetMapping();
$rsm->addScalarResult('c', 'c');
$sql = strtr(self::MOST_OLD_SUBSCRIPTION_OR_ANY_MS_GRAPH, [
'{select}' => 'COUNT(u) AS c',
'LIMIT :limit OFFSET :offset' => '',
]);
return $this->entityManager->createNativeQuery($sql, $rsm)->setParameters([
'interval' => $interval,
])->getSingleScalarResult();
}
/**
* @return array|User[]
*/
public function findByMostOldSubscriptionOrWithoutSubscriptionOrData(DateInterval $interval, int $limit = 50, int $offset = 0): array
{
$rsm = new ResultSetMappingBuilder($this->entityManager);
$rsm->addRootEntityFromClassMetadata(User::class, 'u');
return $this->entityManager->createNativeQuery(
strtr(self::MOST_OLD_SUBSCRIPTION_OR_ANY_MS_GRAPH, ['{select}' => $rsm->generateSelectClause()]),
$rsm
)->setParameters([
'interval' => $interval,
'limit' => $limit,
'offset' => $offset,
])->getResult();
}
}

View File

@@ -0,0 +1,82 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use LogicException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
class MachineHttpClient implements HttpClientInterface
{
use BearerAuthorizationTrait;
private HttpClientInterface $decoratedClient;
private MachineTokenStorage $machineTokenStorage;
/**
* @param HttpClientInterface $decoratedClient
*/
public function __construct(MachineTokenStorage $machineTokenStorage, ?HttpClientInterface $decoratedClient = null)
{
$this->decoratedClient = $decoratedClient ?? \Symfony\Component\HttpClient\HttpClient::create();
$this->machineTokenStorage = $machineTokenStorage;
}
/**
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
* @throws LogicException if method is not supported
*/
public function request(string $method, string $url, array $options = []): ResponseInterface
{
$options['headers'] = array_merge(
$options['headers'] ?? [],
$this->getAuthorizationHeaders($this->machineTokenStorage->getToken())
);
$options['base_uri'] = 'https://graph.microsoft.com/v1.0/';
switch ($method) {
case 'GET':
case 'HEAD':
case 'DELETE':
$options['headers']['Accept'] = 'application/json';
break;
case 'POST':
case 'PUT':
case 'PATCH':
$options['headers']['Content-Type'] = 'application/json';
break;
default:
throw new LogicException("Method not supported: {$method}");
}
return $this->decoratedClient->request($method, $url, $options);
}
public function stream($responses, ?float $timeout = null): ResponseStreamInterface
{
return $this->decoratedClient->stream($responses, $timeout);
}
}

View File

@@ -0,0 +1,57 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph;
use Chill\MainBundle\Redis\ChillRedis;
use League\OAuth2\Client\Token\AccessTokenInterface;
use TheNetworg\OAuth2\Client\Provider\Azure;
use TheNetworg\OAuth2\Client\Token\AccessToken;
class MachineTokenStorage
{
private const KEY = 'msgraph_access_token';
private ?AccessTokenInterface $accessToken = null;
private Azure $azure;
private ChillRedis $chillRedis;
public function __construct(Azure $azure, ChillRedis $chillRedis)
{
$this->azure = $azure;
$this->chillRedis = $chillRedis;
}
public function getToken(): AccessTokenInterface
{
if (null === $this->accessToken || $this->accessToken->hasExpired()) {
$this->accessToken = $this->azure->getAccessToken('client_credentials', [
'scope' => 'https://graph.microsoft.com/.default',
]);
}
return $this->accessToken;
}
public function storeToken(AccessToken $token): void
{
$this->chillRedis->set(self::KEY, serialize($token));
}
}

View File

@@ -0,0 +1,210 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use LogicException;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use function array_key_exists;
/**
* Write metadata to user, which allow to find his default calendar.
*/
class MapCalendarToUser
{
public const EXPIRATION_SUBSCRIPTION_EVENT = 'subscription_events_expiration';
public const ID_SUBSCRIPTION_EVENT = 'subscription_events_id';
public const METADATA_KEY = 'msgraph';
public const SECRET_SUBSCRIPTION_EVENT = 'subscription_events_secret';
private LoggerInterface $logger;
private HttpClientInterface $machineHttpClient;
public function __construct(
HttpClientInterface $machineHttpClient,
LoggerInterface $logger
) {
$this->machineHttpClient = $machineHttpClient;
$this->logger = $logger;
}
public function getActiveSubscriptionId(User $user): string
{
if (!array_key_exists(self::METADATA_KEY, $user->getAttributes())) {
throw new LogicException('do not contains msgraph metadata');
}
if (!array_key_exists(self::ID_SUBSCRIPTION_EVENT, $user->getAttributes()[self::METADATA_KEY])) {
throw new LogicException('do not contains metadata for subscription id');
}
return $user->getAttributes()[self::METADATA_KEY][self::ID_SUBSCRIPTION_EVENT];
}
public function getCalendarId(User $user): ?string
{
if (null === $msKey = ($user->getAttributes()[self::METADATA_KEY] ?? null)) {
return null;
}
return $msKey['defaultCalendarId'] ?? null;
}
public function getDefaultUserCalendar(string $idOrUserPrincipalName): ?array
{
try {
$value = $this->machineHttpClient->request('GET', "users/{$idOrUserPrincipalName}/calendars", [
'query' => ['$filter' => 'isDefaultCalendar eq true'],
])->toArray()['value'];
} catch (ClientExceptionInterface $e) {
$this->logger->error('[MapCalendarToUser] Error while listing calendars for a user', [
'http_status_code' => $e->getResponse()->getStatusCode(),
'id_user' => $idOrUserPrincipalName,
]);
return null;
}
return $value[0] ?? null;
}
public function getSubscriptionSecret(User $user): string
{
if (!array_key_exists(self::METADATA_KEY, $user->getAttributes())) {
throw new LogicException('do not contains msgraph metadata');
}
if (!array_key_exists(self::SECRET_SUBSCRIPTION_EVENT, $user->getAttributes()[self::METADATA_KEY])) {
throw new LogicException('do not contains secret in msgraph');
}
return $user->getAttributes()[self::METADATA_KEY][self::SECRET_SUBSCRIPTION_EVENT];
}
public function getUserByEmail(string $email): ?array
{
$value = $this->machineHttpClient->request('GET', 'users', [
'query' => ['$filter' => "mail eq '{$email}'"],
])->toArray()['value'];
return $value[0] ?? null;
}
public function getUserId(User $user): ?string
{
if (null === $msKey = ($user->getAttributes()[self::METADATA_KEY] ?? null)) {
return null;
}
return $msKey['id'] ?? null;
}
public function hasActiveSubscription(User $user): bool
{
if (!array_key_exists(self::METADATA_KEY, $user->getAttributes())) {
return false;
}
if (!array_key_exists(self::EXPIRATION_SUBSCRIPTION_EVENT, $user->getAttributes()[self::METADATA_KEY])) {
return false;
}
return $user->getAttributes()[self::METADATA_KEY][self::EXPIRATION_SUBSCRIPTION_EVENT]
>= (new DateTimeImmutable('now'))->getTimestamp();
}
public function hasSubscriptionSecret(User $user): bool
{
if (!array_key_exists(self::METADATA_KEY, $user->getAttributes())) {
return false;
}
return array_key_exists(self::SECRET_SUBSCRIPTION_EVENT, $user->getAttributes()[self::METADATA_KEY]);
}
public function hasUserId(User $user): bool
{
if (null === $user->getEmail() || '' === $user->getEmail()) {
return false;
}
if (!array_key_exists(self::METADATA_KEY, $user->getAttributes())) {
return false;
}
return array_key_exists('id', $user->getAttributes()[self::METADATA_KEY]);
}
public function writeMetadata(User $user): User
{
if (null === $user->getEmail() || '' === $user->getEmail()) {
return $user;
}
if (null === $userData = $this->getUserByEmail($user->getEmailCanonical())) {
$this->logger->warning('[MapCalendarToUser] could not find user on msgraph', ['userId' => $user->getId(), 'email' => $user->getEmailCanonical()]);
return $this->writeNullData($user);
}
if (null === $defaultCalendar = $this->getDefaultUserCalendar($userData['id'])) {
$this->logger->warning('[MapCalendarToUser] could not find default calendar', ['userId' => $user->getId(), 'email' => $user->getEmailCanonical()]);
return $this->writeNullData($user);
}
return $user->setAttributes([self::METADATA_KEY => [
'id' => $userData['id'],
'userPrincipalName' => $userData['userPrincipalName'],
'defaultCalendarId' => $defaultCalendar['id'],
]]);
}
/**
* @param int $expiration the expiration time as unix timestamp
*/
public function writeSubscriptionMetadata(
User $user,
int $expiration,
?string $id = null,
?string $secret = null
): void {
$user->setAttributeByDomain(self::METADATA_KEY, self::EXPIRATION_SUBSCRIPTION_EVENT, $expiration);
if (null !== $id) {
$user->setAttributeByDomain(self::METADATA_KEY, self::ID_SUBSCRIPTION_EVENT, $id);
}
if (null !== $secret) {
$user->setAttributeByDomain(self::METADATA_KEY, self::SECRET_SUBSCRIPTION_EVENT, $secret);
}
}
private function writeNullData(User $user): User
{
return $user->unsetAttribute(self::METADATA_KEY);
}
}

View File

@@ -0,0 +1,77 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use LogicException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
class OnBehalfOfUserHttpClient
{
use BearerAuthorizationTrait;
private HttpClientInterface $decoratedClient;
private OnBehalfOfUserTokenStorage $tokenStorage;
/**
* @param HttpClientInterface $decoratedClient
*/
public function __construct(OnBehalfOfUserTokenStorage $tokenStorage, ?HttpClientInterface $decoratedClient = null)
{
$this->decoratedClient = $decoratedClient ?? \Symfony\Component\HttpClient\HttpClient::create();
$this->tokenStorage = $tokenStorage;
}
public function request(string $method, string $url, array $options = []): ResponseInterface
{
$options['headers'] = array_merge(
$options['headers'] ?? [],
$this->getAuthorizationHeaders($this->tokenStorage->getToken())
);
$options['base_uri'] = 'https://graph.microsoft.com/v1.0/';
switch ($method) {
case 'GET':
case 'HEAD':
$options['headers']['Accept'] = 'application/json';
break;
case 'POST':
case 'PUT':
case 'PATCH':
$options['headers']['Content-Type'] = 'application/json';
break;
default:
throw new LogicException("Method not supported: {$method}");
}
return $this->decoratedClient->request($method, $url, $options);
}
public function stream($responses, ?float $timeout = null): ResponseStreamInterface
{
return $this->decoratedClient->stream($responses, $timeout);
}
}

View File

@@ -0,0 +1,72 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph;
use LogicException;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use TheNetworg\OAuth2\Client\Provider\Azure;
use TheNetworg\OAuth2\Client\Token\AccessToken;
/**
* Store token obtained on behalf of a User.
*/
class OnBehalfOfUserTokenStorage
{
public const MS_GRAPH_ACCESS_TOKEN = 'msgraph_access_token';
private Azure $azure;
private SessionInterface $session;
public function __construct(Azure $azure, SessionInterface $session)
{
$this->azure = $azure;
$this->session = $session;
}
public function getToken(): AccessToken
{
/** @var ?AccessToken $token */
$token = $this->session->get(self::MS_GRAPH_ACCESS_TOKEN, null);
if (null === $token) {
throw new LogicException('unexisting token');
}
if ($token->hasExpired()) {
$token = $this->azure->getAccessToken('refresh_token', [
'refresh_token' => $token->getRefreshToken(),
]);
$this->setToken($token);
}
return $token;
}
public function hasToken(): bool
{
return $this->session->has(self::MS_GRAPH_ACCESS_TOKEN);
}
public function setToken(AccessToken $token): void
{
$this->session->set(self::MS_GRAPH_ACCESS_TOKEN, $token);
}
}

View File

@@ -0,0 +1,280 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\CalendarBundle\Entity\Invite;
use Chill\CalendarBundle\RemoteCalendar\Model\RemoteEvent;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Templating\Entity\PersonRenderInterface;
use DateTimeImmutable;
use DateTimeZone;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Convert Chill Calendar event to Remote MS Graph event, and MS Graph
* event to RemoteEvent.
*/
class RemoteEventConverter
{
/**
* valid when the remote string contains also a timezone, like in
* lastModifiedDate.
*/
public const REMOTE_DATETIMEZONE_FORMAT = 'Y-m-d\\TH:i:s.u?P';
/**
* Same as above, but sometimes the date is expressed with only 6 milliseconds.
*/
public const REMOTE_DATETIMEZONE_FORMAT_ALT = 'Y-m-d\\TH:i:s.uP';
private const REMOTE_DATE_FORMAT = 'Y-m-d\TH:i:s.u0';
private const REMOTE_DATETIME_WITHOUT_TZ_FORMAT = 'Y-m-d\TH:i:s.u?';
private DateTimeZone $defaultDateTimeZone;
private EngineInterface $engine;
private LocationConverter $locationConverter;
private LoggerInterface $logger;
private PersonRenderInterface $personRender;
private DateTimeZone $remoteDateTimeZone;
private TranslatorInterface $translator;
public function __construct(
EngineInterface $engine,
LocationConverter $locationConverter,
LoggerInterface $logger,
PersonRenderInterface $personRender,
TranslatorInterface $translator
) {
$this->engine = $engine;
$this->locationConverter = $locationConverter;
$this->logger = $logger;
$this->translator = $translator;
$this->personRender = $personRender;
$this->defaultDateTimeZone = (new DateTimeImmutable())->getTimezone();
$this->remoteDateTimeZone = self::getRemoteTimeZone();
}
/**
* Transform a CalendarRange into a representation suitable for storing into MSGraph.
*
* @return array an array representation for event in MS Graph
*/
public function calendarRangeToEvent(CalendarRange $calendarRange): array
{
return [
'subject' => $this->translator->trans('remote_calendar.calendar_range_title'),
'start' => [
'dateTime' => $calendarRange->getStartDate()->setTimezone($this->remoteDateTimeZone)
->format(self::REMOTE_DATE_FORMAT),
'timeZone' => 'UTC',
],
'end' => [
'dateTime' => $calendarRange->getEndDate()->setTimezone($this->remoteDateTimeZone)
->format(self::REMOTE_DATE_FORMAT),
'timeZone' => 'UTC',
],
'attendees' => [
[
'emailAddress' => [
'address' => $calendarRange->getUser()->getEmailCanonical(),
'name' => $calendarRange->getUser()->getLabel(),
],
],
],
'isReminderOn' => false,
'location' => $this->locationConverter->locationToRemote($calendarRange->getLocation()),
];
}
public function calendarToEvent(Calendar $calendar): array
{
$result = array_merge(
[
'subject' => '[Chill] ' .
implode(
', ',
$calendar->getPersons()->map(fn (Person $p) => $this->personRender->renderString($p, []))->toArray()
),
'start' => [
'dateTime' => $calendar->getStartDate()->setTimezone($this->remoteDateTimeZone)
->format(self::REMOTE_DATE_FORMAT),
'timeZone' => 'UTC',
],
'end' => [
'dateTime' => $calendar->getEndDate()->setTimezone($this->remoteDateTimeZone)
->format(self::REMOTE_DATE_FORMAT),
'timeZone' => 'UTC',
],
'allowNewTimeProposals' => false,
'transactionId' => 'calendar_' . $calendar->getId(),
'body' => [
'contentType' => 'text',
'content' => $this->engine->render(
'@ChillCalendar/MSGraph/calendar_event_body.html.twig',
['calendar' => $calendar]
),
],
'responseRequested' => true,
'isReminderOn' => false,
],
$this->calendarToEventAttendeesOnly($calendar)
);
if ($calendar->hasLocation()) {
$result['location'] = $this->locationConverter->locationToRemote($calendar->getLocation());
}
return $result;
}
public function calendarToEventAttendeesOnly(Calendar $calendar): array
{
return [
'attendees' => $calendar->getInvites()->map(
fn (Invite $i) => $this->buildInviteToAttendee($i)
)->toArray(),
];
}
public function convertAvailabilityToRemoteEvent(array $event): RemoteEvent
{
$startDate =
DateTimeImmutable::createFromFormat(self::REMOTE_DATE_FORMAT, $event['start']['dateTime'], $this->remoteDateTimeZone)
->setTimezone($this->defaultDateTimeZone);
$endDate =
DateTimeImmutable::createFromFormat(self::REMOTE_DATE_FORMAT, $event['end']['dateTime'], $this->remoteDateTimeZone)
->setTimezone($this->defaultDateTimeZone);
return new RemoteEvent(
uniqid('generated_'),
$this->translator->trans('remote_ms_graph.freebusy_statuses.' . $event['status']),
'',
$startDate,
$endDate
);
}
public static function convertStringDateWithoutTimezone(string $date): DateTimeImmutable
{
$d = DateTimeImmutable::createFromFormat(
self::REMOTE_DATETIME_WITHOUT_TZ_FORMAT,
$date,
self::getRemoteTimeZone()
);
if (false === $d) {
throw new RuntimeException("could not convert string date to datetime: {$date}");
}
return $d->setTimezone((new DateTimeImmutable())->getTimezone());
}
public static function convertStringDateWithTimezone(string $date): DateTimeImmutable
{
$d = DateTimeImmutable::createFromFormat(self::REMOTE_DATETIMEZONE_FORMAT, $date);
if (false === $d) {
throw new RuntimeException("could not convert string date to datetime: {$date}");
}
$d->setTimezone((new DateTimeImmutable())->getTimezone());
return $d;
}
public function convertToRemote(array $event): RemoteEvent
{
$startDate =
DateTimeImmutable::createFromFormat(self::REMOTE_DATE_FORMAT, $event['start']['dateTime'], $this->remoteDateTimeZone)
->setTimezone($this->defaultDateTimeZone);
$endDate =
DateTimeImmutable::createFromFormat(self::REMOTE_DATE_FORMAT, $event['end']['dateTime'], $this->remoteDateTimeZone)
->setTimezone($this->defaultDateTimeZone);
return new RemoteEvent(
$event['id'],
$event['subject'],
'',
$startDate,
$endDate,
$event['isAllDay']
);
}
public function getLastModifiedDate(array $event): DateTimeImmutable
{
$date = DateTimeImmutable::createFromFormat(self::REMOTE_DATETIMEZONE_FORMAT, $event['lastModifiedDateTime']);
if (false === $date) {
$date = DateTimeImmutable::createFromFormat(self::REMOTE_DATETIMEZONE_FORMAT_ALT, $event['lastModifiedDateTime']);
}
if (false === $date) {
$this->logger->error(self::class . ' Could not convert lastModifiedDate', [
'actual' => $event['lastModifiedDateTime'],
'format' => self::REMOTE_DATETIMEZONE_FORMAT,
'format_alt' => self::REMOTE_DATETIMEZONE_FORMAT_ALT,
]);
throw new RuntimeException(sprintf(
'could not convert lastModifiedDate: %s, expected format: %s',
$event['lastModifiedDateTime'],
self::REMOTE_DATETIMEZONE_FORMAT . ' and ' . self::REMOTE_DATETIMEZONE_FORMAT_ALT
));
}
return $date;
}
/**
* Return a string which format a DateTime to string. To be used in POST requests,.
*/
public static function getRemoteDateTimeSimpleFormat(): string
{
return 'Y-m-d\TH:i:s';
}
public static function getRemoteTimeZone(): DateTimeZone
{
return new DateTimeZone('UTC');
}
private function buildInviteToAttendee(Invite $invite): array
{
return [
'emailAddress' => [
'address' => $invite->getUser()->getEmail(),
'name' => $invite->getUser()->getLabel(),
],
'type' => 'Required',
];
}
}

View File

@@ -0,0 +1,110 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph\RemoteToLocalSync;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MachineHttpClient;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\RemoteEventConverter;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class CalendarRangeSyncer
{
private EntityManagerInterface $em;
private LoggerInterface $logger;
private HttpClientInterface $machineHttpClient;
/**
* @param MachineHttpClient $machineHttpClient
*/
public function __construct(
EntityManagerInterface $em,
LoggerInterface $logger,
HttpClientInterface $machineHttpClient
) {
$this->em = $em;
$this->logger = $logger;
$this->machineHttpClient = $machineHttpClient;
}
public function handleCalendarRangeSync(CalendarRange $calendarRange, array $notification, User $user): void
{
switch ($notification['changeType']) {
case 'deleted':
// test if the notification is not linked to a Calendar
if (null !== $calendarRange->getCalendar()) {
return;
}
$calendarRange->preventEnqueueChanges = true;
$this->logger->info(self::class . ' remove a calendar range because deleted on remote calendar');
$this->em->remove($calendarRange);
break;
case 'updated':
try {
$new = $this->machineHttpClient->request(
'GET',
$notification['resource']
)->toArray();
} catch (ClientExceptionInterface $clientException) {
$this->logger->warning(self::class . ' could not retrieve event from ms graph. Already deleted ?', [
'calendarRangeId' => $calendarRange->getId(),
'remoteEventId' => $notification['resource'],
]);
throw $clientException;
}
$lastModified = RemoteEventConverter::convertStringDateWithTimezone($new['lastModifiedDateTime']);
if ($calendarRange->getRemoteAttributes()['lastModifiedDateTime'] === $lastModified->getTimestamp()) {
$this->logger->info(self::class . ' change key is equals. Source is probably a local update', [
'calendarRangeId' => $calendarRange->getId(),
'remoteEventId' => $notification['resource'],
]);
return;
}
$startDate = RemoteEventConverter::convertStringDateWithoutTimezone($new['start']['dateTime']);
$endDate = RemoteEventConverter::convertStringDateWithoutTimezone($new['end']['dateTime']);
$calendarRange
->setStartDate($startDate)->setEndDate($endDate)
->addRemoteAttributes([
'lastModifiedDateTime' => $lastModified->getTimestamp(),
'changeKey' => $new['changeKey'],
])
->preventEnqueueChanges = true;
break;
default:
throw new RuntimeException('This changeType is not suppored: ' . $notification['changeType']);
}
}
}

View File

@@ -0,0 +1,189 @@
<?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);
/*
* 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\RemoteCalendar\Connector\MSGraph\RemoteToLocalSync;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\Invite;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\RemoteEventConverter;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\UserRepositoryInterface;
use LogicException;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use function in_array;
class CalendarSyncer
{
private LoggerInterface $logger;
private HttpClientInterface $machineHttpClient;
private UserRepositoryInterface $userRepository;
public function __construct(LoggerInterface $logger, HttpClientInterface $machineHttpClient, UserRepositoryInterface $userRepository)
{
$this->logger = $logger;
$this->machineHttpClient = $machineHttpClient;
$this->userRepository = $userRepository;
}
public function handleCalendarSync(Calendar $calendar, array $notification, User $user): void
{
switch ($notification['changeType']) {
case 'deleted':
$this->handleDeleteCalendar($calendar, $notification, $user);
break;
case 'updated':
$this->handleUpdateCalendar($calendar, $notification, $user);
break;
default:
throw new RuntimeException('this change type is not supported: ' . $notification['changeType']);
}
}
private function handleDeleteCalendar(Calendar $calendar, array $notification, User $user): void
{
$calendar
->setStatus(Calendar::STATUS_CANCELED)
->setCalendarRange(null);
$calendar->preventEnqueueChanges = true;
}
private function handleUpdateCalendar(Calendar $calendar, array $notification, User $user): void
{
try {
$new = $this->machineHttpClient->request(
'GET',
$notification['resource']
)->toArray();
} catch (ClientExceptionInterface $clientException) {
$this->logger->warning(self::class . ' could not retrieve event from ms graph. Already deleted ?', [
'calendarId' => $calendar->getId(),
'remoteEventId' => $notification['resource'],
]);
throw $clientException;
}
if (false === $new['isOrganizer']) {
return;
}
$lastModified = RemoteEventConverter::convertStringDateWithTimezone(
$new['lastModifiedDateTime']
);
if ($calendar->getRemoteAttributes()['lastModifiedDateTime'] === $lastModified->getTimestamp()) {
$this->logger->info(self::class . ' change key is equals. Source is probably a local update', [
'calendarRangeId' => $calendar->getId(),
'remoteEventId' => $notification['resource'],
]);
return;
}
$this->syncAttendees($calendar, $new['attendees']);
$startDate = RemoteEventConverter::convertStringDateWithoutTimezone($new['start']['dateTime']);
$endDate = RemoteEventConverter::convertStringDateWithoutTimezone($new['end']['dateTime']);
if ($startDate->getTimestamp() !== $calendar->getStartDate()->getTimestamp()) {
$calendar->setStartDate($startDate);
}
if ($endDate->getTimestamp() !== $calendar->getEndDate()->getTimestamp()) {
$calendar->setEndDate($endDate);
}
$calendar
->addRemoteAttributes([
'lastModifiedDateTime' => $lastModified->getTimestamp(),
'changeKey' => $new['changeKey'],
])
->preventEnqueueChanges = true;
}
private function syncAttendees(Calendar $calendar, array $attendees): void
{
$emails = [];
foreach ($attendees as $attendee) {
$status = $attendee['status']['response'];
if ('organizer' === $status) {
continue;
}
$email = $attendee['emailAddress']['address'];
$emails[] = strtolower($email);
$user = $this->userRepository->findOneByUsernameOrEmail($email);
if (null === $user) {
continue;
}
if (!$calendar->isInvited($user)) {
$calendar->addUser($user);
}
$invite = $calendar->getInviteForUser($user);
switch ($status) {
// possible cases: none, organizer, tentativelyAccepted, accepted, declined, notResponded.
case 'none':
case 'notResponded':
$invite->setStatus(Invite::PENDING);
break;
case 'tentativelyAccepted':
$invite->setStatus(Invite::TENTATIVELY_ACCEPTED);
break;
case 'accepted':
$invite->setStatus(Invite::ACCEPTED);
break;
case 'declined':
$invite->setStatus(Invite::DECLINED);
break;
default:
throw new LogicException('should not happens, not implemented: ' . $status);
break;
}
}
foreach ($calendar->getUsers() as $user) {
if (!in_array(strtolower($user->getEmailCanonical()), $emails, true)) {
$calendar->removeUser($user);
}
}
}
}

View File

@@ -0,0 +1,760 @@
<?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);
/*
* 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\RemoteCalendar\Connector;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\CalendarBundle\Entity\Invite;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MapCalendarToUser;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\OnBehalfOfUserHttpClient;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\OnBehalfOfUserTokenStorage;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\RemoteEventConverter;
use Chill\CalendarBundle\Repository\CalendarRangeRepository;
use Chill\CalendarBundle\Repository\CalendarRepository;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use Exception;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_key_exists;
use function count;
class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
{
private array $cacheScheduleTimeForUser = [];
private CalendarRangeRepository $calendarRangeRepository;
private CalendarRepository $calendarRepository;
private LoggerInterface $logger;
private HttpClientInterface $machineHttpClient;
private MapCalendarToUser $mapCalendarToUser;
private RemoteEventConverter $remoteEventConverter;
private OnBehalfOfUserTokenStorage $tokenStorage;
private TranslatorInterface $translator;
private UrlGeneratorInterface $urlGenerator;
private OnBehalfOfUserHttpClient $userHttpClient;
private Security $security;
public function __construct(
CalendarRepository $calendarRepository,
CalendarRangeRepository $calendarRangeRepository,
HttpClientInterface $machineHttpClient,
MapCalendarToUser $mapCalendarToUser,
LoggerInterface $logger,
OnBehalfOfUserTokenStorage $tokenStorage,
OnBehalfOfUserHttpClient $userHttpClient,
RemoteEventConverter $remoteEventConverter,
TranslatorInterface $translator,
UrlGeneratorInterface $urlGenerator,
Security $security
) {
$this->calendarRepository = $calendarRepository;
$this->calendarRangeRepository = $calendarRangeRepository;
$this->machineHttpClient = $machineHttpClient;
$this->mapCalendarToUser = $mapCalendarToUser;
$this->logger = $logger;
$this->remoteEventConverter = $remoteEventConverter;
$this->tokenStorage = $tokenStorage;
$this->translator = $translator;
$this->urlGenerator = $urlGenerator;
$this->userHttpClient = $userHttpClient;
$this->security = $security;
}
public function countEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate): int
{
$userId = $this->mapCalendarToUser->getUserId($user);
if (null === $userId) {
return 0;
}
try {
$data = $this->userHttpClient->request(
'GET',
'users/' . $userId . '/calendarView',
[
'query' => [
'startDateTime' => $startDate->setTimezone(RemoteEventConverter::getRemoteTimeZone())->format(RemoteEventConverter::getRemoteDateTimeSimpleFormat()),
'endDateTime' => $endDate->setTimezone(RemoteEventConverter::getRemoteTimeZone())->format(RemoteEventConverter::getRemoteDateTimeSimpleFormat()),
'$count' => 'true',
'$top' => 0,
],
]
)->toArray();
} catch (ClientExceptionInterface $e) {
if (403 === $e->getResponse()->getStatusCode()) {
return count($this->getScheduleTimesForUser($user, $startDate, $endDate));
}
$this->logger->error('Could not get list of event on MSGraph', [
'error_code' => $e->getResponse()->getStatusCode(),
'error' => $e->getResponse()->getInfo(),
]);
return 0;
}
return $data['@odata.count'];
}
public function getMakeReadyResponse(string $returnPath): Response
{
return new RedirectResponse($this->urlGenerator
->generate('chill_calendar_remote_connect_azure', ['returnPath' => $returnPath]));
}
public function isReady(): bool
{
$user = $this->security->getUser();
if (!$user instanceof User) {
// this is not a user from chill. This is not the role of this class to
// restrict access, so we will just say that we do not have to do anything more
// here...
return true;
}
if (null === $this->mapCalendarToUser->getUserId($user)) {
// this user is not mapped with remote calendar. The user will have to wait for
// the next calendar subscription iteration
$this->logger->debug('mark user ready for msgraph calendar as he does not have any mapping', [
'userId' => $user->getId(),
]);
return true;
}
return $this->tokenStorage->hasToken();
}
/**
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*
* @return array|\Chill\CalendarBundle\RemoteCalendar\Model\RemoteEvent[]
*/
public function listEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate, ?int $offset = 0, ?int $limit = 50): array
{
$userId = $this->mapCalendarToUser->getUserId($user);
if (null === $userId) {
return [];
}
try {
$bareEvents = $this->userHttpClient->request(
'GET',
'users/' . $userId . '/calendarView',
[
'query' => [
'startDateTime' => $startDate->setTimezone(RemoteEventConverter::getRemoteTimeZone())->format(RemoteEventConverter::getRemoteDateTimeSimpleFormat()),
'endDateTime' => $endDate->setTimezone(RemoteEventConverter::getRemoteTimeZone())->format(RemoteEventConverter::getRemoteDateTimeSimpleFormat()),
'$select' => 'id,subject,start,end,isAllDay',
'$top' => $limit,
'$skip' => $offset,
],
]
)->toArray();
$ids = array_map(static fn ($item) => $item['id'], $bareEvents['value']);
$existingIdsInRange = $this->calendarRangeRepository->findRemoteIdsPresent($ids);
$existingIdsInCalendar = $this->calendarRepository->findRemoteIdsPresent($ids);
return array_values(
array_map(
fn ($item) => $this->remoteEventConverter->convertToRemote($item),
// filter all event to keep only the one not in range
array_filter(
$bareEvents['value'],
static fn ($item) => ((!$existingIdsInRange[$item['id']]) ?? true) && ((!$existingIdsInCalendar[$item['id']]) ?? true)
)
)
);
} catch (ClientExceptionInterface $e) {
if (403 === $e->getResponse()->getStatusCode()) {
return $this->getScheduleTimesForUser($user, $startDate, $endDate);
}
$this->logger->error('Could not get list of event on MSGraph', [
'error_code' => $e->getResponse()->getStatusCode(),
'error' => $e->getResponse()->getInfo(),
]);
return [];
}
}
public function removeCalendar(string $remoteId, array $remoteAttributes, User $user, ?CalendarRange $associatedCalendarRange = null): void
{
if ('' === $remoteId) {
return;
}
$this->removeEvent($remoteId, $user);
if (null !== $associatedCalendarRange) {
$this->syncCalendarRange($associatedCalendarRange);
}
}
public function removeCalendarRange(string $remoteId, array $remoteAttributes, User $user): void
{
if ('' === $remoteId) {
return;
}
$this->removeEvent($remoteId, $user);
}
public function syncCalendar(Calendar $calendar, string $action, ?CalendarRange $previousCalendarRange, ?User $previousMainUser, ?array $oldInvites, ?array $newInvites): void
{
/*
* cases to support:
*
* * a calendar range is created:
* * create on remote
* * if calendar range is associated: remove the range
* * a Calendar change the CalendarRange:
* * re-create the previous calendar range;
* * remove the current calendar range
* * a calendar change the mainUser
* * cancel the calendar in the previous mainUser
* * recreate the previous calendar range in the previousMainUser, if any
* * delete the current calendar range in the current mainUser, if any
* * create the calendar in the current mainUser
*
*/
if (!$calendar->hasRemoteId()) {
$this->createCalendarOnRemote($calendar);
} else {
if (null !== $previousMainUser) {
// cancel event in previousMainUserCalendar
$this->cancelOnRemote(
$calendar->getRemoteId(),
$this->translator->trans('remote_ms_graph.cancel_event_because_main_user_is_%label%', ['%label%' => $calendar->getMainUser()]),
$previousMainUser,
'calendar_' . $calendar->getRemoteId()
);
$this->createCalendarOnRemote($calendar);
} else {
$this->patchCalendarOnRemote($calendar, $newInvites);
}
}
if ($calendar->hasCalendarRange() && $calendar->getCalendarRange()->hasRemoteId()) {
$this->removeEvent(
$calendar->getCalendarRange()->getRemoteId(),
$calendar->getCalendarRange()->getUser()
);
$calendar->getCalendarRange()
->addRemoteAttributes([
'lastModifiedDateTime' => null,
'changeKey' => null,
'previousId' => $calendar->getCalendarRange()->getRemoteId(),
])
->setRemoteId('');
}
if (null !== $previousCalendarRange) {
$this->createRemoteCalendarRange($previousCalendarRange);
}
}
public function syncCalendarRange(CalendarRange $calendarRange): void
{
if ($calendarRange->hasRemoteId()) {
$this->updateRemoteCalendarRange($calendarRange);
} else {
$this->createRemoteCalendarRange($calendarRange);
}
}
public function syncInvite(Invite $invite): void
{
if ('' === $remoteId = $invite->getCalendar()->getRemoteId()) {
return;
}
if (null === $invite->getUser()) {
return;
}
if (null === $userId = $this->mapCalendarToUser->getUserId($invite->getUser())) {
return;
}
if ($invite->hasRemoteId()) {
$remoteIdAttendeeCalendar = $invite->getRemoteId();
} else {
$remoteIdAttendeeCalendar = $this->findRemoteIdOnUserCalendar($invite->getCalendar(), $invite->getUser());
$invite->setRemoteId($remoteIdAttendeeCalendar);
}
switch ($invite->getStatus()) {
case Invite::PENDING:
return;
case Invite::ACCEPTED:
$url = "/v1.0/users/{$userId}/calendar/events/{$remoteIdAttendeeCalendar}/accept";
break;
case Invite::TENTATIVELY_ACCEPTED:
$url = "/v1.0/users/{$userId}/calendar/events/{$remoteIdAttendeeCalendar}/tentativelyAccept";
break;
case Invite::DECLINED:
$url = "/v1.0/users/{$userId}/calendar/events/{$remoteIdAttendeeCalendar}/decline";
break;
default:
throw new Exception('not supported');
}
try {
$this->machineHttpClient->request(
'POST',
$url,
['json' => ['sendResponse' => true]]
);
} catch (ClientExceptionInterface $e) {
$this->logger->warning('could not update calendar range to remote', [
'exception' => $e->getTraceAsString(),
'content' => $e->getResponse()->getContent(),
'calendarRangeId' => 'invite_' . $invite->getId(),
]);
throw $e;
}
}
private function cancelOnRemote(string $remoteId, string $comment, User $user, string $identifier): void
{
$userId = $this->mapCalendarToUser->getUserId($user);
if (null === $userId) {
return;
}
try {
$this->machineHttpClient->request(
'POST',
"users/{$userId}/calendar/events/{$remoteId}/cancel",
[
'json' => ['Comment' => $comment],
]
);
} catch (ClientExceptionInterface $e) {
$this->logger->warning('could not update calendar range to remote', [
'exception' => $e->getTraceAsString(),
'content' => $e->getResponse()->getContent(),
'calendarRangeId' => $identifier,
]);
throw $e;
}
}
private function createCalendarOnRemote(Calendar $calendar): void
{
$eventData = $this->remoteEventConverter->calendarToEvent($calendar);
[
'id' => $id,
'lastModifiedDateTime' => $lastModified,
'changeKey' => $changeKey
] = $this->createOnRemote($eventData, $calendar->getMainUser(), 'calendar_' . $calendar->getId());
if (null === $id) {
return;
}
$calendar
->setRemoteId($id)
->addRemoteAttributes([
'lastModifiedDateTime' => $lastModified,
'changeKey' => $changeKey,
]);
}
/**
* @param string $identifier an identifier for logging in case of something does not work
*
* @return array{?id: string, ?lastModifiedDateTime: int, ?changeKey: string}
*/
private function createOnRemote(array $eventData, User $user, string $identifier): array
{
$userId = $this->mapCalendarToUser->getUserId($user);
if (null === $userId) {
$this->logger->warning('user does not have userId nor calendarId', [
'user_id' => $user->getId(),
'calendar_identifier' => $identifier,
]);
return ['id' => null, 'lastModifiedDateTime' => null, 'changeKey' => null];
}
try {
$event = $this->machineHttpClient->request(
'POST',
'users/' . $userId . '/calendar/events',
[
'json' => $eventData,
]
)->toArray();
} catch (ClientExceptionInterface $e) {
$this->logger->warning('could not save calendar range to remote', [
'exception' => $e->getTraceAsString(),
'content' => $e->getResponse()->getContent(),
'calendar_identifier' => $identifier,
]);
throw $e;
}
return [
'id' => $event['id'],
'lastModifiedDateTime' => $this->remoteEventConverter->getLastModifiedDate($event)->getTimestamp(),
'changeKey' => $event['changeKey'],
];
}
private function createRemoteCalendarRange(CalendarRange $calendarRange): void
{
$userId = $this->mapCalendarToUser->getUserId($calendarRange->getUser());
if (null === $userId) {
$this->logger->warning('user does not have userId nor calendarId', [
'user_id' => $calendarRange->getUser()->getId(),
'calendar_range_id' => $calendarRange->getId(),
]);
return;
}
$eventData = $this->remoteEventConverter->calendarRangeToEvent($calendarRange);
[
'id' => $id,
'lastModifiedDateTime' => $lastModified,
'changeKey' => $changeKey
] = $this->createOnRemote(
$eventData,
$calendarRange->getUser(),
'calendar_range_' . $calendarRange->getId()
);
$calendarRange->setRemoteId($id)
->addRemoteAttributes([
'lastModifiedDateTime' => $lastModified,
'changeKey' => $changeKey,
]);
}
/**
* the remoteId is not the same across different user calendars. This method allow to find
* the correct remoteId in another calendar.
*
* For achieving this, the iCalUid is used.
*/
private function findRemoteIdOnUserCalendar(Calendar $calendar, User $user): ?string
{
// find the icalUid on original user
$event = $this->getOnRemote($calendar->getMainUser(), $calendar->getRemoteId());
$userId = $this->mapCalendarToUser->getUserId($user);
if ('' === $iCalUid = ($event['iCalUId'] ?? '')) {
throw new Exception('no iCalUid for this event');
}
try {
$events = $this->machineHttpClient->request(
'GET',
"/v1.0/users/{$userId}/calendar/events",
[
'query' => [
'$select' => 'id',
'$filter' => "iCalUId eq '{$iCalUid}'",
],
]
)->toArray();
} catch (ClientExceptionInterface $clientException) {
throw $clientException;
}
if (1 !== count($events['value'])) {
throw new Exception('multiple events found with same iCalUid');
}
return $events['value'][0]['id'];
}
private function getOnRemote(User $user, string $remoteId): array
{
$userId = $this->mapCalendarToUser->getUserId($user);
if (null === $userId) {
throw new Exception(
sprintf(
'no remote calendar for this user: %s, remoteid: %s',
$user->getId(),
$remoteId
)
);
}
try {
return $this->machineHttpClient->request(
'GET',
'users/' . $userId . '/calendar/events/' . $remoteId
)->toArray();
} catch (ClientExceptionInterface $e) {
$this->logger->warning('Could not get event from calendar', [
'remoteId' => $remoteId,
]);
throw $e;
}
}
private function getScheduleTimesForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate): array
{
$userId = $this->mapCalendarToUser->getUserId($user);
if (array_key_exists($userId, $this->cacheScheduleTimeForUser)) {
return $this->cacheScheduleTimeForUser[$userId];
}
if (null === $userId) {
return [];
}
if (null === $user->getEmailCanonical() || '' === $user->getEmailCanonical()) {
return [];
}
$body = [
'schedules' => [$user->getEmailCanonical()],
'startTime' => [
'dateTime' => ($startDate->setTimezone(RemoteEventConverter::getRemoteTimeZone())->format(RemoteEventConverter::getRemoteDateTimeSimpleFormat())),
'timeZone' => 'UTC',
],
'endTime' => [
'dateTime' => ($endDate->setTimezone(RemoteEventConverter::getRemoteTimeZone())->format(RemoteEventConverter::getRemoteDateTimeSimpleFormat())),
'timeZone' => 'UTC',
],
];
try {
$response = $this->userHttpClient->request('POST', 'users/' . $userId . '/calendar/getSchedule', [
'json' => $body,
])->toArray();
} catch (ClientExceptionInterface $e) {
$this->logger->debug('Could not get schedule on MSGraph', [
'error_code' => $e->getResponse()->getStatusCode(),
'error' => $e->getResponse()->getInfo(),
]);
return [];
}
$this->cacheScheduleTimeForUser[$userId] = array_map(
fn ($item) => $this->remoteEventConverter->convertAvailabilityToRemoteEvent($item),
$response['value'][0]['scheduleItems']
);
return $this->cacheScheduleTimeForUser[$userId];
}
private function patchCalendarOnRemote(Calendar $calendar, array $newInvites): void
{
$eventDatas = [];
$eventDatas[] = $this->remoteEventConverter->calendarToEvent($calendar);
if (0 < count($newInvites)) {
// it seems that invitaiton are always send, even if attendee changes are mixed with other datas
// $eventDatas[] = $this->remoteEventConverter->calendarToEventAttendeesOnly($calendar);
}
foreach ($eventDatas as $eventData) {
[
'id' => $id,
'lastModifiedDateTime' => $lastModified,
'changeKey' => $changeKey
] = $this->patchOnRemote(
$calendar->getRemoteId(),
$eventData,
$calendar->getMainUser(),
'calendar_' . $calendar->getId()
);
$calendar->addRemoteAttributes([
'lastModifiedDateTime' => $lastModified,
'changeKey' => $changeKey,
]);
}
}
/**
* @param string $identifier an identifier for logging in case of something does not work
*
* @return array{?id: string, ?lastModifiedDateTime: int, ?changeKey: string}
*/
private function patchOnRemote(string $remoteId, array $eventData, User $user, string $identifier): array
{
$userId = $this->mapCalendarToUser->getUserId($user);
if (null === $userId) {
$this->logger->warning('user does not have userId nor calendarId', [
'user_id' => $user->getId(),
'calendar_identifier' => $identifier,
]);
return ['id' => null, 'lastModifiedDateTime' => null, 'changeKey' => null];
}
try {
$event = $this->machineHttpClient->request(
'PATCH',
'users/' . $userId . '/calendar/events/' . $remoteId,
[
'json' => $eventData,
]
)->toArray();
} catch (ClientExceptionInterface $e) {
$this->logger->warning('could not update calendar range to remote', [
'exception' => $e->getTraceAsString(),
'calendarRangeId' => $identifier,
]);
throw $e;
}
return [
'id' => $event['id'],
'lastModifiedDateTime' => $this->remoteEventConverter->getLastModifiedDate($event)->getTimestamp(),
'changeKey' => $event['changeKey'],
];
}
private function removeEvent($remoteId, User $user): void
{
$userId = $this->mapCalendarToUser->getUserId($user);
try {
$this->machineHttpClient->request(
'DELETE',
'users/' . $userId . '/calendar/events/' . $remoteId
);
} catch (ClientExceptionInterface $e) {
$this->logger->warning('could not remove event from calendar', [
'event_remote_id' => $remoteId,
'user_id' => $user->getId(),
]);
}
}
private function updateRemoteCalendarRange(CalendarRange $calendarRange): void
{
$userId = $this->mapCalendarToUser->getUserId($calendarRange->getUser());
$calendarId = $this->mapCalendarToUser->getCalendarId($calendarRange->getUser());
if (null === $userId || null === $calendarId) {
$this->logger->warning('user does not have userId nor calendarId', [
'user_id' => $calendarRange->getUser()->getId(),
'calendar_range_id' => $calendarRange->getId(),
]);
return;
}
try {
$event = $this->machineHttpClient->request(
'GET',
'users/' . $userId . '/calendar/events/' . $calendarRange->getRemoteId()
)->toArray();
} catch (ClientExceptionInterface $e) {
$this->logger->warning('Could not get event from calendar', [
'calendar_range_id' => $calendarRange->getId(),
'calendar_range_remote_id' => $calendarRange->getRemoteId(),
]);
throw $e;
}
if ($this->remoteEventConverter->getLastModifiedDate($event)->getTimestamp() > $calendarRange->getUpdatedAt()->getTimestamp()) {
$this->logger->info('Skip updating as the lastModified date seems more fresh than the database one', [
'calendar_range_id' => $calendarRange->getId(),
'calendar_range_remote_id' => $calendarRange->getRemoteId(),
'db_last_updated' => $calendarRange->getUpdatedAt()->getTimestamp(),
'remote_last_updated' => $this->remoteEventConverter->getLastModifiedDate($event)->getTimestamp(),
]);
return;
}
$eventData = $this->remoteEventConverter->calendarRangeToEvent($calendarRange);
try {
$event = $this->machineHttpClient->request(
'PATCH',
'users/' . $userId . '/calendar/events/' . $calendarRange->getRemoteId(),
[
'json' => $eventData,
]
)->toArray();
} catch (ClientExceptionInterface $e) {
$this->logger->warning('could not update calendar range to remote', [
'exception' => $e->getTraceAsString(),
'calendarRangeId' => $calendarRange->getId(),
]);
throw $e;
}
$calendarRange
->addRemoteAttributes([
'lastModifiedDateTime' => $this->remoteEventConverter->getLastModifiedDate($event)->getTimestamp(),
'changeKey' => $event['changeKey'],
]);
}
}

View File

@@ -0,0 +1,70 @@
<?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);
/*
* 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\RemoteCalendar\Connector;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\CalendarBundle\Entity\Invite;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use LogicException;
use Symfony\Component\HttpFoundation\Response;
class NullRemoteCalendarConnector implements RemoteCalendarConnectorInterface
{
public function countEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate): int
{
return 0;
}
public function getMakeReadyResponse(string $returnPath): Response
{
throw new LogicException('As this connector is always ready, this method should not be called');
}
public function isReady(): bool
{
return true;
}
public function listEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate, ?int $offset = 0, ?int $limit = 50): array
{
return [];
}
public function removeCalendar(string $remoteId, array $remoteAttributes, User $user, ?CalendarRange $associatedCalendarRange = null): void
{
}
public function removeCalendarRange(string $remoteId, array $remoteAttributes, User $user): void
{
}
public function syncCalendar(Calendar $calendar, string $action, ?CalendarRange $previousCalendarRange, ?User $previousMainUser, ?array $oldInvites, ?array $newInvites): void
{
}
public function syncCalendarRange(CalendarRange $calendarRange): void
{
}
public function syncInvite(Invite $invite): void
{
}
}

View File

@@ -0,0 +1,63 @@
<?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);
/*
* 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\RemoteCalendar\Connector;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\CalendarBundle\Entity\Invite;
use Chill\CalendarBundle\RemoteCalendar\Model\RemoteEvent;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use Symfony\Component\HttpFoundation\Response;
interface RemoteCalendarConnectorInterface
{
public function countEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate): int;
/**
* Return a response, more probably a RedirectResponse, where the user
* will be able to fullfill requirements to prepare this connector and
* make it ready.
*/
public function getMakeReadyResponse(string $returnPath): Response;
/**
* Return true if the connector is ready to act as a proxy for reading
* remote calendars.
*/
public function isReady(): bool;
/**
* @return array|RemoteEvent[]
*/
public function listEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate, ?int $offset = 0, ?int $limit = 50): array;
public function removeCalendar(string $remoteId, array $remoteAttributes, User $user, ?CalendarRange $associatedCalendarRange = null): void;
public function removeCalendarRange(string $remoteId, array $remoteAttributes, User $user): void;
/**
* @param array<array{inviteId: int, userId: int, userEmail: int, userLabel: string}> $oldInvites
*/
public function syncCalendar(Calendar $calendar, string $action, ?CalendarRange $previousCalendarRange, ?User $previousMainUser, ?array $oldInvites, ?array $newInvites): void;
public function syncCalendarRange(CalendarRange $calendarRange): void;
public function syncInvite(Invite $invite): void;
}

View File

@@ -0,0 +1,82 @@
<?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);
/*
* 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\RemoteCalendar\DependencyInjection;
use Chill\CalendarBundle\Command\AzureGrantAdminConsentAndAcquireToken;
use Chill\CalendarBundle\Command\MapAndSubscribeUserCalendarCommand;
use Chill\CalendarBundle\Controller\RemoteCalendarConnectAzureController;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MachineHttpClient;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MachineTokenStorage;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraphRemoteCalendarConnector;
use Chill\CalendarBundle\RemoteCalendar\Connector\NullRemoteCalendarConnector;
use Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface;
use RuntimeException;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use TheNetworg\OAuth2\Client\Provider\Azure;
class RemoteCalendarCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$config = $container->getParameter('chill_calendar');
$connector = null;
if (!$config['remote_calendars_sync']['enabled']) {
$connector = NullRemoteCalendarConnector::class;
}
if ($config['remote_calendars_sync']['microsoft_graph']['enabled']) {
$connector = MSGraphRemoteCalendarConnector::class;
$container->setAlias(HttpClientInterface::class . ' $machineHttpClient', MachineHttpClient::class);
} else {
// remove services which cannot be loaded
$container->removeDefinition(MapAndSubscribeUserCalendarCommand::class);
$container->removeDefinition(AzureGrantAdminConsentAndAcquireToken::class);
$container->removeDefinition(RemoteCalendarConnectAzureController::class);
$container->removeDefinition(MachineTokenStorage::class);
$container->removeDefinition(MachineHttpClient::class);
$container->removeDefinition(MSGraphRemoteCalendarConnector::class);
}
if (!$container->hasAlias(Azure::class) && $container->hasDefinition('knpu.oauth2.client.azure')) {
$container->setAlias(Azure::class, 'knpu.oauth2.provider.azure');
}
if (null === $connector) {
throw new RuntimeException('Could not configure remote calendar');
}
foreach ([
NullRemoteCalendarConnector::class,
MSGraphRemoteCalendarConnector::class, ] as $serviceId) {
if ($connector === $serviceId) {
$container->getDefinition($serviceId)
->setDecoratedService(RemoteCalendarConnectorInterface::class);
} else {
// keep the container lighter by removing definitions
if ($container->hasDefinition($serviceId)) {
$container->removeDefinition($serviceId);
}
}
}
}
}

View File

@@ -0,0 +1,62 @@
<?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);
/*
* 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\RemoteCalendar\Model;
use DateTimeImmutable;
use Symfony\Component\Serializer\Annotation as Serializer;
class RemoteEvent
{
public string $description;
/**
* @Serializer\Groups({"read"})
*/
public DateTimeImmutable $endDate;
/**
* @Serializer\Groups({"read"})
*/
public string $id;
/**
* @Serializer\Groups({"read"})
*/
public bool $isAllDay;
/**
* @Serializer\Groups({"read"})
*/
public DateTimeImmutable $startDate;
/**
* @Serializer\Groups({"read"})
*/
public string $title;
public function __construct(string $id, string $title, string $description, DateTimeImmutable $startDate, DateTimeImmutable $endDate, bool $isAllDay = false)
{
$this->id = $id;
$this->title = $title;
$this->description = $description;
$this->startDate = $startDate;
$this->endDate = $endDate;
$this->isAllDay = $isAllDay;
}
}

View File

@@ -0,0 +1,230 @@
<?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);
/*
* 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\Repository;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\AccompanyingPeriodACLAwareRepositoryInterface;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
class CalendarACLAwareRepository implements CalendarACLAwareRepositoryInterface
{
private AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository;
private EntityManagerInterface $em;
public function __construct(
AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository,
EntityManagerInterface $em
) {
$this->accompanyingPeriodACLAwareRepository = $accompanyingPeriodACLAwareRepository;
$this->em = $em;
}
public function buildQueryByAccompanyingPeriod(AccompanyingPeriod $period, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): QueryBuilder
{
$qb = $this->em->createQueryBuilder();
$qb->from(Calendar::class, 'c');
$andX = $qb->expr()->andX($qb->expr()->eq('c.accompanyingPeriod', ':period'));
$qb->setParameter('period', $period);
if (null !== $startDate) {
$andX->add($qb->expr()->gte('c.startDate', ':startDate'));
$qb->setParameter('startDate', $startDate);
}
if (null !== $endDate) {
$andX->add($qb->expr()->lte('c.endDate', ':endDate'));
$qb->setParameter('endDate', $endDate);
}
$qb->where($andX);
return $qb;
}
public function buildQueryByAccompanyingPeriodIgnoredByDates(AccompanyingPeriod $period, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): QueryBuilder
{
$qb = $this->em->createQueryBuilder();
$qb->from(Calendar::class, 'c');
$andX = $qb->expr()->andX($qb->expr()->eq('c.accompanyingPeriod', ':period'));
$qb->setParameter('period', $period);
if (null !== $startDate) {
$andX->add($qb->expr()->lt('c.startDate', ':startDate'));
$qb->setParameter('startDate', $startDate);
}
if (null !== $endDate) {
$andX->add($qb->expr()->gt('c.endDate', ':endDate'));
$qb->setParameter('endDate', $endDate);
}
$qb->where($andX);
return $qb;
}
/**
* Base implementation. The list of allowed accompanying period is retrieved "manually" from @see{AccompanyingPeriodACLAwareRepository}.
*/
public function buildQueryByPerson(Person $person, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): QueryBuilder
{
$qb = $this->em->createQueryBuilder()
->from(Calendar::class, 'c');
$this->addQueryByPersonWithoutDate($qb, $person);
// filter by date
if (null !== $startDate) {
$qb->andWhere($qb->expr()->gte('c.startDate', ':startDate'))
->setParameter('startDate', $startDate);
}
if (null !== $endDate) {
$qb->andWhere($qb->expr()->lte('c.endDate', ':endDate'))
->setParameter('endDate', $endDate);
}
return $qb;
}
/**
* Base implementation. The list of allowed accompanying period is retrieved "manually" from @see{AccompanyingPeriodACLAwareRepository}.
*/
public function buildQueryByPersonIgnoredByDates(Person $person, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): QueryBuilder
{
$qb = $this->em->createQueryBuilder()
->from(Calendar::class, 'c');
$this->addQueryByPersonWithoutDate($qb, $person);
// filter by date
if (null !== $startDate) {
$qb->andWhere($qb->expr()->lt('c.startDate', ':startDate'))
->setParameter('startDate', $startDate);
}
if (null !== $endDate) {
$qb->andWhere($qb->expr()->gt('c.endDate', ':endDate'))
->setParameter('endDate', $endDate);
}
return $qb;
}
public function countByAccompanyingPeriod(AccompanyingPeriod $period, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): int
{
$qb = $this->buildQueryByAccompanyingPeriod($period, $startDate, $endDate)->select('count(c)');
return $qb->getQuery()->getSingleScalarResult();
}
public function countByPerson(Person $person, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): int
{
return $this->buildQueryByPerson($person, $startDate, $endDate)
->select('COUNT(c)')
->getQuery()
->getSingleScalarResult();
}
public function countIgnoredByAccompanyingPeriod(AccompanyingPeriod $period, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): int
{
$qb = $this->buildQueryByAccompanyingPeriodIgnoredByDates($period, $startDate, $endDate)->select('count(c)');
return $qb->getQuery()->getSingleScalarResult();
}
public function countIgnoredByPerson(Person $person, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): int
{
return $this->buildQueryByPersonIgnoredByDates($person, $startDate, $endDate)
->select('COUNT(c)')
->getQuery()
->getSingleScalarResult();
}
/**
* @return array|Calendar[]
*/
public function findByAccompanyingPeriod(AccompanyingPeriod $period, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate, ?array $orderBy = [], ?int $offset = null, ?int $limit = null): array
{
$qb = $this->buildQueryByAccompanyingPeriod($period, $startDate, $endDate)->select('c');
foreach ($orderBy as $sort => $order) {
$qb->addOrderBy('c.' . $sort, $order);
}
if (null !== $offset) {
$qb->setFirstResult($offset);
}
if (null !== $limit) {
$qb->setMaxResults($limit);
}
return $qb->getQuery()->getResult();
}
public function findByPerson(Person $person, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate, ?array $orderBy = [], ?int $offset = null, ?int $limit = null): array
{
$qb = $this->buildQueryByPerson($person, $startDate, $endDate)
->select('c');
foreach ($orderBy as $sort => $order) {
$qb->addOrderBy('c.' . $sort, $order);
}
if (null !== $offset) {
$qb->setFirstResult($offset);
}
if (null !== $limit) {
$qb->setMaxResults($limit);
}
return $qb->getQuery()->getResult();
}
private function addQueryByPersonWithoutDate(QueryBuilder $qb, Person $person): void
{
// find the reachable accompanying periods for person
$periods = $this->accompanyingPeriodACLAwareRepository->findByPerson($person, AccompanyingPeriodVoter::SEE);
$qb
->where(
$qb->expr()->orX(
// the calendar where the person is the main person:
$qb->expr()->eq('c.person', ':person'),
// when the calendar is in a reachable period, and contains person
$qb->expr()->andX(
$qb->expr()->in('c.accompanyingPeriod', ':periods'),
$qb->expr()->isMemberOf(':person', 'c.persons')
)
)
)
->setParameter('person', $person)
->setParameter('periods', $periods);
}
}

View File

@@ -0,0 +1,63 @@
<?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);
/*
* 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\Repository;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use DateTimeImmutable;
interface CalendarACLAwareRepositoryInterface
{
public function countByAccompanyingPeriod(AccompanyingPeriod $period, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): int;
/**
* Return the number or calendars associated with a person. See condition on @see{self::findByPerson}.
*/
public function countByPerson(Person $person, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): int;
/**
* Return the number or calendars associated with an accompanyign period which **does not** match the date conditions.
*/
public function countIgnoredByAccompanyingPeriod(AccompanyingPeriod $period, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): int;
/**
* Return the number or calendars associated with a person which **does not** match the date conditions.
*
* See condition on @see{self::findByPerson}.
*/
public function countIgnoredByPerson(Person $person, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate): int;
/**
* @return array|Calendar[]
*/
public function findByAccompanyingPeriod(AccompanyingPeriod $period, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate, ?array $orderBy = [], ?int $offset = null, ?int $limit = null): array;
/**
* Return all the calendars which are associated with a person, either on @see{Calendar::person} or within.
*
* @see{Calendar::persons}. The calendar may be associated with a person, or an accompanyingPeriod.
*
* The method may assume that the user is allowed to see the person, but must check that the user is allowed
* to see the calendar's accompanyingPeriod, if any.
*
* @return array|Calendar[]
*/
public function findByPerson(Person $person, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate, ?array $orderBy = [], ?int $offset = null, ?int $limit = null): array;
}

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/*
* 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\Repository;
use Chill\CalendarBundle\Entity\CalendarDoc;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;
class CalendarDocRepository implements ObjectRepository, CalendarDocRepositoryInterface
{
private EntityRepository $repository;
public function __construct(EntityManagerInterface $entityManager)
{
$this->repository = $entityManager->getRepository($this->getClassName());
}
public function find($id): ?CalendarDoc
{
return $this->repository->find($id);
}
public function findAll(): array
{
return $this->repository->findAll();
}
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null)
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
public function findOneBy(array $criteria): ?CalendarDoc
{
return $this->findOneBy($criteria);
}
public function getClassName()
{
return CalendarDoc::class;
}
}

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/*
* 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\Repository;
use Chill\CalendarBundle\Entity\CalendarDoc;
interface CalendarDocRepositoryInterface
{
public function find($id): ?CalendarDoc;
/**
* @return array|CalendarDoc[]
*/
public function findAll(): array;
/**
* @return array|CalendarDoc[]
*/
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null);
public function findOneBy(array $criteria): ?CalendarDoc;
public function getClassName();
}

View File

@@ -1,59 +1,170 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Repository;
use Chill\CalendarBundle\Entity\CalendarRange;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ObjectRepository;
use function count;
/**
* @method CalendarRange|null find($id, $lockMode = null, $lockVersion = null)
* @method CalendarRange|null findOneBy(array $criteria, array $orderBy = null)
* @method CalendarRange[] findAll()
* @method CalendarRange[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CalendarRangeRepository extends ServiceEntityRepository
class CalendarRangeRepository implements ObjectRepository
{
public function __construct(ManagerRegistry $registry)
private EntityManagerInterface $em;
private EntityRepository $repository;
public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct($registry, CalendarRange::class);
$this->em = $entityManager;
$this->repository = $entityManager->getRepository(CalendarRange::class);
}
// /**
// * @return CalendarRange[] Returns an array of CalendarRange objects
// */
/*
public function findByExampleField($value)
public function countByAvailableRangesForUser(User $user, DateTimeImmutable $from, DateTimeImmutable $to): int
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->orderBy('c.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
return $this->buildQueryAvailableRangesForUser($user, $from, $to)
->select('COUNT(cr)')
->getQuery()->getSingleScalarResult();
}
*/
/*
public function findOneBySomeField($value): ?CalendarRange
public function find($id): ?CalendarRange
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
return $this->repository->find($id);
}
/**
* @return array|CalendarRange[]
*/
public function findAll(): array
{
return $this->repository->findAll();
}
/**
* @return array|CalendarRange[]
*/
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null)
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
/**
* @return array|CalendarRange[]
*/
public function findByAvailableRangesForUser(
User $user,
DateTimeImmutable $from,
DateTimeImmutable $to,
?int $limit = null,
?int $offset = null
): array {
$qb = $this->buildQueryAvailableRangesForUser($user, $from, $to);
if (null !== $limit) {
$qb->setMaxResults($limit);
}
if (null !== $offset) {
$qb->setFirstResult($offset);
}
return $qb->getQuery()->getResult();
}
public function findOneBy(array $criteria): ?CalendarRange
{
return $this->repository->findOneBy($criteria);
}
/**
* Given a list of remote ids, return an array where
* keys are the remoteIds, and value is a boolean, true if the
* id is present in database.
*
* @param array<int, string>|list<string> $remoteIds
*
* @return array<string, bool>
*/
public function findRemoteIdsPresent(array $remoteIds): array
{
if (0 === count($remoteIds)) {
return [];
}
$sql = 'SELECT
sq.remoteId as remoteid,
EXISTS (SELECT 1 FROM chill_calendar.calendar_range cr WHERE cr.remoteId = sq.remoteId) AS present
FROM
(
VALUES %remoteIds%
) AS sq(remoteId);
';
$remoteIdsStr = implode(
', ',
array_fill(0, count($remoteIds), '((?))')
);
$rsm = new ResultSetMapping();
$rsm
->addScalarResult('remoteid', 'remoteId', Types::STRING)
->addScalarResult('present', 'present', Types::BOOLEAN);
$rows = $this->em
->createNativeQuery(
strtr($sql, ['%remoteIds%' => $remoteIdsStr]),
$rsm
)
->setParameters(array_values($remoteIds))
->getResult();
$results = [];
foreach ($rows as $r) {
$results[$r['remoteId']] = $r['present'];
}
return $results;
}
public function getClassName(): string
{
return CalendarRange::class;
}
private function buildQueryAvailableRangesForUser(User $user, DateTimeImmutable $from, DateTimeImmutable $to): QueryBuilder
{
$qb = $this->repository->createQueryBuilder('cr');
$qb->leftJoin('cr.calendar', 'calendar');
return $qb
->where(
$qb->expr()->andX(
$qb->expr()->eq('cr.user', ':user'),
$qb->expr()->gte('cr.startDate', ':startDate'),
$qb->expr()->lte('cr.endDate', ':endDate'),
$qb->expr()->isNull('calendar')
)
)
->setParameters([
'user' => $user,
'startDate' => $from,
'endDate' => $to,
]);
}
}

View File

@@ -1,63 +1,231 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Repository;
use Chill\CalendarBundle\Entity\Calendar;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use DateTimeImmutable;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ObjectRepository;
use function count;
/**
* @method Calendar|null find($id, $lockMode = null, $lockVersion = null)
* @method Calendar|null findOneBy(array $criteria, array $orderBy = null)
* @method Calendar[] findAll()
* @method Calendar[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CalendarRepository extends ServiceEntityRepository
class CalendarRepository implements ObjectRepository
{
// private EntityRepository $repository;
private EntityManagerInterface $em;
public function __construct(ManagerRegistry $registry)
private EntityRepository $repository;
public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct($registry, Calendar::class);
// $this->repository = $entityManager->getRepository(AccompanyingPeriodWork::class);
$this->repository = $entityManager->getRepository(Calendar::class);
$this->em = $entityManager;
}
// /**
// * @return Calendar[] Returns an array of Calendar objects
// */
/*
public function findByExampleField($value)
public function countByAccompanyingPeriod(AccompanyingPeriod $period): int
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->orderBy('c.id', 'ASC')
->setMaxResults(10)
return $this->repository->count(['accompanyingPeriod' => $period]);
}
public function countByUser(User $user, DateTimeImmutable $from, DateTimeImmutable $to): int
{
return $this->buildQueryByUser($user, $from, $to)
->select('COUNT(c)')
->getQuery()
->getResult()
;
->getSingleScalarResult();
}
*/
/*
public function findOneBySomeField($value): ?Calendar
public function createQueryBuilder(string $alias, ?string $indexBy = null): QueryBuilder
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
return $this->repository->createQueryBuilder($alias, $indexBy);
}
public function find($id): ?Calendar
{
return $this->repository->find($id);
}
/**
* @return array|Calendar[]
*/
public function findAll(): array
{
return $this->repository->findAll();
}
/**
* @return array|Calendar[]
*/
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
/**
* @return array|Calendar[]
*/
public function findByAccompanyingPeriod(AccompanyingPeriod $period, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
{
return $this->findBy(
[
'accompanyingPeriod' => $period,
],
$orderBy,
$limit,
$orderBy
);
}
public function findByNotificationAvailable(DateTimeImmutable $startDate, DateTimeImmutable $endDate, ?int $limit = null, ?int $offset = null): array
{
$qb = $this->queryByNotificationAvailable($startDate, $endDate)->select('c');
if (null !== $limit) {
$qb->setMaxResults($limit);
}
if (null !== $offset) {
$qb->setFirstResult($offset);
}
return $qb->getQuery()->getResult();
}
/**
* @return array|Calendar[]
*/
public function findByUser(User $user, DateTimeImmutable $from, DateTimeImmutable $to, ?int $limit = null, ?int $offset = null): array
{
$qb = $this->buildQueryByUser($user, $from, $to)->select('c');
if (null !== $limit) {
$qb->setMaxResults($limit);
}
if (null !== $offset) {
$qb->setFirstResult($offset);
}
return $qb->getQuery()->getResult();
}
public function findOneBy(array $criteria): ?Calendar
{
return $this->repository->findOneBy($criteria);
}
/**
* Given a list of remote ids, return an array where
* keys are the remoteIds, and value is a boolean, true if the
* id is present in database.
*
* @param array<int, string>|list<string> $remoteIds
*
* @return array<string, bool>
*/
public function findRemoteIdsPresent(array $remoteIds): array
{
if (0 === count($remoteIds)) {
return [];
}
$remoteIdsStr = implode(
', ',
array_fill(0, count($remoteIds), '((?))')
);
$sql = "SELECT
sq.remoteId as remoteid,
EXISTS (SELECT 1 FROM chill_calendar.calendar c WHERE c.remoteId = sq.remoteId) AS present
FROM
(
VALUES {$remoteIdsStr}
) AS sq(remoteId);
";
$rsm = new ResultSetMapping();
$rsm
->addScalarResult('remoteid', 'remoteId', Types::STRING)
->addScalarResult('present', 'present', Types::BOOLEAN);
$rows = $this->em
->createNativeQuery(
$sql,
$rsm
)
->setParameters(array_values($remoteIds))
->getResult();
$results = [];
foreach ($rows as $r) {
$results[$r['remoteId']] = $r['present'];
}
return $results;
}
public function getClassName()
{
return Calendar::class;
}
private function buildQueryByUser(User $user, DateTimeImmutable $from, DateTimeImmutable $to): QueryBuilder
{
$qb = $this->repository->createQueryBuilder('c');
return $qb
->where(
$qb->expr()->andX(
$qb->expr()->eq('c.mainUser', ':user'),
$qb->expr()->gte('c.startDate', ':startDate'),
$qb->expr()->lte('c.endDate', ':endDate'),
)
)
->setParameters([
'user' => $user,
'startDate' => $from,
'endDate' => $to,
]);
}
private function queryByNotificationAvailable(DateTimeImmutable $startDate, DateTimeImmutable $endDate): QueryBuilder
{
$qb = $this->repository->createQueryBuilder('c');
$qb->where(
$qb->expr()->andX(
$qb->expr()->eq('c.sendSMS', ':true'),
$qb->expr()->gte('c.startDate', ':startDate'),
$qb->expr()->lt('c.startDate', ':endDate'),
$qb->expr()->orX(
$qb->expr()->eq('c.smsStatus', ':pending'),
$qb->expr()->eq('c.smsStatus', ':cancel_pending')
)
)
);
$qb->setParameters([
'true' => true,
'startDate' => $startDate,
'endDate' => $endDate,
'pending' => Calendar::SMS_PENDING,
'cancel_pending' => Calendar::SMS_CANCEL_PENDING,
]);
return $qb;
}
}

View File

@@ -1,14 +1,14 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Repository;
use Chill\CalendarBundle\Entity\CancelReason;

View File

@@ -1,59 +1,58 @@
<?php
/**
declare(strict_types=1);
/*
* 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\Repository;
use Chill\CalendarBundle\Entity\Invite;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;
/**
* @method Invite|null find($id, $lockMode = null, $lockVersion = null)
* @method Invite|null findOneBy(array $criteria, array $orderBy = null)
* @method Invite[] findAll()
* @method Invite[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class InviteRepository extends ServiceEntityRepository
class InviteRepository implements ObjectRepository
{
public function __construct(ManagerRegistry $registry)
private EntityRepository $entityRepository;
public function __construct(EntityManagerInterface $em)
{
parent::__construct($registry, Invite::class);
$this->entityRepository = $em->getRepository(Invite::class);
}
// /**
// * @return Invite[] Returns an array of Invite objects
// */
/*
public function findByExampleField($value)
public function find($id): ?Invite
{
return $this->createQueryBuilder('i')
->andWhere('i.exampleField = :val')
->setParameter('val', $value)
->orderBy('i.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
return $this->entityRepository->find($id);
}
*/
/*
public function findOneBySomeField($value): ?Invite
{
return $this->createQueryBuilder('i')
->andWhere('i.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
/**
* @return array|Invite[]
*/
public function findAll(): array
{
return $this->entityRepository->findAll();
}
/**
* @return array|Invite[]
*/
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null)
{
return $this->entityRepository->findBy($criteria, $orderBy, $limit, $offset);
}
public function findOneBy(array $criteria): ?Invite
{
return $this->entityRepository->findOneBy($criteria);
}
public function getClassName(): string
{
return Invite::class;
}
}

View File

@@ -3,12 +3,41 @@ services:
Chill\CalendarBundle\Repository\:
autowire: true
autoconfigure: true
resource: '../../Repository/'
tags:
- { name: 'doctrine.repository_service' }
Chill\CalendarBundle\Menu\:
autowire: true
autoconfigure: true
resource: '../../Menu/'
tags: ['chill.menu_builder']
Chill\CalendarBundle\Command\:
autowire: true
autoconfigure: true
resource: '../../Command/'
Chill\CalendarBundle\Messenger\:
autowire: true
autoconfigure: true
resource: '../../Messenger/'
Chill\CalendarBundle\Command\AzureGrantAdminConsentAndAcquireToken:
autoconfigure: true
autowire: true
arguments:
$azure: '@knpu.oauth2.provider.azure'
tags: ['console.command']
Chill\CalendarBundle\Security\:
autoconfigure: true
autowire: true
resource: '../../Security/'
Chill\CalendarBundle\Service\:
autoconfigure: true
autowire: true
resource: '../../Service/'
Chill\CalendarBundle\Service\ShortMessageForCalendarBuilderInterface:
alias: Chill\CalendarBundle\Service\DefaultShortMessageForCalendarBuider

View File

@@ -7,4 +7,37 @@ services:
name: 'doctrine.orm.entity_listener'
event: 'postPersist'
entity: 'Chill\ActivityBundle\Entity\Activity'
Chill\CalendarBundle\Messenger\Doctrine\CalendarRangeEntityListener:
autowire: true
autoconfigure: true
tags:
-
name: 'doctrine.orm.entity_listener'
event: 'postPersist'
entity: 'Chill\CalendarBundle\Entity\CalendarRange'
-
name: 'doctrine.orm.entity_listener'
event: 'postUpdate'
entity: 'Chill\CalendarBundle\Entity\CalendarRange'
-
name: 'doctrine.orm.entity_listener'
event: 'postRemove'
entity: 'Chill\CalendarBundle\Entity\CalendarRange'
Chill\CalendarBundle\Messenger\Doctrine\CalendarEntityListener:
autowire: true
autoconfigure: true
tags:
-
name: 'doctrine.orm.entity_listener'
event: 'postPersist'
entity: 'Chill\CalendarBundle\Entity\Calendar'
-
name: 'doctrine.orm.entity_listener'
event: 'postUpdate'
entity: 'Chill\CalendarBundle\Entity\Calendar'
-
name: 'doctrine.orm.entity_listener'
event: 'postRemove'
entity: 'Chill\CalendarBundle\Entity\Calendar'

View File

@@ -0,0 +1,118 @@
services:
## Indicators
chill.calendar.export.count_calendars:
class: Chill\CalendarBundle\Export\Export\CountCalendars
autowire: true
autoconfigure: true
tags:
- { name: chill.export, alias: count_calendars }
chill.calendar.export.average_duration_calendars:
class: Chill\CalendarBundle\Export\Export\StatCalendarAvgDuration
autowire: true
autoconfigure: true
tags:
- { name: chill.export, alias: average_duration_calendars }
chill.calendar.export.sum_duration_calendars:
class: Chill\CalendarBundle\Export\Export\StatCalendarSumDuration
autowire: true
autoconfigure: true
tags:
- { name: chill.export, alias: sum_duration_calendars }
## Filters
chill.calendar.export.agent_filter:
class: Chill\CalendarBundle\Export\Filter\AgentFilter
autowire: true
autoconfigure: true
tags:
- { name: chill.export_filter, alias: agent_filter }
chill.calendar.export.job_filter:
class: Chill\CalendarBundle\Export\Filter\JobFilter
autowire: true
autoconfigure: true
tags:
- { name: chill.export_filter, alias: job_filter }
chill.calendar.export.scope_filter:
class: Chill\CalendarBundle\Export\Filter\ScopeFilter
autowire: true
autoconfigure: true
tags:
- { name: chill.export_filter, alias: scope_filter }
chill.calendar.export.between_dates_filter:
class: Chill\CalendarBundle\Export\Filter\BetweenDatesFilter
autowire: true
autoconfigure: true
tags:
- { name: chill.export_filter, alias: between_dates_filter }
chill.calendar.export.calendar_range_filter:
class: Chill\CalendarBundle\Export\Filter\CalendarRangeFilter
autowire: true
autoconfigure: true
tags:
- { name: chill.export_filter, alias: calendar_range_filter }
## Aggregator
chill.calendar.export.agent_aggregator:
class: Chill\CalendarBundle\Export\Aggregator\AgentAggregator
autowire: true
autoconfigure: true
tags:
- { name: chill.export_aggregator, alias: agent_aggregator }
chill.calendar.export.job_aggregator:
class: Chill\CalendarBundle\Export\Aggregator\JobAggregator
autowire: true
autoconfigure: true
tags:
- { name: chill.export_aggregator, alias: job_aggregator }
chill.calendar.export.scope_aggregator:
class: Chill\CalendarBundle\Export\Aggregator\ScopeAggregator
autowire: true
autoconfigure: true
tags:
- { name: chill.export_aggregator, alias: scope_aggregator }
chill.calendar.export.location_type_aggregator:
class: Chill\CalendarBundle\Export\Aggregator\LocationTypeAggregator
autowire: true
autoconfigure: true
tags:
- { name: chill.export_aggregator, alias: location_type_aggregator }
chill.calendar.export.location_aggregator:
class: Chill\CalendarBundle\Export\Aggregator\LocationAggregator
autowire: true
autoconfigure: true
tags:
- { name: chill.export_aggregator, alias: location_aggregator }
chill.calendar.export.cancel_reason_aggregator:
class: Chill\CalendarBundle\Export\Aggregator\CancelReasonAggregator
autowire: true
autoconfigure: true
tags:
- { name: chill.export_aggregator, alias: cancel_reason_aggregator }
chill.calendar.export.month_aggregator:
class: Chill\CalendarBundle\Export\Aggregator\MonthYearAggregator
autowire: true
autoconfigure: true
tags:
- { name: chill.export_aggregator, alias: month_aggregator }
chill.calendar.export.urgency_aggregator:
class: Chill\CalendarBundle\Export\Aggregator\UrgencyAggregator
autowire: true
autoconfigure: true
tags:
- { name: chill.export_aggregator, alias: urgency_aggregator }

View File

@@ -1,10 +1,6 @@
---
services:
chill.calendar.form.type.calendar:
class: Chill\CalendarBundle\Form\CalendarType
arguments:
- "@chill.main.helper.translatable_string"
- "@doctrine.orm.entity_manager"
tags:
- { name: form.type, alias: chill_calendarbundle_calendar }
services:
Chill\CalendarBundle\Form\:
resource: './../../Form'
autowire: true
autoconfigure: true

View File

@@ -0,0 +1,15 @@
services:
_defaults:
autoconfigure: true
autowire: true
Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface: ~
Chill\CalendarBundle\RemoteCalendar\Connector\NullRemoteCalendarConnector: ~
Chill\CalendarBundle\RemoteCalendar\Connector\MSGraphRemoteCalendarConnector: ~
Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\:
resource: '../../RemoteCalendar/Connector/MSGraph/'

View File

@@ -0,0 +1,33 @@
import { createApp } from 'vue';
import Answer from 'ChillCalendarAssets/vuejs/Invite/Answer';
import { _createI18n } from 'ChillMainAssets/vuejs/_js/i18n';
const i18n = _createI18n({});
document.addEventListener('DOMContentLoaded', function (e) {
console.log('dom loaded answer');
document.querySelectorAll('div[invite-answer]').forEach(function (el) {
console.log('element found', el);
const app = createApp({
components: {
Answer,
},
data() {
return {
status: el.dataset.status,
calendarId: Number.parseInt(el.dataset.calendarId),
}
},
template: '<answer :calendarId="calendarId" :status="status" @statusChanged="onStatusChanged"></answer>',
methods: {
onStatusChanged: function(newStatus) {
this.$data.status = newStatus;
},
}
});
app.use(i18n).mount(el);
});
});

Some files were not shown because too many files have changed in this diff Show More