Feature: [calendar] associate document with calendar

This commit is contained in:
Julien Fastré 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; 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) * @ORM\Column(type="datetime_immutable", nullable=false)
* @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"}) * @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"})
@ -216,12 +227,25 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
public function __construct() public function __construct()
{ {
$this->comment = new CommentEmbeddable(); $this->comment = new CommentEmbeddable();
$this->documents = new ArrayCollection();
$this->privateComment = new PrivateCommentEmbeddable(); $this->privateComment = new PrivateCommentEmbeddable();
$this->persons = new ArrayCollection(); $this->persons = new ArrayCollection();
$this->professionals = new ArrayCollection(); $this->professionals = new ArrayCollection();
$this->invites = 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 * @internal Use {@link (Calendar::addUser)} instead
*/ */
@ -287,6 +311,19 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
return $this->comment; 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"}) * @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 * @internal Use {@link (Calendar::removeUser)} instead
*/ */
@ -562,6 +611,10 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
public function setEndDate(DateTimeImmutable $endDate): self public function setEndDate(DateTimeImmutable $endDate): self
{ {
if ($this->endDate->getTimestamp() !== $endDate->getTimestamp()) {
$this->increaseaDatetimeVersion();
}
$this->endDate = $endDate; $this->endDate = $endDate;
return $this; return $this;
@ -609,6 +662,10 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
public function setStartDate(DateTimeImmutable $startDate): self public function setStartDate(DateTimeImmutable $startDate): self
{ {
if ($this->startDate->getTimestamp() !== $startDate->getTimestamp()) {
$this->increaseaDatetimeVersion();
}
$this->startDate = $startDate; $this->startDate = $startDate;
return $this; return $this;
@ -631,4 +688,9 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
return $this; 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;
}
}

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

@ -0,0 +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.
*/
namespace Chill\Migrations\Calendar;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20221020101547 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('DROP SEQUENCE chill_calendar.calendar_doc_id_seq CASCADE');
$this->addSql('DROP TABLE chill_calendar.calendar_doc');
$this->addSql('ALTER TABLE chill_calendar.calendar DROP dateTimeVersion');
}
public function getDescription(): string
{
return 'Add calendardoc on Calendar';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE SEQUENCE chill_calendar.calendar_doc_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE chill_calendar.calendar_doc (id INT NOT NULL, calendar_id INT NOT NULL, datetimeVersion INT DEFAULT 0 NOT NULL, trackDateTimeVersion BOOLEAN DEFAULT false NOT NULL, createdAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updatedAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, storedObject_id INT NOT NULL, createdBy_id INT DEFAULT NULL, updatedBy_id INT DEFAULT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_4FD11573A40A2C8 ON chill_calendar.calendar_doc (calendar_id)');
$this->addSql('CREATE INDEX IDX_4FD115736C99C13A ON chill_calendar.calendar_doc (storedObject_id)');
$this->addSql('CREATE INDEX IDX_4FD115733174800F ON chill_calendar.calendar_doc (createdBy_id)');
$this->addSql('CREATE INDEX IDX_4FD1157365FF1AEC ON chill_calendar.calendar_doc (updatedBy_id)');
$this->addSql('COMMENT ON COLUMN chill_calendar.calendar_doc.createdAt IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('COMMENT ON COLUMN chill_calendar.calendar_doc.updatedAt IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('ALTER TABLE chill_calendar.calendar_doc ADD CONSTRAINT FK_4FD11573A40A2C8 FOREIGN KEY (calendar_id) REFERENCES chill_calendar.calendar (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_calendar.calendar_doc ADD CONSTRAINT FK_4FD115736C99C13A FOREIGN KEY (storedObject_id) REFERENCES chill_doc.stored_object (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_calendar.calendar_doc ADD CONSTRAINT FK_4FD115733174800F FOREIGN KEY (createdBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_calendar.calendar_doc ADD CONSTRAINT FK_4FD1157365FF1AEC FOREIGN KEY (updatedBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_calendar.calendar ADD dateTimeVersion INT DEFAULT 0 NOT NULL');
}
}