current init

This commit is contained in:
Marek
2026-03-30 15:42:44 +02:00
parent c5229e48ed
commit 2f96caaa23
366 changed files with 6093 additions and 11029 deletions

View File

@@ -0,0 +1,92 @@
<?php
namespace App\Service;
use App\Entity\Task;
use App\Entity\TaskSchema;
use App\Enum\TaskSchemaType;
use App\Repository\TaskRepository;
use Doctrine\ORM\EntityManagerInterface;
class TaskSynchronizer
{
public function __construct(
private EntityManagerInterface $em,
private TaskRepository $taskRepository,
private DeadlineCalculator $deadlineCalculator,
) {}
public function syncForSchema(TaskSchema $schema): void
{
$today = new \DateTimeImmutable('today');
// Range: bis endDate oder mindestens +6 Tage
$minEnd = $today->modify('+6 days');
$end = $schema->getEndDate()
? new \DateTimeImmutable($schema->getEndDate()->format('Y-m-d'))
: $minEnd;
if ($end < $minEnd) {
$end = $minEnd;
}
// Soll-Termine berechnen
$deadlines = $this->deadlineCalculator->getDeadlinesForRange($schema, $today, $end);
$shouldExist = [];
foreach ($deadlines as $dl) {
$shouldExist[$dl->format('Y-m-d')] = true;
}
// Alle zukünftigen Tasks laden (mit Datum)
$futureTasks = $this->taskRepository->findByTaskFromDate($schema, $today);
$existingByDate = [];
foreach ($futureTasks as $occ) {
$existingByDate[$occ->getDate()->format('Y-m-d')] = $occ;
}
// Null-Datum Tasks laden
$nullDateTasks = $this->taskRepository->findBy(['schema' => $schema, 'date' => null]);
// Nicht mehr im Schema -> entfernen
foreach ($existingByDate as $dateKey => $occ) {
if (!isset($shouldExist[$dateKey])) {
$this->em->remove($occ);
unset($existingByDate[$dateKey]);
}
}
// Einzel ohne Deadline: null-date Task behalten
if ($schema->getTaskType() === TaskSchemaType::Single && $schema->getDeadline() === null) {
foreach ($nullDateTasks as $occ) {
$occ->setName(null);
$occ->setCategory(null);
$occ->setCategoryOverridden(false);
}
} else {
// Sonst null-date Tasks entfernen
foreach ($nullDateTasks as $occ) {
$this->em->remove($occ);
}
}
// Bestehende zukünftige Overrides zurücksetzen
foreach ($existingByDate as $occ) {
$occ->setName(null);
$occ->setCategory(null);
$occ->setCategoryOverridden(false);
}
// Fehlende Tasks erstellen
foreach ($deadlines as $dl) {
$dateKey = $dl->format('Y-m-d');
if (!isset($existingByDate[$dateKey])) {
$occ = new Task();
$occ->setSchema($schema);
$occ->setDate(new \DateTime($dateKey));
$this->em->persist($occ);
}
}
$this->em->flush();
}
}