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

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

Small bugs fixed while doing SBImedia

File size: 13.4 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-2012 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    protected $_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    public $pages = array();
51
52    /**
53     * Navigation constructor.
54     */
55    public function __construct($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    public 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    public 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    public function getParam($param)
113    {
114        $app =& App::getInstance();
115
116        if (array_key_exists($param, $this->_params)) {
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    public 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    public 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    public 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 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    /**
218     * Returns the text path from root up to the current page, separated by the
219     * path_delimiter.
220     *
221     * @access  public
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.
224     */
225    public function getPath($key='title')
226    {
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);
246            foreach ($this->pages as $page) {
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--;
282            }
283            return $breadcrumbs;
284        } else {
285            return array();
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
294     * @return  string   Breadcrumbs or empty string if breadcrumbs param not set.
295     */
296    public function getBreadcrumbs()
297    {
298        $app =& App::getInstance();
299
300        if ($this->getParam('breadcrumbs')) {
301            $breadcrumbs = array();
302            $pathmark = '';
303            $crumb_count = sizeof($this->pages);
304            foreach ($this->pages as $page) {
305                if ($crumb_count <= $this->getParam('chop_breadcrumbs')) {
306                    // Stop gathering crumbs.
307                    break;
308                }
309                if ($crumb_count <= 1) {
310                    // The last crumb.
311                    if ('' == trim($page['url']) || $crumb_count <= $this->getParam('chop_breadcrumb_links')) {
312                        // A crumb with no link.
313                        $breadcrumbs[] =  sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true));
314                    } else if ($crumb_count > $this->getParam('chop_breadcrumb_links')) {
315                        // A normal linked crumb.
316                        $breadcrumbs[] =  '<a href="' . $page['url'] . '">' . sprintf($this->getParam('last_crumb_format'), oTxt($page['title'], true)) . '</a>';
317                    }
318                } else {
319                    if ('' == trim($page['url'])) {
320                        // A crumb with no link.
321                        $breadcrumbs[] = oTxt($pathmark . $page['title'], true);
322                    } else {
323                        // A normal linked crumb.
324                        $breadcrumbs[] = '<a href="' . $page['url'] . '">' . oTxt($page['title'], true) . '</a>';
325                    }
326                }
327                $pathmark = $this->getParam('path_delimiter');
328                $crumb_count--;
329            }
330            return join(oTxt($pathmark, true), $breadcrumbs);
331        } else {
332            return '';
333        }
334    }
335
336    /**
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.
344     *
345     * @access  public
346     *
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.
351     *
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).
355     */
356    public function currentPage($test_uri, $true_return=true, $false_return=false, $include_query=false)
357    {
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__);
364            return $true_return;
365        }
366        // $app->logMsg(sprintf('Current page (%s) != test URI (%s)', $actual_uri, $test_uri), LOG_DEBUG, __FILE__, __LINE__);
367        return $false_return;
368    }
369
370}
371// End of class.
Note: See TracBrowser for help on using the repository browser.