mc-cms-namelessmc/core/classes/Core/Pages.php

202 lines
4.6 KiB
PHP
Raw Permalink Normal View History

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