diff --git a/core/classes/Core/Pages.php b/core/classes/Core/Pages.php index ce65d61033..ee6ba1e8fa 100644 --- a/core/classes/Core/Pages.php +++ b/core/classes/Core/Pages.php @@ -48,6 +48,7 @@ public function add(string $module, string $url, string $file, string $name = '' { $this->_pages[$url] = [ 'module' => $module, + 'url' => $url, 'file' => $file, 'name' => $name, 'widgets' => $widgets, @@ -66,6 +67,7 @@ public function addCustom(string $url, string $name, bool $widgets = false): voi { $this->_pages[$url] = [ 'module' => 'Core', + 'url' => $url, 'file' => 'pages/custom.php', 'name' => $name, 'widgets' => $widgets, @@ -126,8 +128,8 @@ public function getSitemapMethods(): array /** * Get page by ID. * - * @param int $page_id ID of page to find. - * @return array Page information. + * @param int $page_id ID of page to find. + * @return array|null Page information. */ public function getPageById(int $page_id): ?array { @@ -145,8 +147,8 @@ public function getPageById(int $page_id): ?array /** * Get page by URL. * - * @param string $url URL of page to find. - * @return array Page information. + * @param string $url URL of page to find. + * @return array|null Page information. */ public function getPageByURL(string $url): ?array { @@ -161,6 +163,36 @@ public function getPageByURL(string $url): ?array return null; } + /** + * Get page by name. + * + * @param string $name + * @return array|null Page information. + */ + public function getPageByName(string $name): ?array + { + foreach ($this->_pages as $key => $page) { + if ($page['name'] == $name) { + $page['key'] = $key; + + return $page; + } + } + + return null; + } + + /** + * Returns all pages belonging to a particular module. + * + * @param string $module + * @return array + */ + public function getPagesByModule(string $module): array + { + return array_filter($this->_pages, fn ($page) => $page['module'] === $module); + } + /** * Get the page details the user currently viewing. * diff --git a/core/classes/Core/URL.php b/core/classes/Core/URL.php index 7c799b8216..ad015bec8e 100644 --- a/core/classes/Core/URL.php +++ b/core/classes/Core/URL.php @@ -188,4 +188,36 @@ public static function urlSafe(string $text): string return urlencode(strtolower(str_replace(' ', '-', $text))); } + + /** + * Breaks any URI into segments (without a leading/closing “/”). + * + * @param string|null $uri If null — takes $_SERVER['REQUEST_URI'] + * @return string[] Array of segments + */ + public static function getSegments(?string $uri = null): array + { + if ($uri === null) { + $uri = $_SERVER['REQUEST_URI'] ?? '/'; + } + $path = parse_url($uri, PHP_URL_PATH) ?: ''; + // We remove the presenter and closing slash, break by "/" + $parts = array_filter(explode('/', trim($path, '/')), fn ($s) => $s !== ''); + + return array_values($parts); + } + + /** + * Returns a specific index segment (0-Based) or $ default if absent. + * + * @param int $index Segment index (0 - first) + * @param string|null $default The default value + * @return string|null + */ + public static function segment(int $index, ?string $default = null): ?string + { + $segments = self::getSegments(); + + return $segments[$index] ?? $default; + } }