[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[461] | 6 | * |
---|
[362] | 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
[461] | 13 | * |
---|
[362] | 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
[461] | 18 | * |
---|
[362] | 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[184] | 24 | * Navigation.inc.php |
---|
[136] | 25 | * |
---|
| 26 | * The Nav class provides a system for working with navigation elements. |
---|
[184] | 27 | * It supports storing page titles and URLs for printing breadcrumbs |
---|
| 28 | * and titles, as well as setting page params such as hiding the page title on |
---|
| 29 | * some pages but not others, and storing vars like the page title itself. |
---|
[461] | 30 | * |
---|
[184] | 31 | * Note: this class was renamed from "Nav" because of the change in API and to be more descriptive. |
---|
[1] | 32 | * |
---|
| 33 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[184] | 34 | * @version 2.0 |
---|
[1] | 35 | */ |
---|
[502] | 36 | class Navigation |
---|
| 37 | { |
---|
[1] | 38 | |
---|
[184] | 39 | // Configuration parameters for this object. |
---|
[484] | 40 | protected $_params = array( |
---|
[185] | 41 | 'head_title' => true, |
---|
| 42 | 'body_title' => true, |
---|
[1] | 43 | 'title' => true, |
---|
| 44 | 'path' => true, |
---|
| 45 | 'breadcrumbs' => true, |
---|
| 46 | 'chop_breadcrumbs' => 0, |
---|
| 47 | 'chop_breadcrumb_links' => 1, |
---|
[184] | 48 | 'path_delimiter' => ' / ', |
---|
| 49 | 'last_crumb_format' => '%s', |
---|
[534] | 50 | 'current_page_url' => null, // This should be set at runtime using, e.g., $_SERVER['REQUEST_URI'] |
---|
[1] | 51 | ); |
---|
[468] | 52 | public $pages = array(); |
---|
[1] | 53 | |
---|
| 54 | /** |
---|
[184] | 55 | * Navigation constructor. |
---|
[1] | 56 | */ |
---|
[468] | 57 | public function __construct($params=null) |
---|
[1] | 58 | { |
---|
[479] | 59 | $app =& App::getInstance(); |
---|
[184] | 60 | |
---|
[534] | 61 | // Define current_page_url here because _SERVER, not a static scalar, cannot be defined in the defaults above. |
---|
| 62 | // Using PHP_SELF for legacy compatability, but it might make sense to override this with REQUEST_URI. |
---|
| 63 | // This could be overwritten by passed params. |
---|
| 64 | $this->_params['current_page_url'] = $_SERVER['PHP_SELF']; |
---|
| 65 | |
---|
[184] | 66 | if (isset($params) && is_array($params)) { |
---|
| 67 | // Merge new parameters with old overriding only those passed. |
---|
| 68 | $this->_params = array_merge($this->_params, $params); |
---|
[1] | 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * Add a page to the internal pages array. Pages must be added sequentially |
---|
| 74 | * as they are to be printed. The root page must be added first, and the |
---|
[184] | 75 | * current page added last. Vars can be specified for any page, but only vars |
---|
| 76 | * from the "current" page will be accessed with Nav::get. |
---|
[461] | 77 | * |
---|
[1] | 78 | * @access public |
---|
| 79 | * @param string $title The title of the page. |
---|
[534] | 80 | * @param string $url The URL to the page. Set to null to use REQUEST_URI. |
---|
[334] | 81 | * @param array $vars Additional page variables. |
---|
[1] | 82 | */ |
---|
[468] | 83 | public function add($title, $url=null, $vars=array()) |
---|
[1] | 84 | { |
---|
[184] | 85 | $page = array( |
---|
| 86 | 'title' => $title, |
---|
[186] | 87 | 'head_title' => $title, |
---|
| 88 | 'body_title' => $title, |
---|
[534] | 89 | 'url' => is_null($url) ? $this->_params['current_page_url'] : $url, |
---|
[1] | 90 | ); |
---|
[534] | 91 | // An "unformed page element" has settings applied (via ->set()) but no page added (via ->add()). |
---|
| 92 | if (empty($this->pages) || isset(end($this->pages)['title'])) { |
---|
| 93 | // There are no unformed page elements; add a whole new page. |
---|
| 94 | $this->pages[] = array_merge($page, $vars); |
---|
| 95 | } else { |
---|
| 96 | // Append the new page to the unformed page element. |
---|
| 97 | $curr_page =& $this->pages[key($this->pages)]; |
---|
| 98 | $curr_page = array_merge($curr_page, $page, $vars); |
---|
| 99 | } |
---|
[1] | 100 | } |
---|
[42] | 101 | |
---|
[1] | 102 | /** |
---|
[184] | 103 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
[1] | 104 | * |
---|
[184] | 105 | * @access public |
---|
| 106 | * @param array $params Array of parameters (key => val pairs). |
---|
| 107 | */ |
---|
[468] | 108 | public function setParam($params) |
---|
[184] | 109 | { |
---|
[479] | 110 | $app =& App::getInstance(); |
---|
[461] | 111 | |
---|
[184] | 112 | if (isset($params) && is_array($params)) { |
---|
| 113 | // Merge new parameters with old overriding only those passed. |
---|
| 114 | $this->_params = array_merge($this->_params, $params); |
---|
| 115 | } else { |
---|
| 116 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * Return the value of a parameter, if it exists. |
---|
[1] | 122 | * |
---|
[184] | 123 | * @access public |
---|
| 124 | * @param string $param Which parameter to return. |
---|
| 125 | * @return mixed Configured parameter value. |
---|
[1] | 126 | */ |
---|
[468] | 127 | public function getParam($param) |
---|
[1] | 128 | { |
---|
[479] | 129 | $app =& App::getInstance(); |
---|
[461] | 130 | |
---|
[478] | 131 | if (array_key_exists($param, $this->_params)) { |
---|
[184] | 132 | return $this->_params[$param]; |
---|
| 133 | } else { |
---|
| 134 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 135 | return null; |
---|
[1] | 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
[184] | 140 | * Unsets all pages. |
---|
[1] | 141 | * |
---|
| 142 | * @access public |
---|
| 143 | */ |
---|
[468] | 144 | public function clear() |
---|
[1] | 145 | { |
---|
| 146 | $this->pages = array(); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
[184] | 150 | * Sets a variable into the current page. |
---|
[1] | 151 | * |
---|
[184] | 152 | * @access public |
---|
| 153 | * @param mixed $key Which value to set. |
---|
| 154 | * @param mixed $val Value to set variable to. |
---|
[1] | 155 | */ |
---|
[468] | 156 | public function set($key, $val) |
---|
[1] | 157 | { |
---|
[184] | 158 | // Set params of current page. |
---|
[534] | 159 | if (empty($this->pages)) { |
---|
| 160 | // If we're setting a value on an empty pages array, we need to add one "unformed" element first. |
---|
| 161 | $this->pages[] = array(); |
---|
| 162 | } |
---|
| 163 | end($this->pages); |
---|
| 164 | $curr_page =& $this->pages[key($this->pages)]; |
---|
[184] | 165 | $curr_page[$key] = $val; |
---|
[1] | 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
[184] | 169 | * Returns a specified value from the current page. |
---|
[1] | 170 | * |
---|
[184] | 171 | * @access public |
---|
| 172 | * @param mixed $key Which value to return. |
---|
| 173 | * @param mixed $default Value to return if key not found in user_data. |
---|
| 174 | * @return mixed Value stored in session. |
---|
[1] | 175 | */ |
---|
[468] | 176 | public function get($key, $default='') |
---|
[1] | 177 | { |
---|
[534] | 178 | end($this->pages); |
---|
| 179 | $curr_page =& $this->pages[key($this->pages)]; |
---|
[461] | 180 | |
---|
[184] | 181 | switch ($key) { |
---|
| 182 | case 'title' : |
---|
| 183 | if ($this->getParam('title') && isset($curr_page['title'])) { |
---|
| 184 | return $curr_page['title']; |
---|
| 185 | } |
---|
| 186 | break; |
---|
[42] | 187 | |
---|
[184] | 188 | case 'head_title' : |
---|
| 189 | if ($this->getParam('head_title') && $this->getParam('title') && isset($curr_page['head_title'])) { |
---|
| 190 | return $curr_page['head_title']; |
---|
| 191 | } |
---|
| 192 | break; |
---|
| 193 | |
---|
| 194 | case 'body_title' : |
---|
[186] | 195 | if ($this->getParam('body_title') && $this->getParam('title') && isset($curr_page['body_title'])) { |
---|
[184] | 196 | return $curr_page['body_title']; |
---|
| 197 | } |
---|
| 198 | break; |
---|
| 199 | |
---|
| 200 | case 'path' : |
---|
| 201 | if ($this->getParam('path')) { |
---|
| 202 | return $this->getPath(); |
---|
| 203 | } |
---|
| 204 | break; |
---|
| 205 | |
---|
| 206 | case 'breadcrumbs' : |
---|
| 207 | if ($this->getParam('breadcrumbs')) { |
---|
| 208 | return $this->getBreadcrumbs(); |
---|
| 209 | } |
---|
| 210 | break; |
---|
| 211 | |
---|
| 212 | default : |
---|
| 213 | return isset($curr_page[$key]) ? $curr_page[$key] : $default; |
---|
| 214 | break; |
---|
[1] | 215 | } |
---|
| 216 | |
---|
[184] | 217 | return $default; |
---|
[1] | 218 | } |
---|
| 219 | |
---|
| 220 | /** |
---|
[485] | 221 | * Returns the path from root up to the current page as an array. |
---|
| 222 | * |
---|
| 223 | * @access public |
---|
| 224 | * @param string $key Which value to use in the path (usually head_title or body_title or just title). |
---|
| 225 | * @return mixed Path (string) or false if path param is not set. |
---|
| 226 | */ |
---|
| 227 | public function getPathArray($key='title') |
---|
| 228 | { |
---|
| 229 | $path = array(); |
---|
| 230 | if ($this->getParam('path')) { |
---|
| 231 | foreach ($this->pages as $page) { |
---|
| 232 | $path[] = strip_tags($page[$key]); |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | return $path; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
[334] | 239 | * Returns the text path from root up to the current page, separated by the |
---|
| 240 | * path_delimiter. |
---|
[1] | 241 | * |
---|
| 242 | * @access public |
---|
[304] | 243 | * @param string $key Which value to use in the path (usually head_title or body_title or just title). |
---|
| 244 | * @return mixed Path (string) or false if path param is not set. |
---|
[1] | 245 | */ |
---|
[468] | 246 | public function getPath($key='title') |
---|
[1] | 247 | { |
---|
[485] | 248 | |
---|
| 249 | $path = $this->getPathArray(); |
---|
| 250 | return empty($path) ? '' : join(oTxt($this->getParam('path_delimiter'), true), $path); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | /** |
---|
| 254 | * Returns the breadcrumbs from the root page to the current page. |
---|
| 255 | * Breadcrumbs are the text path with pages titles linked to that page. |
---|
| 256 | * |
---|
| 257 | * @access public |
---|
| 258 | * @return string Breadcrumbs or empty string if breadcrumbs param not set. |
---|
| 259 | */ |
---|
| 260 | public function getBreadcrumbsArray() |
---|
| 261 | { |
---|
| 262 | $app =& App::getInstance(); |
---|
| 263 | |
---|
| 264 | if ($this->getParam('breadcrumbs')) { |
---|
| 265 | $breadcrumbs = array(); |
---|
| 266 | $crumb_count = sizeof($this->pages); |
---|
[184] | 267 | foreach ($this->pages as $page) { |
---|
[485] | 268 | if ($crumb_count <= $this->getParam('chop_breadcrumbs')) { |
---|
| 269 | // Stop gathering crumbs. |
---|
| 270 | break; |
---|
| 271 | } |
---|
| 272 | if ($crumb_count <= 1) { |
---|
| 273 | // The last crumb. |
---|
| 274 | if ('' == trim($page['url']) || $crumb_count <= $this->getParam('chop_breadcrumb_links')) { |
---|
| 275 | // A crumb with no link. |
---|
| 276 | $breadcrumbs[] = array( |
---|
[497] | 277 | 'url' => $_SERVER['REQUEST_URI'], |
---|
| 278 | 'title' => sprintf($this->getParam('last_crumb_format'), $page['title']), |
---|
| 279 | 'class' => 'current' |
---|
[485] | 280 | ); |
---|
| 281 | } else if ($crumb_count > $this->getParam('chop_breadcrumb_links')) { |
---|
| 282 | // A normal linked crumb. |
---|
| 283 | $breadcrumbs[] = array( |
---|
| 284 | 'url' => $page['url'], |
---|
[497] | 285 | 'title' => sprintf($this->getParam('last_crumb_format'), $page['title']), |
---|
| 286 | 'class' => '', |
---|
[485] | 287 | ); |
---|
| 288 | } |
---|
| 289 | } else { |
---|
| 290 | if ('' == trim($page['url'])) { |
---|
| 291 | // A crumb with no link. |
---|
| 292 | $breadcrumbs[] = array( |
---|
| 293 | 'url' => false, |
---|
[497] | 294 | 'title' => $page['title'], |
---|
| 295 | 'class' => 'unavailable', |
---|
[485] | 296 | ); |
---|
| 297 | } else { |
---|
| 298 | // A normal linked crumb. |
---|
| 299 | $breadcrumbs[] = array( |
---|
| 300 | 'url' => $page['url'], |
---|
[497] | 301 | 'title' => $page['title'], |
---|
| 302 | 'class' => '', |
---|
[485] | 303 | ); |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | $crumb_count--; |
---|
[1] | 307 | } |
---|
[485] | 308 | return $breadcrumbs; |
---|
[1] | 309 | } else { |
---|
[485] | 310 | return array(); |
---|
[1] | 311 | } |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | /** |
---|
| 315 | * Returns the breadcrumbs from the root page to the current page. |
---|
| 316 | * Breadcrumbs are the text path with pages titles linked to that page. |
---|
| 317 | * |
---|
| 318 | * @access public |
---|
[324] | 319 | * @return string Breadcrumbs or empty string if breadcrumbs param not set. |
---|
[1] | 320 | */ |
---|
[468] | 321 | public function getBreadcrumbs() |
---|
[1] | 322 | { |
---|
[479] | 323 | $app =& App::getInstance(); |
---|
[136] | 324 | |
---|
[184] | 325 | if ($this->getParam('breadcrumbs')) { |
---|
[324] | 326 | $breadcrumbs = array(); |
---|
[1] | 327 | $pathmark = ''; |
---|
| 328 | $crumb_count = sizeof($this->pages); |
---|
[184] | 329 | foreach ($this->pages as $page) { |
---|
| 330 | if ($crumb_count <= $this->getParam('chop_breadcrumbs')) { |
---|
[1] | 331 | // Stop gathering crumbs. |
---|
[324] | 332 | break; |
---|
[1] | 333 | } |
---|
| 334 | if ($crumb_count <= 1) { |
---|
| 335 | // The last crumb. |
---|
[184] | 336 | if ('' == trim($page['url']) || $crumb_count <= $this->getParam('chop_breadcrumb_links')) { |
---|
[1] | 337 | // A crumb with no link. |
---|
[324] | 338 | $breadcrumbs[] = sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)); |
---|
[184] | 339 | } else if ($crumb_count > $this->getParam('chop_breadcrumb_links')) { |
---|
[1] | 340 | // A normal linked crumb. |
---|
[485] | 341 | $breadcrumbs[] = '<a href="' . $page['url'] . '">' . sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)) . '</a>'; |
---|
[1] | 342 | } |
---|
| 343 | } else { |
---|
[184] | 344 | if ('' == trim($page['url'])) { |
---|
[1] | 345 | // A crumb with no link. |
---|
[324] | 346 | $breadcrumbs[] = oTxt($pathmark . $page['title'], true); |
---|
[1] | 347 | } else { |
---|
| 348 | // A normal linked crumb. |
---|
[485] | 349 | $breadcrumbs[] = '<a href="' . $page['url'] . '">' . oTxt($page['title'], true) . '</a>'; |
---|
[1] | 350 | } |
---|
| 351 | } |
---|
[184] | 352 | $pathmark = $this->getParam('path_delimiter'); |
---|
[1] | 353 | $crumb_count--; |
---|
| 354 | } |
---|
[324] | 355 | return join(oTxt($pathmark, true), $breadcrumbs); |
---|
[1] | 356 | } else { |
---|
[324] | 357 | return ''; |
---|
[1] | 358 | } |
---|
| 359 | } |
---|
| 360 | |
---|
[497] | 361 | /* |
---|
| 362 | * |
---|
| 363 | * |
---|
| 364 | * @access public |
---|
| 365 | * @param |
---|
| 366 | * @return |
---|
| 367 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 368 | * @version 1.0 |
---|
| 369 | * @since 07 Sep 2014 12:22:19 |
---|
| 370 | */ |
---|
| 371 | public function getBreadcrumbsUL() |
---|
| 372 | { |
---|
[523] | 373 | $out = ''; |
---|
[497] | 374 | $breadcrumbs = $this->getBreadcrumbsArray(); |
---|
| 375 | if (!empty($breadcrumbs)) { |
---|
[523] | 376 | $out = '<ul class="breadcrumbs">'; |
---|
[497] | 377 | foreach ($breadcrumbs as $b) { |
---|
| 378 | $printclass = '' != $b['class'] ? sprintf(' class="%s"', $b['class']) : ''; |
---|
[523] | 379 | $out .= sprintf('<li%s><a href="%s">%s</a></li>', $printclass, $b['url'], $b['title']); |
---|
[497] | 380 | } |
---|
[523] | 381 | $out .= '</ul>'; |
---|
[497] | 382 | } |
---|
[523] | 383 | return $out; |
---|
[497] | 384 | } |
---|
| 385 | |
---|
[1] | 386 | /** |
---|
[475] | 387 | * Test if the given URI matches the URL of the current page. By default the URI is tested |
---|
| 388 | * without concern |
---|
| 389 | * One use is to change the returned value for a positive match |
---|
| 390 | * so a css class prints for an element representing the current page: |
---|
| 391 | * echo $nav->currentPage('/script.php?op=info', ' class="current"', '', true); |
---|
| 392 | * The above will match only if the current page (REQUEST_URI) is also '/script.php?op=info', |
---|
| 393 | * and will return the string ' class="current"' if it is. |
---|
[1] | 394 | * |
---|
| 395 | * @access public |
---|
| 396 | * |
---|
[534] | 397 | * @param mixed $test_uri A URI, or an array of URIs, to test against the current page. |
---|
[475] | 398 | * @param mixed $true_return The value to return if the current page matches the test URI. |
---|
| 399 | * @param mixed $false_return The value to return if the current page does not match the test URI. |
---|
| 400 | * @param bool $include_query If set true, include the URI query string in the test. |
---|
[1] | 401 | * |
---|
[475] | 402 | * @return mixed If the test URI matches the current page URI, the value given for $true_return |
---|
| 403 | * is returned (true by default), otherwise the value given for $false_return is |
---|
| 404 | * returned (false by default). |
---|
[1] | 405 | */ |
---|
[475] | 406 | public function currentPage($test_uri, $true_return=true, $false_return=false, $include_query=false) |
---|
[1] | 407 | { |
---|
[475] | 408 | $app =& App::getInstance(); |
---|
| 409 | |
---|
[534] | 410 | // If given an array, test each URI recursively returning TRUE on a first match, or FALSE if none match. |
---|
| 411 | if (is_array($test_uri)) { |
---|
| 412 | foreach ($test_uri as $uri) { |
---|
| 413 | if ($this->currentPage($uri, $true_return, $false_return, $include_query)) { |
---|
| 414 | return true; |
---|
| 415 | } |
---|
| 416 | } |
---|
| 417 | return false; |
---|
| 418 | } |
---|
| 419 | |
---|
[475] | 420 | $actual_uri = $include_query ? $_SERVER['REQUEST_URI'] : strtok($_SERVER['REQUEST_URI'], '?'); |
---|
| 421 | $test_uri = $include_query ? $test_uri : strtok($test_uri, '?'); |
---|
| 422 | if (mb_strtolower($test_uri) == mb_strtolower($actual_uri)) { |
---|
| 423 | // $app->logMsg(sprintf('Current page (%s) == test URI (%s)', $actual_uri, $test_uri), LOG_DEBUG, __FILE__, __LINE__); |
---|
[461] | 424 | return $true_return; |
---|
[1] | 425 | } |
---|
[475] | 426 | // $app->logMsg(sprintf('Current page (%s) != test URI (%s)', $actual_uri, $test_uri), LOG_DEBUG, __FILE__, __LINE__); |
---|
[461] | 427 | return $false_return; |
---|
[1] | 428 | } |
---|
[42] | 429 | |
---|
| 430 | } |
---|
[1] | 431 | // End of class. |
---|