source: trunk/lib/Navigation.inc.php @ 490

Last change on this file since 490 was 485, checked in by anonymous, 10 years ago

Small bugs fixed while doing SBImedia

File size: 13.4 KB
RevLine 
[1]1<?php
2/**
[362]3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
[396]5 * Copyright 2001-2012 Strangecode, LLC
[461]6 *
[362]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.
[461]13 *
[362]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.
[461]18 *
[362]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/**
[184]24 * Navigation.inc.php
[136]25 *
26 * The Nav class provides a system for working with navigation elements.
[184]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.
[461]30 *
[184]31 * Note: this class was renamed from "Nav" because of the change in API and to be more descriptive.
[1]32 *
33 * @author  Quinn Comendant <quinn@strangecode.com>
[184]34 * @version 2.0
[1]35 */
[184]36class Navigation {
[1]37
[184]38    // Configuration parameters for this object.
[484]39    protected $_params = array(
[185]40        'head_title' => true,
41        'body_title' => true,
[1]42        'title' => true,
43        'path' => true,
44        'breadcrumbs' => true,
45        'chop_breadcrumbs' => 0,
46        'chop_breadcrumb_links' => 1,
[184]47        'path_delimiter' => ' / ',
48        'last_crumb_format' => '%s',
[1]49    );
[468]50    public $pages = array();
[1]51
52    /**
[184]53     * Navigation constructor.
[1]54     */
[468]55    public function __construct($params=null)
[1]56    {
[479]57        $app =& App::getInstance();
[184]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);
[1]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
[184]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.
[461]70     *
[1]71     * @access  public
72     * @param   string  $title      The title of the page.
[184]73     * @param   string  $url        The URL to the page. Set to null to use PHP_SELF.
[334]74     * @param   array   $vars       Additional page variables.
[1]75     */
[468]76    public function add($title, $url=null, $vars=array())
[1]77    {
[184]78        $page = array(
79            'title' => $title,
[186]80            'head_title' => $title,
81            'body_title' => $title,
[185]82            'url' => is_null($url) ? $_SERVER['PHP_SELF'] : $url,
[1]83        );
[184]84        $this->pages[] = array_merge($page, $vars);
[1]85    }
[42]86
[1]87    /**
[184]88     * Set (or overwrite existing) parameters by passing an array of new parameters.
[1]89     *
[184]90     * @access public
91     * @param  array    $params     Array of parameters (key => val pairs).
92     */
[468]93    public function setParam($params)
[184]94    {
[479]95        $app =& App::getInstance();
[461]96
[184]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.
[1]107     *
[184]108     * @access public
109     * @param string $param        Which parameter to return.
110     * @return mixed               Configured parameter value.
[1]111     */
[468]112    public function getParam($param)
[1]113    {
[479]114        $app =& App::getInstance();
[461]115
[478]116        if (array_key_exists($param, $this->_params)) {
[184]117            return $this->_params[$param];
118        } else {
119            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
120            return null;
[1]121        }
122    }
123
124    /**
[184]125     * Unsets all pages.
[1]126     *
127     * @access  public
128     */
[468]129    public function clear()
[1]130    {
131        $this->pages = array();
132    }
133
134    /**
[184]135     * Sets a variable into the current page.
[1]136     *
[184]137     * @access public
138     * @param mixed $key      Which value to set.
139     * @param mixed $val      Value to set variable to.
[1]140     */
[468]141    public function set($key, $val)
[1]142    {
[184]143        // Set params of current page.
[461]144        $curr_page =& $this->pages[sizeof($this->pages) - 1];
[184]145        $curr_page[$key] = $val;
[1]146    }
147
148    /**
[184]149     * Returns a specified value from the current page.
[1]150     *
[184]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.
[1]155     */
[468]156    public function get($key, $default='')
[1]157    {
[184]158        $curr_page =& $this->pages[sizeof($this->pages) - 1];
[461]159
[184]160        switch ($key) {
161        case 'title' :
162            if ($this->getParam('title') && isset($curr_page['title'])) {
163                return $curr_page['title'];
164            }
165            break;
[42]166
[184]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' :
[186]174            if ($this->getParam('body_title') && $this->getParam('title') && isset($curr_page['body_title'])) {
[184]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;
[1]194        }
195
[184]196        return $default;
[1]197    }
198
199    /**
[485]200     * Returns the path from root up to the current page as an array.
201     *
202     * @access  public
203     * @param   string   $key   Which value to use in the path (usually head_title or body_title or just title).
204     * @return  mixed           Path (string) or false if path param is not set.
205     */
206    public function getPathArray($key='title')
207    {
208        $path = array();
209        if ($this->getParam('path')) {
210            foreach ($this->pages as $page) {
211                $path[] = strip_tags($page[$key]);
212            }
213        }
214        return $path;
215    }
216
217    /**
[334]218     * Returns the text path from root up to the current page, separated by the
219     * path_delimiter.
[1]220     *
221     * @access  public
[304]222     * @param   string   $key   Which value to use in the path (usually head_title or body_title or just title).
223     * @return  mixed           Path (string) or false if path param is not set.
[1]224     */
[468]225    public function getPath($key='title')
[1]226    {
[485]227
228        $path = $this->getPathArray();
229        return empty($path) ? '' : join(oTxt($this->getParam('path_delimiter'), true), $path);
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     * @return  string   Breadcrumbs or empty string if breadcrumbs param not set.
238     */
239    public function getBreadcrumbsArray()
240    {
241        $app =& App::getInstance();
242
243        if ($this->getParam('breadcrumbs')) {
244            $breadcrumbs = array();
245            $crumb_count = sizeof($this->pages);
[184]246            foreach ($this->pages as $page) {
[485]247                if ($crumb_count <= $this->getParam('chop_breadcrumbs')) {
248                    // Stop gathering crumbs.
249                    break;
250                }
251                if ($crumb_count <= 1) {
252                    // The last crumb.
253                    if ('' == trim($page['url']) || $crumb_count <= $this->getParam('chop_breadcrumb_links')) {
254                        // A crumb with no link.
255                        $breadcrumbs[] = array(
256                            'url' => false,
257                            'title' => sprintf($this->getParam('last_crumb_format'), $page['title'])
258                        );
259                    } else if ($crumb_count > $this->getParam('chop_breadcrumb_links')) {
260                        // A normal linked crumb.
261                        $breadcrumbs[] = array(
262                            'url' => $page['url'],
263                            'title' => sprintf($this->getParam('last_crumb_format'), $page['title'])
264                        );
265                    }
266                } else {
267                    if ('' == trim($page['url'])) {
268                        // A crumb with no link.
269                        $breadcrumbs[] = array(
270                            'url' => false,
271                            'title' => $page['title']
272                        );
273                    } else {
274                        // A normal linked crumb.
275                        $breadcrumbs[] = array(
276                            'url' => $page['url'],
277                            'title' => $page['title']
278                        );
279                    }
280                }
281                $crumb_count--;
[1]282            }
[485]283            return $breadcrumbs;
[1]284        } else {
[485]285            return array();
[1]286        }
287    }
288
289    /**
290     * Returns the breadcrumbs from the root page to the current page.
291     * Breadcrumbs are the text path with pages titles linked to that page.
292     *
293     * @access  public
[324]294     * @return  string   Breadcrumbs or empty string if breadcrumbs param not set.
[1]295     */
[468]296    public function getBreadcrumbs()
[1]297    {
[479]298        $app =& App::getInstance();
[136]299
[184]300        if ($this->getParam('breadcrumbs')) {
[324]301            $breadcrumbs = array();
[1]302            $pathmark = '';
303            $crumb_count = sizeof($this->pages);
[184]304            foreach ($this->pages as $page) {
305                if ($crumb_count <= $this->getParam('chop_breadcrumbs')) {
[1]306                    // Stop gathering crumbs.
[324]307                    break;
[1]308                }
309                if ($crumb_count <= 1) {
310                    // The last crumb.
[184]311                    if ('' == trim($page['url']) || $crumb_count <= $this->getParam('chop_breadcrumb_links')) {
[1]312                        // A crumb with no link.
[324]313                        $breadcrumbs[] =  sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true));
[184]314                    } else if ($crumb_count > $this->getParam('chop_breadcrumb_links')) {
[1]315                        // A normal linked crumb.
[485]316                        $breadcrumbs[] =  '<a href="' . $page['url'] . '">' . sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)) . '</a>';
[1]317                    }
318                } else {
[184]319                    if ('' == trim($page['url'])) {
[1]320                        // A crumb with no link.
[324]321                        $breadcrumbs[] = oTxt($pathmark . $page['title'], true);
[1]322                    } else {
323                        // A normal linked crumb.
[485]324                        $breadcrumbs[] = '<a href="' . $page['url'] . '">' . oTxt($page['title'], true) . '</a>';
[1]325                    }
326                }
[184]327                $pathmark = $this->getParam('path_delimiter');
[1]328                $crumb_count--;
329            }
[324]330            return join(oTxt($pathmark, true), $breadcrumbs);
[1]331        } else {
[324]332            return '';
[1]333        }
334    }
335
336    /**
[475]337     * Test if the given URI matches the URL of the current page. By default the URI is tested
338     * without concern
339     * One use is to change the returned value for a positive match
340     * so a css class prints for an element representing the current page:
341     *   echo $nav->currentPage('/script.php?op=info', ' class="current"', '', true);
342     * The above will match only if the current page (REQUEST_URI) is also '/script.php?op=info',
343     * and will return the string ' class="current"' if it is.
[1]344     *
345     * @access  public
346     *
[475]347     * @param   string  $test_uri       A URI to test against the current page.
348     * @param   mixed   $true_return    The value to return if the current page matches the test URI.
349     * @param   mixed   $false_return   The value to return if the current page does not match the test URI.
350     * @param   bool    $include_query  If set true, include the URI query string in the test.
[1]351     *
[475]352     * @return  mixed   If the test URI matches the current page URI, the value given for $true_return
353     *                  is returned (true by default), otherwise the value given for $false_return is
354     *                  returned (false by default).
[1]355     */
[475]356    public function currentPage($test_uri, $true_return=true, $false_return=false, $include_query=false)
[1]357    {
[475]358        $app =& App::getInstance();
359
360        $actual_uri = $include_query ? $_SERVER['REQUEST_URI'] : strtok($_SERVER['REQUEST_URI'], '?');
361        $test_uri = $include_query ? $test_uri : strtok($test_uri, '?');
362        if (mb_strtolower($test_uri) == mb_strtolower($actual_uri)) {
363            // $app->logMsg(sprintf('Current page (%s) == test URI (%s)', $actual_uri, $test_uri), LOG_DEBUG, __FILE__, __LINE__);
[461]364            return $true_return;
[1]365        }
[475]366        // $app->logMsg(sprintf('Current page (%s) != test URI (%s)', $actual_uri, $test_uri), LOG_DEBUG, __FILE__, __LINE__);
[461]367        return $false_return;
[1]368    }
[42]369
370}
[1]371// End of class.
Note: See TracBrowser for help on using the repository browser.