source: tags/2.1.5/lib/Navigation.inc.php

Last change on this file was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

File size: 9.7 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2010 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/**
24 * Navigation.inc.php
25 *
26 * The Nav class provides a system for working with navigation elements.
27 * It supports storing page titles and URLs for printing breadcrumbs
28 * and titles, as well as setting page params such as hiding the page title on
29 * some pages but not others, and storing vars like the page title itself.
30 *
31 * Note: this class was renamed from "Nav" because of the change in API and to be more descriptive.
32 *
33 * @author  Quinn Comendant <quinn@strangecode.com>
34 * @version 2.0
35 */
36class Navigation {
37
38    // Configuration parameters for this object.
39    var $_params = array(       
40        'head_title' => true,
41        'body_title' => true,
42        'title' => true,
43        'path' => true,
44        'breadcrumbs' => true,
45        'chop_breadcrumbs' => 0,
46        'chop_breadcrumb_links' => 1,
47        'path_delimiter' => ' / ',
48        'last_crumb_format' => '%s',
49    );
50    var $pages = array();
51
52    /**
53     * Navigation constructor.
54     */
55    function Navigation($params=null)
56    {
57        $app =& App::getInstance();
58
59        if (isset($params) && is_array($params)) {
60            // Merge new parameters with old overriding only those passed.
61            $this->_params = array_merge($this->_params, $params);
62        }
63    }
64
65    /**
66     * Add a page to the internal pages array. Pages must be added sequentially
67     * as they are to be printed. The root page must be added first, and the
68     * current page added last. Vars can be specified for any page, but only vars
69     * from the "current" page will be accessed with Nav::get.
70     *
71     * @access  public
72     * @param   string  $title      The title of the page.
73     * @param   string  $url        The URL to the page. Set to null to use PHP_SELF.
74     * @param   array   $vars       Additional page variables.
75     */
76    function add($title, $url=null, $vars=array())
77    {
78        $page = array(
79            'title' => $title,
80            'head_title' => $title,
81            'body_title' => $title,
82            'url' => is_null($url) ? $_SERVER['PHP_SELF'] : $url,
83        );
84        $this->pages[] = array_merge($page, $vars);
85    }
86
87    /**
88     * Set (or overwrite existing) parameters by passing an array of new parameters.
89     *
90     * @access public
91     * @param  array    $params     Array of parameters (key => val pairs).
92     */
93    function setParam($params)
94    {
95        $app =& App::getInstance();
96   
97        if (isset($params) && is_array($params)) {
98            // Merge new parameters with old overriding only those passed.
99            $this->_params = array_merge($this->_params, $params);
100        } else {
101            $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
102        }
103    }
104
105    /**
106     * Return the value of a parameter, if it exists.
107     *
108     * @access public
109     * @param string $param        Which parameter to return.
110     * @return mixed               Configured parameter value.
111     */
112    function getParam($param)
113    {
114        $app =& App::getInstance();
115   
116        if (isset($this->_params[$param])) {
117            return $this->_params[$param];
118        } else {
119            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
120            return null;
121        }
122    }
123
124    /**
125     * Unsets all pages.
126     *
127     * @access  public
128     */
129    function clear()
130    {
131        $this->pages = array();
132    }
133
134    /**
135     * Sets a variable into the current page.
136     *
137     * @access public
138     * @param mixed $key      Which value to set.
139     * @param mixed $val      Value to set variable to.
140     */
141    function set($key, $val)
142    {
143        // Set params of current page.
144        $curr_page =& $this->pages[sizeof($this->pages) - 1];       
145        $curr_page[$key] = $val;
146    }
147
148    /**
149     * Returns a specified value from the current page.
150     *
151     * @access public
152     * @param mixed $key      Which value to return.
153     * @param mixed $default  Value to return if key not found in user_data.
154     * @return mixed          Value stored in session.
155     */
156    function get($key, $default='')
157    {
158        $curr_page =& $this->pages[sizeof($this->pages) - 1];
159       
160        switch ($key) {
161        case 'title' :
162            if ($this->getParam('title') && isset($curr_page['title'])) {
163                return $curr_page['title'];
164            }
165            break;
166
167        case 'head_title' :
168            if ($this->getParam('head_title') && $this->getParam('title') && isset($curr_page['head_title'])) {
169                return $curr_page['head_title'];
170            }
171            break;
172
173        case 'body_title' :
174            if ($this->getParam('body_title') && $this->getParam('title') && isset($curr_page['body_title'])) {
175                return $curr_page['body_title'];
176            }
177            break;
178
179        case 'path' :
180            if ($this->getParam('path')) {
181                return $this->getPath();
182            }
183            break;
184
185        case 'breadcrumbs' :
186            if ($this->getParam('breadcrumbs')) {
187                return $this->getBreadcrumbs();
188            }
189            break;
190
191        default :
192            return isset($curr_page[$key]) ? $curr_page[$key] : $default;
193            break;
194        }
195
196        return $default;
197    }
198
199    /**
200     * Returns the text path from root up to the current page, separated by the
201     * path_delimiter.
202     *
203     * @access  public
204     * @param   string   $key   Which value to use in the path (usually head_title or body_title or just title).
205     * @return  mixed           Path (string) or false if path param is not set.
206     */
207    function getPath($key='title')
208    {
209        if ($this->getParam('path')) {
210            $path = '';
211            $pathmark = '';
212            foreach ($this->pages as $page) {
213                $path .= oTxt($pathmark . strip_tags($page[$key]), true);
214                $pathmark = $this->getParam('path_delimiter');
215            }
216            return $path;
217        } else {
218            return false;
219        }
220    }
221
222    /**
223     * Returns the breadcrumbs from the root page to the current page.
224     * Breadcrumbs are the text path with pages titles linked to that page.
225     *
226     * @access  public
227     * @return  string   Breadcrumbs or empty string if breadcrumbs param not set.
228     */
229    function getBreadcrumbs()
230    {
231        $app =& App::getInstance();
232
233        if ($this->getParam('breadcrumbs')) {
234            $breadcrumbs = array();
235            $pathmark = '';
236            $crumb_count = sizeof($this->pages);
237            foreach ($this->pages as $page) {
238                if ($crumb_count <= $this->getParam('chop_breadcrumbs')) {
239                    // Stop gathering crumbs.
240                    break;
241                }
242                if ($crumb_count <= 1) {
243                    // The last crumb.
244                    if ('' == trim($page['url']) || $crumb_count <= $this->getParam('chop_breadcrumb_links')) {
245                        // A crumb with no link.
246                        $breadcrumbs[] =  sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true));
247                    } else if ($crumb_count > $this->getParam('chop_breadcrumb_links')) {
248                        // A normal linked crumb.
249                        $breadcrumbs[] =  '<a href="' . $app->oHREF($page['url']) . '">' . sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)) . '</a>';
250                    }
251                } else {
252                    if ('' == trim($page['url'])) {
253                        // A crumb with no link.
254                        $breadcrumbs[] = oTxt($pathmark . $page['title'], true);
255                    } else {
256                        // A normal linked crumb.
257                        $breadcrumbs[] = '<a href="' . $app->oHREF($page['url']) . '">' . oTxt($page['title'], true) . '</a>';
258                    }
259                }
260                $pathmark = $this->getParam('path_delimiter');
261                $crumb_count--;
262            }
263            return join(oTxt($pathmark, true), $breadcrumbs);
264        } else {
265            return '';
266        }
267    }
268
269    /**
270     * Returns a string if the queried page is the current page. One use is to print
271     * CSS tags if the current page matches a link, such as:
272     * $nav->currentPage('mypage.php', ' id="current"');
273     *
274     * @access  public
275     *
276     * @param   mixed   $page   The URI of the page to query, with PREG express markup, if needed.
277     * @param   mixed   $return The value to return if the current page matches the page queried.
278     *
279     * @return  mixed   The value set for $return, TRUE by default.
280     */
281    function currentPage($page_uri, $return=true)
282    {
283        // $page_uri = str_replace('/', '\/', $page_uri);
284        if (preg_match('/^' . preg_quote(urldecode($page_uri), '/') . '/i', $_SERVER['PHP_SELF'])) {
285            return $return;
286        }
287    }
288
289}
290// End of class.
291
292?>
Note: See TracBrowser for help on using the repository browser.