diff --git a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php index 98e1c6eda..2eb04fca9 100644 --- a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php +++ b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php @@ -17,6 +17,7 @@ use Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterfa use Chill\CalendarBundle\Repository\CalendarRepository; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Pagination\PaginatorFactory; +use Chill\MainBundle\Repository\UserRepository; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; @@ -36,20 +37,22 @@ use Symfony\Component\Serializer\SerializerInterface; class CalendarController extends AbstractController { - protected AuthorizationHelper $authorizationHelper; + private AuthorizationHelper $authorizationHelper; - protected EventDispatcherInterface $eventDispatcher; + private EventDispatcherInterface $eventDispatcher; - protected LoggerInterface $logger; + private LoggerInterface $logger; - protected PaginatorFactory $paginator; + private PaginatorFactory $paginator; - protected SerializerInterface $serializer; + private SerializerInterface $serializer; private CalendarRepository $calendarRepository; private RemoteCalendarConnectorInterface $remoteCalendarConnector; + private UserRepository $userRepository; + public function __construct( AuthorizationHelper $authorizationHelper, CalendarRepository $calendarRepository, @@ -57,7 +60,8 @@ class CalendarController extends AbstractController LoggerInterface $logger, PaginatorFactory $paginator, RemoteCalendarConnectorInterface $remoteCalendarConnector, - SerializerInterface $serializer + SerializerInterface $serializer, + UserRepository $userRepository ) { $this->authorizationHelper = $authorizationHelper; $this->calendarRepository = $calendarRepository; @@ -66,6 +70,7 @@ class CalendarController extends AbstractController $this->paginator = $paginator; $this->remoteCalendarConnector = $remoteCalendarConnector; $this->serializer = $serializer; + $this->userRepository = $userRepository; } /** @@ -277,8 +282,13 @@ class CalendarController extends AbstractController // } $entity = new Calendar(); - $entity->setUser($this->getUser()); - $entity->setStatus($entity::STATUS_VALID); + if ($request->query->has('mainUser')) { + $entity->setMainUser($this->userRepository->find($request->query->getInt('mainUser'))); + } else { + $entity->setMainUser($this->getUser()); + } + + dump($entity); // if ($user instanceof User) { // $entity->setPerson($user); diff --git a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php index 11104cc6a..e6c5a8e97 100644 --- a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php +++ b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php @@ -13,6 +13,10 @@ 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\Location; use Chill\MainBundle\Entity\User; @@ -35,8 +39,12 @@ use function in_array; * @ORM\Table(name="chill_calendar.calendar") * @ORM\Entity(repositoryClass=CalendarRepository::class) */ -class Calendar +class Calendar implements TrackCreationInterface, TrackUpdateInterface { + use TrackCreationTrait; + + use TrackUpdateTrait; + public const STATUS_CANCELED = 'canceled'; public const STATUS_MOVED = 'moved'; @@ -82,12 +90,15 @@ class Calendar * @ORM\Column(type="integer") * @Serializer\Groups({"calendar: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"}) */ @@ -137,16 +148,9 @@ class Calendar private ?DateTimeImmutable $startDate = null; /** - * @ORM\Column(type="string", length=255) + * @ORM\Column(type="string", length=255, nullable=false, options={"default": "valid"}) */ - private ?string $status = null; - - /** - * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User") - * @Groups({"read"}) - * @Serializer\Groups({"calendar:read"}) - */ - private ?User $user = null; + private string $status = self::STATUS_VALID; public function __construct() { @@ -156,29 +160,28 @@ class Calendar $this->invites = new ArrayCollection(); } - public function addInvite(?Invite $invite): self + public function addInvite(Invite $invite): self { - if (null !== $invite) { - $this->invites[] = $invite; + if ($invite->getCalendar() instanceof Calendar && $this !== $invite->getCalendar()) { + throw new \LogicException('Not allowed to move an invitation to another Calendar'); } + $this->invites[] = $invite; + $invite->setCalendar($this); + return $this; } - public function addPerson(?Person $person): self + public function addPerson(Person $person): self { - if (null !== $person) { - $this->persons[] = $person; - } + $this->persons[] = $person; return $this; } - public function addProfessional(?ThirdParty $professional): self + public function addProfessional(ThirdParty $professional): self { - if (null !== $professional) { - $this->professionals[] = $professional; - } + $this->professionals[] = $professional; return $this; } @@ -428,10 +431,5 @@ class Calendar return $this; } - public function setUser(?User $user): self - { - $this->user = $user; - return $this; - } } diff --git a/src/Bundle/ChillCalendarBundle/Entity/Invite.php b/src/Bundle/ChillCalendarBundle/Entity/Invite.php index 464c5485a..fac0a833b 100644 --- a/src/Bundle/ChillCalendarBundle/Entity/Invite.php +++ b/src/Bundle/ChillCalendarBundle/Entity/Invite.php @@ -12,6 +12,10 @@ 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; @@ -19,31 +23,54 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Table(name="chill_calendar.invite") * @ORM\Entity(repositoryClass=InviteRepository::class) */ -class Invite +class Invite implements TrackUpdateInterface, TrackCreationInterface { + use TrackCreationTrait; + + use TrackUpdateTrait; + + public const ACCEPTED = 'accepted'; + + public const DECLINED = 'declined'; + + public const PENDING = 'pending'; + + public const TENTATIVELY_ACCEPTED = 'tentative'; + + /** + * @ORM\ManyToOne(targetEntity=Calendar::class, inversedBy="invites") + */ + private ?Calendar $calendar; + /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ - private $id; + private ?int $id; /** - * @ORM\Column(type="json") + * @ORM\Column(type="text", nullable=false, options={"default": "pending"}) */ - private array $status = []; + private string $status = self::PENDING; /** * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User") + * @ORM\JoinColumn(nullable=false) */ - private User $user; + private ?User $user; + + 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 +80,17 @@ class Invite return $this->user; } - public function setStatus(array $status): self + /** + * @internal use Calendar::addInvite instead + * @param Calendar|null $calendar + * @return void + */ + public function setCalendar(?Calendar $calendar): void + { + $this->calendar = $calendar; + } + + public function setStatus(string $status): self { $this->status = $status; diff --git a/src/Bundle/ChillCalendarBundle/Form/CalendarType.php b/src/Bundle/ChillCalendarBundle/Form/CalendarType.php index 560d12a40..74ad6343a 100644 --- a/src/Bundle/ChillCalendarBundle/Form/CalendarType.php +++ b/src/Bundle/ChillCalendarBundle/Form/CalendarType.php @@ -18,6 +18,7 @@ use Chill\CalendarBundle\Entity\Invite; use Chill\MainBundle\Entity\Location; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Form\Type\CommentType; +use Chill\MainBundle\Form\Type\PickUserDynamicType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Entity\Person; use Chill\ThirdPartyBundle\Entity\ThirdParty; @@ -67,22 +68,12 @@ class CalendarType extends AbstractType 'expanded' => true, ]); - $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]); - } - )); + if ($options['data'] instanceof Calendar && $options['data']->getId() === null) { + $builder->add('mainUser', PickUserDynamicType::class, [ + 'required' => true, + 'help' => 'chill_calendar.form.The main user is mandatory. He will organize the appointment.', + ]); + } $builder->add('startDate', HiddenType::class); $builder->get('startDate') diff --git a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/edit.html.twig b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/edit.html.twig index d67202920..03aa22b18 100644 --- a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/edit.html.twig +++ b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/edit.html.twig @@ -3,9 +3,6 @@ {{ form_start(form) }} {{ form_errors(form) }} -{%- if form.mainUser is defined -%} - {{ form_row(form.mainUser) }} -{% endif %}

{{ 'Concerned groups'|trans }}

diff --git a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig index 6cf97a58d..56e7b70e0 100644 --- a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig +++ b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig @@ -118,10 +118,17 @@ {% endif %} -