breadcrumb

This commit is contained in:
Alessandro Ferro 2022-03-07 12:00:41 +01:00
parent dbf55c4c5b
commit 8f542ffd02
4 changed files with 117 additions and 3 deletions

View File

@ -0,0 +1,20 @@
<html>
<head>
<title>Card example</title>
</head>
<body>
<?php
include "../phpxpress/breadcrumb.php";
$links["Github"] = "https://www.github.com";
$links["xfarrow"] = "https://www.github.com/xfarrow";
$links["PhpXpress"] = "https://www.github.com/xfarrow/phoxpress";
$breadcrumb = new BreadCrumb;
$breadcrumb->setDataSource($links);
$breadcrumb->draw();
?>
</body>
</html>

View File

@ -0,0 +1,94 @@
<?php
include "include.php";
class BreadCrumb{
private $locations;
// unless otherwise specified via stating 'setActiveLocation(-1)'
// the active location will be the last element
private $activeLocation;
// unless otherwise specified, the divider character will be: "/"
private $divider;
/*
** The datasource must be an associative array.
** array("Home" => "xyz.com/home" , "Starred" => "xyz.com/home/starred", "Reading" => "xyz.com/home/starred/reading");
** Will produce:
** Home / Starred / Reading
*/
function setDataSource($dataSource){
if(!is_array($dataSource))
throw new InvalidArgumentException('Parameter dataSource must be an array.');
foreach($dataSource as $caption => $link){
$this->addElement($caption, $link);
}
}
/*
** Adds an element at the end of the Breadcrumb
*/
function addElement($caption, $link){
if(isset($this->locations))
$this->locations[$caption] = $link;
else
$this->locations = array($caption => $link);
$this->activeLocation = $caption;
}
/*
** You can specify either a scalar (the (n-1)th to be activated)
** or a string (the caption to be activated)
*/
function setActiveLocation($activeLocation){
if(!is_scalar($activeLocation) && !is_string($activeLocation))
throw new InvalidArgumentException('Parameter activeLocation can be either an int or a string. None of these provided.');
$this->activeLocation = $activeLocation;
}
function setDivider($divider){
$this->divider = $divider;
}
function draw(){
$iterator = 0;
if(!isset($this->divider))
echo '<nav aria-label="breadcrumb">';
else
echo '<nav style="--bs-breadcrumb-divider: \'' . $this->divider . '\';" aria-label="breadcrumb">';
echo '<ol class="breadcrumb">';
foreach($this->locations as $caption => $link){
// active location
if(
(is_int($this->activeLocation) && $this->activeLocation == $iterator)
|| (is_string($this->activeLocation) && $this->activeLocation == $caption)
){
echo '<li class="breadcrumb-item active" aria-current="page">' . $caption . '</li>';
}
// non active location
else{
echo '<li class="breadcrumb-item">';
echo '<a href="' . $link . '">' . $caption;
echo '</a></li>';
}
$iterator++;
}
echo '</ol></nav>';
}
}
?>

View File

@ -68,7 +68,7 @@
} }
function setDataSource($datasource){ function setDataSource($datasource){
foreach($datasource as $caption=>$value){ foreach($datasource as $caption => $value){
$this->addField($caption,$value); $this->addField($caption,$value);
} }
} }

View File

@ -54,11 +54,11 @@
$this->dataSource = $dataSource; $this->dataSource = $dataSource;
// if array of objects provided // if array of objects provided
if(is_object($this->dataSource[0])) if(is_object($dataSource[0]))
$this->columnCaptions = array_keys(get_object_vars($this->dataSource[0])); $this->columnCaptions = array_keys(get_object_vars($this->dataSource[0]));
// if array of arrays provided // if array of arrays provided
else else if(is_array($dataSource[0]))
$this->columnCaptions = array_keys($this->dataSource[0]); $this->columnCaptions = array_keys($this->dataSource[0]);
} }