From 72ecfa89e08628712c729ead5204c00bbca23485 Mon Sep 17 00:00:00 2001 From: Audrey Jensen Date: Wed, 19 Jul 2023 18:38:12 +0000 Subject: [PATCH] add logic finding index of prev and next topics --- src/Controller/SafetyController.php | 50 +++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/src/Controller/SafetyController.php b/src/Controller/SafetyController.php index 1d19a26..2551eeb 100644 --- a/src/Controller/SafetyController.php +++ b/src/Controller/SafetyController.php @@ -46,14 +46,58 @@ class SafetyController extends AbstractController return $this->render('Training/Safety/SafetyQuiz.html.twig',['quiz'=>$form]); } #[Route('/Training/Safety/Topics/{topic?}')] - public function Topics(?string $topic) : Response + public function Topics(?string $topic, LoggerInterface $log) : Response { //We want to show the safety homepage if this is blank if($topic == null) { return $this->redirectToRoute('safetyHome'); } + + //We need to get the previous and next topics for nav buttons + //FEATURING: Regret for not having mapped this YAML file to a data model that would have saved myself a lot of trouble and spaghetti + $contentFile = Yaml::parseFile('../config/safetyLinks.yaml', 2, 2, Yaml::PARSE_OBJECT_FOR_MAP); + $previousTopic = $nextTopic = -1; //declare ahead of time for scoping + + $arrayKeys = array_keys($contentFile['Cards']); + foreach($arrayKeys as $key) + { + $card = $contentFile['Cards'][$key]; + for($i = 0; $i < count($card); $i++) + { + $entry = $card[$i]; + $cardIndex = array_search($key, $arrayKeys); + if($entry['url'] == $topic) + { + //Assign Previous Topic + if($i > 0) + { + $previousTopic = $i - 1; + } + else if($cardIndex > 0) //there's a previous card, use the last entry in it + { + $previousTopic = count($contentFile['Cards'][$arrayKeys[$cardIndex-1]]) -1; + } + + //Assign Next Topic + if($i < count($card)) //Next topic should be within the same card + { + $nextTopic = $i + 1; + } + else if($cardIndex > count($contentFile['Cards'])) //Next topic is not in same card, or does not exist + { + $nextTopic = $contentFile['Cards'][$arrayKeys[$cardIndex+1]][0]; //Set to first entry in next card + } + break 2; //Got what we needed, break out of both for loops + } + } + } + $topic = strtolower($topic); - return $this->render('Training/Safety/Topics/'.$topic.".html.twig"); + //TODO: Insert back button + $contents = $this->render('Training/Safety/Topics/'.$topic.".html.twig"); + $navButtons = $this->render('Training/Safety/navButtons.html.twig'); + $debug = "

Prev: " . $previousTopic . ". Next: " . $nextTopic . "

"; + return new Response($debug . $contents . $navButtons); } -} \ No newline at end of file +}