Changeset 184 for trunk/lib/Navigation.inc.php
- Timestamp:
- Jun 24, 2006 10:30:29 PM (18 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Navigation.inc.php
r174 r184 1 1 <?php 2 2 /** 3 * Nav .inc.php3 * Navigation.inc.php 4 4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information 5 5 * 6 6 * The Nav class provides a system for working with navigation elements. 7 * Currently it supports storing page titles and URLs for printing breadcrumbs 8 * and titles, as well as setting page features such as hiding the page title on 9 * some pages but not others. 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. 10 12 * 11 13 * @author Quinn Comendant <quinn@strangecode.com> 12 * @version 1.014 * @version 2.0 13 15 */ 14 class Nav {15 16 var $pages = array();17 var $ path_delimiter = ' / ';18 var $features = array();19 var $default_features = array(16 class Navigation { 17 18 // Configuration parameters for this object. 19 var $_params = array( 20 'html_title' = true, 21 'body_title' = true, 20 22 'title' => true, 21 23 'path' => true, … … 23 25 'chop_breadcrumbs' => 0, 24 26 'chop_breadcrumb_links' => 1, 27 'path_delimiter' => ' / ', 28 'last_crumb_format' => '%s', 25 29 ); 26 var $last_crumb_format = '%s'; 27 28 /** 29 * Constructor. Set default features to apply to all added pages. 30 */ 31 function Nav($default_features=null) 32 { 33 if (isset($default_features) && is_array($default_features)) { 34 $this->default_features = array_merge($this->default_features, $default_features); 35 } 36 $this->features = $this->default_features; 37 } 38 39 /******************************************************************** 40 * INPUT 41 ********************************************************************/ 30 var $pages = array(); 31 32 /** 33 * Navigation constructor. 34 */ 35 function Navigation($params=null) 36 { 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); 42 } 43 } 42 44 43 45 /** 44 46 * Add a page to the internal pages array. Pages must be added sequentially 45 47 * as they are to be printed. The root page must be added first, and the 46 * current page added last. Features can be specified for a page, but currently 47 * only the features for the current page can be set. Future versions of this 48 * class may have the ability to set features for each page specifically. 49 * 50 * @access public 51 * 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 * 51 * @access public 52 52 * @param string $title The title of the page. 53 * @param string $url The URL to the page. Leave blank (or null) if 54 * page is to not be linked. 55 * @param array $features Set the features of the current page. 56 */ 57 function addPage($title, $url=null, $features=null) 58 { 59 $this->pages[] = array( 60 'title' => $title, 61 'url' => $url, 62 'features' => (isset($features) && is_array($features)) ? array_merge($this->default_features, $features) : $this->default_features 53 * @param string $url The URL to the page. Set to null to use PHP_SELF. 54 * @param array $vars Additoinal page variables. 55 */ 56 function add($title, $url=null, $vars=array()) 57 { 58 $page = array( 59 'title' => $title, 60 'url' => is_null($url) ? $_SERVER['PHP_SELF'] : $url; 63 61 ); 64 } 65 66 /** 67 * Set the features of the current page. Future versions of this class 68 * may have the ability to set features for a specific page. In that case 69 * some form of ID will need to be specified for each page. 70 * 71 * @param array $feature Array of feature keys and value to set. 72 * 73 * @return bool true on success, false on failure 74 */ 75 function setFeature($features=null, $page_id=null) 76 { 77 $page_id = $this->_calculatePageID($page_id); 78 79 if (isset($features) && is_array($features) && isset($this->pages[sizeof($this->pages)-1]['features']) && is_array($this->pages[sizeof($this->pages)-1]['features'])) { 80 // Set features for specified page. 81 $this->pages[sizeof($this->pages)-1]['features'] = array_merge($this->pages[sizeof($this->pages)-1]['features'], $features); 82 // TODO: Store "current page" features. 83 // $this->pages[$page_id]['features'] = array_merge($this->pages[$page_id]['features'], $features); 84 } 85 } 86 87 /** 88 * Unsets all page variables and resets features to the default set. 89 * 90 * @access public 91 */ 92 function clearPath() 62 $this->pages[] = array_merge($page, $vars); 63 } 64 65 /** 66 * Set (or overwrite existing) parameters by passing an array of new parameters. 67 * 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. 85 * 86 * @access public 87 * @param string $param Which parameter to return. 88 * @return mixed Configured parameter value. 89 */ 90 function getParam($param) 91 { 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; 99 } 100 } 101 102 /** 103 * Unsets all pages. 104 * 105 * @access public 106 */ 107 function clear() 93 108 { 94 109 $this->pages = array(); 95 $this->features = $this->default_features; 96 } 97 98 /******************************************************************** 99 * OUTPUT 100 ********************************************************************/ 101 102 /** 103 * Get the value of a feature for specified page_id or current page if page_id not specified (future use). 104 * 105 * @param string $feature Name of feature value to retreive. 106 * @param mixed $page_id Future use: ID of page. 107 * 108 * @return bool true on success, false on failure 109 */ 110 function getFeature($feature, $page_id=null, $default=null) 111 { 112 $page_id = $this->_calculatePageID($page_id); 113 114 if (isset($this->pages[$page_id]['features'][$feature])) { 115 switch ($feature) { 116 case 'breadcrumbs' : 117 // No breadcrumbs if displayed quantity of crumbs is less than 1. 118 return $this->pages[$page_id]['features'][$feature] && ((sizeof($this->pages) - $this->getFeature('chop_breadcrumbs')) > 0); 119 break; 120 default : 121 return $this->pages[$page_id]['features'][$feature]; 122 } 123 } else { 124 return $default; 125 } 126 } 127 128 129 /** 130 * Returns the title of current page. 131 * 132 * @access public 133 * 134 * @return mixed Title of page (string) or false if title feature not set. 135 */ 136 function getTitle($page_id=null) 137 { 138 $page_id = $this->_calculatePageID($page_id); 139 140 if ($this->getFeature('title', $page_id)) { 141 return oTxt($this->pages[$page_id]['title'], true); 142 } else { 143 return false; 144 } 145 } 146 147 /** 148 * Prints the title of page returned by getTitle(). 149 * 150 * @access public 151 */ 152 function printTitle($page_id=null) 153 { 154 echo $this->getTitle($page_id); 110 } 111 112 /** 113 * Sets a variable into the current page. 114 * 115 * @access public 116 * @param mixed $key Which value to set. 117 * @param mixed $val Value to set variable to. 118 */ 119 function set($key, $val) 120 { 121 // Set params of current page. 122 $curr_page =& $this->pages[sizeof($this->pages) - 1]; 123 $curr_page[$key] = $val; 124 } 125 126 /** 127 * Returns a specified value from the current page. 128 * 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. 133 */ 134 function get($key, $default='') 135 { 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; 144 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; 172 } 173 174 return $default; 155 175 } 156 176 … … 161 181 * @access public 162 182 * 163 * @return mixed Path (string) or false if path feature is not set. 164 */ 165 function getPath($page_id=null) 166 { 167 $page_id = $this->_calculatePageID($page_id); 168 169 if ($this->getFeature('path', $page_id)) { 183 * @return mixed Path (string) or false if path param is not set. 184 */ 185 function getPath() 186 { 187 if ($this->getParam('path')) { 170 188 $path = ''; 171 189 $pathmark = ''; 172 foreach ($this->pages as $ curr_id => $page) {190 foreach ($this->pages as $page) { 173 191 $path .= oTxt($pathmark . strip_tags($page['title']), true); 174 $pathmark = $this->path_delimiter; 175 if ($curr_id === $page_id) { 176 // Reached requested page. 177 return $path; 178 } 192 $pathmark = $this->getParam('path_delimiter'); 179 193 } 180 194 return $path; … … 182 196 return false; 183 197 } 184 }185 186 /**187 * Prints the path returned by getPath().188 *189 * @access public190 */191 function printPath($page_id=null)192 {193 echo $this->getPath($page_id);194 198 } 195 199 … … 200 204 * @access public 201 205 * 202 * @return mixed Breadcrumbs (string) or false if breadcrumbs feature not set. 203 */ 204 function getBreadcrumbs($page_id=null) 205 { 206 $app =& App::getInstance(); 207 208 $page_id = $this->_calculatePageID($page_id); 209 210 if ($this->getFeature('breadcrumbs')) { 206 * @return mixed Breadcrumbs (string) or false if breadcrumbs param not set. 207 */ 208 function getBreadcrumbs() 209 { 210 $app =& App::getInstance(); 211 212 if ($this->getParam('breadcrumbs')) { 211 213 $breadcrumbs = ''; 212 214 $pathmark = ''; 213 215 $crumb_count = sizeof($this->pages); 214 foreach ($this->pages as $ curr_id => $page) {215 if ($crumb_count <= $this->get Feature('chop_breadcrumbs')) {216 foreach ($this->pages as $page) { 217 if ($crumb_count <= $this->getParam('chop_breadcrumbs')) { 216 218 // Stop gathering crumbs. 217 219 return $breadcrumbs; … … 219 221 if ($crumb_count <= 1) { 220 222 // The last crumb. 221 if ( empty($page['url']) || $crumb_count <= $this->getFeature('chop_breadcrumb_links')) {223 if ('' == trim($page['url']) || $crumb_count <= $this->getParam('chop_breadcrumb_links')) { 222 224 // A crumb with no link. 223 $breadcrumbs .= oTxt($pathmark, true) . sprintf($this-> last_crumb_format, oTxt($page['title'], true));224 } else if ($crumb_count > $this->get Feature('chop_breadcrumb_links')) {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')) { 225 227 // A normal linked crumb. 226 $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . $app->oHREF($page['url']) . '">' . sprintf($this-> last_crumb_format, oTxt($page['title'], true)) . '</a>';228 $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . $app->oHREF($page['url']) . '">' . sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)) . '</a>'; 227 229 } 228 230 } else { 229 if ( empty($page['url'])) {231 if ('' == trim($page['url'])) { 230 232 // A crumb with no link. 231 233 $breadcrumbs .= oTxt($pathmark . $page['title'], true); … … 235 237 } 236 238 } 237 $pathmark = $this-> path_delimiter;239 $pathmark = $this->getParam('path_delimiter'); 238 240 $crumb_count--; 239 240 if ($curr_id === $page_id) {241 // Reached requested page.242 return $breadcrumbs;243 }244 241 } 245 242 return $breadcrumbs; … … 247 244 return false; 248 245 } 249 }250 251 /**252 * Prints the breadcrumbs returned by getBreadcrumbs().253 *254 * @access public255 */256 function printBreadcrumbs($page_id=null)257 {258 echo $this->getBreadcrumbs($page_id);259 246 } 260 247 … … 279 266 } 280 267 281 /**282 * Returns the ID of the current page, or the adjusted ID for a given page ID.283 *284 * @access private285 * @return string The value of the current page id.286 */287 function _calculatePageID($page_id=null)288 {289 if (!isset($page_id)) {290 return sizeof($this->pages) - 1;291 } else if ($page_id < 0 && is_numeric($page_id)) {292 return sizeof($this->pages) + intval($page_id);293 } else {294 return $page_id;295 }296 }297 298 268 } 299 269 // End of class.
Note: See TracChangeset
for help on using the changeset viewer.