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