[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', |
---|
[1] | 50 | ); |
---|
[468] | 51 | public $pages = array(); |
---|
[1] | 52 | |
---|
| 53 | /** |
---|
[184] | 54 | * Navigation constructor. |
---|
[1] | 55 | */ |
---|
[468] | 56 | public function __construct($params=null) |
---|
[1] | 57 | { |
---|
[479] | 58 | $app =& App::getInstance(); |
---|
[184] | 59 | |
---|
| 60 | if (isset($params) && is_array($params)) { |
---|
| 61 | // Merge new parameters with old overriding only those passed. |
---|
| 62 | $this->_params = array_merge($this->_params, $params); |
---|
[1] | 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Add a page to the internal pages array. Pages must be added sequentially |
---|
| 68 | * as they are to be printed. The root page must be added first, and the |
---|
[184] | 69 | * current page added last. Vars can be specified for any page, but only vars |
---|
| 70 | * from the "current" page will be accessed with Nav::get. |
---|
[461] | 71 | * |
---|
[1] | 72 | * @access public |
---|
| 73 | * @param string $title The title of the page. |
---|
[184] | 74 | * @param string $url The URL to the page. Set to null to use PHP_SELF. |
---|
[334] | 75 | * @param array $vars Additional page variables. |
---|
[1] | 76 | */ |
---|
[468] | 77 | public function add($title, $url=null, $vars=array()) |
---|
[1] | 78 | { |
---|
[184] | 79 | $page = array( |
---|
| 80 | 'title' => $title, |
---|
[186] | 81 | 'head_title' => $title, |
---|
| 82 | 'body_title' => $title, |
---|
[185] | 83 | 'url' => is_null($url) ? $_SERVER['PHP_SELF'] : $url, |
---|
[1] | 84 | ); |
---|
[184] | 85 | $this->pages[] = array_merge($page, $vars); |
---|
[1] | 86 | } |
---|
[42] | 87 | |
---|
[1] | 88 | /** |
---|
[184] | 89 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
[1] | 90 | * |
---|
[184] | 91 | * @access public |
---|
| 92 | * @param array $params Array of parameters (key => val pairs). |
---|
| 93 | */ |
---|
[468] | 94 | public function setParam($params) |
---|
[184] | 95 | { |
---|
[479] | 96 | $app =& App::getInstance(); |
---|
[461] | 97 | |
---|
[184] | 98 | if (isset($params) && is_array($params)) { |
---|
| 99 | // Merge new parameters with old overriding only those passed. |
---|
| 100 | $this->_params = array_merge($this->_params, $params); |
---|
| 101 | } else { |
---|
| 102 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * Return the value of a parameter, if it exists. |
---|
[1] | 108 | * |
---|
[184] | 109 | * @access public |
---|
| 110 | * @param string $param Which parameter to return. |
---|
| 111 | * @return mixed Configured parameter value. |
---|
[1] | 112 | */ |
---|
[468] | 113 | public function getParam($param) |
---|
[1] | 114 | { |
---|
[479] | 115 | $app =& App::getInstance(); |
---|
[461] | 116 | |
---|
[478] | 117 | if (array_key_exists($param, $this->_params)) { |
---|
[184] | 118 | return $this->_params[$param]; |
---|
| 119 | } else { |
---|
| 120 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 121 | return null; |
---|
[1] | 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
[184] | 126 | * Unsets all pages. |
---|
[1] | 127 | * |
---|
| 128 | * @access public |
---|
| 129 | */ |
---|
[468] | 130 | public function clear() |
---|
[1] | 131 | { |
---|
| 132 | $this->pages = array(); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
[184] | 136 | * Sets a variable into the current page. |
---|
[1] | 137 | * |
---|
[184] | 138 | * @access public |
---|
| 139 | * @param mixed $key Which value to set. |
---|
| 140 | * @param mixed $val Value to set variable to. |
---|
[1] | 141 | */ |
---|
[468] | 142 | public function set($key, $val) |
---|
[1] | 143 | { |
---|
[184] | 144 | // Set params of current page. |
---|
[461] | 145 | $curr_page =& $this->pages[sizeof($this->pages) - 1]; |
---|
[184] | 146 | $curr_page[$key] = $val; |
---|
[1] | 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
[184] | 150 | * Returns a specified value from the current page. |
---|
[1] | 151 | * |
---|
[184] | 152 | * @access public |
---|
| 153 | * @param mixed $key Which value to return. |
---|
| 154 | * @param mixed $default Value to return if key not found in user_data. |
---|
| 155 | * @return mixed Value stored in session. |
---|
[1] | 156 | */ |
---|
[468] | 157 | public function get($key, $default='') |
---|
[1] | 158 | { |
---|
[184] | 159 | $curr_page =& $this->pages[sizeof($this->pages) - 1]; |
---|
[461] | 160 | |
---|
[184] | 161 | switch ($key) { |
---|
| 162 | case 'title' : |
---|
| 163 | if ($this->getParam('title') && isset($curr_page['title'])) { |
---|
| 164 | return $curr_page['title']; |
---|
| 165 | } |
---|
| 166 | break; |
---|
[42] | 167 | |
---|
[184] | 168 | case 'head_title' : |
---|
| 169 | if ($this->getParam('head_title') && $this->getParam('title') && isset($curr_page['head_title'])) { |
---|
| 170 | return $curr_page['head_title']; |
---|
| 171 | } |
---|
| 172 | break; |
---|
| 173 | |
---|
| 174 | case 'body_title' : |
---|
[186] | 175 | if ($this->getParam('body_title') && $this->getParam('title') && isset($curr_page['body_title'])) { |
---|
[184] | 176 | return $curr_page['body_title']; |
---|
| 177 | } |
---|
| 178 | break; |
---|
| 179 | |
---|
| 180 | case 'path' : |
---|
| 181 | if ($this->getParam('path')) { |
---|
| 182 | return $this->getPath(); |
---|
| 183 | } |
---|
| 184 | break; |
---|
| 185 | |
---|
| 186 | case 'breadcrumbs' : |
---|
| 187 | if ($this->getParam('breadcrumbs')) { |
---|
| 188 | return $this->getBreadcrumbs(); |
---|
| 189 | } |
---|
| 190 | break; |
---|
| 191 | |
---|
| 192 | default : |
---|
| 193 | return isset($curr_page[$key]) ? $curr_page[$key] : $default; |
---|
| 194 | break; |
---|
[1] | 195 | } |
---|
| 196 | |
---|
[184] | 197 | return $default; |
---|
[1] | 198 | } |
---|
| 199 | |
---|
| 200 | /** |
---|
[485] | 201 | * Returns the path from root up to the current page as an array. |
---|
| 202 | * |
---|
| 203 | * @access public |
---|
| 204 | * @param string $key Which value to use in the path (usually head_title or body_title or just title). |
---|
| 205 | * @return mixed Path (string) or false if path param is not set. |
---|
| 206 | */ |
---|
| 207 | public function getPathArray($key='title') |
---|
| 208 | { |
---|
| 209 | $path = array(); |
---|
| 210 | if ($this->getParam('path')) { |
---|
| 211 | foreach ($this->pages as $page) { |
---|
| 212 | $path[] = strip_tags($page[$key]); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | return $path; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /** |
---|
[334] | 219 | * Returns the text path from root up to the current page, separated by the |
---|
| 220 | * path_delimiter. |
---|
[1] | 221 | * |
---|
| 222 | * @access public |
---|
[304] | 223 | * @param string $key Which value to use in the path (usually head_title or body_title or just title). |
---|
| 224 | * @return mixed Path (string) or false if path param is not set. |
---|
[1] | 225 | */ |
---|
[468] | 226 | public function getPath($key='title') |
---|
[1] | 227 | { |
---|
[485] | 228 | |
---|
| 229 | $path = $this->getPathArray(); |
---|
| 230 | return empty($path) ? '' : join(oTxt($this->getParam('path_delimiter'), true), $path); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | * Returns the breadcrumbs from the root page to the current page. |
---|
| 235 | * Breadcrumbs are the text path with pages titles linked to that page. |
---|
| 236 | * |
---|
| 237 | * @access public |
---|
| 238 | * @return string Breadcrumbs or empty string if breadcrumbs param not set. |
---|
| 239 | */ |
---|
| 240 | public function getBreadcrumbsArray() |
---|
| 241 | { |
---|
| 242 | $app =& App::getInstance(); |
---|
| 243 | |
---|
| 244 | if ($this->getParam('breadcrumbs')) { |
---|
| 245 | $breadcrumbs = array(); |
---|
| 246 | $crumb_count = sizeof($this->pages); |
---|
[184] | 247 | foreach ($this->pages as $page) { |
---|
[485] | 248 | if ($crumb_count <= $this->getParam('chop_breadcrumbs')) { |
---|
| 249 | // Stop gathering crumbs. |
---|
| 250 | break; |
---|
| 251 | } |
---|
| 252 | if ($crumb_count <= 1) { |
---|
| 253 | // The last crumb. |
---|
| 254 | if ('' == trim($page['url']) || $crumb_count <= $this->getParam('chop_breadcrumb_links')) { |
---|
| 255 | // A crumb with no link. |
---|
| 256 | $breadcrumbs[] = array( |
---|
[497] | 257 | 'url' => $_SERVER['REQUEST_URI'], |
---|
| 258 | 'title' => sprintf($this->getParam('last_crumb_format'), $page['title']), |
---|
| 259 | 'class' => 'current' |
---|
[485] | 260 | ); |
---|
| 261 | } else if ($crumb_count > $this->getParam('chop_breadcrumb_links')) { |
---|
| 262 | // A normal linked crumb. |
---|
| 263 | $breadcrumbs[] = array( |
---|
| 264 | 'url' => $page['url'], |
---|
[497] | 265 | 'title' => sprintf($this->getParam('last_crumb_format'), $page['title']), |
---|
| 266 | 'class' => '', |
---|
[485] | 267 | ); |
---|
| 268 | } |
---|
| 269 | } else { |
---|
| 270 | if ('' == trim($page['url'])) { |
---|
| 271 | // A crumb with no link. |
---|
| 272 | $breadcrumbs[] = array( |
---|
| 273 | 'url' => false, |
---|
[497] | 274 | 'title' => $page['title'], |
---|
| 275 | 'class' => 'unavailable', |
---|
[485] | 276 | ); |
---|
| 277 | } else { |
---|
| 278 | // A normal linked crumb. |
---|
| 279 | $breadcrumbs[] = array( |
---|
| 280 | 'url' => $page['url'], |
---|
[497] | 281 | 'title' => $page['title'], |
---|
| 282 | 'class' => '', |
---|
[485] | 283 | ); |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | $crumb_count--; |
---|
[1] | 287 | } |
---|
[485] | 288 | return $breadcrumbs; |
---|
[1] | 289 | } else { |
---|
[485] | 290 | return array(); |
---|
[1] | 291 | } |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | /** |
---|
| 295 | * Returns the breadcrumbs from the root page to the current page. |
---|
| 296 | * Breadcrumbs are the text path with pages titles linked to that page. |
---|
| 297 | * |
---|
| 298 | * @access public |
---|
[324] | 299 | * @return string Breadcrumbs or empty string if breadcrumbs param not set. |
---|
[1] | 300 | */ |
---|
[468] | 301 | public function getBreadcrumbs() |
---|
[1] | 302 | { |
---|
[479] | 303 | $app =& App::getInstance(); |
---|
[136] | 304 | |
---|
[184] | 305 | if ($this->getParam('breadcrumbs')) { |
---|
[324] | 306 | $breadcrumbs = array(); |
---|
[1] | 307 | $pathmark = ''; |
---|
| 308 | $crumb_count = sizeof($this->pages); |
---|
[184] | 309 | foreach ($this->pages as $page) { |
---|
| 310 | if ($crumb_count <= $this->getParam('chop_breadcrumbs')) { |
---|
[1] | 311 | // Stop gathering crumbs. |
---|
[324] | 312 | break; |
---|
[1] | 313 | } |
---|
| 314 | if ($crumb_count <= 1) { |
---|
| 315 | // The last crumb. |
---|
[184] | 316 | if ('' == trim($page['url']) || $crumb_count <= $this->getParam('chop_breadcrumb_links')) { |
---|
[1] | 317 | // A crumb with no link. |
---|
[324] | 318 | $breadcrumbs[] = sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)); |
---|
[184] | 319 | } else if ($crumb_count > $this->getParam('chop_breadcrumb_links')) { |
---|
[1] | 320 | // A normal linked crumb. |
---|
[485] | 321 | $breadcrumbs[] = '<a href="' . $page['url'] . '">' . sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)) . '</a>'; |
---|
[1] | 322 | } |
---|
| 323 | } else { |
---|
[184] | 324 | if ('' == trim($page['url'])) { |
---|
[1] | 325 | // A crumb with no link. |
---|
[324] | 326 | $breadcrumbs[] = oTxt($pathmark . $page['title'], true); |
---|
[1] | 327 | } else { |
---|
| 328 | // A normal linked crumb. |
---|
[485] | 329 | $breadcrumbs[] = '<a href="' . $page['url'] . '">' . oTxt($page['title'], true) . '</a>'; |
---|
[1] | 330 | } |
---|
| 331 | } |
---|
[184] | 332 | $pathmark = $this->getParam('path_delimiter'); |
---|
[1] | 333 | $crumb_count--; |
---|
| 334 | } |
---|
[324] | 335 | return join(oTxt($pathmark, true), $breadcrumbs); |
---|
[1] | 336 | } else { |
---|
[324] | 337 | return ''; |
---|
[1] | 338 | } |
---|
| 339 | } |
---|
| 340 | |
---|
[497] | 341 | /* |
---|
| 342 | * |
---|
| 343 | * |
---|
| 344 | * @access public |
---|
| 345 | * @param |
---|
| 346 | * @return |
---|
| 347 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 348 | * @version 1.0 |
---|
| 349 | * @since 07 Sep 2014 12:22:19 |
---|
| 350 | */ |
---|
| 351 | public function getBreadcrumbsUL() |
---|
| 352 | { |
---|
| 353 | $breadcrumbs = $this->getBreadcrumbsArray(); |
---|
| 354 | if (!empty($breadcrumbs)) { |
---|
| 355 | ?><ul class="breadcrumbs"><?php |
---|
| 356 | foreach ($breadcrumbs as $b) { |
---|
| 357 | $printclass = '' != $b['class'] ? sprintf(' class="%s"', $b['class']) : ''; |
---|
| 358 | printf('<li%s><a href="%s">%s</a></li>', $printclass, $b['url'], $b['title']); |
---|
| 359 | } |
---|
| 360 | ?></ul><?php |
---|
| 361 | } |
---|
| 362 | unset($key, $value); |
---|
| 363 | } |
---|
| 364 | |
---|
[1] | 365 | /** |
---|
[475] | 366 | * Test if the given URI matches the URL of the current page. By default the URI is tested |
---|
| 367 | * without concern |
---|
| 368 | * One use is to change the returned value for a positive match |
---|
| 369 | * so a css class prints for an element representing the current page: |
---|
| 370 | * echo $nav->currentPage('/script.php?op=info', ' class="current"', '', true); |
---|
| 371 | * The above will match only if the current page (REQUEST_URI) is also '/script.php?op=info', |
---|
| 372 | * and will return the string ' class="current"' if it is. |
---|
[1] | 373 | * |
---|
| 374 | * @access public |
---|
| 375 | * |
---|
[475] | 376 | * @param string $test_uri A URI to test against the current page. |
---|
| 377 | * @param mixed $true_return The value to return if the current page matches the test URI. |
---|
| 378 | * @param mixed $false_return The value to return if the current page does not match the test URI. |
---|
| 379 | * @param bool $include_query If set true, include the URI query string in the test. |
---|
[1] | 380 | * |
---|
[475] | 381 | * @return mixed If the test URI matches the current page URI, the value given for $true_return |
---|
| 382 | * is returned (true by default), otherwise the value given for $false_return is |
---|
| 383 | * returned (false by default). |
---|
[1] | 384 | */ |
---|
[475] | 385 | public function currentPage($test_uri, $true_return=true, $false_return=false, $include_query=false) |
---|
[1] | 386 | { |
---|
[475] | 387 | $app =& App::getInstance(); |
---|
| 388 | |
---|
| 389 | $actual_uri = $include_query ? $_SERVER['REQUEST_URI'] : strtok($_SERVER['REQUEST_URI'], '?'); |
---|
| 390 | $test_uri = $include_query ? $test_uri : strtok($test_uri, '?'); |
---|
| 391 | if (mb_strtolower($test_uri) == mb_strtolower($actual_uri)) { |
---|
| 392 | // $app->logMsg(sprintf('Current page (%s) == test URI (%s)', $actual_uri, $test_uri), LOG_DEBUG, __FILE__, __LINE__); |
---|
[461] | 393 | return $true_return; |
---|
[1] | 394 | } |
---|
[475] | 395 | // $app->logMsg(sprintf('Current page (%s) != test URI (%s)', $actual_uri, $test_uri), LOG_DEBUG, __FILE__, __LINE__); |
---|
[461] | 396 | return $false_return; |
---|
[1] | 397 | } |
---|
[42] | 398 | |
---|
| 399 | } |
---|
[1] | 400 | // End of class. |
---|