Rename the Plan bundle to Program across config, routes, menus, and controllers (Plan -> Initiative), bump version to 0.16.0, and introduce new audience and UTM taxonomy routes and permissions to support the expanded Programs feature set.
460 lines
13 KiB
PHP
460 lines
13 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\ProgramBundle\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\ProgramBundle\Validator\Constraints\EndDateAfterStartDate;
|
|
use MauticPlugin\ProgramBundle\Validator\Constraints\TaskDates;
|
|
use MauticPlugin\ProgramBundle\Validator\Constraints\ValidTaskDependency;
|
|
use Symfony\Component\Validator\Constraints\Length;
|
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
|
use Symfony\Component\Validator\Constraints\PositiveOrZero;
|
|
use Symfony\Component\Validator\Constraints\Range;
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
|
|
|
/**
|
|
* A task of an initiative. Every task belongs to exactly one initiative.
|
|
*
|
|
* Kinds (taskKind): a regular task; a milestone, whose end date is enslaved to its start date; or a
|
|
* phase, which has no status of its own — it is a collection of tasks.
|
|
*
|
|
* Dependencies: a task may depend on other tasks (TaskDependency), with the relation type End2Start,
|
|
* End2End, Start2Start or Start2End. The former parent/child tree is replaced by this dependency
|
|
* graph.
|
|
*
|
|
* RACI: a task is assigned to the initiative's stakeholders through TaskRaci rows (R/A/C/I), several
|
|
* stakeholders per role.
|
|
*
|
|
* Database rules: deleting the initiative deletes its tasks (ON DELETE CASCADE); deleting a task deletes
|
|
* its dependencies and RACI rows (ON DELETE CASCADE).
|
|
*/
|
|
class Task extends FormEntity implements DateRangeInterface
|
|
{
|
|
public const TABLE_NAME = 'initiative_tasks';
|
|
|
|
public const TYPE_MAX_LENGTH = 64;
|
|
|
|
/**
|
|
* The planned work is whole hours, capped to keep the column sane.
|
|
*/
|
|
public const PLANNED_WORK_MAX = 100000;
|
|
|
|
private ?int $id = null;
|
|
|
|
private ?Initiative $initiative = null;
|
|
|
|
/**
|
|
* Key of a task type set up in Settings > Configuration > Program Settings (parameter program_task_types);
|
|
* see ItemTypeProvider (kind "task") for its name, description and pictogram.
|
|
*/
|
|
private ?string $type = null;
|
|
|
|
private ?string $name = null;
|
|
|
|
private ?string $description = null;
|
|
|
|
private ?\DateTime $startDate = null;
|
|
|
|
private ?\DateTime $endDate = null;
|
|
|
|
private TaskStatus $status = TaskStatus::New;
|
|
|
|
private ?TaskKind $taskKind = null;
|
|
|
|
private ?string $definitionOfDone = null;
|
|
|
|
private ?int $plannedWork = null;
|
|
|
|
/**
|
|
* @var Collection<int, TaskDependency>
|
|
*/
|
|
private Collection $dependencies;
|
|
|
|
/**
|
|
* @var Collection<int, TaskRaci>
|
|
*/
|
|
private Collection $raci;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->dependencies = new ArrayCollection();
|
|
$this->raci = new ArrayCollection();
|
|
}
|
|
|
|
public function __clone()
|
|
{
|
|
$this->id = null;
|
|
$this->dependencies = new ArrayCollection();
|
|
$this->raci = 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('initiative', Initiative::class)
|
|
->inversedBy('tasks')
|
|
->addJoinColumn('initiative_id', 'id', false, false, 'CASCADE')
|
|
->build();
|
|
|
|
$builder->createField('type', Types::STRING)
|
|
->length(self::TYPE_MAX_LENGTH)
|
|
->nullable()
|
|
->build();
|
|
|
|
$builder->createField('name', Types::STRING)
|
|
->length(255)
|
|
->build();
|
|
|
|
$builder->createField('description', Types::TEXT)
|
|
->nullable()
|
|
->build();
|
|
|
|
// 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'], 'program_task_status');
|
|
|
|
$metadata->mapField([
|
|
'fieldName' => 'taskKind',
|
|
'columnName' => 'task_kind',
|
|
'type' => Types::STRING,
|
|
'length' => 20,
|
|
'enumType' => TaskKind::class,
|
|
'options' => ['default' => TaskKind::Task->value],
|
|
]);
|
|
|
|
// Dates are DATE (no time): tasks are planned at day granularity.
|
|
$metadata->mapField([
|
|
'fieldName' => 'startDate',
|
|
'columnName' => 'start_date',
|
|
'type' => Types::DATE_MUTABLE,
|
|
'nullable' => true,
|
|
]);
|
|
$metadata->mapField([
|
|
'fieldName' => 'endDate',
|
|
'columnName' => 'end_date',
|
|
'type' => Types::DATE_MUTABLE,
|
|
'nullable' => true,
|
|
]);
|
|
|
|
$builder->createField('definitionOfDone', Types::TEXT)
|
|
->columnName('definition_of_done')
|
|
->nullable()
|
|
->build();
|
|
|
|
$builder->createField('plannedWork', Types::INTEGER)
|
|
->columnName('planned_work')
|
|
->nullable()
|
|
->option('unsigned', true)
|
|
->build();
|
|
|
|
// Dependencies: outgoing edges (this task depends on those) and incoming edges (those depend
|
|
// on this task). Both sides are owned by the rows; deleting a task deletes its edges.
|
|
$builder->createOneToMany('dependencies', TaskDependency::class)
|
|
->mappedBy('task')
|
|
->setOrderBy(['position' => 'ASC'])
|
|
->cascadePersist()
|
|
->cascadeRemove()
|
|
->orphanRemoval()
|
|
->build();
|
|
|
|
$builder->createOneToMany('raci', TaskRaci::class)
|
|
->mappedBy('task')
|
|
->setOrderBy(['position' => 'ASC'])
|
|
->cascadePersist()
|
|
->cascadeRemove()
|
|
->orphanRemoval()
|
|
->build();
|
|
}
|
|
|
|
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('initiative', new NotBlank(message: 'mautic.program.task.initiative.required'));
|
|
$metadata->addPropertyConstraint('type', new NotBlank(message: 'mautic.program.task.type.required'));
|
|
$metadata->addPropertyConstraint('type', new Length(max: self::TYPE_MAX_LENGTH));
|
|
$metadata->addPropertyConstraint('plannedWork', new PositiveOrZero(message: 'mautic.program.task.planned_work.invalid'));
|
|
$metadata->addPropertyConstraint('plannedWork', new Range(notInRangeMessage: 'mautic.program.task.planned_work.range', min: 0, max: self::PLANNED_WORK_MAX));
|
|
$metadata->addConstraint(new EndDateAfterStartDate());
|
|
$metadata->addConstraint(new TaskDates());
|
|
$metadata->addConstraint(new ValidTaskDependency());
|
|
}
|
|
|
|
public function getId(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getInitiative(): ?Initiative
|
|
{
|
|
return $this->initiative;
|
|
}
|
|
|
|
public function setInitiative(?Initiative $initiative): static
|
|
{
|
|
$this->isChanged('initiative', $initiative);
|
|
$this->initiative = $initiative;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getType(): ?string
|
|
{
|
|
return $this->type;
|
|
}
|
|
|
|
public function setType(?string $type): static
|
|
{
|
|
// FormEntity logs changes for the audit log; enums aren't stringable, plain strings are fine.
|
|
$this->isChanged('type', $type);
|
|
|
|
$this->type = $type;
|
|
|
|
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 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;
|
|
}
|
|
|
|
public function getTaskKind(): TaskKind
|
|
{
|
|
// Never null once stored (the column defaults to 'task'); a new entity reads as a plain task.
|
|
return $this->taskKind ?? TaskKind::Task;
|
|
}
|
|
|
|
public function setTaskKind(TaskKind $taskKind): static
|
|
{
|
|
if ($taskKind !== $this->taskKind) {
|
|
// isChanged() can't log enums (they aren't stringable), so log the backing values.
|
|
$this->addChange('taskKind', [$this->taskKind?->value, $taskKind->value]);
|
|
$taskKind->enslaveDates($this);
|
|
$this->taskKind = $taskKind;
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getDefinitionOfDone(): ?string
|
|
{
|
|
return $this->definitionOfDone;
|
|
}
|
|
|
|
public function setDefinitionOfDone(?string $definitionOfDone): static
|
|
{
|
|
$this->isChanged('definitionOfDone', $definitionOfDone);
|
|
$this->definitionOfDone = $definitionOfDone;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getPlannedWork(): ?int
|
|
{
|
|
return $this->plannedWork;
|
|
}
|
|
|
|
public function setPlannedWork(?int $plannedWork): static
|
|
{
|
|
$this->isChanged('plannedWork', $this->plannedWork);
|
|
$this->plannedWork = $plannedWork;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, TaskDependency>
|
|
*/
|
|
public function getDependencies(): Collection
|
|
{
|
|
return $this->dependencies;
|
|
}
|
|
|
|
public function addDependency(TaskDependency $dependency): static
|
|
{
|
|
if (!$this->dependencies->contains($dependency)) {
|
|
$dependency->setTask($this);
|
|
$this->dependencies->add($dependency);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeDependency(TaskDependency $dependency): static
|
|
{
|
|
$this->dependencies->removeElement($dependency);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, TaskRaci>
|
|
*/
|
|
public function getRaci(): Collection
|
|
{
|
|
return $this->raci;
|
|
}
|
|
|
|
/**
|
|
* Replaces the RACI rows. The form writes the whole collection: the inflector singularizes
|
|
* "raci" to "racus", so PropertyAccess can't find addRaci/removeRaci for the collection write
|
|
* and needs this setter. The back-reference is set here, so the rows persist with their task.
|
|
*
|
|
* @param iterable<int, TaskRaci> $raci
|
|
*/
|
|
public function setRaci(iterable $raci): static
|
|
{
|
|
$this->raci = new ArrayCollection();
|
|
|
|
foreach (is_array($raci) ? $raci : iterator_to_array($raci) as $row) {
|
|
$this->addRaci($row);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function addRaci(TaskRaci $raci): static
|
|
{
|
|
if (!$this->raci->contains($raci)) {
|
|
$raci->setTask($this);
|
|
$this->raci->add($raci);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeRaci(TaskRaci $raci): static
|
|
{
|
|
$this->raci->removeElement($raci);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* A textual summary of the dependencies, the transitively blocked tasks and the RACI roles —
|
|
* used by the tab tables and the list. Formerly the parent/children tree, removed in 0.13.0.
|
|
*/
|
|
public function getDependencySummary(): string
|
|
{
|
|
$parts = [];
|
|
|
|
foreach ($this->dependencies as $dependency) {
|
|
if (null === $dependency->getDependsOn()) {
|
|
continue;
|
|
}
|
|
$parts[] = sprintf(
|
|
'%s (%s)',
|
|
$dependency->getDependsOn()->getName(),
|
|
$dependency->getDependencyType()->getLabelKey(),
|
|
);
|
|
}
|
|
|
|
return implode(', ', $parts);
|
|
}
|
|
|
|
/**
|
|
* A textual summary of the task's RACI rows, for the tables.
|
|
*/
|
|
public function getRaciSummary(): string
|
|
{
|
|
$parts = [];
|
|
|
|
foreach ($this->raci as $raci) {
|
|
$names = [];
|
|
foreach ($raci->getStakeholders() as $stakeholder) {
|
|
$names[] = $stakeholder->getName();
|
|
}
|
|
if ([] === $names) {
|
|
continue;
|
|
}
|
|
$parts[] = sprintf('%s: %s', $raci->getRole()->getLabelKey(), implode(', ', $names));
|
|
}
|
|
|
|
return implode(' · ', $parts);
|
|
}
|
|
}
|