From 4075467465f60341f01734fed6a3168f3e1389b6 Mon Sep 17 00:00:00 2001 From: Matteo Date: Mon, 23 Feb 2026 14:39:54 +0100 Subject: [PATCH] feat : plan du site (WIP) --- .../BuildingInfrastructureFixtures.php | 299 ++++++++++++++++++ 1 file changed, 299 insertions(+) create mode 100644 src/DataFixtures/BuildingInfrastructureFixtures.php diff --git a/src/DataFixtures/BuildingInfrastructureFixtures.php b/src/DataFixtures/BuildingInfrastructureFixtures.php new file mode 100644 index 0000000..f95e873 --- /dev/null +++ b/src/DataFixtures/BuildingInfrastructureFixtures.php @@ -0,0 +1,299 @@ +loadStatuts($manager); + $buildings = $this->getBuildingsByCode($manager, ['B1', 'B2', 'B3']); + $layouts = $this->loadLayouts($manager, $buildings); + $cases = $this->loadBuildingCases($manager, $buildings, $statuts); + $this->loadCasePositions($manager, $layouts, $cases); + + $manager->flush(); + } + + public function getDependencies(): array + { + return [ + ReferenceFixtures::class, + ]; + } + + /** + * @return array + */ + private function loadStatuts(ObjectManager $manager): array + { + $repo = $manager->getRepository(Statut::class); + + $data = [ + ['label' => 'Libre', 'code' => 'LB', 'color' => '#A3B18A'], + ['label' => 'Occupé', 'code' => 'OC', 'color' => '#3A506B'], + ['label' => 'Malade', 'code' => 'ML', 'color' => '#E07A5F'], + ]; + + $result = []; + foreach ($data as $row) { + /** @var null|Statut $statut */ + $statut = $repo->findOneBy(['code' => $row['code']]); + if (!$statut instanceof Statut) { + $statut = new Statut() + ->setLabel($row['label']) + ->setCode($row['code']) + ->setColor($row['color']) + ; + $manager->persist($statut); + } + + $result[$row['code']] = $statut; + } + + return $result; + } + + /** + * @param list $codes + * + * @return array + */ + private function getBuildingsByCode(ObjectManager $manager, array $codes): array + { + $repo = $manager->getRepository(Building::class); + $result = []; + + foreach ($codes as $code) { + /** @var null|Building $building */ + $building = $repo->findOneBy(['code' => $code]); + if (!$building instanceof Building) { + throw new RuntimeException(sprintf('Building "%s" not found. Load ReferenceFixtures first.', $code)); + } + + $result[$code] = $building; + } + + return $result; + } + + /** + * @param array $buildings + * + * @return array + */ + private function loadLayouts(ObjectManager $manager, array $buildings): array + { + $repo = $manager->getRepository(BuildingLayout::class); + + $data = [ + ['name' => 'plan1', 'columns' => 23, 'rows' => 2, 'buildingCode' => 'B1'], + ['name' => 'plan2', 'columns' => 23, 'rows' => 2, 'buildingCode' => 'B2'], + ['name' => 'plan3', 'columns' => 23, 'rows' => 2, 'buildingCode' => 'B3'], + ]; + + $result = []; + foreach ($data as $row) { + /** @var null|BuildingLayout $layout */ + $layout = $repo->findOneBy(['name' => $row['name']]); + if (!$layout instanceof BuildingLayout) { + $layout = new BuildingLayout() + ->setName($row['name']) + ->setColumns($row['columns']) + ->setRows($row['rows']) + ->setIdBuilding($buildings[$row['buildingCode']]) + ; + $manager->persist($layout); + } + + $result[$row['buildingCode']] = $layout; + } + + return $result; + } + + /** + * @param array $buildings + * @param array $statuts + * + * @return array + */ + private function loadBuildingCases(ObjectManager $manager, array $buildings, array $statuts): array + { + $repo = $manager->getRepository(BuildingCase::class); + + $statusRanges = [ + // B1 + ['buildingCode' => 'B1', 'from' => 1, 'to' => 12, 'statut' => 'LB'], + ['buildingCode' => 'B1', 'from' => 13, 'to' => 24, 'statut' => 'OC'], + ['buildingCode' => 'B1', 'from' => 25, 'to' => 32, 'statut' => 'ML'], + ['buildingCode' => 'B1', 'from' => 33, 'to' => 44, 'statut' => 'LB'], + // B2 + ['buildingCode' => 'B2', 'from' => 1, 'to' => 10, 'statut' => 'OC'], + ['buildingCode' => 'B2', 'from' => 11, 'to' => 22, 'statut' => 'LB'], + ['buildingCode' => 'B2', 'from' => 23, 'to' => 30, 'statut' => 'ML'], + ['buildingCode' => 'B2', 'from' => 31, 'to' => 44, 'statut' => 'OC'], + // B3 + ['buildingCode' => 'B3', 'from' => 1, 'to' => 8, 'statut' => 'ML'], + ['buildingCode' => 'B3', 'from' => 9, 'to' => 20, 'statut' => 'LB'], + ['buildingCode' => 'B3', 'from' => 21, 'to' => 34, 'statut' => 'OC'], + ['buildingCode' => 'B3', 'from' => 35, 'to' => 44, 'statut' => 'ML'], + ]; + + $result = []; + foreach ($statusRanges as $range) { + for ($caseNumber = $range['from']; $caseNumber <= $range['to']; ++$caseNumber) { + $code = sprintf('%s-C%d', $range['buildingCode'], $caseNumber); + + if (isset($result[$code])) { + continue; + } + + /** @var null|BuildingCase $buildingCase */ + $buildingCase = $repo->findOneBy(['code' => $code]); + if (!$buildingCase instanceof BuildingCase) { + $buildingCase = new BuildingCase() + ->setCaseNumber($caseNumber) + ->setCode($code) + ->setCapacity(15) + ->setIdBuilding($buildings[$range['buildingCode']]) + ->setStatut($statuts[$range['statut']]) + ; + $manager->persist($buildingCase); + } + + $result[$code] = $buildingCase; + } + } + + return $result; + } + + /** + * @param array $layouts + * @param array $casesByCode + */ + private function loadCasePositions(ObjectManager $manager, array $layouts, array $casesByCode): void + { + $repo = $manager->getRepository(BuildingCasePosition::class); + + $layoutMap = [ + 'B1' => 'plan1', + 'B2' => 'plan2', + 'B3' => 'plan3', + ]; + + $slots = $this->buildSlotMap(); + + foreach ($layoutMap as $buildingCode => $layoutName) { + $layout = $layouts[$buildingCode] ?? null; + if (!$layout instanceof BuildingLayout || $layout->getName() !== $layoutName) { + throw new RuntimeException(sprintf('Layout "%s" for building "%s" not found.', $layoutName, $buildingCode)); + } + + foreach ($slots as $slot) { + $caseCode = sprintf('%s-C%d', $buildingCode, $slot['caseNumber']); + $buildingCase = $casesByCode[$caseCode] ?? null; + + if (!$buildingCase instanceof BuildingCase) { + throw new RuntimeException(sprintf('Building case "%s" not found.', $caseCode)); + } + + /** @var null|BuildingCasePosition $position */ + $position = $repo->findOneBy([ + 'buildingLayout' => $layout, + 'buildingCase' => $buildingCase, + 'x' => $slot['x'], + 'y' => $slot['y'], + ]); + + if ($position instanceof BuildingCasePosition) { + continue; + } + + $position = new BuildingCasePosition() + ->setX($slot['x']) + ->setY($slot['y']) + ->setW($slot['w']) + ->setH($slot['h']) + ->setRenderOrder((string) $slot['renderOrder']) + ->setBuildingLayout($layout) + ->setBuildingCase($buildingCase) + ; + $manager->persist($position); + } + } + } + + /** + * Reproduit le slot_map SQL (44 emplacements sur 2 lignes avec un gap en colonne 13). + * + * @return list + */ + private function buildSlotMap(): array + { + $slots = []; + + // Ligne 1, colonnes 1..12 => cases 13..24 + for ($c = 1; $c <= 12; ++$c) { + $slots[] = [ + 'x' => $c, + 'y' => 1, + 'w' => 1, + 'h' => 1, + 'renderOrder' => $c, + 'caseNumber' => $c + 12, + ]; + } + + // Ligne 1, colonnes 14..23 => cases 25..34 + for ($c = 14; $c <= 23; ++$c) { + $slots[] = [ + 'x' => $c, + 'y' => 1, + 'w' => 1, + 'h' => 1, + 'renderOrder' => $c - 1, + 'caseNumber' => $c + 11, + ]; + } + + // Ligne 2, colonnes 1..12 => cases 12..1 + for ($c = 1; $c <= 12; ++$c) { + $slots[] = [ + 'x' => $c, + 'y' => 2, + 'w' => 1, + 'h' => 1, + 'renderOrder' => 22 + $c, + 'caseNumber' => 13 - $c, + ]; + } + + // Ligne 2, colonnes 14..23 => cases 44..35 + for ($c = 14; $c <= 23; ++$c) { + $slots[] = [ + 'x' => $c, + 'y' => 2, + 'w' => 1, + 'h' => 1, + 'renderOrder' => 21 + $c, + 'caseNumber' => 58 - $c, + ]; + } + + return $slots; + } +}