first delivery
This commit is contained in:
1 parent
793fbc0c91
commit
f6684f3b2e
65 files changed
+5828
No files matched your search
+251
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\PlanBundle\Entity;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata as OrmClassMetadata;
|
||||
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
|
||||
use Mautic\CoreBundle\Entity\FormEntity;
|
||||
use MauticPlugin\PlanBundle\Validator\Constraints\EndDateAfterStartDate;
|
||||
use MauticPlugin\PlanBundle\Validator\Constraints\ValidTaskParent;
|
||||
use Symfony\Component\Validator\Constraints\Length;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||
|
||||
/**
|
||||
* A task of a plan. Every task belongs to exactly one plan, and may have a parent task in the same plan.
|
||||
*
|
||||
* Database rules: deleting the plan deletes its tasks (ON DELETE CASCADE); deleting a task makes its
|
||||
* sub-tasks top-level (parent ON DELETE SET NULL).
|
||||
*/
|
||||
class Task extends FormEntity implements DateRangeInterface
|
||||
{
|
||||
public const TABLE_NAME = 'plan_tasks';
|
||||
|
||||
private ?int $id = null;
|
||||
|
||||
private ?Plan $plan = null;
|
||||
|
||||
private ?string $name = null;
|
||||
|
||||
private ?string $description = null;
|
||||
|
||||
private ?Task $parent = null;
|
||||
|
||||
private ?\DateTime $startDate = null;
|
||||
|
||||
private ?\DateTime $endDate = null;
|
||||
|
||||
private TaskStatus $status = TaskStatus::New;
|
||||
|
||||
/**
|
||||
* @var Collection<int, Task>
|
||||
*/
|
||||
private Collection $children;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->children = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
$this->id = null;
|
||||
$this->children = new ArrayCollection();
|
||||
|
||||
parent::__clone();
|
||||
}
|
||||
|
||||
public static function loadMetadata(OrmClassMetadata $metadata): void
|
||||
{
|
||||
$builder = new ClassMetadataBuilder($metadata);
|
||||
|
||||
$builder->setTable(self::TABLE_NAME)
|
||||
->setCustomRepositoryClass(TaskRepository::class);
|
||||
|
||||
$builder->addId();
|
||||
|
||||
$builder->createManyToOne('plan', Plan::class)
|
||||
->inversedBy('tasks')
|
||||
->addJoinColumn('plan_id', 'id', false, false, 'CASCADE')
|
||||
->build();
|
||||
|
||||
$builder->createField('name', Types::STRING)
|
||||
->length(255)
|
||||
->build();
|
||||
|
||||
$builder->createField('description', Types::TEXT)
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createManyToOne('parent', self::class)
|
||||
->inversedBy('children')
|
||||
->addJoinColumn('parent_id', 'id', true, false, 'SET NULL')
|
||||
->build();
|
||||
|
||||
$builder->createOneToMany('children', self::class)
|
||||
->mappedBy('parent')
|
||||
->setOrderBy(['name' => 'ASC'])
|
||||
->fetchExtraLazy()
|
||||
->build();
|
||||
|
||||
$builder->addNamedField('startDate', Types::DATETIME_MUTABLE, 'start_date', true);
|
||||
$builder->addNamedField('endDate', Types::DATETIME_MUTABLE, 'end_date', true);
|
||||
|
||||
// Mapped on the raw metadata: ClassMetadataBuilder::addField() forces every string to VARCHAR(191).
|
||||
$metadata->mapField([
|
||||
'fieldName' => 'status',
|
||||
'type' => Types::STRING,
|
||||
'length' => 20,
|
||||
'enumType' => TaskStatus::class,
|
||||
'options' => ['default' => TaskStatus::New->value],
|
||||
]);
|
||||
$builder->addIndex(['status'], 'plan_task_status');
|
||||
}
|
||||
|
||||
public static function loadValidatorMetadata(ClassMetadata $metadata): void
|
||||
{
|
||||
$metadata->addPropertyConstraint('name', new NotBlank(message: 'mautic.core.name.required'));
|
||||
$metadata->addPropertyConstraint('name', new Length(max: 255));
|
||||
$metadata->addPropertyConstraint('plan', new NotBlank(message: 'mautic.plan.task.plan.required'));
|
||||
$metadata->addConstraint(new ValidTaskParent());
|
||||
$metadata->addConstraint(new EndDateAfterStartDate());
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getPlan(): ?Plan
|
||||
{
|
||||
return $this->plan;
|
||||
}
|
||||
|
||||
public function setPlan(?Plan $plan): static
|
||||
{
|
||||
$this->isChanged('plan', $plan);
|
||||
$this->plan = $plan;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(?string $name): static
|
||||
{
|
||||
$this->isChanged('name', $name);
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDescription(): ?string
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function setDescription(?string $description): static
|
||||
{
|
||||
$this->isChanged('description', $description);
|
||||
$this->description = $description;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getParent(): ?Task
|
||||
{
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
public function setParent(?Task $parent): static
|
||||
{
|
||||
$this->isChanged('parent', $parent);
|
||||
$this->parent = $parent;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStartDate(): ?\DateTime
|
||||
{
|
||||
return $this->startDate;
|
||||
}
|
||||
|
||||
public function setStartDate(?\DateTime $startDate): static
|
||||
{
|
||||
$this->isChanged('startDate', $startDate);
|
||||
$this->startDate = $startDate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEndDate(): ?\DateTime
|
||||
{
|
||||
return $this->endDate;
|
||||
}
|
||||
|
||||
public function setEndDate(?\DateTime $endDate): static
|
||||
{
|
||||
$this->isChanged('endDate', $endDate);
|
||||
$this->endDate = $endDate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStatus(): TaskStatus
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function setStatus(TaskStatus $status): static
|
||||
{
|
||||
if ($status !== $this->status) {
|
||||
// isChanged() can't log enums (they aren't stringable), so log the backing values.
|
||||
$this->addChange('status', [$this->status->value, $status->value]);
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Task>
|
||||
*/
|
||||
public function getChildren(): Collection
|
||||
{
|
||||
return $this->children;
|
||||
}
|
||||
|
||||
/**
|
||||
* True when $task is this task or one of its ancestors.
|
||||
*/
|
||||
public function hasAncestorOrSelf(Task $task): bool
|
||||
{
|
||||
$seen = [];
|
||||
$current = $this;
|
||||
|
||||
while (null !== $current) {
|
||||
if ($current === $task || (null !== $task->getId() && $current->getId() === $task->getId())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Guard against a cycle that is already stored.
|
||||
$key = null !== $current->getId() ? 'id'.$current->getId() : spl_object_id($current);
|
||||
if (isset($seen[$key])) {
|
||||
return false;
|
||||
}
|
||||
$seen[$key] = true;
|
||||
|
||||
$current = $current->getParent();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user