[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/> |
---|
[376] | 5 | * Copyright 2001-2010 Strangecode, LLC |
---|
[362] | 6 | * |
---|
| 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. |
---|
| 13 | * |
---|
| 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. |
---|
| 18 | * |
---|
| 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. |
---|
| 30 | * |
---|
| 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. |
---|
| 39 | var $_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 | ); |
---|
[184] | 50 | var $pages = array(); |
---|
[1] | 51 | |
---|
| 52 | /** |
---|
[184] | 53 | * Navigation constructor. |
---|
[1] | 54 | */ |
---|
[184] | 55 | function Navigation($params=null) |
---|
[1] | 56 | { |
---|
[184] | 57 | $app =& App::getInstance(); |
---|
| 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. |
---|
| 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 | */ |
---|
[184] | 76 | 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 | */ |
---|
| 93 | function setParam($params) |
---|
| 94 | { |
---|
| 95 | $app =& App::getInstance(); |
---|
| 96 | |
---|
| 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 | */ |
---|
[184] | 112 | function getParam($param) |
---|
[1] | 113 | { |
---|
[184] | 114 | $app =& App::getInstance(); |
---|
| 115 | |
---|
| 116 | if (isset($this->_params[$param])) { |
---|
| 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 | */ |
---|
[184] | 129 | 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 | */ |
---|
[184] | 141 | function set($key, $val) |
---|
[1] | 142 | { |
---|
[184] | 143 | // Set params of current page. |
---|
[186] | 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 | */ |
---|
[184] | 156 | function get($key, $default='') |
---|
[1] | 157 | { |
---|
[184] | 158 | $curr_page =& $this->pages[sizeof($this->pages) - 1]; |
---|
| 159 | |
---|
| 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 | */ |
---|
[304] | 207 | 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 | */ |
---|
[184] | 229 | function getBreadcrumbs() |
---|
[1] | 230 | { |
---|
[136] | 231 | $app =& App::getInstance(); |
---|
| 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 | /** |
---|
| 270 | * Returns a string if the queried page is the current page. One use is to print |
---|
| 271 | * CSS tags if the current page matches a link, such as: |
---|
| 272 | * $nav->currentPage('mypage.php', ' id="current"'); |
---|
| 273 | * |
---|
| 274 | * @access public |
---|
| 275 | * |
---|
[44] | 276 | * @param mixed $page The URI of the page to query, with PREG express markup, if needed. |
---|
[1] | 277 | * @param mixed $return The value to return if the current page matches the page queried. |
---|
| 278 | * |
---|
[44] | 279 | * @return mixed The value set for $return, TRUE by default. |
---|
[1] | 280 | */ |
---|
| 281 | function currentPage($page_uri, $return=true) |
---|
| 282 | { |
---|
[155] | 283 | // $page_uri = str_replace('/', '\/', $page_uri); |
---|
| 284 | if (preg_match('/^' . preg_quote(urldecode($page_uri), '/') . '/i', $_SERVER['PHP_SELF'])) { |
---|
[1] | 285 | return $return; |
---|
| 286 | } |
---|
| 287 | } |
---|
[42] | 288 | |
---|
| 289 | } |
---|
[1] | 290 | // End of class. |
---|
| 291 | |
---|
| 292 | ?> |
---|