2016-11-29 22:27:19 +00:00
|
|
|
<?php
|
2025-03-29 10:50:03 +00:00
|
|
|
|
2022-02-14 20:48:15 -08:00
|
|
|
/**
|
|
|
|
|
* Repesents a single navigation menu.
|
2016-11-29 22:27:19 +00:00
|
|
|
*
|
2022-02-14 20:48:15 -08:00
|
|
|
* @package NamelessMC\Core
|
|
|
|
|
* @author Samerton
|
2022-07-30 17:01:34 +02:00
|
|
|
* @version 2.0.0
|
2022-02-14 20:48:15 -08:00
|
|
|
* @license MIT
|
2016-11-29 22:27:19 +00:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
class Navigation
|
|
|
|
|
{
|
2022-02-14 20:48:15 -08:00
|
|
|
/**
|
|
|
|
|
* @var array Top navigation items.
|
|
|
|
|
*/
|
2021-10-29 22:00:05 -07:00
|
|
|
private array $_topNavbar = [];
|
2022-02-14 20:48:15 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array Footer navigation items.
|
|
|
|
|
*/
|
2021-10-29 22:00:05 -07:00
|
|
|
private array $_footerNav = [];
|
2022-02-14 20:48:15 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool Whether this nav bar is for StaffCP.
|
|
|
|
|
*/
|
2021-10-07 14:02:46 -07:00
|
|
|
private bool $_panel;
|
2020-12-30 12:19:09 +00:00
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
public function __construct(bool $panel = false)
|
|
|
|
|
{
|
2020-12-30 12:19:09 +00:00
|
|
|
$this->_panel = $panel;
|
|
|
|
|
}
|
2020-11-03 20:27:17 +01:00
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Add a simple item to this navigation instance.
|
2021-10-29 22:09:38 -07:00
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $name Unique name for the navbar item, if the page name equals this the item will display as active.
|
|
|
|
|
* @param string $title Item title.
|
|
|
|
|
* @param string $link HTML href attribute, can be link built with URL class or hyperlink.
|
|
|
|
|
* @param string $location Location to add item to, either 'top' or 'footer' (defaults to 'top').
|
|
|
|
|
* @param string|null $target HTML target attribute (eg '_blank').
|
|
|
|
|
* @param float $order Nav item order (default 10).
|
|
|
|
|
* @param string|null $icon Icon to prepend to nav item.
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2022-02-14 20:48:15 -08:00
|
|
|
public function add(
|
|
|
|
|
string $name,
|
|
|
|
|
string $title,
|
|
|
|
|
string $link,
|
|
|
|
|
string $location = 'top',
|
2025-03-29 10:50:03 +00:00
|
|
|
?string $target = null,
|
2022-05-28 12:01:28 +02:00
|
|
|
float $order = 10,
|
2022-02-14 20:48:15 -08:00
|
|
|
?string $icon = ''
|
|
|
|
|
): void {
|
2020-12-30 12:19:09 +00:00
|
|
|
if ($this->_panel && $location == 'top') {
|
|
|
|
|
// Discard order
|
|
|
|
|
// TODO: only a temporary solution to the link conflict issue in the StaffCP
|
|
|
|
|
if (count($this->_topNavbar)) {
|
|
|
|
|
$key = array_keys($this->_topNavbar)[count($this->_topNavbar) - 1];
|
|
|
|
|
$previous_order = $this->_topNavbar[$key]['order'];
|
|
|
|
|
} else {
|
|
|
|
|
$previous_order = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$order = $previous_order + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 20:27:17 +01:00
|
|
|
// Add the link to the navigation
|
2022-02-14 20:48:15 -08:00
|
|
|
if ($location === 'top') {
|
2020-11-03 20:27:17 +01:00
|
|
|
// Add to top navbar
|
2021-10-29 22:00:05 -07:00
|
|
|
$this->_topNavbar[$name] = [
|
2020-11-03 20:27:17 +01:00
|
|
|
'title' => $title,
|
|
|
|
|
'link' => $link,
|
|
|
|
|
'target' => $target,
|
2018-05-31 13:21:11 +01:00
|
|
|
'order' => $order,
|
2024-03-09 03:19:55 -08:00
|
|
|
'icon' => $icon,
|
2021-10-29 22:00:05 -07:00
|
|
|
];
|
2020-11-03 20:27:17 +01:00
|
|
|
} else {
|
|
|
|
|
// Add to footer navigation
|
2021-10-29 22:00:05 -07:00
|
|
|
$this->_footerNav[$name] = [
|
2020-11-03 20:27:17 +01:00
|
|
|
'title' => $title,
|
|
|
|
|
'link' => $link,
|
|
|
|
|
'target' => $target,
|
2018-05-31 13:21:11 +01:00
|
|
|
'order' => $order,
|
2024-03-09 03:19:55 -08:00
|
|
|
'icon' => $icon,
|
2021-10-29 22:00:05 -07:00
|
|
|
];
|
2020-11-03 20:27:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Add a dropdown menu to the navigation.
|
|
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $name Unique name for the dropdown
|
|
|
|
|
* @param string $title Dropdown title
|
2021-10-07 14:02:46 -07:00
|
|
|
* @param string $location Location to add item to, either 'top' or 'footer' (defaults to 'top').
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param int $order Nav item order (default 10).
|
|
|
|
|
* @param string $icon Icon to prepend to nav item.
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function addDropdown(string $name, string $title, string $location = 'top', int $order = 10, string $icon = ''): void
|
|
|
|
|
{
|
2022-07-30 17:01:34 +02:00
|
|
|
if ($this->_panel && $location == 'top') {
|
|
|
|
|
// Discard order
|
|
|
|
|
// TODO: only a temporary solution to the link conflict issue in the StaffCP
|
|
|
|
|
if (count($this->_topNavbar)) {
|
|
|
|
|
$key = array_keys($this->_topNavbar)[count($this->_topNavbar) - 1];
|
|
|
|
|
$previous_order = $this->_topNavbar[$key]['order'];
|
|
|
|
|
} else {
|
|
|
|
|
$previous_order = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$order = $previous_order + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 20:27:17 +01:00
|
|
|
// Add the dropdown
|
2020-12-13 20:42:04 -08:00
|
|
|
if ($location == 'top') {
|
2020-11-03 20:27:17 +01:00
|
|
|
// Navbar
|
2021-10-29 22:00:05 -07:00
|
|
|
$this->_topNavbar[$name] = [
|
2020-11-03 20:27:17 +01:00
|
|
|
'title' => $title,
|
2021-10-29 22:00:05 -07:00
|
|
|
'items' => [],
|
2018-05-31 13:21:11 +01:00
|
|
|
'order' => $order,
|
2024-03-09 03:19:55 -08:00
|
|
|
'icon' => $icon,
|
2021-10-29 22:00:05 -07:00
|
|
|
];
|
2020-11-03 20:27:17 +01:00
|
|
|
} else {
|
|
|
|
|
// Footer
|
2021-10-29 22:00:05 -07:00
|
|
|
$this->_footerNav[$name] = [
|
2020-11-03 20:27:17 +01:00
|
|
|
'title' => $title,
|
2021-10-29 22:00:05 -07:00
|
|
|
'items' => [],
|
2018-05-31 13:21:11 +01:00
|
|
|
'order' => $order,
|
2024-03-09 03:19:55 -08:00
|
|
|
'icon' => $icon,
|
2021-10-29 22:00:05 -07:00
|
|
|
];
|
2020-11-03 20:27:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Add an item to a menu dropdown.
|
|
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $dropdown Name of dropdown to add item to.
|
|
|
|
|
* @param string $name Unique name for the item, if the page name equals this the item will display as active.
|
|
|
|
|
* @param string $title Item title.
|
|
|
|
|
* @param string $link HTML href attribute, can be link built with URL class or hyperlink.
|
|
|
|
|
* @param string $location Location to add item to, either 'top' or 'footer' (defaults to 'top').
|
|
|
|
|
* @param string|null $target HTML target attribute (eg '_blank')
|
|
|
|
|
* @param string $icon Icon to prepend to nav item
|
|
|
|
|
* @param int $order Nav item order
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2025-03-29 10:50:03 +00:00
|
|
|
public function addItemToDropdown(string $dropdown, string $name, string $title, string $link, string $location = 'top', ?string $target = null, string $icon = '', int $order = 10): void
|
2024-03-09 03:19:55 -08:00
|
|
|
{
|
2020-11-03 20:27:17 +01:00
|
|
|
// Add the item
|
|
|
|
|
if ($location == 'top' && isset($this->_topNavbar[$dropdown])) {
|
|
|
|
|
// Navbar
|
2021-10-29 22:00:05 -07:00
|
|
|
$this->_topNavbar[$dropdown]['items'][$name] = [
|
2020-11-03 20:27:17 +01:00
|
|
|
'title' => $title,
|
|
|
|
|
'link' => $link,
|
|
|
|
|
'target' => $target,
|
2018-09-23 22:56:16 +01:00
|
|
|
'icon' => $icon,
|
2023-03-23 20:01:23 -07:00
|
|
|
'order' => $order,
|
2021-10-29 22:00:05 -07:00
|
|
|
];
|
2024-03-09 03:19:55 -08:00
|
|
|
} elseif (isset($this->_footerNav[$dropdown])) {
|
2022-02-14 20:48:15 -08:00
|
|
|
// Footer
|
|
|
|
|
$this->_footerNav[$dropdown]['items'][$name] = [
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'link' => $link,
|
|
|
|
|
'target' => $target,
|
|
|
|
|
'icon' => $icon,
|
2023-03-23 20:01:23 -07:00
|
|
|
'order' => $order,
|
2022-02-14 20:48:15 -08:00
|
|
|
];
|
2020-11-03 20:27:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-14 20:58:17 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return top navigation.
|
|
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $location Either 'top' or 'footer' (defaults to 'top').
|
|
|
|
|
* @return array Array to pass to template
|
2021-11-14 20:58:17 -08:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function returnNav(string $location = 'top'): array
|
|
|
|
|
{
|
2021-11-14 20:58:17 -08:00
|
|
|
$return = []; // String to return
|
|
|
|
|
if ($location == 'top') {
|
|
|
|
|
if (count($this->_topNavbar)) {
|
|
|
|
|
foreach ($this->_topNavbar as $key => $item) {
|
|
|
|
|
$return[$key] = $item;
|
|
|
|
|
if (defined('PAGE') && PAGE == $key) {
|
|
|
|
|
$return[$key]['active'] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort dropdown
|
|
|
|
|
if (isset($return[$key]['items'])) {
|
|
|
|
|
if (count($return[$key]['items'])) {
|
|
|
|
|
uasort(
|
|
|
|
|
$return[$key]['items'],
|
2022-01-15 15:18:05 -08:00
|
|
|
static function ($a, $b) {
|
2021-11-14 20:58:17 -08:00
|
|
|
if ($a['order'] > $b['order']) {
|
|
|
|
|
return 1;
|
2022-01-15 15:18:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($a['order'] < $b['order']) {
|
|
|
|
|
return -1;
|
2021-11-14 20:58:17 -08:00
|
|
|
}
|
2024-03-09 03:19:55 -08:00
|
|
|
|
2021-11-14 20:58:17 -08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
unset($return[$key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-09 03:19:55 -08:00
|
|
|
} elseif (count($this->_footerNav)) {
|
2022-02-14 20:48:15 -08:00
|
|
|
foreach ($this->_footerNav as $key => $item) {
|
|
|
|
|
$return[$key] = $item;
|
|
|
|
|
if (defined('PAGE') && PAGE == $key) {
|
|
|
|
|
$return[$key]['active'] = true;
|
|
|
|
|
}
|
2021-11-14 20:58:17 -08:00
|
|
|
|
2022-02-14 20:48:15 -08:00
|
|
|
// Sort dropdown
|
|
|
|
|
if (isset($return[$key]['items']) && count($return[$key]['items'])) {
|
|
|
|
|
uasort(
|
|
|
|
|
$return[$key]['items'],
|
|
|
|
|
static function ($a, $b) {
|
|
|
|
|
if ($a['order'] > $b['order']) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2022-01-15 15:18:05 -08:00
|
|
|
|
2022-02-14 20:48:15 -08:00
|
|
|
if ($a['order'] < $b['order']) {
|
|
|
|
|
return -1;
|
2021-11-14 20:58:17 -08:00
|
|
|
}
|
2024-03-09 03:19:55 -08:00
|
|
|
|
2022-02-14 20:48:15 -08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-11-14 20:58:17 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-15 15:18:05 -08:00
|
|
|
uasort($return, static function ($a, $b) {
|
2021-11-14 20:58:17 -08:00
|
|
|
$result = 0;
|
|
|
|
|
if ($a['order'] > $b['order']) {
|
|
|
|
|
$result = 1;
|
2024-03-09 03:19:55 -08:00
|
|
|
} elseif ($a['order'] < $b['order']) {
|
2022-02-14 20:48:15 -08:00
|
|
|
$result = -1;
|
2021-11-14 20:58:17 -08:00
|
|
|
}
|
2024-03-09 03:19:55 -08:00
|
|
|
|
2021-11-14 20:58:17 -08:00
|
|
|
return $result;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
|
}
|
2019-02-02 21:59:46 +01:00
|
|
|
}
|