jimsite/scripts/nav.php
2023-06-04 08:43:55 -04:00

45 lines
1.7 KiB
PHP

<?php
function page_comparator($a,$b){
//This function is the sorting criterion for the later call of usort, which will sort the page objects
//from pages.json in increasing order of their "index" value.
return $a->index <=> $b->index;
//For some reason, PHP8 requires the spaceship operator (<=>)
//instead of a greater than symbol.
}
function gen_nav_element($page){
$iframe = false;
//By default, echo the contents of a file instead of embedding it in an iframe.
if(isset($page->file)){
$href = "index.php?page=".$page->query_value;
$target = "_self";
//If the page is associated with a file, then point the navibar href to the file's query value.
$iframe = (isset($page->iframe)) ? true : false;
//If the page has the iframe attribute, then keep track of that.
} elseif(isset($page->link)) {
$href = $page->link;
$target = "_blank";
//If instead the page is associated with an external link, then point the navibar href there and make it open in a new window.
}
echo "<a href='".$href."' target='".$target."'><div class='nav-tab'><span class='nav-text'>".$page->name."</span></div></a>";
}
$page_array = get_object_vars($pages);
//Convert $pages from object to an associative array of [key=>val, key=>val]
//where the keys are the names of the page objects from the JSON file and the
//vals are the page objects themselves.
usort($page_array,"page_comparator");
//Sort the pages in order of increasing "index" value.
foreach($page_array as $key=>$p){
if($p->index>-1){
gen_nav_element($p);
//Pages with indices less than 0 are hidden from the navibar.
}
}
?>