[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 | */ |
---|
[184] | 36 | class Navigation { |
---|
[1] | 37 | |
---|
[184] | 38 | // Configuration parameters for this object. |
---|
[468] | 39 | private $_params = array( |
---|
[185] | 40 | 'head_title' => true, |
---|
| 41 | 'body_title' => true, |
---|
[1] | 42 | 'title' => true, |
---|
| 43 | 'path' => true, |
---|
| 44 | 'breadcrumbs' => true, |
---|
| 45 | 'chop_breadcrumbs' => 0, |
---|
| 46 | 'chop_breadcrumb_links' => 1, |
---|
[184] | 47 | 'path_delimiter' => ' / ', |
---|
| 48 | 'last_crumb_format' => '%s', |
---|
[1] | 49 | ); |
---|
[468] | 50 | public $pages = array(); |
---|
[1] | 51 | |
---|
| 52 | /** |
---|
[184] | 53 | * Navigation constructor. |
---|
[1] | 54 | */ |
---|
[468] | 55 | public function __construct($params=null) |
---|
[1] | 56 | { |
---|
[479] | 57 | $app =& App::getInstance(); |
---|
[184] | 58 | |
---|
| 59 | if (isset($params) && is_array($params)) { |
---|
| 60 | // Merge new parameters with old overriding only those passed. |
---|
| 61 | $this->_params = array_merge($this->_params, $params); |
---|
[1] | 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * Add a page to the internal pages array. Pages must be added sequentially |
---|
| 67 | * as they are to be printed. The root page must be added first, and the |
---|
[184] | 68 | * current page added last. Vars can be specified for any page, but only vars |
---|
| 69 | * from the "current" page will be accessed with Nav::get. |
---|
[461] | 70 | * |
---|
[1] | 71 | * @access public |
---|
| 72 | * @param string $title The title of the page. |
---|
[184] | 73 | * @param string $url The URL to the page. Set to null to use PHP_SELF. |
---|
[334] | 74 | * @param array $vars Additional page variables. |
---|
[1] | 75 | */ |
---|
[468] | 76 | public function add($title, $url=null, $vars=array()) |
---|
[1] | 77 | { |
---|
[184] | 78 | $page = array( |
---|
| 79 | 'title' => $title, |
---|
[186] | 80 | 'head_title' => $title, |
---|
| 81 | 'body_title' => $title, |
---|
[185] | 82 | 'url' => is_null($url) ? $_SERVER['PHP_SELF'] : $url, |
---|
[1] | 83 | ); |
---|
[184] | 84 | $this->pages[] = array_merge($page, $vars); |
---|
[1] | 85 | } |
---|
[42] | 86 | |
---|
[1] | 87 | /** |
---|
[184] | 88 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
[1] | 89 | * |
---|
[184] | 90 | * @access public |
---|
| 91 | * @param array $params Array of parameters (key => val pairs). |
---|
| 92 | */ |
---|
[468] | 93 | public function setParam($params) |
---|
[184] | 94 | { |
---|
[479] | 95 | $app =& App::getInstance(); |
---|
[461] | 96 | |
---|
[184] | 97 | if (isset($params) && is_array($params)) { |
---|
| 98 | // Merge new parameters with old overriding only those passed. |
---|
| 99 | $this->_params = array_merge($this->_params, $params); |
---|
| 100 | } else { |
---|
| 101 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * Return the value of a parameter, if it exists. |
---|
[1] | 107 | * |
---|
[184] | 108 | * @access public |
---|
| 109 | * @param string $param Which parameter to return. |
---|
| 110 | * @return mixed Configured parameter value. |
---|
[1] | 111 | */ |
---|
[468] | 112 | public function getParam($param) |
---|
[1] | 113 | { |
---|
[479] | 114 | $app =& App::getInstance(); |
---|
[461] | 115 | |
---|
[478] | 116 | if (array_key_exists($param, $this->_params)) { |
---|
[184] | 117 | return $this->_params[$param]; |
---|
| 118 | } else { |
---|
| 119 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 120 | return null; |
---|
[1] | 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
[184] | 125 | * Unsets all pages. |
---|
[1] | 126 | * |
---|
| 127 | * @access public |
---|
| 128 | */ |
---|
[468] | 129 | public function clear() |
---|
[1] | 130 | { |
---|
| 131 | $this->pages = array(); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
[184] | 135 | * Sets a variable into the current page. |
---|
[1] | 136 | * |
---|
[184] | 137 | * @access public |
---|
| 138 | * @param mixed $key Which value to set. |
---|
| 139 | * @param mixed $val Value to set variable to. |
---|
[1] | 140 | */ |
---|
[468] | 141 | public function set($key, $val) |
---|
[1] | 142 | { |
---|
[184] | 143 | // Set params of current page. |
---|
[461] | 144 | $curr_page =& $this->pages[sizeof($this->pages) - 1]; |
---|
[184] | 145 | $curr_page[$key] = $val; |
---|
[1] | 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
[184] | 149 | * Returns a specified value from the current page. |
---|
[1] | 150 | * |
---|
[184] | 151 | * @access public |
---|
| 152 | * @param mixed $key Which value to return. |
---|
| 153 | * @param mixed $default Value to return if key not found in user_data. |
---|
| 154 | * @return mixed Value stored in session. |
---|
[1] | 155 | */ |
---|
[468] | 156 | public function get($key, $default='') |
---|
[1] | 157 | { |
---|
[184] | 158 | $curr_page =& $this->pages[sizeof($this->pages) - 1]; |
---|
[461] | 159 | |
---|
[184] | 160 | switch ($key) { |
---|
| 161 | case 'title' : |
---|
| 162 | if ($this->getParam('title') && isset($curr_page['title'])) { |
---|
| 163 | return $curr_page['title']; |
---|
| 164 | } |
---|
| 165 | break; |
---|
[42] | 166 | |
---|
[184] | 167 | case 'head_title' : |
---|
| 168 | if ($this->getParam('head_title') && $this->getParam('title') && isset($curr_page['head_title'])) { |
---|
| 169 | return $curr_page['head_title']; |
---|
| 170 | } |
---|
| 171 | break; |
---|
| 172 | |
---|
| 173 | case 'body_title' : |
---|
[186] | 174 | if ($this->getParam('body_title') && $this->getParam('title') && isset($curr_page['body_title'])) { |
---|
[184] | 175 | return $curr_page['body_title']; |
---|
| 176 | } |
---|
| 177 | break; |
---|
| 178 | |
---|
| 179 | case 'path' : |
---|
| 180 | if ($this->getParam('path')) { |
---|
| 181 | return $this->getPath(); |
---|
| 182 | } |
---|
| 183 | break; |
---|
| 184 | |
---|
| 185 | case 'breadcrumbs' : |
---|
| 186 | if ($this->getParam('breadcrumbs')) { |
---|
| 187 | return $this->getBreadcrumbs(); |
---|
| 188 | } |
---|
| 189 | break; |
---|
| 190 | |
---|
| 191 | default : |
---|
| 192 | return isset($curr_page[$key]) ? $curr_page[$key] : $default; |
---|
| 193 | break; |
---|
[1] | 194 | } |
---|
| 195 | |
---|
[184] | 196 | return $default; |
---|
[1] | 197 | } |
---|
| 198 | |
---|
| 199 | /** |
---|
[334] | 200 | * Returns the text path from root up to the current page, separated by the |
---|
| 201 | * path_delimiter. |
---|
[1] | 202 | * |
---|
| 203 | * @access public |
---|
[304] | 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. |
---|
[1] | 206 | */ |
---|
[468] | 207 | public function getPath($key='title') |
---|
[1] | 208 | { |
---|
[184] | 209 | if ($this->getParam('path')) { |
---|
[1] | 210 | $path = ''; |
---|
| 211 | $pathmark = ''; |
---|
[184] | 212 | foreach ($this->pages as $page) { |
---|
[304] | 213 | $path .= oTxt($pathmark . strip_tags($page[$key]), true); |
---|
[184] | 214 | $pathmark = $this->getParam('path_delimiter'); |
---|
[1] | 215 | } |
---|
| 216 | return $path; |
---|
| 217 | } else { |
---|
| 218 | return false; |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | /** |
---|
| 223 | * Returns the breadcrumbs from the root page to the current page. |
---|
| 224 | * Breadcrumbs are the text path with pages titles linked to that page. |
---|
| 225 | * |
---|
| 226 | * @access public |
---|
[324] | 227 | * @return string Breadcrumbs or empty string if breadcrumbs param not set. |
---|
[1] | 228 | */ |
---|
[468] | 229 | public function getBreadcrumbs() |
---|
[1] | 230 | { |
---|
[479] | 231 | $app =& App::getInstance(); |
---|
[136] | 232 | |
---|
[184] | 233 | if ($this->getParam('breadcrumbs')) { |
---|
[324] | 234 | $breadcrumbs = array(); |
---|
[1] | 235 | $pathmark = ''; |
---|
| 236 | $crumb_count = sizeof($this->pages); |
---|
[184] | 237 | foreach ($this->pages as $page) { |
---|
| 238 | if ($crumb_count <= $this->getParam('chop_breadcrumbs')) { |
---|
[1] | 239 | // Stop gathering crumbs. |
---|
[324] | 240 | break; |
---|
[1] | 241 | } |
---|
| 242 | if ($crumb_count <= 1) { |
---|
| 243 | // The last crumb. |
---|
[184] | 244 | if ('' == trim($page['url']) || $crumb_count <= $this->getParam('chop_breadcrumb_links')) { |
---|
[1] | 245 | // A crumb with no link. |
---|
[324] | 246 | $breadcrumbs[] = sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)); |
---|
[184] | 247 | } else if ($crumb_count > $this->getParam('chop_breadcrumb_links')) { |
---|
[1] | 248 | // A normal linked crumb. |
---|
[324] | 249 | $breadcrumbs[] = '<a href="' . $app->oHREF($page['url']) . '">' . sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)) . '</a>'; |
---|
[1] | 250 | } |
---|
| 251 | } else { |
---|
[184] | 252 | if ('' == trim($page['url'])) { |
---|
[1] | 253 | // A crumb with no link. |
---|
[324] | 254 | $breadcrumbs[] = oTxt($pathmark . $page['title'], true); |
---|
[1] | 255 | } else { |
---|
| 256 | // A normal linked crumb. |
---|
[324] | 257 | $breadcrumbs[] = '<a href="' . $app->oHREF($page['url']) . '">' . oTxt($page['title'], true) . '</a>'; |
---|
[1] | 258 | } |
---|
| 259 | } |
---|
[184] | 260 | $pathmark = $this->getParam('path_delimiter'); |
---|
[1] | 261 | $crumb_count--; |
---|
| 262 | } |
---|
[324] | 263 | return join(oTxt($pathmark, true), $breadcrumbs); |
---|
[1] | 264 | } else { |
---|
[324] | 265 | return ''; |
---|
[1] | 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | /** |
---|
[475] | 270 | * Test if the given URI matches the URL of the current page. By default the URI is tested |
---|
| 271 | * without concern |
---|
| 272 | * One use is to change the returned value for a positive match |
---|
| 273 | * so a css class prints for an element representing the current page: |
---|
| 274 | * echo $nav->currentPage('/script.php?op=info', ' class="current"', '', true); |
---|
| 275 | * The above will match only if the current page (REQUEST_URI) is also '/script.php?op=info', |
---|
| 276 | * and will return the string ' class="current"' if it is. |
---|
[1] | 277 | * |
---|
| 278 | * @access public |
---|
| 279 | * |
---|
[475] | 280 | * @param string $test_uri A URI to test against the current page. |
---|
| 281 | * @param mixed $true_return The value to return if the current page matches the test URI. |
---|
| 282 | * @param mixed $false_return The value to return if the current page does not match the test URI. |
---|
| 283 | * @param bool $include_query If set true, include the URI query string in the test. |
---|
[1] | 284 | * |
---|
[475] | 285 | * @return mixed If the test URI matches the current page URI, the value given for $true_return |
---|
| 286 | * is returned (true by default), otherwise the value given for $false_return is |
---|
| 287 | * returned (false by default). |
---|
[1] | 288 | */ |
---|
[475] | 289 | public function currentPage($test_uri, $true_return=true, $false_return=false, $include_query=false) |
---|
[1] | 290 | { |
---|
[475] | 291 | $app =& App::getInstance(); |
---|
| 292 | |
---|
| 293 | $actual_uri = $include_query ? $_SERVER['REQUEST_URI'] : strtok($_SERVER['REQUEST_URI'], '?'); |
---|
| 294 | $test_uri = $include_query ? $test_uri : strtok($test_uri, '?'); |
---|
| 295 | if (mb_strtolower($test_uri) == mb_strtolower($actual_uri)) { |
---|
| 296 | // $app->logMsg(sprintf('Current page (%s) == test URI (%s)', $actual_uri, $test_uri), LOG_DEBUG, __FILE__, __LINE__); |
---|
[461] | 297 | return $true_return; |
---|
[1] | 298 | } |
---|
[475] | 299 | // $app->logMsg(sprintf('Current page (%s) != test URI (%s)', $actual_uri, $test_uri), LOG_DEBUG, __FILE__, __LINE__); |
---|
[461] | 300 | return $false_return; |
---|
[1] | 301 | } |
---|
[42] | 302 | |
---|
| 303 | } |
---|
[1] | 304 | // End of class. |
---|