| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Route
|
| 7 |
*/
|
| 8 |
|
| 9 |
class pageroute_route {
|
| 10 |
|
| 11 |
var $prid;
|
| 12 |
var $options;
|
| 13 |
var $pages;
|
| 14 |
var $page_index;
|
| 15 |
var $page_access;
|
| 16 |
var $path;
|
| 17 |
|
| 18 |
public function pageroute_route($route_data) {
|
| 19 |
$this->prid = $route_data->prid;
|
| 20 |
$this->path = $route_data->path;
|
| 21 |
$this->options = $route_data->options;
|
| 22 |
$this->pages = $route_data->pages;
|
| 23 |
$this->page_index = $route_data->page_index;
|
| 24 |
$this->page_access = $route_data->page_access;
|
| 25 |
}
|
| 26 |
|
| 27 |
/*
|
| 28 |
* Load a route from the pageroute cache
|
| 29 |
*/
|
| 30 |
public static function load($prid, $reset = FALSE) {
|
| 31 |
static $allroutes;
|
| 32 |
|
| 33 |
if (!isset($allroutes) || $reset) {
|
| 34 |
$allroutes = cache_get('pageroute');
|
| 35 |
}
|
| 36 |
|
| 37 |
if ($allroutes)
|
| 38 |
$allroutes = $allroutes->data;
|
| 39 |
else
|
| 40 |
$allroutes = array();
|
| 41 |
|
| 42 |
if (isset($allroutes[$prid])) {
|
| 43 |
$route_data = $allroutes[$prid];
|
| 44 |
}
|
| 45 |
else {
|
| 46 |
$route_data = pageroute_route::load_from_database($prid);
|
| 47 |
|
| 48 |
if (!$route_data) {
|
| 49 |
return FALSE;
|
| 50 |
}
|
| 51 |
|
| 52 |
$allroutes[$prid] = $route_data;
|
| 53 |
|
| 54 |
cache_set('pageroute', $allroutes);
|
| 55 |
}
|
| 56 |
|
| 57 |
$route = new pageroute_route($route_data);
|
| 58 |
|
| 59 |
drupal_alter('pageroute', $route);
|
| 60 |
|
| 61 |
return $route;
|
| 62 |
}
|
| 63 |
|
| 64 |
/*
|
| 65 |
* Load a route from the database
|
| 66 |
*/
|
| 67 |
private static function load_from_database($prid) {
|
| 68 |
|
| 69 |
$result = db_query("SELECT * FROM {pageroute_routes} WHERE prid = %d", $prid);
|
| 70 |
|
| 71 |
$route_data = db_fetch_object($result);
|
| 72 |
|
| 73 |
if (!$route_data) {
|
| 74 |
return FALSE;
|
| 75 |
}
|
| 76 |
|
| 77 |
$route_data->options = unserialize($route_data->options);
|
| 78 |
|
| 79 |
$result = db_query("SELECT * FROM {pageroute_pages} WHERE prid = %d ORDER BY weight, name", $route_data->prid);
|
| 80 |
|
| 81 |
$index = 0;
|
| 82 |
$route_data->pages = array();
|
| 83 |
$route_data->page_index = array();
|
| 84 |
$route_data->page_access = array();
|
| 85 |
|
| 86 |
while ($page = db_fetch_object($result)) {
|
| 87 |
$page->options = unserialize($page->options);
|
| 88 |
|
| 89 |
if (isset($page->options['activated']) && !$page->options['activated']) {
|
| 90 |
//add deactivated pages to the page_index, but don't give them an index
|
| 91 |
//so this pages are reachable, but we won't route to them
|
| 92 |
$route_data->page_index[$page->name] = FALSE;
|
| 93 |
continue;
|
| 94 |
}
|
| 95 |
|
| 96 |
$route_data->pages[$index] = $page;
|
| 97 |
if ($route_data->options['tabs'] && isset($page->options['no_tab']) && $page->options['no_tab']) {
|
| 98 |
$route_data->pages[$index]->no_tab = TRUE;
|
| 99 |
}
|
| 100 |
$route_data->page_index[$page->name] = $index;
|
| 101 |
$index++;
|
| 102 |
}
|
| 103 |
|
| 104 |
return $route_data;
|
| 105 |
}
|
| 106 |
|
| 107 |
/*
|
| 108 |
* Get suggested page:
|
| 109 |
* Default page if no page was specified or access denied to specified page
|
| 110 |
* else specified page
|
| 111 |
*/
|
| 112 |
public function get_suggested_page($page_name = NULL, $target = PAGEROUTE_CURRENT) {
|
| 113 |
if ($page_name) {
|
| 114 |
$page = $this->get_page($page_name, $target);
|
| 115 |
|
| 116 |
if ($page) {
|
| 117 |
if (empty($this->page_access) || $this->page_access[$page->name]) {
|
| 118 |
$page->route = &$this;
|
| 119 |
return $page;
|
| 120 |
}
|
| 121 |
}
|
| 122 |
}
|
| 123 |
|
| 124 |
return $this->get_default_page();
|
| 125 |
}
|
| 126 |
|
| 127 |
/*
|
| 128 |
* Get default page:
|
| 129 |
* First page that is accessable
|
| 130 |
*/
|
| 131 |
private function get_default_page() {
|
| 132 |
foreach ($this->pages as $page) {
|
| 133 |
if (empty($this->page_access) || $this->page_access[$page->name]) {
|
| 134 |
$page->route = &$this;
|
| 135 |
return $page;
|
| 136 |
}
|
| 137 |
}
|
| 138 |
}
|
| 139 |
|
| 140 |
/*
|
| 141 |
* Checks if this is a valid page and if access to the page shall be granted
|
| 142 |
*/
|
| 143 |
public function check_page_access($page_name, $target = PAGEROUTE_CURRENT) {
|
| 144 |
if ($target == PAGEROUTE_CURRENT) {
|
| 145 |
return !isset($this->page_access[$page_name]) || $this->page_access[$page_name];
|
| 146 |
}
|
| 147 |
|
| 148 |
$access = FALSE;
|
| 149 |
|
| 150 |
do {
|
| 151 |
$page = $this->get_page($page_name, $target);
|
| 152 |
|
| 153 |
if (!$page) {
|
| 154 |
break;
|
| 155 |
}
|
| 156 |
|
| 157 |
$page_name = $page->name;
|
| 158 |
|
| 159 |
$access = !isset($this->page_access[$page_name]) || $this->page_access[$page_name];
|
| 160 |
|
| 161 |
} while ($access == FALSE);
|
| 162 |
|
| 163 |
if ($access) {
|
| 164 |
return $page;
|
| 165 |
}
|
| 166 |
|
| 167 |
return NULL;
|
| 168 |
}
|
| 169 |
|
| 170 |
/*
|
| 171 |
* Build and return the form for the current page
|
| 172 |
*/
|
| 173 |
public function get_form(&$page_name, &$page, &$form_state = NULL, &$args = NULL) {
|
| 174 |
$page_data = $this->get_suggested_page($page_name);
|
| 175 |
|
| 176 |
if (!$page_data) {
|
| 177 |
return FALSE;
|
| 178 |
}
|
| 179 |
|
| 180 |
// Include needed type!
|
| 181 |
$page = pageroute_page::get_object($page_data);
|
| 182 |
|
| 183 |
if (!$page) {
|
| 184 |
return FALSE;
|
| 185 |
}
|
| 186 |
|
| 187 |
return $page->get_form($form_state, $args);
|
| 188 |
}
|
| 189 |
|
| 190 |
private function get_page($page_name, $target) {
|
| 191 |
|
| 192 |
if (!isset($this->page_index[$page_name])) {
|
| 193 |
return FALSE;
|
| 194 |
}
|
| 195 |
|
| 196 |
$index = $this->page_index[$page_name];
|
| 197 |
|
| 198 |
switch ($target) {
|
| 199 |
case PAGEROUTE_BACK:
|
| 200 |
return $this->pages[$index - 1];
|
| 201 |
case PAGEROUTE_FORWARD:
|
| 202 |
return $this->pages[$index + 1];
|
| 203 |
case PAGEROUTE_CURRENT:
|
| 204 |
default:
|
| 205 |
return $this->pages[$index];
|
| 206 |
}
|
| 207 |
}
|
| 208 |
}
|