1 | <?php |
||
28 | class LinkHelper extends Helper |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * Default configuration for this class. |
||
33 | * |
||
34 | * - `breadcrumbGuessing`: Whether to mark an item as "active" if its URL is on |
||
35 | * the breadcrumb stack. Defaults to true |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $_defaultConfig = [ |
||
40 | 'breadcrumbGuessing' => true, |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Returns a safe URL string for later use with HtmlHelper. |
||
45 | * |
||
46 | * @param string|array $url URL given as string or an array compatible |
||
47 | * with `Router::url()` |
||
48 | * @return string |
||
49 | */ |
||
50 | public function url($url) |
||
64 | |||
65 | /** |
||
66 | * Checks if the given menu link should be marked as active. |
||
67 | * |
||
68 | * If `$item->activation` is a callable function it will be used to determinate |
||
69 | * if the link should be active or not, returning true from callable indicates |
||
70 | * link should be active, false indicates it should not be marked as active. |
||
71 | * Callable receives current request object as first argument and $item as second. |
||
72 | * |
||
73 | * `$item->url` property MUST exists if "activation" is not a callable, and can |
||
74 | * be either: |
||
75 | * |
||
76 | * - A string representing an external or internal URL (all internal links must |
||
77 | * starts with "/"). e.g. `/user/login` |
||
78 | * |
||
79 | * - An array compatible with \Cake\Routing\Router::url(). e.g. `['controller' |
||
80 | * => 'users', 'action' => 'login']` |
||
81 | * |
||
82 | * Both examples are equivalent. |
||
83 | * |
||
84 | * @param \Cake\Datasource\EntityInterface $item A menu's item |
||
85 | * @return bool |
||
86 | */ |
||
87 | public function isActive(EntityInterface $item) |
||
88 | { |
||
89 | if ($item->has('activation') && is_callable($item->get('activation'))) { |
||
90 | $callable = $item->get('activation'); |
||
91 | |||
92 | return $callable($this->_View->request, $item); |
||
93 | } |
||
94 | |||
95 | $itemUrl = $this->sanitize($item->get('url')); |
||
96 | if (!str_starts_with($itemUrl, '/')) { |
||
97 | return false; |
||
98 | } |
||
99 | |||
100 | switch ($item->get('activation')) { |
||
101 | case 'any': |
||
102 | return $this->_requestMatches($item->get('active')); |
||
103 | case 'none': |
||
104 | return !$this->_requestMatches($item->get('active')); |
||
105 | case 'php': |
||
106 | return php_eval($item->get('active'), [ |
||
107 | 'view', &$this->_View, |
||
108 | 'item', &$item, |
||
109 | ]) === true; |
||
110 | case 'auto': |
||
111 | default: |
||
112 | static $requestUri = null; |
||
113 | static $requestUrl = null; |
||
114 | |||
115 | if ($requestUri === null) { |
||
116 | $requestUri = urldecode(env('REQUEST_URI')); |
||
117 | $requestUrl = str_replace('//', '/', '/' . urldecode($this->_View->request->url) . '/'); |
||
118 | } |
||
119 | |||
120 | $isInternal = |
||
121 | $itemUrl !== '/' && |
||
122 | str_ends_with($itemUrl, str_replace_once($this->baseUrl(), '', $requestUri)); |
||
123 | $isIndex = |
||
124 | $itemUrl === '/' && |
||
125 | $this->_View->request->isHome(); |
||
126 | $isExact = |
||
127 | str_replace('//', '/', "{$itemUrl}/") === $requestUrl || |
||
128 | $itemUrl == $requestUri; |
||
129 | |||
130 | if ($this->config('breadcrumbGuessing')) { |
||
131 | return ($isInternal || $isIndex || $isExact || in_array($itemUrl, $this->_crumbs())); |
||
132 | } |
||
133 | |||
134 | return ($isInternal || $isIndex || $isExact); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Sanitizes the given URL by making sure it's suitable for menu links. |
||
140 | * |
||
141 | * @param string $url Item's URL to sanitize |
||
142 | * @return string Valid URL, empty string on error |
||
143 | */ |
||
144 | public function sanitize($url) |
||
162 | |||
163 | /** |
||
164 | * Prepends language code to the given URL if the "url_locale_prefix" directive |
||
165 | * is enabled. |
||
166 | * |
||
167 | * @param string $url The URL to fix |
||
168 | * @return string Locale prefixed URL |
||
169 | */ |
||
170 | public function localePrefix($url) |
||
171 | { |
||
172 | if (option('url_locale_prefix') && |
||
173 | str_starts_with($url, '/') && |
||
174 | !preg_match('/^\/' . $this->_localesPattern() . '/', $url) |
||
175 | ) { |
||
176 | $url = '/' . I18n::locale() . $url; |
||
177 | } |
||
178 | |||
179 | return $url; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Calculates site's base URL. |
||
184 | * |
||
185 | * @return string Site's base URL |
||
186 | */ |
||
187 | public function baseUrl() |
||
188 | { |
||
189 | static $base = null; |
||
190 | if ($base === null) { |
||
191 | $base = $this->_View->request->base; |
||
192 | } |
||
193 | |||
194 | return $base; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Gets a list of all URLs present in current crumbs stack. |
||
199 | * |
||
200 | * @return array List of URLs |
||
201 | */ |
||
202 | protected function _crumbs() |
||
214 | |||
215 | /** |
||
216 | * Check if current request path matches any pattern in a set of patterns. |
||
217 | * |
||
218 | * @param string $patterns String containing a set of patterns separated by \n, |
||
219 | * \r or \r\n |
||
220 | * @return bool TRUE if the path matches a pattern, FALSE otherwise |
||
221 | */ |
||
222 | protected function _requestMatches($patterns) |
||
260 | |||
261 | /** |
||
262 | * Returns a regular expression that is used to verify if an URL starts |
||
263 | * or not with a language prefix. |
||
264 | * |
||
265 | * ## Example: |
||
266 | * |
||
267 | * ``` |
||
268 | * (en\-us|fr|es|it) |
||
269 | * ``` |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | protected function _localesPattern() |
||
293 | } |
||
294 |