*/ 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 */ 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 $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); } }