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

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

Initial import.

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