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 $path_delimiter = ' / '; |
---|
15 | var $features = array(); |
---|
16 | var $default_features = array( |
---|
17 | 'title' => true, |
---|
18 | 'path' => true, |
---|
19 | 'breadcrumbs' => true, |
---|
20 | 'chop_breadcrumbs' => 0, |
---|
21 | 'chop_breadcrumb_links' => 1, |
---|
22 | ); |
---|
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 | $page_id = $this->_calculatePageID($page_id); |
---|
75 | |
---|
76 | if (isset($features) && is_array($features) && isset($this->pages[sizeof($this->pages)-1]['features']) && is_array($this->pages[sizeof($this->pages)-1]['features'])) { |
---|
77 | // Set features for specified page. |
---|
78 | $this->pages[sizeof($this->pages)-1]['features'] = array_merge($this->pages[sizeof($this->pages)-1]['features'], $features); |
---|
79 | // TODO: Store "current page" features. |
---|
80 | // $this->pages[$page_id]['features'] = array_merge($this->pages[$page_id]['features'], $features); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Unsets all page variables and resets features to the default set. |
---|
86 | * |
---|
87 | * @access public |
---|
88 | */ |
---|
89 | function clearPath() |
---|
90 | { |
---|
91 | $this->pages = array(); |
---|
92 | $this->features = $this->default_features; |
---|
93 | } |
---|
94 | |
---|
95 | /****************************************************************************** |
---|
96 | * OUTPUT |
---|
97 | *****************************************************************************/ |
---|
98 | |
---|
99 | /** |
---|
100 | * Get the value of a feature for specified page_id or current page if page_id not specified (future use). |
---|
101 | * |
---|
102 | * @param string $feature Name of feature value to retreive. |
---|
103 | * @param mixed $page_id Future use: ID of page. |
---|
104 | * |
---|
105 | * @return bool true on success, false on failure |
---|
106 | */ |
---|
107 | function getFeature($feature, $page_id=null, $default=null) |
---|
108 | { |
---|
109 | $page_id = $this->_calculatePageID($page_id); |
---|
110 | |
---|
111 | if (isset($this->pages[$page_id]['features'][$feature])) { |
---|
112 | switch ($feature) { |
---|
113 | case 'breadcrumbs' : |
---|
114 | // No breadcrumbs if displayed quantity of crumbs is less than 1. |
---|
115 | return $this->pages[$page_id]['features'][$feature] && ((sizeof($this->pages) - $this->getFeature('chop_breadcrumbs')) > 0); |
---|
116 | break; |
---|
117 | default : |
---|
118 | return $this->pages[$page_id]['features'][$feature]; |
---|
119 | } |
---|
120 | } else { |
---|
121 | return $default; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | /** |
---|
127 | * Returns the title of current page. |
---|
128 | * |
---|
129 | * @access public |
---|
130 | * |
---|
131 | * @return mixed Title of page (string) or false if title feature not set. |
---|
132 | */ |
---|
133 | function getTitle($page_id=null) |
---|
134 | { |
---|
135 | $page_id = $this->_calculatePageID($page_id); |
---|
136 | |
---|
137 | if ($this->getFeature('title', $page_id)) { |
---|
138 | return oTxt($this->pages[$page_id]['title'], true); |
---|
139 | } else { |
---|
140 | return false; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * Prints the title of page returned by getTitle(). |
---|
146 | * |
---|
147 | * @access public |
---|
148 | */ |
---|
149 | function printTitle($page_id=null) |
---|
150 | { |
---|
151 | echo $this->getTitle($page_id); |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Returns the text path from root up to the current page, seperated by the |
---|
156 | * path_delimeter. |
---|
157 | * |
---|
158 | * @access public |
---|
159 | * |
---|
160 | * @return mixed Path (string) or false if path feature is not set. |
---|
161 | */ |
---|
162 | function getPath($page_id=null) |
---|
163 | { |
---|
164 | $page_id = $this->_calculatePageID($page_id); |
---|
165 | |
---|
166 | if ($this->getFeature('path', $page_id)) { |
---|
167 | $path = ''; |
---|
168 | $pathmark = ''; |
---|
169 | foreach ($this->pages as $curr_id => $page) { |
---|
170 | $path .= oTxt($pathmark . strip_tags($page['title']), true); |
---|
171 | $pathmark = $this->path_delimiter; |
---|
172 | if ($curr_id === $page_id) { |
---|
173 | // Reached requested page. |
---|
174 | return $path; |
---|
175 | } |
---|
176 | } |
---|
177 | return $path; |
---|
178 | } else { |
---|
179 | return false; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * Prints the path returned by getPath(). |
---|
185 | * |
---|
186 | * @access public |
---|
187 | */ |
---|
188 | function printPath($page_id=null) |
---|
189 | { |
---|
190 | echo $this->getPath($page_id); |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * Returns the breadcrumbs from the root page to the current page. |
---|
195 | * Breadcrumbs are the text path with pages titles linked to that page. |
---|
196 | * |
---|
197 | * @access public |
---|
198 | * |
---|
199 | * @return mixed Breadcrumbs (string) or false if breadcrumbs feature not set. |
---|
200 | */ |
---|
201 | function getBreadcrumbs($page_id=null) |
---|
202 | { |
---|
203 | $page_id = $this->_calculatePageID($page_id); |
---|
204 | |
---|
205 | if ($this->getFeature('breadcrumbs')) { |
---|
206 | $breadcrumbs = ''; |
---|
207 | $pathmark = ''; |
---|
208 | $crumb_count = sizeof($this->pages); |
---|
209 | foreach ($this->pages as $curr_id => $page) { |
---|
210 | if ($crumb_count <= $this->getFeature('chop_breadcrumbs')) { |
---|
211 | // Stop gathering crumbs. |
---|
212 | return $breadcrumbs; |
---|
213 | } |
---|
214 | if ($crumb_count <= 1) { |
---|
215 | // The last crumb. |
---|
216 | if (empty($page['url']) || $crumb_count <= $this->getFeature('chop_breadcrumb_links')) { |
---|
217 | // A crumb with no link. |
---|
218 | $breadcrumbs .= oTxt($pathmark, true) . sprintf($this->last_crumb_format, oTxt($page['title'], true)); |
---|
219 | } else if ($crumb_count > $this->getFeature('chop_breadcrumb_links')) { |
---|
220 | // A normal linked crumb. |
---|
221 | $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . App::oHREF($page['url']) . '">' . sprintf($this->last_crumb_format, oTxt($page['title'], true)) . '</a>'; |
---|
222 | } |
---|
223 | } else { |
---|
224 | if (empty($page['url'])) { |
---|
225 | // A crumb with no link. |
---|
226 | $breadcrumbs .= oTxt($pathmark . $page['title'], true); |
---|
227 | } else { |
---|
228 | // A normal linked crumb. |
---|
229 | $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . App::oHREF($page['url']) . '">' . oTxt($page['title'], true) . '</a>'; |
---|
230 | } |
---|
231 | } |
---|
232 | $pathmark = $this->path_delimiter; |
---|
233 | $crumb_count--; |
---|
234 | |
---|
235 | if ($curr_id === $page_id) { |
---|
236 | // Reached requested page. |
---|
237 | return $breadcrumbs; |
---|
238 | } |
---|
239 | } |
---|
240 | return $breadcrumbs; |
---|
241 | } else { |
---|
242 | return false; |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | * Prints the breadcrumbs returned by getBreadcrumbs(). |
---|
248 | * |
---|
249 | * @access public |
---|
250 | */ |
---|
251 | function printBreadcrumbs($page_id=null) |
---|
252 | { |
---|
253 | echo $this->getBreadcrumbs($page_id); |
---|
254 | } |
---|
255 | |
---|
256 | /** |
---|
257 | * Returns a string if the queried page is the current page. One use is to print |
---|
258 | * CSS tags if the current page matches a link, such as: |
---|
259 | * $nav->currentPage('mypage.php', ' id="current"'); |
---|
260 | * |
---|
261 | * @access public |
---|
262 | * |
---|
263 | * @param mixed $page The URI of the page to query, with PREG express markup, if needed. |
---|
264 | * @param mixed $return The value to return if the current page matches the page queried. |
---|
265 | * |
---|
266 | * @return mixed The value set for $return, TRUE by default. |
---|
267 | */ |
---|
268 | function currentPage($page_uri, $return=true) |
---|
269 | { |
---|
270 | $page_uri = str_replace('/', '\/', $page_uri); |
---|
271 | if (preg_match('/^' . $page_uri . '/i', $_SERVER['PHP_SELF'])) { |
---|
272 | return $return; |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * Returns the ID of the current page, or the adjusted ID for a given page ID. |
---|
278 | * |
---|
279 | * @access private |
---|
280 | * @return string The value of the current page id. |
---|
281 | */ |
---|
282 | function _calculatePageID($page_id=null) |
---|
283 | { |
---|
284 | if (!isset($page_id)) { |
---|
285 | return sizeof($this->pages) - 1; |
---|
286 | } else if ($page_id < 0 && is_numeric($page_id)) { |
---|
287 | return sizeof($this->pages) + intval($page_id); |
---|
288 | } else { |
---|
289 | return $page_id; |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | } |
---|
294 | // End of class. |
---|
295 | |
---|
296 | ?> |
---|