89 lines
2.5 KiB
PHP
89 lines
2.5 KiB
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace CheeseCake\View\Helper;
|
||
|
|
|
||
|
|
use Cake\Log\Log;
|
||
|
|
use Cake\Routing\Router;
|
||
|
|
use Cake\View\Helper;
|
||
|
|
use Cake\View\View;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* ActiveLink helper
|
||
|
|
*/
|
||
|
|
class ActiveLinkHelper extends Helper
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Default configuration.
|
||
|
|
*
|
||
|
|
* @var array<string, mixed>
|
||
|
|
*/
|
||
|
|
protected array $_defaultConfig = [
|
||
|
|
'activeClass' => 'active',
|
||
|
|
];
|
||
|
|
/**
|
||
|
|
* List of helpers used by this helper
|
||
|
|
*
|
||
|
|
* @var string[]
|
||
|
|
*/
|
||
|
|
protected array $helpers = [
|
||
|
|
'Html',
|
||
|
|
'Url',
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array|string $title
|
||
|
|
* @param array|string|null $url
|
||
|
|
* @param array $options
|
||
|
|
*
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
public function link(array|string $title, array|string|null $url = null, array $options = []): string
|
||
|
|
{
|
||
|
|
$currentUrl = Router::parseRequest($this->getView()->getRequest());
|
||
|
|
if (!array_key_exists('target', $options) || !$currentUrl) {
|
||
|
|
return $this->Html->link($title, $url, $options);
|
||
|
|
}
|
||
|
|
$target = $options['target'];
|
||
|
|
unset($options['target']);
|
||
|
|
if (is_string($target)) {
|
||
|
|
if (Router::normalize($currentUrl) == Router::normalize($target)) {
|
||
|
|
$options['class'] = $this->_addClass($options);
|
||
|
|
|
||
|
|
return $this->Html->link($title, $url, $options);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (is_array($target)) {
|
||
|
|
if (!array_key_exists('plugin', $currentUrl)) {
|
||
|
|
$currentUrl['plugin'] = false;
|
||
|
|
}
|
||
|
|
if (!array_key_exists('prefix', $currentUrl)) {
|
||
|
|
$currentUrl['prefix'] = false;
|
||
|
|
}
|
||
|
|
foreach ($target as $targetKey => $targetValue) {
|
||
|
|
if (is_array($targetValue)) {
|
||
|
|
return 'test';
|
||
|
|
}
|
||
|
|
if (!array_key_exists($targetKey, $currentUrl) || $targetValue != $currentUrl[$targetKey]) {
|
||
|
|
return $this->Html->link($title, $url, $options);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$options['class'] = $this->_addClass($options);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->Html->link($title, $url, $options);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array $providedOptions
|
||
|
|
*
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
protected function _addClass(array $providedOptions): string
|
||
|
|
{
|
||
|
|
$activeClass = array_key_exists('activeClass', $providedOptions) ? $providedOptions['activeClass'] : $this->getConfig('activeClass');
|
||
|
|
|
||
|
|
return array_key_exists('class', $providedOptions) ? $providedOptions['class'] . ' ' . $activeClass : $activeClass;
|
||
|
|
}
|
||
|
|
}
|