mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-25 16:14:59 +00:00
Partage d'export enregistré et génération asynchrone des exports
This commit is contained in:
150
src/Bundle/ChillMainBundle/Entity/ExportGeneration.php
Normal file
150
src/Bundle/ChillMainBundle/Entity/ExportGeneration.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?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\MainBundle\Entity;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
|
||||
use Chill\MainBundle\Doctrine\Model\TrackCreationTrait;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
use Symfony\Component\Serializer\Annotation as Serializer;
|
||||
|
||||
/**
|
||||
* Contains the single execution of an export.
|
||||
*
|
||||
* Attached to a stored object, which will contains the result of the execution. The status of the stored object
|
||||
* is the status of the export generation.
|
||||
*
|
||||
* Generated exports should be deleted after a certain time, given by the column `deletedAt`. The stored object can be
|
||||
* deleted after the export generation removal.
|
||||
*/
|
||||
#[ORM\Entity()]
|
||||
#[ORM\Table('chill_main_export_generation')]
|
||||
#[Serializer\DiscriminatorMap('type', ['export_generation' => ExportGeneration::class])]
|
||||
class ExportGeneration implements TrackCreationInterface
|
||||
{
|
||||
use TrackCreationTrait;
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\Column(type: 'uuid', unique: true)]
|
||||
#[Serializer\Groups(['read'])]
|
||||
private UuidInterface $id;
|
||||
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: StoredObject::class, cascade: ['persist', 'refresh'])]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
#[Serializer\Groups(['read'])]
|
||||
private StoredObject $storedObject;
|
||||
|
||||
public function __construct(
|
||||
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::TEXT, nullable: false, options: ['default' => ''])]
|
||||
#[Serializer\Groups(['read'])]
|
||||
private string $exportAlias,
|
||||
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::JSON, nullable: false, options: ['default' => '[]'])]
|
||||
private array $options = [],
|
||||
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE, nullable: true)]
|
||||
private ?\DateTimeImmutable $deleteAt = null,
|
||||
|
||||
/**
|
||||
* The related saved export.
|
||||
*
|
||||
* Note that, in some case, the options of this ExportGeneration are not equals to the options of the saved export.
|
||||
* This happens when the options of the saved export are updated.
|
||||
*/
|
||||
#[ORM\ManyToOne(targetEntity: SavedExport::class)]
|
||||
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
|
||||
private ?SavedExport $savedExport = null,
|
||||
) {
|
||||
$this->id = Uuid::uuid4();
|
||||
$this->storedObject = new StoredObject(StoredObject::STATUS_PENDING);
|
||||
}
|
||||
|
||||
public function setDeleteAt(?\DateTimeImmutable $deleteAt): ExportGeneration
|
||||
{
|
||||
$this->deleteAt = $deleteAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDeleteAt(): ?\DateTimeImmutable
|
||||
{
|
||||
return $this->deleteAt;
|
||||
}
|
||||
|
||||
public function getId(): UuidInterface
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getStoredObject(): StoredObject
|
||||
{
|
||||
return $this->storedObject;
|
||||
}
|
||||
|
||||
public function getExportAlias(): string
|
||||
{
|
||||
return $this->exportAlias;
|
||||
}
|
||||
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
public function getSavedExport(): ?SavedExport
|
||||
{
|
||||
return $this->savedExport;
|
||||
}
|
||||
|
||||
#[Serializer\Groups(['read'])]
|
||||
#[Serializer\SerializedName('status')]
|
||||
public function getStatus(): string
|
||||
{
|
||||
return $this->getStoredObject()->getStatus();
|
||||
}
|
||||
|
||||
public function setSavedExport(SavedExport $savedExport): self
|
||||
{
|
||||
$this->savedExport = $savedExport;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isLinkedToSavedExport(): bool
|
||||
{
|
||||
return null !== $this->savedExport;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the options of the saved export and the current export generation.
|
||||
*
|
||||
* Return false if the current export generation's options are not equal to the one in the saved export. This may
|
||||
* happens when we update the configuration of a saved export.
|
||||
*/
|
||||
public function isConfigurationDifferentFromSavedExport(): bool
|
||||
{
|
||||
if (!$this->isLinkedToSavedExport()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->savedExport->getOptions() !== $this->getOptions();
|
||||
}
|
||||
|
||||
public static function fromSavedExport(SavedExport $savedExport, ?\DateTimeImmutable $deletedAt = null, ?array $overrideOptions = null): self
|
||||
{
|
||||
$generation = new self($savedExport->getExportAlias(), $overrideOptions ?? $savedExport->getOptions(), $deletedAt, $savedExport);
|
||||
$generation->getStoredObject()->setTitle($savedExport->getTitle());
|
||||
|
||||
return $generation;
|
||||
}
|
||||
}
|
@@ -50,4 +50,9 @@ class SimpleGeographicalUnitDTO
|
||||
#[Serializer\Groups(['read'])]
|
||||
public int $layerId,
|
||||
) {}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
}
|
||||
|
@@ -102,4 +102,22 @@ class Regroupment
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the given center is contained into this regroupment.
|
||||
*/
|
||||
public function containsCenter(Center $center): bool
|
||||
{
|
||||
return $this->centers->contains($center);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if at least one of the given centers is contained into this regroupment.
|
||||
*
|
||||
* @param list<Center> $centers
|
||||
*/
|
||||
public function containsAtLeastOneCenter(array $centers): bool
|
||||
{
|
||||
return array_reduce($centers, fn (bool $carry, Center $center) => $carry || $this->containsCenter($center), false);
|
||||
}
|
||||
}
|
||||
|
@@ -15,6 +15,9 @@ 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\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\Common\Collections\ReadableCollection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
@@ -50,9 +53,25 @@ class SavedExport implements TrackCreationInterface, TrackUpdateInterface
|
||||
#[ORM\ManyToOne(targetEntity: User::class)]
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* @var Collection<int, User>
|
||||
*/
|
||||
#[ORM\ManyToMany(targetEntity: User::class)]
|
||||
#[ORM\JoinTable(name: 'chill_main_saved_export_users')]
|
||||
private Collection $sharedWithUsers;
|
||||
|
||||
/**
|
||||
* @var Collection<int, UserGroup>
|
||||
*/
|
||||
#[ORM\ManyToMany(targetEntity: UserGroup::class)]
|
||||
#[ORM\JoinTable(name: 'chill_main_saved_export_usergroups')]
|
||||
private Collection $sharedWithGroups;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->id = Uuid::uuid4();
|
||||
$this->sharedWithUsers = new ArrayCollection();
|
||||
$this->sharedWithGroups = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
@@ -119,4 +138,71 @@ class SavedExport implements TrackCreationInterface, TrackUpdateInterface
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addShare(User|UserGroup $shareUser): SavedExport
|
||||
{
|
||||
if ($shareUser instanceof User) {
|
||||
if (!$this->sharedWithUsers->contains($shareUser)) {
|
||||
$this->sharedWithUsers->add($shareUser);
|
||||
}
|
||||
} else {
|
||||
if (!$this->sharedWithGroups->contains($shareUser)) {
|
||||
$this->sharedWithGroups->add($shareUser);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeShare(User|UserGroup $shareUser): SavedExport
|
||||
{
|
||||
if ($shareUser instanceof User) {
|
||||
$this->sharedWithUsers->removeElement($shareUser);
|
||||
} else {
|
||||
$this->sharedWithGroups->removeElement($shareUser);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ReadableCollection<int, User|UserGroup>
|
||||
*/
|
||||
public function getShare(): ReadableCollection
|
||||
{
|
||||
return new ArrayCollection([
|
||||
...$this->sharedWithUsers->toArray(),
|
||||
...$this->sharedWithGroups->toArray(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if shared with at least one user or one group.
|
||||
*/
|
||||
public function isShared(): bool
|
||||
{
|
||||
return $this->sharedWithUsers->count() > 0 || $this->sharedWithGroups->count() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the user is shared with either directly or through a group.
|
||||
*
|
||||
* @param User $user the user to check
|
||||
*
|
||||
* @return bool returns true if the user is shared with directly or via group, otherwise false
|
||||
*/
|
||||
public function isSharedWithUser(User $user): bool
|
||||
{
|
||||
if ($this->sharedWithUsers->contains($user)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($this->sharedWithGroups as $group) {
|
||||
if ($group->contains($user)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -21,6 +21,19 @@ use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
/**
|
||||
* Represents a user group entity in the system.
|
||||
*
|
||||
* This class is used for managing user groups, including their relationships
|
||||
* with users, administrative users, and additional metadata such as colors and labels.
|
||||
*
|
||||
* Groups may be configured to have mutual exclusion properties based on an
|
||||
* exclusion key. This ensures that groups sharing the same key cannot coexist
|
||||
* in certain relationship contexts.
|
||||
*
|
||||
* Groups may be related to a UserJob. In that case, a cronjob task ensure that the members of the groups are
|
||||
* automatically synced with this group. Such groups are also automatically created by the cronjob.
|
||||
*/
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'chill_main_user_group')]
|
||||
// this discriminator key is required for automated denormalization
|
||||
@@ -71,6 +84,13 @@ class UserGroup
|
||||
#[Assert\Email]
|
||||
private string $email = '';
|
||||
|
||||
/**
|
||||
* UserJob to which the group is related.
|
||||
*/
|
||||
#[ORM\ManyToOne(targetEntity: UserJob::class)]
|
||||
#[ORM\JoinColumn(nullable: true)]
|
||||
private ?UserJob $userJob = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->adminUsers = new ArrayCollection();
|
||||
@@ -209,6 +229,21 @@ class UserGroup
|
||||
return '' !== $this->email;
|
||||
}
|
||||
|
||||
public function hasUserJob(): bool
|
||||
{
|
||||
return null !== $this->userJob;
|
||||
}
|
||||
|
||||
public function getUserJob(): ?UserJob
|
||||
{
|
||||
return $this->userJob;
|
||||
}
|
||||
|
||||
public function setUserJob(?UserJob $userJob): void
|
||||
{
|
||||
$this->userJob = $userJob;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current object is an instance of the UserGroup class.
|
||||
*
|
||||
|
Reference in New Issue
Block a user