first delivery
This commit is contained in:
1 parent
793fbc0c91
commit
f6684f3b2e
65 files changed
+5828
No files matched your search
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\PlanBundle\Model;
|
||||
|
||||
use Mautic\CoreBundle\Model\FormModel;
|
||||
use MauticPlugin\PlanBundle\Entity\Plan;
|
||||
use MauticPlugin\PlanBundle\Entity\PlanRepository;
|
||||
use MauticPlugin\PlanBundle\Form\Type\PlanType;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||
|
||||
/**
|
||||
* @extends FormModel<Plan>
|
||||
*/
|
||||
class PlanModel extends FormModel
|
||||
{
|
||||
public function getRepository(): PlanRepository
|
||||
{
|
||||
$repository = $this->em->getRepository(Plan::class);
|
||||
\assert($repository instanceof PlanRepository);
|
||||
|
||||
return $repository;
|
||||
}
|
||||
|
||||
public function getPermissionBase(): string
|
||||
{
|
||||
return 'plan:plans';
|
||||
}
|
||||
|
||||
/**
|
||||
* With no ID, returns a new plan with the current user as owner.
|
||||
*/
|
||||
public function getEntity($id = null): ?Plan
|
||||
{
|
||||
if (null === $id) {
|
||||
$plan = new Plan();
|
||||
$plan->setOwner($this->userHelper->getUser(true));
|
||||
|
||||
return $plan;
|
||||
}
|
||||
|
||||
$plan = parent::getEntity($id);
|
||||
|
||||
return $plan instanceof Plan ? $plan : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps objective and description item positions contiguous (0..n-1) before saving.
|
||||
*/
|
||||
public function saveEntity($entity, $unlock = true): void
|
||||
{
|
||||
if ($entity instanceof Plan) {
|
||||
$entity->normalizePositions();
|
||||
}
|
||||
|
||||
parent::saveEntity($entity, $unlock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $options
|
||||
*/
|
||||
public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): FormInterface
|
||||
{
|
||||
if (!$entity instanceof Plan) {
|
||||
throw new MethodNotAllowedHttpException(['Plan']);
|
||||
}
|
||||
|
||||
if (!empty($action)) {
|
||||
$options['action'] = $action;
|
||||
}
|
||||
|
||||
return $formFactory->create(PlanType::class, $entity, $options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\PlanBundle\Model;
|
||||
|
||||
use Mautic\CoreBundle\Model\FormModel;
|
||||
use MauticPlugin\PlanBundle\Entity\Plan;
|
||||
use MauticPlugin\PlanBundle\Entity\Task;
|
||||
use MauticPlugin\PlanBundle\Entity\TaskRepository;
|
||||
use MauticPlugin\PlanBundle\Form\Type\TaskType;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||
|
||||
/**
|
||||
* Available as mautic.plan.model.task, or getModel('plan.task').
|
||||
*
|
||||
* @extends FormModel<Task>
|
||||
*/
|
||||
class TaskModel extends FormModel
|
||||
{
|
||||
public function getRepository(): TaskRepository
|
||||
{
|
||||
$repository = $this->em->getRepository(Task::class);
|
||||
\assert($repository instanceof TaskRepository);
|
||||
|
||||
return $repository;
|
||||
}
|
||||
|
||||
public function getPermissionBase(): string
|
||||
{
|
||||
return 'plan:tasks';
|
||||
}
|
||||
|
||||
public function getEntity($id = null): ?Task
|
||||
{
|
||||
if (null === $id) {
|
||||
return new Task();
|
||||
}
|
||||
|
||||
$task = parent::getEntity($id);
|
||||
|
||||
return $task instanceof Task ? $task : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The plan's tasks as a tree: top-level tasks first, each with its sub-tasks, ordered by name.
|
||||
*
|
||||
* @return array<int, array{task: Task, depth: int}>
|
||||
*/
|
||||
public function getTaskTree(Plan $plan): array
|
||||
{
|
||||
$byParent = [];
|
||||
foreach ($this->getRepository()->findByPlan($plan) as $task) {
|
||||
$byParent[$task->getParent()?->getId() ?? 0][] = $task;
|
||||
}
|
||||
|
||||
$rows = [];
|
||||
$seen = [];
|
||||
$walk = function (int $parentId, int $depth) use (&$walk, &$rows, &$seen, $byParent): void {
|
||||
foreach ($byParent[$parentId] ?? [] as $task) {
|
||||
if (isset($seen[$task->getId()])) {
|
||||
continue;
|
||||
}
|
||||
$seen[$task->getId()] = true;
|
||||
$rows[] = ['task' => $task, 'depth' => $depth];
|
||||
$walk((int) $task->getId(), $depth + 1);
|
||||
}
|
||||
};
|
||||
$walk(0, 0);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $options
|
||||
*/
|
||||
public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): FormInterface
|
||||
{
|
||||
if (!$entity instanceof Task) {
|
||||
throw new MethodNotAllowedHttpException(['Task']);
|
||||
}
|
||||
|
||||
if (!empty($action)) {
|
||||
$options['action'] = $action;
|
||||
}
|
||||
|
||||
return $formFactory->create(TaskType::class, $entity, $options);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user