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

Last change on this file was 708, checked in by anonymous, 4 years ago

Update class constructor method names to construct

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 __construct($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            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 isset($this->pages[$page_id]['features'][$feature]) && $this->pages[$page_id]['features'][$feature] && ((sizeof($this->pages) - $this->getFeature('chop_breadcrumbs')) > 0);
151        default :
152            return isset($this->pages[$page_id]['features'][$feature]) ? $this->pages[$page_id]['features'][$feature] : '';
153        }
154    }
155
156
157    /**
158     * Returns the title of current page.
159     *
160     * @access  public
161     *
162     * @return  mixed  Title of page (string) or false if title feature not set.
163     */
164    function getTitle($page_id=null)
165    {
166        if (!isset($page_id)) {
167            $page_id = sizeof($this->pages) - 1;
168        } else if ($page_id < 0 && is_numeric($page_id)) {
169            $page_id = sizeof($this->pages) + intval($page_id);
170        }
171
172        if ($this->getFeature('title', $page_id)) {
173            return oTxt($this->pages[$page_id]['title'], true);
174        } else {
175            return false;
176        }
177    }
178
179    /**
180     * Prints the title of page returned by getTitle().
181     *
182     * @access  public
183     */
184    function printTitle($page_id=null)
185    {
186        echo $this->getTitle($page_id);
187    }
188
189    /**
190     * Returns the text path from root up to the current page, seperated by the
191     * path_delimeter.
192     *
193     * @access  public
194     *
195     * @return  mixed   Path (string) or false if path feature is not set.
196     */
197    function getPath($page_id=null)
198    {
199        if (!isset($page_id)) {
200            $page_id = sizeof($this->pages) - 1;
201        } else if ($page_id < 0 && is_numeric($page_id)) {
202            $page_id = sizeof($this->pages) + intval($page_id);
203        }
204
205        if ($this->getFeature('path', $page_id)) {
206            $path = '';
207            $pathmark = '';
208            foreach ($this->pages as $curr_id => $page) {
209                $path .= oTxt($pathmark . strip_tags($page['title']), true);
210                $pathmark = $this->path_delimiter;
211                if ($curr_id === $page_id) {
212                    // Reached requested page.
213                    return $path;
214                }
215            }
216            return $path;
217        } else {
218            return false;
219        }
220    }
221
222    /**
223     * Prints the path returned by getPath().
224     *
225     * @access  public
226     */
227    function printPath($page_id=null)
228    {
229        echo $this->getPath($page_id);
230    }
231
232    /**
233     * Returns the breadcrumbs from the root page to the current page.
234     * Breadcrumbs are the text path with pages titles linked to that page.
235     *
236     * @access  public
237     *
238     * @return  mixed   Breadcrumbs (string) or false if breadcrumbs feature not set.
239     */
240    function getBreadcrumbs($page_id=null)
241    {
242        if (!isset($page_id)) {
243            $page_id = sizeof($this->pages) - 1;
244        } else if ($page_id < 0 && is_numeric($page_id)) {
245            $page_id = sizeof($this->pages) + intval($page_id);
246        }
247
248        if ($this->getFeature('breadcrumbs')) {
249            $breadcrumbs = '';
250            $pathmark = '';
251            $crumb_count = sizeof($this->pages);
252            foreach ($this->pages as $curr_id => $page) {
253                if ($crumb_count <= $this->getFeature('chop_breadcrumbs')) {
254                    // Stop gathering crumbs.
255                    return $breadcrumbs;
256                }
257                if ($crumb_count <= 1) {
258                    // The last crumb.
259                    if (empty($page['url']) || $crumb_count <= $this->getFeature('chop_breadcrumb_links')) {
260                        // A crumb with no link.
261                        $breadcrumbs .= oTxt($pathmark, true) . sprintf($this->last_crumb_format, oTxt($page['title'], true));
262                    } else if ($crumb_count > $this->getFeature('chop_breadcrumb_links')) {
263                        // A normal linked crumb.
264                        $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . ohref($page['url']) . '">' . sprintf($this->last_crumb_format, oTxt($page['title'], true)) . '</a>';
265                    }
266                } else {
267                    if (empty($page['url'])) {
268                        // A crumb with no link.
269                        $breadcrumbs .= oTxt($pathmark . $page['title'], true);
270                    } else {
271                        // A normal linked crumb.
272                        $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . ohref($page['url']) . '">' . oTxt($page['title'], true) . '</a>';
273                    }
274                }
275                $pathmark = $this->path_delimiter;
276                $crumb_count--;
277
278                if ($curr_id === $page_id) {
279                    // Reached requested page.
280                    return $breadcrumbs;
281                }
282            }
283            return $breadcrumbs;
284        } else {
285            return false;
286        }
287    }
288
289    /**
290     * Prints the breadcrumbs returned by getBreadcrumbs().
291     *
292     * @access  public
293     */
294    function printBreadcrumbs($page_id=null)
295    {
296        echo $this->getBreadcrumbs($page_id);
297    }
298
299    /**
300     * Returns a string if the queried page is the current page. One use is to print
301     * CSS tags if the current page matches a link, such as:
302     * $nav->currentPage('mypage.php', ' id="current"');
303     *
304     * @access  public
305     *
306     * @param   mixed   $page   The URI of the page to query.
307     * @param   mixed   $return The value to return if the current page matches the page queried.
308     *
309     * @return  bool    True on success, false on failure.
310     */
311    function currentPage($page_uri, $return=true)
312    {
313        if (preg_match('/^' . preg_quote($page_uri, '/') . '/i', $_SERVER['PHP_SELF'])) {
314            return $return;
315        }
316    }
317
318}
319// End of class.
320
321?>
Note: See TracBrowser for help on using the repository browser.