add logic finding index of prev and next topics

This commit is contained in:
Audrey Jensen 2023-07-19 18:38:12 +00:00
parent a1de87749d
commit 72ecfa89e0

View File

@ -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 = "<body><h3>Prev: " . $previousTopic . ". Next: " . $nextTopic . "</h3></body>";
return new Response($debug . $contents . $navButtons);
}
}
}