From b129794b06645cf74a82ef410927411202acd862 Mon Sep 17 00:00:00 2001 From: Joren de Graaf Date: Mon, 7 Sep 2026 17:50:09 +0200 Subject: [PATCH] first version of defer/sick mode --- assets/styles/app.scss | 29 ++++ migrations/Version20260907123000.php | 43 ++++++ src/Controller/TaskController.php | 44 +++++- src/Entity/Task.php | 84 +++++++++++ src/Form/TaskType.php | 11 ++ src/Repository/TaskRepository.php | 21 +++ src/Service/TaskSchedulingService.php | 38 ++++- templates/task/_task_composer.html.twig | 1 + .../task/_task_composer_response.html.twig | 2 +- templates/task/_task_item.html.twig | 131 ++++++++++-------- templates/task/_task_list.html.twig | 2 +- .../task/_task_recommendations.html.twig | 2 +- 12 files changed, 338 insertions(+), 70 deletions(-) create mode 100644 migrations/Version20260907123000.php diff --git a/assets/styles/app.scss b/assets/styles/app.scss index 87e39fa..cf13bb0 100644 --- a/assets/styles/app.scss +++ b/assets/styles/app.scss @@ -403,6 +403,35 @@ header { text-decoration: line-through; } +.task-list__item { + display: block; +} + +.task-list__item-content { + display: flex; + align-items: center; + gap: .5rem; +} + +.task-list__subtasks { + display: grid; + gap: .5rem; + padding: .5rem 0 0 2.75rem; + margin: .5rem 0 0; + list-style: none; + border-top: 1px solid #d9f4ff; +} + +.task-list__item--subtask { + background-color: #f2fbff; +} + +.task-list__add-subtask { + color: #087aa9; + font-size: .85rem; + white-space: nowrap; +} + .task-result-container { .task-result { diff --git a/migrations/Version20260907123000.php b/migrations/Version20260907123000.php new file mode 100644 index 0000000..39dd05c --- /dev/null +++ b/migrations/Version20260907123000.php @@ -0,0 +1,43 @@ +connection->getDatabasePlatform() instanceof PostgreSQLPlatform) { + $this->addSql('ALTER TABLE task ADD parent_task_id INT DEFAULT NULL'); + $this->addSql('CREATE INDEX IDX_TASK_PARENT_TASK ON task (parent_task_id)'); + $this->addSql('ALTER TABLE task ADD CONSTRAINT FK_TASK_PARENT_TASK FOREIGN KEY (parent_task_id) REFERENCES task (id) ON DELETE SET NULL'); + + return; + } + + $this->addSql('ALTER TABLE task ADD parent_task_id INT DEFAULT NULL, ADD INDEX IDX_TASK_PARENT_TASK (parent_task_id), ADD CONSTRAINT FK_TASK_PARENT_TASK FOREIGN KEY (parent_task_id) REFERENCES task (id) ON DELETE SET NULL'); + } + + public function down(Schema $schema): void + { + if ($this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform) { + $this->addSql('ALTER TABLE task DROP CONSTRAINT FK_TASK_PARENT_TASK'); + $this->addSql('DROP INDEX IDX_TASK_PARENT_TASK'); + $this->addSql('ALTER TABLE task DROP parent_task_id'); + + return; + } + + $this->addSql('ALTER TABLE task DROP FOREIGN KEY FK_TASK_PARENT_TASK, DROP INDEX IDX_TASK_PARENT_TASK, DROP parent_task_id'); + } +} diff --git a/src/Controller/TaskController.php b/src/Controller/TaskController.php index 6de3a7b..5f7cf8b 100644 --- a/src/Controller/TaskController.php +++ b/src/Controller/TaskController.php @@ -5,6 +5,7 @@ use App\Entity\Settings; use App\Entity\User; use App\Entity\Task; +use App\Repository\TaskRepository; use App\Form\TaskType; use App\Service\TaskDeferralService; use App\Service\TaskRecommendationService; @@ -33,8 +34,11 @@ final class TaskController extends AbstractController /** @var User|null $user */ $user = $security->getUser(); + $taskGroups = $taskSchedulingService->taskGroupsForDate($batteryLevel, $selectedDate, $settings); + return $this->render('task/index.html.twig', [ - 'tasks' => $taskSchedulingService->tasksForDate($batteryLevel, $selectedDate, $settings), + 'tasks' => $taskGroups['tasks'], + 'subtasks_by_parent' => $taskGroups['subtasks_by_parent'], 'user' => $user, 'battery_level' => $batteryLevel, 'battery_budget' => $taskRecommendationService->budgetFor($batteryLevel), @@ -59,9 +63,20 @@ final class TaskController extends AbstractController } #[Route('/new', name: 'app_task_new', methods: ['GET', 'POST'])] - public function new(Request $request, EntityManagerInterface $entityManager, TaskSchedulingService $taskSchedulingService, TaskRecommendationService $taskRecommendationService): Response + public function new(Request $request, EntityManagerInterface $entityManager, TaskRepository $tasks, TaskSchedulingService $taskSchedulingService, TaskRecommendationService $taskRecommendationService): Response { $task = new Task(); + $task->setScheduledFor($this->selectedDate($request)); + + $parentId = $request->query->getInt('parent'); + if ($parentId > 0) { + $parentTask = $tasks->find($parentId); + if (null === $parentTask || null !== $parentTask->getParentTask()) { + throw $this->createNotFoundException('Choose a top-level task as the parent.'); + } + + $task->setParentTask($parentTask); + } $form = $this->createForm(TaskType::class, $task, ['include_active' => false]); $form->handleRequest($request); @@ -76,9 +91,12 @@ final class TaskController extends AbstractController $selectedDate = $task->getScheduledFor() ?? new \DateTimeImmutable('today'); $newTask = (new Task())->setScheduledFor($selectedDate); + $taskGroups = $taskSchedulingService->taskGroupsForDate($batteryLevel, $selectedDate, $this->currentSettings()); + return $this->render('task/_task_composer_response.html.twig', [ 'form' => $this->createForm(TaskType::class, $newTask, ['include_active' => false]), - 'tasks' => $taskSchedulingService->tasksForDate($batteryLevel, $selectedDate, $this->currentSettings()), + 'tasks' => $taskGroups['tasks'], + 'subtasks_by_parent' => $taskGroups['subtasks_by_parent'], 'selected_date' => $selectedDate, ]); } @@ -169,15 +187,21 @@ final class TaskController extends AbstractController #[Route('/{id}', name: 'app_task_delete', methods: ['POST'])] - public function delete(Request $request, Task $task, EntityManagerInterface $entityManager): Response + public function delete(Request $request, Task $task, EntityManagerInterface $entityManager, TaskSchedulingService $taskSchedulingService, TaskRecommendationService $taskRecommendationService): Response { if ($this->isCsrfTokenValid('delete' . $task->getId(), $request->getPayload()->getString('_token'))) { + foreach ($task->getSubtasks()->toArray() as $subtask) { + $subtask->setParentTask(null); + } + $entityManager->remove($task); $entityManager->flush(); } if ($request->headers->has('HX-Request')) { - return new Response('', Response::HTTP_OK); + $batteryLevel = $taskRecommendationService->normaliseBatteryLevel($request->getSession()->get('social_battery', 3)); + + return $this->renderTaskList($taskSchedulingService, $taskRecommendationService, $batteryLevel, $this->selectedDate($request)); } return $this->redirectToRoute('app_task_index', [], Response::HTTP_SEE_OTHER); @@ -185,16 +209,22 @@ final class TaskController extends AbstractController private function renderTaskList(TaskSchedulingService $taskSchedulingService, TaskRecommendationService $taskRecommendationService, int $batteryLevel, \DateTimeImmutable $selectedDate): Response { + $taskGroups = $taskSchedulingService->taskGroupsForDate($batteryLevel, $selectedDate, $this->currentSettings()); + return $this->render('task/_task_list.html.twig', [ - 'tasks' => $taskSchedulingService->tasksForDate($batteryLevel, $selectedDate, $this->currentSettings()), + 'tasks' => $taskGroups['tasks'], + 'subtasks_by_parent' => $taskGroups['subtasks_by_parent'], 'selected_date' => $selectedDate, ]); } private function renderTaskRecommendations(TaskSchedulingService $taskSchedulingService, TaskRecommendationService $taskRecommendationService, int $batteryLevel, \DateTimeImmutable $selectedDate): Response { + $taskGroups = $taskSchedulingService->taskGroupsForDate($batteryLevel, $selectedDate, $this->currentSettings()); + return $this->render('task/_task_recommendations.html.twig', [ - 'tasks' => $taskSchedulingService->tasksForDate($batteryLevel, $selectedDate, $this->currentSettings()), + 'tasks' => $taskGroups['tasks'], + 'subtasks_by_parent' => $taskGroups['subtasks_by_parent'], 'battery_level' => $batteryLevel, 'battery_budget' => $taskRecommendationService->budgetFor($batteryLevel), 'selected_date' => $selectedDate, diff --git a/src/Entity/Task.php b/src/Entity/Task.php index cf0825d..e313743 100644 --- a/src/Entity/Task.php +++ b/src/Entity/Task.php @@ -3,9 +3,12 @@ namespace App\Entity; use App\Repository\TaskRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; +use Symfony\Component\Validator\Context\ExecutionContextInterface; #[ORM\Entity(repositoryClass: TaskRepository::class)] class Task @@ -67,6 +70,14 @@ class Task #[ORM\Column(length: 20, nullable: true)] private ?string $deferralSource = null; + #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'subtasks')] + #[ORM\JoinColumn(name: 'parent_task_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')] + private ?self $parentTask = null; + + /** @var Collection */ + #[ORM\OneToMany(mappedBy: 'parentTask', targetEntity: self::class)] + private Collection $subtasks; + #[ORM\Column] private ?\DateTimeImmutable $updatedAt = null; @@ -83,6 +94,7 @@ class Task $this->scheduledFor = $now->setTime(0, 0); $this->createdAt = $now; $this->updatedAt = $now; + $this->subtasks = new ArrayCollection(); } public function getId(): ?int @@ -276,6 +288,78 @@ class Task return $this; } + public function getParentTask(): ?self + { + return $this->parentTask; + } + + public function setParentTask(?self $parentTask): static + { + if ($this->parentTask === $parentTask) { + return $this; + } + + $this->parentTask?->subtasks->removeElement($this); + $this->parentTask = $parentTask; + + if (null !== $parentTask && !$parentTask->subtasks->contains($this)) { + $parentTask->subtasks->add($this); + } + + return $this; + } + + /** @return Collection */ + public function getSubtasks(): Collection + { + return $this->subtasks; + } + + public function addSubtask(self $subtask): static + { + $subtask->setParentTask($this); + + return $this; + } + + public function removeSubtask(self $subtask): static + { + if ($subtask->getParentTask() === $this) { + $subtask->setParentTask(null); + } + + return $this; + } + + #[Assert\Callback] + public function validateParentTask(ExecutionContextInterface $context): void + { + $parent = $this->parentTask; + $seen = []; + + while (null !== $parent) { + if ($parent === $this) { + $context->buildViolation('A task cannot be its own parent or create a circular parent relationship.') + ->atPath('parentTask') + ->addViolation(); + + return; + } + + $key = null === $parent->getId() ? 'new-' . spl_object_id($parent) : 'task-' . $parent->getId(); + if (isset($seen[$key])) { + $context->buildViolation('A task parent relationship cannot contain a cycle.') + ->atPath('parentTask') + ->addViolation(); + + return; + } + + $seen[$key] = true; + $parent = $parent->getParentTask(); + } + } + public function setRecurrenceUnit(?string $recurrenceUnit): static { $this->recurrenceUnit = $recurrenceUnit; diff --git a/src/Form/TaskType.php b/src/Form/TaskType.php index 63892a1..83533a7 100644 --- a/src/Form/TaskType.php +++ b/src/Form/TaskType.php @@ -3,6 +3,8 @@ namespace App\Form; use App\Entity\Task; +use App\Repository\TaskRepository; +use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; @@ -41,6 +43,15 @@ class TaskType extends AbstractType 'required' => false, 'attr' => ['placeholder' => 'e.g. Home'], ]) + ->add('parentTask', EntityType::class, [ + 'class' => Task::class, + 'label' => 'Parent task', + 'required' => false, + 'placeholder' => 'No parent (top-level task)', + 'choice_label' => static fn (Task $task): string => $task->getTitle() ?? '', + 'query_builder' => static fn (TaskRepository $repository) => $repository->createTopLevelParentQueryBuilder($options['data']), + 'help' => 'Optional. Sub-tasks remain independent tasks with their own schedule and completion state.', + ]) ->add('requiredOnSickDay', CheckboxType::class, [ 'label' => 'Required on sick days', 'required' => false, diff --git a/src/Repository/TaskRepository.php b/src/Repository/TaskRepository.php index 97061ee..2f83f02 100644 --- a/src/Repository/TaskRepository.php +++ b/src/Repository/TaskRepository.php @@ -4,6 +4,7 @@ namespace App\Repository; use App\Entity\Task; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; /** @@ -16,6 +17,26 @@ class TaskRepository extends ServiceEntityRepository parent::__construct($registry, Task::class); } + /** + * The first sub-task UI level is attached only to visible top-level tasks. + * Keeping this in the repository also prevents a task selecting itself or + * one of its children as a parent in the normal form. + */ + public function createTopLevelParentQueryBuilder(?Task $excluding = null): QueryBuilder + { + $queryBuilder = $this->createQueryBuilder('t') + ->andWhere('t.parentTask IS NULL') + ->orderBy('LOWER(t.title)', 'ASC'); + + if (null !== $excluding?->getId()) { + $queryBuilder + ->andWhere('t.id != :excludingTask') + ->setParameter('excludingTask', $excluding->getId()); + } + + return $queryBuilder; + } + // /** // * @return Task[] Returns an array of Task objects // */ diff --git a/src/Service/TaskSchedulingService.php b/src/Service/TaskSchedulingService.php index c5995cf..079c28e 100644 --- a/src/Service/TaskSchedulingService.php +++ b/src/Service/TaskSchedulingService.php @@ -21,12 +21,48 @@ final class TaskSchedulingService * @return Task[] */ public function tasksForDate(int $batteryLevel, \DateTimeImmutable $selectedDate, ?Settings $settings): array + { + return $this->taskGroupsForDate($batteryLevel, $selectedDate, $settings)['tasks']; + } + + /** + * @return array{tasks: Task[], subtasks_by_parent: array} + */ + public function taskGroupsForDate(int $batteryLevel, \DateTimeImmutable $selectedDate, ?Settings $settings): array { $tasks = $this->tasks->findAll(); $this->deferrals->deferMissedTasks($tasks, $selectedDate); $this->sickDays->applyForDate($tasks, $settings, $selectedDate); - return $this->recommendations->sort($tasks, $batteryLevel, $selectedDate, $settings); + $eligibleTasks = $this->recommendations->sort($tasks, $batteryLevel, $selectedDate, $settings); + $topLevelTasks = []; + $subtasksByParent = []; + $visibleParentIds = []; + + foreach ($eligibleTasks as $task) { + if (null !== $task->getParentTask()) { + continue; + } + + $topLevelTasks[] = $task; + if (null !== $task->getId()) { + $visibleParentIds[$task->getId()] = true; + } + } + + foreach ($eligibleTasks as $task) { + $parentId = $task->getParentTask()?->getId(); + if (null === $parentId || !isset($visibleParentIds[$parentId])) { + continue; + } + + $subtasksByParent[$parentId][] = $task; + } + + return [ + 'tasks' => $topLevelTasks, + 'subtasks_by_parent' => $subtasksByParent, + ]; } } diff --git a/templates/task/_task_composer.html.twig b/templates/task/_task_composer.html.twig index 5262232..fc76bd1 100644 --- a/templates/task/_task_composer.html.twig +++ b/templates/task/_task_composer.html.twig @@ -24,6 +24,7 @@
{{ form_row(form.estimatedDurationMinutes) }}
{{ form_row(form.priority) }}
{{ form_row(form.category) }}
+
{{ form_row(form.parentTask) }}
{{ form_row(form.requiredOnSickDay) }}
{{ form_row(form.deferDays) }}
diff --git a/templates/task/_task_composer_response.html.twig b/templates/task/_task_composer_response.html.twig index a219e22..b216a64 100644 --- a/templates/task/_task_composer_response.html.twig +++ b/templates/task/_task_composer_response.html.twig @@ -1,2 +1,2 @@ {{ include('task/_task_composer.html.twig', {form: form}) }} -{{ include('task/_task_list.html.twig', {tasks: tasks, selected_date: selected_date|default(null), out_of_band: true}) }} +{{ include('task/_task_list.html.twig', {tasks: tasks, subtasks_by_parent: subtasks_by_parent|default({}), selected_date: selected_date|default(null), out_of_band: true}) }} diff --git a/templates/task/_task_item.html.twig b/templates/task/_task_item.html.twig index 7839fc4..9070eda 100644 --- a/templates/task/_task_item.html.twig +++ b/templates/task/_task_item.html.twig @@ -1,67 +1,80 @@ -
  • -
    - - - - - {{ task.title }} - {% if task.isRecurring %} - Every {{ task.recurrenceInterval }} {{ task.recurrenceUnit }}{{ task.recurrenceInterval != 1 ? 's' : '' }} - {% endif %} -
    +
  • +
    +
    + + + + + {{ task.title }} + {% if task.isRecurring %} + Every {{ task.recurrenceInterval }} {{ task.recurrenceUnit }}{{ task.recurrenceInterval != 1 ? 's' : '' }} + {% endif %} +
    + + {% if task.isActive %} +
    + + + + + + + +
    + {% endif %} - {% if task.isActive %}
    - - - - - - + +
    + + + + {% if not is_subtask|default(false) %} + Add sub-task + {% endif %} +
    + + {% if subtasks|default([]) is not empty %} +
      + {% for subtask in subtasks %} + {{ include('task/_task_item.html.twig', {task: subtask, subtasks: [], is_subtask: true, selected_date: selected_date|default(null)}) }} + {% endfor %} +
    {% endif %} - -
    - - -
    - - - -
  • diff --git a/templates/task/_task_list.html.twig b/templates/task/_task_list.html.twig index bff253a..097e85a 100644 --- a/templates/task/_task_list.html.twig +++ b/templates/task/_task_list.html.twig @@ -1,6 +1,6 @@
      {% for task in tasks %} - {{ include('task/_task_item.html.twig', {task: task, selected_date: selected_date|default(null)}) }} + {{ include('task/_task_item.html.twig', {task: task, subtasks: subtasks_by_parent[task.id]|default([]), selected_date: selected_date|default(null)}) }} {% else %}
    • No tasks yet.
    • {% endfor %} diff --git a/templates/task/_task_recommendations.html.twig b/templates/task/_task_recommendations.html.twig index 0fcbed4..2e9a690 100644 --- a/templates/task/_task_recommendations.html.twig +++ b/templates/task/_task_recommendations.html.twig @@ -36,5 +36,5 @@ - {{ include('task/_task_list.html.twig', {tasks: tasks}) }} + {{ include('task/_task_list.html.twig', {tasks: tasks, subtasks_by_parent: subtasks_by_parent|default({}), selected_date: selected_date|default(null)}) }}