2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
15 * 1. Generate main menu items based on login user
16 * 2. Generate path in bread crumb
17 * 3. Decide the page to display (redirect) based on login user's privilege.
18 * This page is set as current page
19 * 4. Generate sub menus of current page
20 * 5. Generate back to page of current page
26 if (!defined('TR_INCLUDE_PATH')) exit;
28 require_once(TR_INCLUDE_PATH. 'classes/DAO/PrivilegesDAO.class.php');
29 require_once(TR_INCLUDE_PATH. 'classes/Utility.class.php');
34 var $pages; // top tab pages
35 var $current_page; // current page
36 var $root_page; // root page relative to current page
37 var $breadcrumb_path = array(); // array of breadcrumb path
38 var $sub_menus; // array of sub-menus of current page
39 var $path; // array of all parent pages to current page, used for breadcrumb path and generating back to page
40 var $back_to_page; // string of parent page to go back to
43 * Constructor: Initialize top pages (tab menu), all pages accessible by current user, current page.
44 * Generate top tab menu items based on session user_id. If no user login in (public view), use public menu
51 $this->pages[TR_NAV_TOP] = array(); // top tab pages
53 $this->init(); // Initialize $this->pages[TR_NAV_PUBLIC] & $this->pages
54 $this->setTopPages(); // set top pages based on user id
56 // decide current page.
57 // if the page that user tries to access is from one of the public link
58 // but not define in user's priviledge pages, re-direct to the first $this->pages[TR_NAV_TOP]
59 $this->setCurrentPage();
60 $this->sub_menus = $this->setSubMenus($this->current_page); // loop recursively to set $this->submenus to the top parent of $this->current_page
61 $this->root_page = $this->setRootPage($this->current_page);
62 $this->path = $this->setPath($this->current_page);
63 $this->back_to_page = $this->setBackToPage();
67 * initialize: public accessible items ($this->pages[TR_NAV_PUBLIC]); all accessible pages ($this->pages)
73 private function init()
75 // $_pages is defined in include/constants.inc.php
76 global $_pages, $_base_path;
78 // initialize $this->pages
79 $this->pages = $_pages;
80 // end of initializing $this->pages
82 $priviledgesDAO = new PrivilegesDAO();
83 $rows = $priviledgesDAO->getPublicPrivileges();
87 foreach ($rows as $id => $row)
89 $this->pages[TR_NAV_PUBLIC][] = array($row['link'] => array('title_var'=>$row['title_var'], 'parent'=>TR_NAV_TOP));
92 // end of initializing $this->pages[TR_NAV_PUBLIC]
98 * Set top pages array based on login user's priviledge. If there's no login user, use priviledges that are open to public.
102 * @author Cindy Qi Li
104 private function setTopPages()
106 global $_base_path, $_course_id, $_content_id;
108 $priviledgesDAO = new PrivilegesDAO();
110 if (isset($_SESSION['user_id']) && $_SESSION['user_id'] <> 0)
112 $rows = $priviledgesDAO->getUserPrivileges($_SESSION['user_id']);
116 $rows = $priviledgesDAO->getPublicPrivileges();
120 foreach ($rows as $id => $row)
122 // replace the required constants in link
123 $row['link'] = Utility::replaceConstants($row['link']);
124 list($url, $param) = Utility::separateURLAndParam($row['link']);
125 if (Utility::authenticate($row['user_requirement'], false)) {
126 $this->pages[TR_NAV_TOP][] = array('url' => $_base_path.$row['link'],
127 'title' => _AT($row['title_var']),
131 // add section pages if it has not been defined in $this->pages
132 if (!isset($this->pages[$url]))
134 $this->pages = array_merge($this->pages,
135 array($url => array('title_var'=>$row['title_var'], 'parent'=>TR_NAV_TOP, 'param' => $param)));
139 $this->pages[$url]['param'] = $param;
148 * Decide current page.
149 * if the page that user tries to access is from one of the public link
150 * but not define in user's priviledge pages, re-direct to the first $this->pages[TR_NAV_TOP]
153 * @author Cindy Qi Li
155 private function setCurrentPage()
157 global $_base_path, $_base_href, $msg;
159 $this->current_page = substr($_SERVER['PHP_SELF'], strlen($_base_path));
161 if (!isset($this->pages[$this->current_page]))
163 if (!$this->isPublicLink($this->current_page)) // report error if the link is not from a public link
165 $msg->addError(array('PAGE_NOT_FOUND', $_base_href.$this->current_page));
168 // re-direct to first $_pages URL
169 foreach ($this->pages[TR_NAV_TOP] as $page)
171 // debug($_base_path.$this->current_page);debug($page);
172 if ($_base_path.$this->current_page != $page['url'])
174 header('Location: '.$page['url']);
176 // reset current_page after re-direction
177 $this->current_page = substr($_SERVER['PHP_SELF'], strlen($_base_path));
179 // Note: must exit. otherwise, the rest of includeheader.inc.php proceeds and prints out all messages
180 // which is not going to be displayed at re-directed page.
188 * Set sub-menus of current page by $_pages[$current_page]['children']
191 * @author Cindy Qi Li
193 private function setSubMenus($page) {
194 global $_base_path, $_course_id;
196 if (isset($page) && defined($page))
201 else if (isset($this->pages[$page]['children']))
203 $param = $this->getParam($page);
204 // $sub_menus[] = array('url' => $_base_path . $page.$param, 'title' => $this->getPageTitle($page), 'param' => $param);
206 foreach ($this->pages[$page]['children'] as $child)
208 $this->pages[$child]['param'] = $param;
209 $sub_menus[] = array('url' => $this->addUrlParam($_base_path . $child, $param),
210 'title' => $this->getPageTitle($child),
211 'has_children' => isset($this->pages[$child]['children']),
215 else if (isset($this->pages[$page]['parent']))
218 return $this->setSubMenus($this->pages[$page]['parent']);
225 * Set the back to page of $this->current_page
228 * @author Cindy Qi Li
230 private function setBackToPage()
234 unset($this->path[0]);
235 if (isset($this->path[2]['url'], $this->sub_menus[0]['url']) && $this->path[2]['url'] == $this->sub_menus[0]['url']) {
236 $back_to_page = $this->path[3];
238 else if (isset($this->path[1]['url'], $this->sub_menus[0]['url']) && $this->path[1]['url'] == $this->sub_menus[0]['url']) {
239 $back_to_page = isset($this->path[2]) ? $this->path[2] : null;
241 else if (isset($this->path[1])) {
242 $back_to_page = $this->path[1];
245 return $back_to_page;
249 * Check if the given link is a pre-defined public link
252 * @return true if is a pre-defined public link
253 * false if not a pre-defined public link
254 * @author Cindy Qi Li
256 private function isPublicLink($url)
258 foreach ($this->pages[TR_NAV_PUBLIC] as $page => $garbage)
260 if ($page == $url) return true;
267 * Return the page title of given page
271 * empty if page is not defined
272 * @author Cindy Qi Li
274 private function getPageTitle($page)
276 if (isset($this->pages[$page]['title']))
278 $page_title = $this->pages[$page]['title'];
282 $page_title = _AT($this->pages[$page]['title_var']);
289 * Return the URL with the given parameter attached. $param can have multiple parameters.
290 * The repetitive parameter is skipped
292 * @param $url: URL, can be completed or not completed. For example: tests/index.php?a=1
293 * $param: URL parameters. For example: ?a=3&b=4
294 * @return the URL with the given parameter attached at the end. The repetitive parameter is skipped.
295 * @author Cindy Qi Li
297 private function addUrlParam($url, $param)
300 $param = str_replace('?', '', trim($param));
301 if ($param == '') return $url;
303 $has_question_mark = false;
304 if (strpos($url, '?') > 0) $has_question_mark = true;
307 $all_params = explode('&', $param);
308 if (is_array($all_params)) {
309 foreach ($all_params as $each_param)
311 $pair = explode('=', $each_param);
312 // check if the parameter is already in the url
313 if (strpos($url, $pair[0].'=') > 0) continue;
315 if ($has_question_mark || $counter > 0)
316 $url .= '&'.$each_param;
318 $url .= '?'.$each_param;
329 * Return all pages array
331 * @return all pages array
332 * @author Cindy Qi Li
334 public function getAllPages()
340 * Return top tab menu item array
342 * @return top tab menu item array
343 * @author Cindy Qi Li
345 public function getTopPages()
347 return $this->pages[TR_NAV_TOP];
351 * Return top tab menu item array
353 * @return top tab menu item array
354 * @author Cindy Qi Li
356 public function getCurrentPage()
358 return $this->current_page;
362 * Return sub menus of current page
364 * @return top tab menu item array
365 * @author Cindy Qi Li
367 public function getSubMenus()
369 return $this->sub_menus;
373 * Return back to page of current page
375 * @return back to page array
376 * @author Cindy Qi Li
378 public function getBackToPage()
380 return $this->back_to_page;
384 * Set root page relative to the current page
387 * @author Cindy Qi Li
389 private function setRootPage($page)
393 $parent_page = $this->pages[$page]['parent'];
395 if (isset($parent_page) && defined($parent_page)) // check if $parent_page is
397 return $_base_path . $page;
399 else if (isset($parent_page))
401 return $this->setRootPage($parent_page);
405 return $_base_path . $page;
410 * Return root page relative to the current page
413 * @author Cindy Qi Li
415 public function getRootPage()
417 return $this->root_page;
421 * Return array of all parent items path to current page
422 * this array is used to determine back to page
424 * @return array of breadcrumb path
425 * @author Cindy Qi Li
427 public function setPath($page)
431 // all children pages inherit URL parameter of the parent page
432 $parent_page = $this->pages[$page]['parent'];
433 $parent_page_param = $this->getParam($page);
435 if (stripos($page, str_replace(array(SEP, '?'), array('', ''), $parent_page_param)) > 0) {
438 $page_url = $page.$parent_page_param;
441 $page_title = $this->getPageTitle($page);
443 if (isset($parent_page) && defined($parent_page))
445 $path[] = array('url' => $_base_path . $page_url, 'title' => $page_title, 'param' => $parent_page_param);
447 else if (isset($parent_page))
449 $path[] = array('url' => $_base_path . $page_url, 'title' => $page_title, 'param' => $parent_page_param);
450 $path = array_merge((array) $path, $this->setPath($parent_page));
452 $path[] = array('url' => $_base_path . $page_url, 'title' => $page_title, 'param' => $parent_page_param);
459 * Return breadcrumb path
462 * @author Cindy Qi Li
464 public function getPath()
470 * return "param" element of the given page or the parents of the given page
472 * @return "param" element value
474 private function getParam($page)
476 if (isset($this->pages[$page]['param'])) return $this->pages[$page]['param'];
478 if ($page == TR_NAV_TOP || $page == TR_NAV_PUBLIC) return '';
480 if (isset($this->pages[$this->pages[$page]['parent']]['param']))
481 return $this->pages[$this->pages[$page]['parent']]['param'];
483 return $this->getParam($this->pages[$page]['parent']);