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

Last change on this file since 21 was 8, checked in by scdev, 19 years ago

checking isset($this) on self-instanciating function calls in App and DB.

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