source: trunk/lib/Nav.inc.php @ 155

Last change on this file since 155 was 155, checked in by scdev, 18 years ago

Q - urldecoding the input value to compare with PHP_SELF currentPage.

File size: 10.1 KB
Line 
1<?php
2/**
3 * Nav.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 * 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.
10 *
11 * @author  Quinn Comendant <quinn@strangecode.com>
12 * @version 1.0
13 */
14class Nav {
15
16    var $pages = array();
17    var $path_delimiter = ' / ';
18    var $features = array();
19    var $default_features = array(
20        'title' => true,
21        'path' => true,
22        'breadcrumbs' => true,
23        'chop_breadcrumbs' => 0,
24        'chop_breadcrumb_links' => 1,
25    );
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********************************************************************/
42
43    /**
44     * Add a page to the internal pages array. Pages must be added sequentially
45     * 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     *
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
63        );
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()
93    {
94        $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);
155    }
156
157    /**
158     * Returns the text path from root up to the current page, seperated by the
159     * path_delimeter.
160     *
161     * @access  public
162     *
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)) {
170            $path = '';
171            $pathmark = '';
172            foreach ($this->pages as $curr_id => $page) {
173                $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                }
179            }
180            return $path;
181        } else {
182            return false;
183        }
184    }
185
186    /**
187     * Prints the path returned by getPath().
188     *
189     * @access  public
190     */
191    function printPath($page_id=null)
192    {
193        echo $this->getPath($page_id);
194    }
195
196    /**
197     * Returns the breadcrumbs from the root page to the current page.
198     * Breadcrumbs are the text path with pages titles linked to that page.
199     *
200     * @access  public
201     *
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')) {
211            $breadcrumbs = '';
212            $pathmark = '';
213            $crumb_count = sizeof($this->pages);
214            foreach ($this->pages as $curr_id => $page) {
215                if ($crumb_count <= $this->getFeature('chop_breadcrumbs')) {
216                    // Stop gathering crumbs.
217                    return $breadcrumbs;
218                }
219                if ($crumb_count <= 1) {
220                    // The last crumb.
221                    if (empty($page['url']) || $crumb_count <= $this->getFeature('chop_breadcrumb_links')) {
222                        // 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->getFeature('chop_breadcrumb_links')) {
225                        // 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>';
227                    }
228                } else {
229                    if (empty($page['url'])) {
230                        // A crumb with no link.
231                        $breadcrumbs .= oTxt($pathmark . $page['title'], true);
232                    } else {
233                        // A normal linked crumb.
234                        $breadcrumbs .= oTxt($pathmark, true) . '<a href="' . $app->oHREF($page['url']) . '">' . oTxt($page['title'], true) . '</a>';
235                    }
236                }
237                $pathmark = $this->path_delimiter;
238                $crumb_count--;
239
240                if ($curr_id === $page_id) {
241                    // Reached requested page.
242                    return $breadcrumbs;
243                }
244            }
245            return $breadcrumbs;
246        } else {
247            return false;
248        }
249    }
250
251    /**
252     * Prints the breadcrumbs returned by getBreadcrumbs().
253     *
254     * @access  public
255     */
256    function printBreadcrumbs($page_id=null)
257    {
258        echo $this->getBreadcrumbs($page_id);
259    }
260
261    /**
262     * Returns a string if the queried page is the current page. One use is to print
263     * CSS tags if the current page matches a link, such as:
264     * $nav->currentPage('mypage.php', ' id="current"');
265     *
266     * @access  public
267     *
268     * @param   mixed   $page   The URI of the page to query, with PREG express markup, if needed.
269     * @param   mixed   $return The value to return if the current page matches the page queried.
270     *
271     * @return  mixed   The value set for $return, TRUE by default.
272     */
273    function currentPage($page_uri, $return=true)
274    {
275        // $page_uri = str_replace('/', '\/', $page_uri);
276        if (preg_match('/^' . preg_quote(urldecode($page_uri), '/') . '/i', $_SERVER['PHP_SELF'])) {
277            return $return;
278        }
279    }
280
281    /**
282     * Returns the ID of the current page, or the adjusted ID for a given page ID.
283     *
284     * @access  private
285     * @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}
299// End of class.
300
301?>
Note: See TracBrowser for help on using the repository browser.