<?php
class NavigationDef {
/* Properties */
private $topFolder = "Navi-Projekt";
private $structure;
/* Constructor
Parameter: array with the structure of the navigation */
public function __construct($navArr) {
$this->structure = $navArr;
}
/* Method:
build the HTML-code for the navigation
Parameter: First and second level of the navigation */
public function writeNav($firstLvl,$secondLvl) {
$code = "";
$code .= "<nav>\n";
// Home Link
$code .= "<div id=\"home_link\">\n";
$code .= "<a href=\"".$this->pathHelperHome()."index.php\">Home</a>";
$code .= "</div>\n";
// Main part of navigation:
$code .= "<ul class=\"menu\">\n";
// The 1st Level of the navigation
$counter1 = 1;
foreach ($this->structure as $key => $out1) {
$code .= "<!-- ******* Menu item ".$counter1." ******* -->\n";
$code .= "<li";
if ($counter1 == $firstLvl) {
$code .= " class=\"current\"";
}
$code .= ">\n";
// Is it a menu item with a dropdown or just one single link?
if (count($out1) == 1) {
// It's a single link WITHOUT a dropdown
$path = $this->pathHelper2ndLvl($key).$out1[0]['fileName'];
$code .= "<a class=\"nav-link\" href=\"".$path."\">".$key."</a>\n";
}
else {
// It's menu item WITH a dropdown
$code .= "<a class=\"nav-link dropdown-btn\"";
$code .= " id=\"dropdownBtn".$counter1."\" data-dropdown=\"dropdown".$counter1."\"";
$code .= " aria-haspopup=\"true\" aria-expanded=\"false\" href=\"#\">";
$code .= $key;
$code .= " ▾</a>\n";
// The 2nd Level of the navigation
$code .= "<!-- Start dropdwn -->\n";
$code .= "<ul class=\"dropdown\" aria-labelledby=\"dropdownBtn".$counter1."\" id=\"dropdown".$counter1."\">\n";
$counter2 = 0;
foreach ($out1 as $out2) {
$code .= "<li";
if ($counter1 == $firstLvl && $counter2 == $secondLvl) {
$code .= " class=\"current\"";
}
$code .= ">\n";
$path = $this->pathHelper2ndLvl($key).$out2['fileName'];
$code .= "<a href=\"".$path."\">".$out2['linkText']."</a>";
$code .= "</li>\n";
$counter2++;
}
$code .= "</ul>\n";
$code .= "<!-- End dropdown -->\n";
}
$code .= "</li>\n";
$counter1++;
}
$code .= "</ul>\n";
// Hamburger
$code .= "<div id=\"hamburger\">\n";
$code .= "<button type=\"button\" aria-haspopup=\"true\" aria-expanded=\"false\" aria-label=\"Open/Close Navigation\">☰</button>\n";
$code .= "</div>\n";
$code .= "</nav>\n";
return $code;
}
/* Helper-Methods: There is no need for that,
if you are using this project in a virtual host */
private function pathHelperHome() {
if (basename(getcwd()) != $this->topFolder) {
return "../";
}
}
private function pathHelper2ndLvl($folder) {
if (basename(getcwd()) == $this->topFolder) {
return $folder."/";
}
elseif (basename(getcwd()) != $folder) {
return "../".$folder."/";
}
}
/* NOTCH START */
/* Write a sitmap.xml */
public function writeSitemap() {
// Your domain
$domain = "https://mydomain.ch/";
// Array für error reporting
$errors = [];
$code = "";
// XML prolog
$code .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
// XML root element
$code .= "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n";
// Add the URL of the homepage
$code .= "\t<url>\r\n";
$home = $domain."index.php";
$code .= "\t\t<loc>".$home."</loc>\r\n";
$fileTimeHome = filemtime("index.php");
$lastModifiedHome = date("Y-m-d", $fileTimeHome);
$code .= "\t\t<lastmod>".$lastModifiedHome."</lastmod>\r\n";
$code .= "\t</url>\r\n";
// All pages
foreach ($this->structure as $key => $out1) {
$folder = $key."/";
foreach ($out1 as $out2) {
$file = $folder.$out2['fileName'];
if (file_exists($file)) {
$code .= "\t<url>\n";
$url = $domain.$file;
$code .= "\t\t<loc>".$url."</loc>\r\n";
$fileTime = filemtime($file);
$lastModified = date("Y-m-d", $fileTime);
$code .= "\t\t<lastmod>".$lastModified."</lastmod>\r\n";
$code .= "\t</url>\r\n";
}
else {
$errors[] = "Nicht gefunden: ".$file;
}
}
}
// End XML root element
$code .= "</urlset>\r\n";
// Write Sitemap-File
$handle = fopen("sitemap.xml","w");
fwrite($handle,$code);
fclose($handle);
// Are there errors?
$nrErrors = count($errors);
if ($nrErrors > 0) {
$str = "Ich habe die Sitemap geschrieben. Leider konnte ich nicht alle Files finden:<br>";
foreach ($errors as $out3){
$str .= $out3."<br>";
}
$str .= "<br><strong>Bitte checke das Array mit der Struktur nochmals!</strong>";
return $str;
}
else {
return "Es gab keine Fehler. Habe das Sitemap-File geschrieben.";
}
}
/* NOTCH END */
}
?>