Feature: [calendar] associate document with calendar

This commit is contained in:
2022-10-20 21:35:57 +02:00
parent 1ec3e176fb
commit 2b1d9cabff
5 changed files with 329 additions and 0 deletions

View File

@@ -116,6 +116,17 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
*/
private CommentEmbeddable $comment;
/**
* @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"})
@@ -216,12 +227,25 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
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();
}
/**
* @internal use @{CalendarDoc::__construct} instead
*/
public function addDocument(CalendarDoc $calendarDoc): self
{
if ($this->documents->contains($calendarDoc)) {
$this->documents[] = $calendarDoc;
}
return $this;
}
/**
* @internal Use {@link (Calendar::addUser)} instead
*/
@@ -287,6 +311,19 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
return $this->comment;
}
/**
* 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"})
*/
@@ -472,6 +509,18 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
]));
}
/**
* @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
*/
@@ -562,6 +611,10 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
public function setEndDate(DateTimeImmutable $endDate): self
{
if ($this->endDate->getTimestamp() !== $endDate->getTimestamp()) {
$this->increaseaDatetimeVersion();
}
$this->endDate = $endDate;
return $this;
@@ -609,6 +662,10 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
public function setStartDate(DateTimeImmutable $startDate): self
{
if ($this->startDate->getTimestamp() !== $startDate->getTimestamp()) {
$this->increaseaDatetimeVersion();
}
$this->startDate = $startDate;
return $this;
@@ -631,4 +688,9 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
return $this;
}
private function increaseaDatetimeVersion(): void
{
++$this->dateTimeVersion;
}
}

View File

@@ -0,0 +1,135 @@
<?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\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 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;
}
}