[1] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * The Nav:: class provides a system for working with navigation elements. |
---|
| 4 | * Currently it supports storing page titles and URLs for printing breadcrumbs |
---|
| 5 | * and titles, as well as setting page features such as hiding the page title on |
---|
| 6 | * some pages but not others. |
---|
| 7 | * |
---|
| 8 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 9 | * @version 1.0 |
---|
| 10 | */ |
---|
| 11 | class Nav { |
---|
| 12 | |
---|
| 13 | var $pages = array(); |
---|
| 14 | var $default_features = array( |
---|
| 15 | 'title' => true, |
---|
| 16 | 'path' => true, |
---|
| 17 | 'breadcrumbs' => true, |
---|
| 18 | 'chop_breadcrumbs' => 0, |
---|
| 19 | 'chop_breadcrumb_links' => 1, |
---|
| 20 | ); |
---|
| 21 | var $path_delimiter = ' / '; |
---|
| 22 | var $features = array(); |
---|
| 23 | var $last_crumb_format = '%s'; |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * Constructor. Set default features to apply to all added pages. |
---|
| 27 | */ |
---|
| 28 | function Nav($default_features=null) |
---|
| 29 | { |
---|
| 30 | if (isset($default_features) && is_array($default_features)) { |
---|
| 31 | $this->default_features = array_merge($this->default_features, $default_features); |
---|
| 32 | } |
---|
| 33 | $this->features = $this->default_features; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | /****************************************************************************** |
---|
| 37 | * INPUT |
---|
| 38 | *****************************************************************************/ |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * Add a page to the internal pages array. Pages must be added sequentially |
---|
| 42 | * as they are to be printed. The root page must be added first, and the |
---|
| 43 | * current page added last. Features can be specified for a page, but currently |
---|
| 44 | * only the features for the current page can be set. Future versions of this |
---|
| 45 | * class may have the ability to set features for each page specifically. |
---|
| 46 | * |
---|
| 47 | * @access public |
---|
| 48 | * |
---|
| 49 | * @param string $title The title of the page. |
---|
| 50 | * @param string $url The URL to the page. Leave blank (or null) if |
---|
| 51 | * page is to not be linked. |
---|
| 52 | * @param array $features Set the features of the current page. |
---|
| 53 | */ |
---|
| 54 | function addPage($title, $url=null, $features=null) |
---|
| 55 | { |
---|
| 56 | $this->pages[] = array( |
---|
| 57 | 'title' => $title, |
---|
| 58 | 'url' => $url, |
---|
| 59 | 'features' => (isset($features) && is_array($features)) ? array_merge($this->default_features, $features) : $this->default_features |
---|
| 60 | ); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Set the features of the current page. Future versions of this class |
---|
| 65 | * may have the ability to set features for a specific page. In that case |
---|
| 66 | * some form of ID will need to be specified for each page. |
---|
| 67 | * |
---|
| 68 | * @param array $feature Array of feature keys and value to set. |
---|
| 69 | * |
---|
| 70 | * @return bool true on success, false on failure |
---|
| 71 | */ |
---|
| 72 | function setFeature($features=null, $page_id=null) |
---|
| 73 | { |
---|
| 74 | if (!isset($page_id)) { |
---|
| 75 | $page_id = sizeof($this->pages) - 1; |
---|
| 76 | } else if ($page_id < 0 && is_numeric($page_id)) { |
---|
| 77 | $page_id = sizeof($this->pages) + intval($page_id); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | if (isset($features) && is_array($features)) { |
---|
| 81 | // Set features for specified page. |
---|
| 82 | $this->pages[sizeof($this->pages)-1]['features'] = array_merge($this->pages[sizeof($this->pages)-1]['features'], $features); |
---|
| 83 | // Store "current page" features. |
---|
| 84 | $this->pages[$page_id]['features'] = array_merge($this->pages[$page_id]['features'], $features); |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * Unsets all page variables and resets features to the default set. |
---|
| 90 | * |
---|
| 91 | * @access public |
---|
| 92 | */ |
---|
| 93 | function clearPath() |
---|
| 94 | { |
---|
| 95 | $this->pages = array(); |
---|
| 96 | $this->features = $this->default_features; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /****************************************************************************** |
---|
| 100 | * OUTPUT |
---|
| 101 | *****************************************************************************/ |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Get the value of a feature for specified page_id or current page if page_id not specified (future use). |
---|
| 105 | * |
---|
| 106 | * @param string $feature Name of feature value to retreive. |
---|
| 107 | * @param mixed $page_id Future use: ID of page. |
---|
| 108 | * |
---|
| 109 | * @return bool true on success, false on failure |
---|
| 110 | */ |
---|
| 111 | function getFeature($feature, $page_id=null) |
---|
| 112 | { |
---|
| 113 | if (!isset($page_id)) { |
---|
| 114 | $page_id = sizeof($this->pages) - 1; |
---|
| 115 | } else if ($page_id < 0 && is_numeric($page_id)) { |
---|
| 116 | $page_id = sizeof($this->pages) + intval($page_id); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | switch ($feature) { |
---|
| 120 | case 'breadcrumbs' : |
---|
| 121 | // No breadcrumbs if displayed quantity of crumbs is less than 1. |
---|
| 122 | return $this->pages[$page_id]['features'][$feature] && ((sizeof($this->pages) - $this->getFeature('chop_breadcrumbs')) > 0); |
---|
| 123 | break; |
---|
| 124 | default : |
---|
| 125 | return $this->pages[$page_id]['features'][$feature]; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Returns the title of current page. |
---|
| 132 | * |
---|
| 133 | * @access public |
---|
| 134 | * |
---|
| 135 | * @return mixed Title of page (string) or false if title feature not set. |
---|
| 136 | */ |
---|
| 137 | function getTitle($page_id=null) |
---|
| 138 | { |
---|
| 139 | if (!isset($page_id)) { |
---|
| 140 | $page_id = sizeof($this->pages) - 1; |
---|
| 141 | } else if ($page_id < 0 && is_numeric($page_id)) { |
---|
| 142 | $page_id = sizeof($this->pages) + intval($page_id); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | if ($this->getFeature('title', $page_id)) { |
---|
| 146 | return oTxt($this->pages[$page_id]['title'], true); |
---|
| 147 | } else { |
---|
| 148 | return false; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * Prints the title of page returned by getTitle(). |
---|
| 154 | * |
---|
| 155 | * @access public |
---|
| 156 | */ |
---|
| 157 | function printTitle($page_id=null) |
---|
| 158 | { |
---|
| 159 | echo $this->getTitle($page_id); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * Returns the text path from root up to the current page, seperated by the |
---|
| 164 | * path_delimeter. |
---|
| 165 | * |
---|
| 166 | * @access public |
---|
| 167 | * |
---|
| 168 | * @return mixed Path (string) or false if path feature is not set. |
---|
| 169 | */ |
---|
| 170 | function getPath($page_id=null) |
---|
| 171 | { |
---|
| 172 | if (!isset($page_id)) { |
---|
| 173 | $page_id = sizeof($this->pages) - 1; |
---|
| 174 | } else if ($page_id < 0 && is_numeric($page_id)) { |
---|
| 175 | $page_id = sizeof($this->pages) + intval($page_id); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | if ($this->getFeature('path', $page_id)) { |
---|
| 179 | $path = ''; |
---|
| 180 | $pathmark = ''; |
---|
| 181 | foreach ($this->pages as $curr_id => $page) { |
---|
| 182 | $path .= oTxt($pathmark . strip_tags($page['title']), true); |
---|
| 183 | $pathmark = $this->path_delimiter; |
---|
| 184 | if ($curr_id === $page_id) { |
---|
| 185 | // Reached requested page. |
---|
| 186 | return $path; |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | return $path; |
---|
| 190 | } else { |
---|
| 191 | return false; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /** |
---|
| 196 | * Prints the path returned by getPath(). |
---|
| 197 | * |
---|
| 198 | * @access public |
---|
| 199 | */ |
---|
| 200 | function printPath($page_id=null) |
---|
| 201 | { |
---|
| 202 | echo $this->getPath($page_id); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /** |
---|
| 206 | * Returns the breadcrumbs from the root page to the current page. |
---|
| 207 | * Breadcrumbs are the text path with pages titles linked to that page. |
---|
| 208 | * |
---|
| 209 | * @access public |
---|
| 210 | * |
---|
| 211 | * @return mixed Breadcrumbs (string) or false if breadcrumbs feature not set. |
---|
| 212 | */ |
---|
| 213 | function getBreadcrumbs($page_id=null) |
---|
| 214 | { |
---|
| 215 | if (!isset($page_id)) { |
---|
| 216 | $page_id = sizeof($this->pages) - 1; |
---|
| 217 | } else if ($page_id < 0 && is_numeric($page_id)) { |
---|
| 218 | $page_id = sizeof($this->pages) + intval($page_id); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | if ($this->getFeature('breadcrumbs')) { |
---|
| 222 | $breadcrumbs = ''; |
---|
| 223 | $pathmark = ''; |
---|
| 224 | $crumb_count = sizeof($this->pages); |
---|
| 225 | foreach ($this->pages as $curr_id => $page) { |
---|
| 226 | if ($crumb_count <= $this->getFeature('chop_breadcrumbs')) { |
---|
| 227 | // Stop gathering crumbs. |
---|
| 228 | return $breadcrumbs; |
---|
| 229 | } |
---|
| 230 | if ($crumb_count <= 1) { |
---|
| 231 | // The last crumb. |
---|
| 232 | if (empty($page['url']) || $crumb_count <= $this->getFeature('chop_breadcrumb_links')) { |
---|
| 233 | // A crumb with no link. |
---|
| 234 | $breadcrumbs .= oTxt($pathmark, true) . sprintf($this->last_crumb_format, oTxt($page['title'], true)); |
---|
| 235 | } else if ($crumb_count > $this->getFeature('chop_breadcrumb_links')) { |
---|
| 236 | // A normal linked crumb. |
---|
| 237 | $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . ohref($page['url']) . '">' . sprintf($this->last_crumb_format, oTxt($page['title'], true)) . '</a>'; |
---|
| 238 | } |
---|
| 239 | } else { |
---|
| 240 | if (empty($page['url'])) { |
---|
| 241 | // A crumb with no link. |
---|
| 242 | $breadcrumbs .= oTxt($pathmark . $page['title'], true); |
---|
| 243 | } else { |
---|
| 244 | // A normal linked crumb. |
---|
| 245 | $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . ohref($page['url']) . '">' . oTxt($page['title'], true) . '</a>'; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | $pathmark = $this->path_delimiter; |
---|
| 249 | $crumb_count--; |
---|
| 250 | |
---|
| 251 | if ($curr_id === $page_id) { |
---|
| 252 | // Reached requested page. |
---|
| 253 | return $breadcrumbs; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | return $breadcrumbs; |
---|
| 257 | } else { |
---|
| 258 | return false; |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * Prints the breadcrumbs returned by getBreadcrumbs(). |
---|
| 264 | * |
---|
| 265 | * @access public |
---|
| 266 | */ |
---|
| 267 | function printBreadcrumbs($page_id=null) |
---|
| 268 | { |
---|
| 269 | echo $this->getBreadcrumbs($page_id); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | /** |
---|
| 273 | * Returns a string if the queried page is the current page. One use is to print |
---|
| 274 | * CSS tags if the current page matches a link, such as: |
---|
| 275 | * $nav->currentPage('mypage.php', ' id="current"'); |
---|
| 276 | * |
---|
| 277 | * @access public |
---|
| 278 | * |
---|
| 279 | * @param mixed $page The URI of the page to query. |
---|
| 280 | * @param mixed $return The value to return if the current page matches the page queried. |
---|
| 281 | * |
---|
| 282 | * @return bool True on success, false on failure. |
---|
| 283 | */ |
---|
| 284 | function currentPage($page_uri, $return=true) |
---|
| 285 | { |
---|
| 286 | if (preg_match('/^' . preg_quote($page_uri, '/') . '/i', $_SERVER['PHP_SELF'])) { |
---|
| 287 | return $return; |
---|
| 288 | } |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | } |
---|
| 292 | // End of class. |
---|
| 293 | |
---|
| 294 | ?> |
---|