source: branches/1.1dev/lib/Nav.inc.php @ 646

Last change on this file since 646 was 389, checked in by anonymous, 13 years ago

Added Nav::updatePage().

File size: 11.1 KB
Line 
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 */
11class 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'       => is_null($url) ? $_SERVER['PHP_SELF'] : $url,
59            'features'  => (isset($features) && is_array($features)) ? array_merge($this->default_features, $features) : $this->default_features
60        );
61    }
62
63    /**
64     * Update the title, url, and features for the current page (the last added).
65     *
66     * @access  public
67     *
68     * @param   string  $title      The title of the page.
69     * @param   string  $url        The URL to the page. Leave blank (or null) if
70     *                              page is to not be linked.
71     * @param   array   $features   Set the features of the current page.
72     */
73    function updatePage($title, $url=null, $features=null)
74    {
75        if (isset($title)) {
76            $this->pages[sizeof($this->pages)-1]['title'] = $title;           
77        }
78        if (isset($url)) {
79            $this->pages[sizeof($this->pages)-1]['url'] = $url;           
80        }
81        if (isset($features) && is_array($features)) {
82            $this->pages[sizeof($this->pages)-1]['features'] = array_merge($this->pages[sizeof($this->pages)-1]['features'], $features);           
83        }
84    }
85   
86    /**
87     * Set the features of the current page. Future versions of this class
88     * may have the ability to set features for a specific page. In that case
89     * some form of ID will need to be specified for each page.
90     *
91     * @param  array $feature   Array of feature keys and value to set.
92     *
93     * @return bool true on success, false on failure
94     */
95    function setFeature($features=null, $page_id=null)
96    {
97        if (!isset($page_id)) {
98            $page_id = sizeof($this->pages) - 1;
99        } else if ($page_id < 0 && is_numeric($page_id)) {
100            $page_id = sizeof($this->pages) + intval($page_id);
101        }
102       
103        if (!isset($this->pages[sizeof($this->pages)-1]['features']) || !isset($this->pages[$page_id]['features'])) {
104            $app->logMsg(sprintf('Page not available to set feature: page_id = %s', $page_id), LOG_ERR, __FILE__, __LINE__);
105            return false;
106        }
107       
108        if (isset($features) && is_array($features)) {
109            // Set features for specified page.
110            $this->pages[$page_id]['features'] = array_merge($this->pages[$page_id]['features'], $features);
111            // Store "current page" features.
112            $this->pages[sizeof($this->pages)-1]['features'] = array_merge($this->pages[sizeof($this->pages)-1]['features'], $features);
113        }
114    }
115
116    /**
117     * Unsets all page variables and resets features to the default set.
118     *
119     * @access  public
120     */
121    function clearPath()
122    {
123        $this->pages = array();
124        $this->features = $this->default_features;
125    }
126
127/******************************************************************************
128 * OUTPUT
129 *****************************************************************************/
130 
131    /**
132     * Get the value of a feature for specified page_id or current page if page_id not specified (future use).
133     *
134     * @param  string $feature   Name of feature value to retreive.
135     * @param  mixed $page_id    Future use: ID of page.
136     *
137     * @return bool true on success, false on failure
138     */
139    function getFeature($feature, $page_id=null)
140    {
141        if (!isset($page_id)) {
142            $page_id = sizeof($this->pages) - 1;
143        } else if ($page_id < 0 && is_numeric($page_id)) {
144            $page_id = sizeof($this->pages) + intval($page_id);
145        }
146       
147        switch ($feature) {
148        case 'breadcrumbs' :
149            // No breadcrumbs if displayed quantity of crumbs is less than 1.
150            return $this->pages[$page_id]['features'][$feature] && ((sizeof($this->pages) - $this->getFeature('chop_breadcrumbs')) > 0);
151            break;
152        default :
153            return $this->pages[$page_id]['features'][$feature];
154        }
155    }
156
157       
158    /**
159     * Returns the title of current page.
160     *
161     * @access  public
162     *
163     * @return  mixed  Title of page (string) or false if title feature not set.
164     */
165    function getTitle($page_id=null)
166    {
167        if (!isset($page_id)) {
168            $page_id = sizeof($this->pages) - 1;
169        } else if ($page_id < 0 && is_numeric($page_id)) {
170            $page_id = sizeof($this->pages) + intval($page_id);
171        }
172       
173        if ($this->getFeature('title', $page_id)) {
174            return oTxt($this->pages[$page_id]['title'], true);
175        } else {
176            return false;
177        }
178    }
179
180    /**
181     * Prints the title of page returned by getTitle().
182     *
183     * @access  public
184     */
185    function printTitle($page_id=null)
186    {
187        echo $this->getTitle($page_id);
188    }
189
190    /**
191     * Returns the text path from root up to the current page, seperated by the
192     * path_delimeter.
193     *
194     * @access  public
195     *
196     * @return  mixed   Path (string) or false if path feature is not set.
197     */
198    function getPath($page_id=null)
199    {
200        if (!isset($page_id)) {
201            $page_id = sizeof($this->pages) - 1;
202        } else if ($page_id < 0 && is_numeric($page_id)) {
203            $page_id = sizeof($this->pages) + intval($page_id);
204        }
205       
206        if ($this->getFeature('path', $page_id)) {
207            $path = '';
208            $pathmark = '';
209            foreach ($this->pages as $curr_id => $page) {
210                $path .= oTxt($pathmark . strip_tags($page['title']), true);
211                $pathmark = $this->path_delimiter;
212                if ($curr_id === $page_id) {
213                    // Reached requested page.
214                    return $path;
215                }
216            }
217            return $path;
218        } else {
219            return false;
220        }
221    }
222
223    /**
224     * Prints the path returned by getPath().
225     *
226     * @access  public
227     */
228    function printPath($page_id=null)
229    {
230        echo $this->getPath($page_id);
231    }
232
233    /**
234     * Returns the breadcrumbs from the root page to the current page.
235     * Breadcrumbs are the text path with pages titles linked to that page.
236     *
237     * @access  public
238     *
239     * @return  mixed   Breadcrumbs (string) or false if breadcrumbs feature not set.
240     */
241    function getBreadcrumbs($page_id=null)
242    {
243        if (!isset($page_id)) {
244            $page_id = sizeof($this->pages) - 1;
245        } else if ($page_id < 0 && is_numeric($page_id)) {
246            $page_id = sizeof($this->pages) + intval($page_id);
247        }
248       
249        if ($this->getFeature('breadcrumbs')) {
250            $breadcrumbs = '';
251            $pathmark = '';
252            $crumb_count = sizeof($this->pages);
253            foreach ($this->pages as $curr_id => $page) {
254                if ($crumb_count <= $this->getFeature('chop_breadcrumbs')) {
255                    // Stop gathering crumbs.
256                    return $breadcrumbs;
257                }
258                if ($crumb_count <= 1) {
259                    // The last crumb.
260                    if (empty($page['url']) || $crumb_count <= $this->getFeature('chop_breadcrumb_links')) {
261                        // A crumb with no link.
262                        $breadcrumbs .= oTxt($pathmark, true) . sprintf($this->last_crumb_format, oTxt($page['title'], true));
263                    } else if ($crumb_count > $this->getFeature('chop_breadcrumb_links')) {
264                        // A normal linked crumb.
265                        $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . ohref($page['url']) . '">' . sprintf($this->last_crumb_format, oTxt($page['title'], true)) . '</a>';
266                    }
267                } else {
268                    if (empty($page['url'])) {
269                        // A crumb with no link.
270                        $breadcrumbs .= oTxt($pathmark . $page['title'], true);
271                    } else {
272                        // A normal linked crumb.
273                        $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . ohref($page['url']) . '">' . oTxt($page['title'], true) . '</a>';
274                    }
275                }
276                $pathmark = $this->path_delimiter;
277                $crumb_count--;
278               
279                if ($curr_id === $page_id) {
280                    // Reached requested page.
281                    return $breadcrumbs;
282                }
283            }
284            return $breadcrumbs;
285        } else {
286            return false;
287        }
288    }
289
290    /**
291     * Prints the breadcrumbs returned by getBreadcrumbs().
292     *
293     * @access  public
294     */
295    function printBreadcrumbs($page_id=null)
296    {
297        echo $this->getBreadcrumbs($page_id);
298    }
299   
300    /**
301     * Returns a string if the queried page is the current page. One use is to print
302     * CSS tags if the current page matches a link, such as:
303     * $nav->currentPage('mypage.php', ' id="current"');
304     *
305     * @access  public
306     *
307     * @param   mixed   $page   The URI of the page to query.
308     * @param   mixed   $return The value to return if the current page matches the page queried.
309     *
310     * @return  bool    True on success, false on failure.
311     */
312    function currentPage($page_uri, $return=true)
313    {
314        if (preg_match('/^' . preg_quote($page_uri, '/') . '/i', $_SERVER['PHP_SELF'])) {
315            return $return;
316        }
317    }
318   
319} 
320// End of class.
321
322?>
Note: See TracBrowser for help on using the repository browser.