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
|
|
|
/**
|
|
|
|
|
* Contains data about all the registered pages in the application.
|
2016-11-29 22:27:19 +00:00
|
|
|
*
|
2022-02-14 20:48:15 -08:00
|
|
|
* @package NamelessMC\Core
|
|
|
|
|
* @author Samerton
|
|
|
|
|
* @version 2.0.0-pr8
|
|
|
|
|
* @license MIT
|
2018-08-19 16:50:36 +01:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
class Pages
|
|
|
|
|
{
|
2022-02-14 20:48:15 -08:00
|
|
|
/**
|
|
|
|
|
* @var array Array of all the registered pages.
|
|
|
|
|
*/
|
2021-10-07 14:02:46 -07:00
|
|
|
private array $_pages;
|
2022-02-14 20:48:15 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array Array of data about the active page.
|
|
|
|
|
*/
|
2022-06-14 18:21:48 +02:00
|
|
|
private array $_active_page = [];
|
2022-02-14 20:48:15 -08:00
|
|
|
|
|
|
|
|
/**
|
2022-04-01 08:57:13 -07:00
|
|
|
* @var array<callable> Array of sitemap files and methods.
|
2022-02-14 20:48:15 -08:00
|
|
|
*/
|
2021-12-05 08:17:24 -08:00
|
|
|
private array $_sm_methods = [];
|
2022-02-14 20:48:15 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array Array of URLs to call with ajax.
|
|
|
|
|
*/
|
2021-10-29 22:00:05 -07:00
|
|
|
private array $_ajax_requests = [];
|
2018-08-19 16:50:36 +01:00
|
|
|
|
2022-02-14 20:48:15 -08:00
|
|
|
/**
|
|
|
|
|
* @var int ID of last created page.
|
|
|
|
|
*/
|
2021-10-07 14:02:46 -07:00
|
|
|
private int $_id = 1;
|
2018-08-19 16:50:36 +01:00
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Defines a page and assigns it to a module.
|
|
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $module Module which the page belongs to.
|
|
|
|
|
* @param string $url URL string.
|
|
|
|
|
* @param string $file Path (from module folder) to page file.
|
|
|
|
|
* @param string $name Name of page.
|
|
|
|
|
* @param bool $widgets Can widgets be used on the page? Default false.
|
|
|
|
|
*/
|
|
|
|
|
public function add(string $module, string $url, string $file, string $name = '', bool $widgets = false): void
|
|
|
|
|
{
|
2021-10-29 22:00:05 -07:00
|
|
|
$this->_pages[$url] = [
|
2020-12-13 19:38:04 +01:00
|
|
|
'module' => $module,
|
|
|
|
|
'file' => $file,
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'widgets' => $widgets,
|
2024-03-09 03:19:55 -08:00
|
|
|
'id' => $this->_id++,
|
2021-10-29 22:00:05 -07:00
|
|
|
];
|
2020-12-13 19:38:04 +01:00
|
|
|
}
|
2016-11-29 22:27:19 +00:00
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Defines a custom page.
|
|
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $url URL string.
|
|
|
|
|
* @param string $name Name of page.
|
|
|
|
|
* @param bool $widgets Can widgets be used on the page? Default false.
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function addCustom(string $url, string $name, bool $widgets = false): void
|
|
|
|
|
{
|
2021-10-29 22:00:05 -07:00
|
|
|
$this->_pages[$url] = [
|
2020-12-13 19:38:04 +01:00
|
|
|
'module' => 'Core',
|
2024-07-14 11:39:01 +02:00
|
|
|
'file' => 'pages/custom.php',
|
2020-12-13 19:38:04 +01:00
|
|
|
'name' => $name,
|
|
|
|
|
'widgets' => $widgets,
|
|
|
|
|
'custom' => true,
|
2024-03-09 03:19:55 -08:00
|
|
|
'id' => $this->_id++,
|
2021-10-29 22:00:05 -07:00
|
|
|
];
|
2020-12-13 19:38:04 +01:00
|
|
|
}
|
2017-10-06 17:32:50 +01:00
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Get array of all pages.
|
2021-12-07 22:14:12 -08:00
|
|
|
*
|
2021-04-12 18:36:34 -07:00
|
|
|
* @return array All pages.
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function returnPages(): array
|
|
|
|
|
{
|
2020-12-13 19:38:04 +01:00
|
|
|
return $this->_pages;
|
|
|
|
|
}
|
2017-08-08 19:38:42 +01:00
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Return pages which allow widgets.
|
2021-12-07 22:14:12 -08:00
|
|
|
*
|
2021-04-12 18:36:34 -07:00
|
|
|
* @return array All pages which allow widgets.
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function returnWidgetPages(): array
|
|
|
|
|
{
|
2021-10-29 22:00:05 -07:00
|
|
|
$ret = [];
|
2021-04-12 18:36:34 -07:00
|
|
|
|
|
|
|
|
foreach ($this->_pages as $page) {
|
|
|
|
|
if (!empty($page['name']) && $page['widgets'] === true) {
|
2020-12-13 19:38:04 +01:00
|
|
|
$ret[$page['module']][$page['name']] = true;
|
2021-04-12 18:36:34 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-08-19 16:50:36 +01:00
|
|
|
|
2020-12-13 19:38:04 +01:00
|
|
|
return $ret;
|
|
|
|
|
}
|
2018-08-19 16:50:36 +01:00
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Register a method for sitemap generation.
|
2022-02-14 20:48:15 -08:00
|
|
|
* @see \SitemapPHP\Sitemap
|
2022-03-29 15:09:21 -07:00
|
|
|
*
|
2022-04-01 08:57:13 -07:00
|
|
|
* @param callable $method Array callable of the sitemap class and method to execute.
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function registerSitemapMethod(callable $method): void
|
|
|
|
|
{
|
2022-03-29 15:09:21 -07:00
|
|
|
$this->_sm_methods[] = $method;
|
2020-12-13 19:38:04 +01:00
|
|
|
}
|
2018-08-19 16:50:36 +01:00
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Get registered sitemap methods.
|
2021-12-07 22:14:12 -08:00
|
|
|
*
|
2023-03-23 20:01:23 -07:00
|
|
|
* @return array<callable> Array of sitemap methods.
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function getSitemapMethods(): array
|
|
|
|
|
{
|
2020-12-13 19:38:04 +01:00
|
|
|
return $this->_sm_methods;
|
|
|
|
|
}
|
2018-08-23 22:43:15 +01:00
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
2024-03-09 03:19:55 -08:00
|
|
|
* Get page by ID.
|
2021-04-12 18:36:34 -07:00
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param int $page_id ID of page to find.
|
2021-04-12 18:36:34 -07:00
|
|
|
* @return array Page information.
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function getPageById(int $page_id): ?array
|
|
|
|
|
{
|
2022-02-14 20:48:15 -08:00
|
|
|
foreach ($this->_pages as $key => $page) {
|
|
|
|
|
if ($page['id'] == $page_id) {
|
|
|
|
|
$page['key'] = $key;
|
2024-03-09 03:19:55 -08:00
|
|
|
|
2022-02-14 20:48:15 -08:00
|
|
|
return $page;
|
2020-12-13 19:38:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-12 18:36:34 -07:00
|
|
|
|
2020-12-13 19:38:04 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
2021-04-12 18:36:34 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get page by URL.
|
|
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $url URL of page to find.
|
|
|
|
|
* @return array Page information.
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function getPageByURL(string $url): ?array
|
|
|
|
|
{
|
2022-02-14 20:48:15 -08:00
|
|
|
foreach ($this->_pages as $key => $page) {
|
|
|
|
|
if ($key == $url) {
|
|
|
|
|
$page['key'] = $key;
|
2024-03-09 03:19:55 -08:00
|
|
|
|
2022-02-14 20:48:15 -08:00
|
|
|
return $page;
|
2020-12-26 20:02:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-12 18:36:34 -07:00
|
|
|
|
2020-12-26 20:02:30 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
2021-04-12 18:36:34 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the page details the user currently viewing.
|
|
|
|
|
*
|
|
|
|
|
* @return array Details of current page.
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function getActivePage(): array
|
|
|
|
|
{
|
2021-12-07 22:14:12 -08:00
|
|
|
return $this->_active_page;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the page the user currently viewing.
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function setActivePage(array $page): void
|
|
|
|
|
{
|
2021-12-07 22:14:12 -08:00
|
|
|
$this->_active_page = $page;
|
|
|
|
|
}
|
2018-09-10 01:14:58 +01:00
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Add a script for Javascript to perform a GET request to.
|
|
|
|
|
*
|
2022-02-14 20:48:15 -08:00
|
|
|
* @param string $script URL of js script to add.
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function addAjaxScript(string $script): void
|
|
|
|
|
{
|
2022-02-14 20:48:15 -08:00
|
|
|
$this->_ajax_requests[] = $script;
|
2020-12-13 19:38:04 +01:00
|
|
|
}
|
2018-09-10 01:14:58 +01:00
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Get scripts for Javascript to perform a GET request to.
|
|
|
|
|
*
|
|
|
|
|
* @return array All registered ajax script URLs.
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function getAjaxScripts(): array
|
|
|
|
|
{
|
2020-12-13 19:38:04 +01:00
|
|
|
return $this->_ajax_requests;
|
|
|
|
|
}
|
|
|
|
|
}
|