Files
cheese-cake/src/View/Helper/ActiveLinkHelper.php

170 lines
4.3 KiB
PHP
Raw Normal View History

2024-03-31 02:13:19 -07:00
<?php
declare(strict_types=1);
namespace CheeseCake\View\Helper;
use Cake\Routing\Router;
use Cake\View\Helper;
/**
* ActiveLink helper
*
2026-03-20 21:54:16 -07:00
* @property \Cake\View\Helper\HtmlHelper $Html
2024-03-31 02:13:19 -07:00
*/
2026-03-17 01:35:40 -07:00
class ActiveLinkHelper extends Helper {
2026-03-20 21:54:16 -07:00
2026-03-17 01:35:40 -07:00
/**
2024-03-31 02:13:19 -07:00
* Default configuration.
*
* @var array<string, mixed>
*/
2026-03-17 01:35:40 -07:00
protected array $_defaultConfig = [
'activeClass' => 'active',
];
/**
2024-03-31 02:13:19 -07:00
* List of helpers used by this helper
*
* @var string[]
*/
2026-03-17 01:35:40 -07:00
protected array $helpers = [
'Html',
'Url',
];
2024-03-31 02:13:19 -07:00
2026-03-17 01:35:40 -07:00
/**
2024-03-31 02:13:19 -07:00
* @param array|string $title
* @param array|string|null $url
* @param array $options
*
* @return string
*/
2026-03-17 01:35:40 -07:00
public function link(array|string $title, array|string|null $url = null, array $options = []): string {
$currentUrl = $options['current'] ?? Router::parseRequest($this->getView()->getRequest());
if (!array_key_exists('target', $options) || !$currentUrl) {
return $this->Html->link($title, $url, $options);
}
2026-03-17 01:35:40 -07:00
$target = $options['target'];
$activeClass = $options['activeClass'] ?? $this->getConfig('activeClass');
unset($options['target']);
unset($options['activeClass']);
2026-03-17 01:35:40 -07:00
if (is_string($target)) {
return $this->_linkFromStringTarget($currentUrl, $target, $title, $url, $activeClass, $options);
}
if (!is_array($target)) {
return $this->Html->link($title, $url, $options);
}
if (!array_key_exists('plugin', $currentUrl)) {
$currentUrl['plugin'] = false;
}
if (!array_key_exists('prefix', $currentUrl)) {
$currentUrl['prefix'] = false;
}
if (isset($target['or']) && $target['or']) {
foreach ($target['or'] as $singleTargetToMatch) {
if ($this->_matchesUrlFromArrayTarget($currentUrl, $singleTargetToMatch)) {
$options['class'] = $this->_addClass($options, $activeClass);
return $this->Html->link($title, $url, $options);
}
}
return $this->Html->link($title, $url, $options);
}
if (!$this->doesUrlMatchTarget($target, $currentUrl)) {
return $this->Html->link($title, $url, $options);
}
$options['class'] = $this->_addClass($options, $activeClass);
2026-03-17 01:35:40 -07:00
return $this->Html->link($title, $url, $options);
}
/**
* @param array|string $targetUrl
* @param array|null $current |null current url
*
* @return bool
*/
public function doesUrlMatchTarget(array|string $targetUrl, array|null $current = null) {
if (!isset($current)) {
$current = Router::parseRequest($this->getView()->getRequest());
}
if (is_string($targetUrl) && Router::normalize($current) == Router::normalize($targetUrl)) {
return true;
}
if (is_array($targetUrl)) {
if (isset($targetUrl['or']) && $targetUrl['or']) {
foreach ($targetUrl['or'] as $singleTargetToMatch) {
$matched = $this->_matchesUrlFromArrayTarget($current, $singleTargetToMatch);
if ($matched) {
return true;
}
}
}
return $this->_matchesUrlFromArrayTarget($current, $targetUrl);
}
return false;
}
/**
2024-03-31 02:13:19 -07:00
* @param array $providedOptions
* @param string $toAdd
2024-03-31 02:13:19 -07:00
*
* @return string
*/
2026-03-17 01:35:40 -07:00
protected function _addClass(array $providedOptions, string $toAdd): string {
return array_key_exists('class', $providedOptions) ? $providedOptions['class'] . ' ' . $toAdd : $toAdd;
}
/**
2026-03-17 01:35:40 -07:00
* @param array $current
* @param string $targetString
* @param string $title
* @param array|string|null $url
* @param string $activeClass
2026-03-17 01:35:40 -07:00
* @param array $options
*
* @return string
*/
protected function _linkFromStringTarget(array $current, string $targetString, string $title, array|string|null $url, string $activeClass, array $options) {
if (Router::normalize($current) == Router::normalize($targetString)) {
$options['class'] = $this->_addClass($options, $activeClass);
return $this->Html->link($title, $url, $options);
}
return $this->Html->link($title, $url, $options);
}
/**
2026-03-17 01:35:40 -07:00
* @param array $current
* @param array $targetUrl
*
* @return bool
*/
protected function _matchesUrlFromArrayTarget(array $current, array $targetUrl) {
foreach ($targetUrl as $targetKey => $targetValue) {
if (is_array($targetValue)) {
if (!in_array($current[$targetKey], $targetValue)) {
return false;
}
continue;
}
if (!array_key_exists($targetKey, $current) || $targetValue != $current[$targetKey]) {
return false;
}
}
return true;
}
2024-03-31 02:13:19 -07:00
}