[1] | 1 | <?php |
---|
| 2 | /** |
---|
[184] | 3 | * Navigation.inc.php |
---|
[136] | 4 | * code by strangecode :: www.strangecode.com :: this document contains copyrighted information |
---|
| 5 | * |
---|
| 6 | * The Nav class provides a system for working with navigation elements. |
---|
[184] | 7 | * It supports storing page titles and URLs for printing breadcrumbs |
---|
| 8 | * and titles, as well as setting page params such as hiding the page title on |
---|
| 9 | * some pages but not others, and storing vars like the page title itself. |
---|
| 10 | * |
---|
| 11 | * Note: this class was renamed from "Nav" because of the change in API and to be more descriptive. |
---|
[1] | 12 | * |
---|
| 13 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[184] | 14 | * @version 2.0 |
---|
[1] | 15 | */ |
---|
[184] | 16 | class Navigation { |
---|
[1] | 17 | |
---|
[184] | 18 | // Configuration parameters for this object. |
---|
| 19 | var $_params = array( |
---|
[185] | 20 | 'head_title' => true, |
---|
| 21 | 'body_title' => true, |
---|
[1] | 22 | 'title' => true, |
---|
| 23 | 'path' => true, |
---|
| 24 | 'breadcrumbs' => true, |
---|
| 25 | 'chop_breadcrumbs' => 0, |
---|
| 26 | 'chop_breadcrumb_links' => 1, |
---|
[184] | 27 | 'path_delimiter' => ' / ', |
---|
| 28 | 'last_crumb_format' => '%s', |
---|
[1] | 29 | ); |
---|
[184] | 30 | var $pages = array(); |
---|
[1] | 31 | |
---|
| 32 | /** |
---|
[184] | 33 | * Navigation constructor. |
---|
[1] | 34 | */ |
---|
[184] | 35 | function Navigation($params=null) |
---|
[1] | 36 | { |
---|
[184] | 37 | $app =& App::getInstance(); |
---|
| 38 | |
---|
| 39 | if (isset($params) && is_array($params)) { |
---|
| 40 | // Merge new parameters with old overriding only those passed. |
---|
| 41 | $this->_params = array_merge($this->_params, $params); |
---|
[1] | 42 | } |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * Add a page to the internal pages array. Pages must be added sequentially |
---|
| 47 | * as they are to be printed. The root page must be added first, and the |
---|
[184] | 48 | * current page added last. Vars can be specified for any page, but only vars |
---|
| 49 | * from the "current" page will be accessed with Nav::get. |
---|
| 50 | * |
---|
[1] | 51 | * @access public |
---|
| 52 | * @param string $title The title of the page. |
---|
[184] | 53 | * @param string $url The URL to the page. Set to null to use PHP_SELF. |
---|
| 54 | * @param array $vars Additoinal page variables. |
---|
[1] | 55 | */ |
---|
[184] | 56 | function add($title, $url=null, $vars=array()) |
---|
[1] | 57 | { |
---|
[184] | 58 | $page = array( |
---|
| 59 | 'title' => $title, |
---|
[185] | 60 | 'url' => is_null($url) ? $_SERVER['PHP_SELF'] : $url, |
---|
[1] | 61 | ); |
---|
[184] | 62 | $this->pages[] = array_merge($page, $vars); |
---|
[1] | 63 | } |
---|
[42] | 64 | |
---|
[1] | 65 | /** |
---|
[184] | 66 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
[1] | 67 | * |
---|
[184] | 68 | * @access public |
---|
| 69 | * @param array $params Array of parameters (key => val pairs). |
---|
| 70 | */ |
---|
| 71 | function setParam($params) |
---|
| 72 | { |
---|
| 73 | $app =& App::getInstance(); |
---|
| 74 | |
---|
| 75 | if (isset($params) && is_array($params)) { |
---|
| 76 | // Merge new parameters with old overriding only those passed. |
---|
| 77 | $this->_params = array_merge($this->_params, $params); |
---|
| 78 | } else { |
---|
| 79 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Return the value of a parameter, if it exists. |
---|
[1] | 85 | * |
---|
[184] | 86 | * @access public |
---|
| 87 | * @param string $param Which parameter to return. |
---|
| 88 | * @return mixed Configured parameter value. |
---|
[1] | 89 | */ |
---|
[184] | 90 | function getParam($param) |
---|
[1] | 91 | { |
---|
[184] | 92 | $app =& App::getInstance(); |
---|
| 93 | |
---|
| 94 | if (isset($this->_params[$param])) { |
---|
| 95 | return $this->_params[$param]; |
---|
| 96 | } else { |
---|
| 97 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 98 | return null; |
---|
[1] | 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | /** |
---|
[184] | 103 | * Unsets all pages. |
---|
[1] | 104 | * |
---|
| 105 | * @access public |
---|
| 106 | */ |
---|
[184] | 107 | function clear() |
---|
[1] | 108 | { |
---|
| 109 | $this->pages = array(); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
[184] | 113 | * Sets a variable into the current page. |
---|
[1] | 114 | * |
---|
[184] | 115 | * @access public |
---|
| 116 | * @param mixed $key Which value to set. |
---|
| 117 | * @param mixed $val Value to set variable to. |
---|
[1] | 118 | */ |
---|
[184] | 119 | function set($key, $val) |
---|
[1] | 120 | { |
---|
[184] | 121 | // Set params of current page. |
---|
| 122 | $curr_page =& $this->pages[sizeof($this->pages) - 1]; |
---|
| 123 | $curr_page[$key] = $val; |
---|
[1] | 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
[184] | 127 | * Returns a specified value from the current page. |
---|
[1] | 128 | * |
---|
[184] | 129 | * @access public |
---|
| 130 | * @param mixed $key Which value to return. |
---|
| 131 | * @param mixed $default Value to return if key not found in user_data. |
---|
| 132 | * @return mixed Value stored in session. |
---|
[1] | 133 | */ |
---|
[184] | 134 | function get($key, $default='') |
---|
[1] | 135 | { |
---|
[184] | 136 | $curr_page =& $this->pages[sizeof($this->pages) - 1]; |
---|
| 137 | |
---|
| 138 | switch ($key) { |
---|
| 139 | case 'title' : |
---|
| 140 | if ($this->getParam('title') && isset($curr_page['title'])) { |
---|
| 141 | return $curr_page['title']; |
---|
| 142 | } |
---|
| 143 | break; |
---|
[42] | 144 | |
---|
[184] | 145 | case 'head_title' : |
---|
| 146 | if ($this->getParam('head_title') && $this->getParam('title') && isset($curr_page['head_title'])) { |
---|
| 147 | return $curr_page['head_title']; |
---|
| 148 | } |
---|
| 149 | break; |
---|
| 150 | |
---|
| 151 | case 'body_title' : |
---|
| 152 | if (!$this->getParam('body_title') && $this->getParam('title') && isset($curr_page['body_title'])) { |
---|
| 153 | return $curr_page['body_title']; |
---|
| 154 | } |
---|
| 155 | break; |
---|
| 156 | |
---|
| 157 | case 'path' : |
---|
| 158 | if ($this->getParam('path')) { |
---|
| 159 | return $this->getPath(); |
---|
| 160 | } |
---|
| 161 | break; |
---|
| 162 | |
---|
| 163 | case 'breadcrumbs' : |
---|
| 164 | if ($this->getParam('breadcrumbs')) { |
---|
| 165 | return $this->getBreadcrumbs(); |
---|
| 166 | } |
---|
| 167 | break; |
---|
| 168 | |
---|
| 169 | default : |
---|
| 170 | return isset($curr_page[$key]) ? $curr_page[$key] : $default; |
---|
| 171 | break; |
---|
[1] | 172 | } |
---|
| 173 | |
---|
[184] | 174 | return $default; |
---|
[1] | 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * Returns the text path from root up to the current page, seperated by the |
---|
| 179 | * path_delimeter. |
---|
| 180 | * |
---|
| 181 | * @access public |
---|
| 182 | * |
---|
[184] | 183 | * @return mixed Path (string) or false if path param is not set. |
---|
[1] | 184 | */ |
---|
[184] | 185 | function getPath() |
---|
[1] | 186 | { |
---|
[184] | 187 | if ($this->getParam('path')) { |
---|
[1] | 188 | $path = ''; |
---|
| 189 | $pathmark = ''; |
---|
[184] | 190 | foreach ($this->pages as $page) { |
---|
[1] | 191 | $path .= oTxt($pathmark . strip_tags($page['title']), true); |
---|
[184] | 192 | $pathmark = $this->getParam('path_delimiter'); |
---|
[1] | 193 | } |
---|
| 194 | return $path; |
---|
| 195 | } else { |
---|
| 196 | return false; |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | * Returns the breadcrumbs from the root page to the current page. |
---|
| 202 | * Breadcrumbs are the text path with pages titles linked to that page. |
---|
| 203 | * |
---|
| 204 | * @access public |
---|
| 205 | * |
---|
[184] | 206 | * @return mixed Breadcrumbs (string) or false if breadcrumbs param not set. |
---|
[1] | 207 | */ |
---|
[184] | 208 | function getBreadcrumbs() |
---|
[1] | 209 | { |
---|
[136] | 210 | $app =& App::getInstance(); |
---|
| 211 | |
---|
[184] | 212 | if ($this->getParam('breadcrumbs')) { |
---|
[1] | 213 | $breadcrumbs = ''; |
---|
| 214 | $pathmark = ''; |
---|
| 215 | $crumb_count = sizeof($this->pages); |
---|
[184] | 216 | foreach ($this->pages as $page) { |
---|
| 217 | if ($crumb_count <= $this->getParam('chop_breadcrumbs')) { |
---|
[1] | 218 | // Stop gathering crumbs. |
---|
| 219 | return $breadcrumbs; |
---|
| 220 | } |
---|
| 221 | if ($crumb_count <= 1) { |
---|
| 222 | // The last crumb. |
---|
[184] | 223 | if ('' == trim($page['url']) || $crumb_count <= $this->getParam('chop_breadcrumb_links')) { |
---|
[1] | 224 | // A crumb with no link. |
---|
[184] | 225 | $breadcrumbs .= oTxt($pathmark, true) . sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)); |
---|
| 226 | } else if ($crumb_count > $this->getParam('chop_breadcrumb_links')) { |
---|
[1] | 227 | // A normal linked crumb. |
---|
[184] | 228 | $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . $app->oHREF($page['url']) . '">' . sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)) . '</a>'; |
---|
[1] | 229 | } |
---|
| 230 | } else { |
---|
[184] | 231 | if ('' == trim($page['url'])) { |
---|
[1] | 232 | // A crumb with no link. |
---|
| 233 | $breadcrumbs .= oTxt($pathmark . $page['title'], true); |
---|
| 234 | } else { |
---|
| 235 | // A normal linked crumb. |
---|
[136] | 236 | $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . $app->oHREF($page['url']) . '">' . oTxt($page['title'], true) . '</a>'; |
---|
[1] | 237 | } |
---|
| 238 | } |
---|
[184] | 239 | $pathmark = $this->getParam('path_delimiter'); |
---|
[1] | 240 | $crumb_count--; |
---|
| 241 | } |
---|
| 242 | return $breadcrumbs; |
---|
| 243 | } else { |
---|
| 244 | return false; |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | * Returns a string if the queried page is the current page. One use is to print |
---|
| 250 | * CSS tags if the current page matches a link, such as: |
---|
| 251 | * $nav->currentPage('mypage.php', ' id="current"'); |
---|
| 252 | * |
---|
| 253 | * @access public |
---|
| 254 | * |
---|
[44] | 255 | * @param mixed $page The URI of the page to query, with PREG express markup, if needed. |
---|
[1] | 256 | * @param mixed $return The value to return if the current page matches the page queried. |
---|
| 257 | * |
---|
[44] | 258 | * @return mixed The value set for $return, TRUE by default. |
---|
[1] | 259 | */ |
---|
| 260 | function currentPage($page_uri, $return=true) |
---|
| 261 | { |
---|
[155] | 262 | // $page_uri = str_replace('/', '\/', $page_uri); |
---|
| 263 | if (preg_match('/^' . preg_quote(urldecode($page_uri), '/') . '/i', $_SERVER['PHP_SELF'])) { |
---|
[1] | 264 | return $return; |
---|
| 265 | } |
---|
| 266 | } |
---|
[42] | 267 | |
---|
| 268 | } |
---|
[1] | 269 | // End of class. |
---|
| 270 | |
---|
| 271 | ?> |
---|