| 1 |
<?php |
<?php |
| 2 |
// $Id: view.inc,v 1.3 2007/08/19 23:29:50 merlinofchaos Exp $ |
// $Id: view.inc,v 1.4 2007/08/19 23:43:43 merlinofchaos Exp $ |
| 3 |
/** |
/** |
| 4 |
* @file view.inc |
* @file view.inc |
| 5 |
* Provides the view object type and associated methods. |
* Provides the view object type and associated methods. |
| 77 |
$this->offset = $offset; |
$this->offset = $offset; |
| 78 |
} |
} |
| 79 |
|
|
| 80 |
|
/** |
| 81 |
|
* Set the exposed filters input to an array. If unset they will be taken |
| 82 |
|
* from $_GET when the time comes. |
| 83 |
|
*/ |
| 84 |
function set_filter_input($filters) { |
function set_filter_input($filters) { |
| 85 |
$this->filter_input = $filters; |
$this->filter_input = $filters; |
| 86 |
} |
} |
| 87 |
|
|
| 88 |
function set_display($display_id) { |
function get_fields($display_id = NULL) { |
| 89 |
$this->display = $display_id; |
if (!isset($display_id)) { |
| 90 |
|
$display_id = $this->current_display; |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
// TODO: Fix this: |
| 94 |
|
return $this->field; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
/** |
| 98 |
|
* Set the display for this view, and initialize the display handler. |
| 99 |
|
*/ |
| 100 |
|
function set_display($display_id = NULL) { |
| 101 |
|
// The default display is always the first one in the list. |
| 102 |
|
if (isset($this->current_display)) { |
| 103 |
|
return TRUE; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
$this->default_display = 0; |
| 107 |
|
if (!isset($display_id)) { |
| 108 |
|
$this->current_display = 0; |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
foreach ($this->display as $id => $display) { |
| 112 |
|
if ($display->id == $display_id) { |
| 113 |
|
$this->current_display = $id; |
| 114 |
|
} |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
// If the id was invalid, then just use the default display. |
| 118 |
|
if (!isset($this->current_display)) { |
| 119 |
|
$this->current_display = 0; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
if (!isset($this->display[$this->current_display])) { |
| 123 |
|
return FALSE; |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
// Fetch the display handler data and instantiate an object. |
| 127 |
|
$display_data = $this->display[$this->current_display]; |
| 128 |
|
$this->display_handler = views_get_plugin('display', $display_data->display_plugin); |
| 129 |
|
if (empty($this->display_handler)) { |
| 130 |
|
return FALSE; |
| 131 |
|
} |
| 132 |
|
|
| 133 |
|
// Seed the new display handler with data. |
| 134 |
|
$this->display_handler->seed($this, $display_data); |
| 135 |
|
|
| 136 |
|
// If this is NOT the default display handler, let it know which is |
| 137 |
|
// since it may well utilize some data from the default. |
| 138 |
|
if ($this->current_display != $this->default_display) { |
| 139 |
|
$this->display_handler->default_display = $this->display[$this->default_display]; |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
// Find and initialize the style plugin. Note that arguments may have changed |
| 143 |
|
// which style plugin we use, so check the view object first, then ask |
| 144 |
|
// the display handler. |
| 145 |
|
if (!isset($this->style_plugin)) { |
| 146 |
|
$this->style_plugin = $display_data->style_plugin; |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
$this->style_handler = views_get_plugin('style', $this->style_plugin); |
| 150 |
|
if (empty($this->style_handler)) { |
| 151 |
|
return FALSE; |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
// Seed the new display handler with data. |
| 155 |
|
$this->style_handler->seed($this, $display_data); |
| 156 |
|
|
| 157 |
|
return TRUE; |
| 158 |
} |
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
function build($display_id = NULL) { |
function build($display_id = NULL) { |
| 162 |
if (!empty($this->built)) { |
if (!empty($this->built)) { |
| 163 |
return; |
return; |
| 164 |
} |
} |
| 165 |
|
|
| 166 |
$this->display_id = $display_id; |
if (!$this->set_display($display_id)) { |
| 167 |
|
return FALSE; |
| 168 |
|
} |
| 169 |
|
|
| 170 |
|
// Execute the initial PHP code that a view can have. |
| 171 |
|
if ($this->view_args_php) { |
| 172 |
|
ob_start(); |
| 173 |
|
$result = eval($this->view_args_php); |
| 174 |
|
if (is_array($result)) { |
| 175 |
|
$this->args = $result; |
| 176 |
|
} |
| 177 |
|
ob_end_clean(); |
| 178 |
|
} |
| 179 |
|
|
| 180 |
// Attempt to load from cache. |
// Attempt to load from cache. |
| 181 |
// TODO: Load a build_info from cache. |
// TODO: Load a build_info from cache. |
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
// If that fails, let's build! |
| 186 |
|
$this->build_info = array(); |
| 187 |
|
|
| 188 |
|
$views_data = views_fetch_data($this->base_table); |
| 189 |
|
$this->base_field = $views_data['table']['base']['field']; |
| 190 |
|
$this->query = new views_query($this->base_table, $this->base_field); |
| 191 |
|
|
| 192 |
// Call a module hook and see if it wants to present us with a |
// Call a module hook and see if it wants to present us with a |
| 193 |
// pre-built query or instruct us not to build the query for |
// pre-built query or instruct us not to build the query for |
| 194 |
// some reason. |
// some reason. |
| 195 |
// TODO: Implement this. |
// TODO: Implement this. |
|
|
|
|
// If that fails, let's build! |
|
|
$this->build_info = array(); |
|
| 196 |
|
|
| 197 |
$this->query = new views_query(); |
// Run through our handlers and ensure they have necessary information. |
| 198 |
$this->_seed_handlers(); |
$this->_seed_handlers(); |
| 199 |
|
|
| 200 |
|
// Build all the filters. |
| 201 |
$this->_build('filter'); |
$this->_build('filter'); |
| 202 |
|
|
| 203 |
$this->build_sort = $this->build_fields = TRUE; |
$this->build_sort = $this->build_fields = TRUE; |
| 204 |
|
|
| 205 |
|
$argument_title = ''; |
| 206 |
// build arguments. |
// build arguments. |
| 207 |
foreach ($this->argument as $id => $argument) { |
foreach ($this->argument as $id => $arg) { |
| 208 |
|
unset ($argument); |
| 209 |
|
$argument = &$this->argument[$id]; |
| 210 |
|
|
| 211 |
|
if (!is_object($argument->handler)) { |
| 212 |
|
// TODO: Set some kind of warning. |
| 213 |
|
continue; |
| 214 |
|
} |
| 215 |
|
|
| 216 |
if (isset($this->args[$id])) { |
if (isset($this->args[$id])) { |
| 217 |
// handle argument that is present. |
// handle argument that is present. |
| 218 |
// TODO: Do we want to put in argument placeholders here |
// TODO: Do we want to put in argument placeholders here |
| 219 |
// So that we can try to cache queries with arguments too? |
// So that we can try to cache queries with arguments too? |
| 220 |
$argument->handler->argument = $this->args[$id]; |
$argument->handler->argument = $this->args[$id]; |
| 221 |
$argument->handler->query(); |
$argument->handler->query(); |
| 222 |
|
|
| 223 |
|
// Test to see if we should use this argument's title |
| 224 |
|
if (!empty($argument->title)) { |
| 225 |
|
$argument_title = $id; |
| 226 |
|
} |
| 227 |
} |
} |
| 228 |
else { |
else { |
| 229 |
// determine default condition and handle. |
// determine default condition and handle. |
| 230 |
$argument->handler->default_action(); |
if (!$argument->handler->default_action()) { |
| 231 |
|
return; |
| 232 |
|
} |
| 233 |
} |
} |
| 234 |
} |
} |
| 235 |
|
|
| 247 |
$this->build_info['count_query'] = $this->query->query(TRUE); |
$this->build_info['count_query'] = $this->query->query(TRUE); |
| 248 |
$this->build_info['query_args'] = $this->query->get_where_args(); |
$this->build_info['query_args'] = $this->query->get_where_args(); |
| 249 |
$this->built = TRUE; |
$this->built = TRUE; |
| 250 |
|
return TRUE; |
| 251 |
} |
} |
| 252 |
|
|
| 253 |
/** |
/** |
| 254 |
* Internal method to build an individual set of handlers. |
* Internal method to build an individual set of handlers. |
| 255 |
*/ |
*/ |
| 256 |
function _build($key) { |
function _build($key) { |
| 257 |
foreach ($this->$key as $data) { |
$array = &$this->$key; |
| 258 |
$data->handler->query(); |
foreach ($array as $id => $data) { |
| 259 |
} |
// TODO: we should report an error here if this is not an object. |
| 260 |
|
if (is_object($array[$id]->handler)) { |
| 261 |
|
$array[$id]->handler->query(); |
| 262 |
|
} |
| 263 |
|
} |
| 264 |
} |
} |
| 265 |
|
|
| 266 |
/** |
/** |
| 283 |
*/ |
*/ |
| 284 |
function _seed_handler($key) { |
function _seed_handler($key) { |
| 285 |
foreach ($this->$key as $data) { |
foreach ($this->$key as $data) { |
| 286 |
$handler = views_get_handler($data->table, $data->field, $key); |
$handler = views_get_handler($data->tablename, $data->field, $key); |
| 287 |
if (is_object($handler)) { |
if (is_object($handler)) { |
| 288 |
$handler->seed($this, $data); |
$handler->seed($this, $data); |
| 289 |
$data->handler = $handler; |
$data->handler = $handler; |
| 292 |
} |
} |
| 293 |
|
|
| 294 |
/** |
/** |
| 295 |
|
* Execute the view's query. |
| 296 |
|
*/ |
| 297 |
|
function execute($display_id = NULL) { |
| 298 |
|
if (!empty($this->executed)) { |
| 299 |
|
return TRUE; |
| 300 |
|
} |
| 301 |
|
|
| 302 |
|
if (empty($this->built)) { |
| 303 |
|
if (!$this->build($display_id)) { |
| 304 |
|
return FALSE; |
| 305 |
|
} |
| 306 |
|
} |
| 307 |
|
|
| 308 |
|
$query = db_rewrite_sql($this->build_info['query'], $this->base_table, $this->base_field); |
| 309 |
|
$args = $this->build_info['query_args']; |
| 310 |
|
|
| 311 |
|
// TODO: Fill in execution defaults if they are not set. |
| 312 |
|
// use_pager, page_size, offset |
| 313 |
|
|
| 314 |
|
$items = array(); |
| 315 |
|
if ($query) { |
| 316 |
|
if (!empty($this->use_pager)) { |
| 317 |
|
$count_query = db_rewrite_sql($this->build_info['count_query'], $this->base_table, $this->base_field); |
| 318 |
|
$this->result = pager_query($query, $view->page_size, $view->use_pager - 1, $count_query, $args); |
| 319 |
|
$this->total_rows = $GLOBALS['pager_total_items'][$this->use_pager - 1]; |
| 320 |
|
} |
| 321 |
|
else if (!empty($this->page_size)) { |
| 322 |
|
$offset = $this->current_page * $this->page_size + $this->offset; |
| 323 |
|
$this->result = db_query_range($query, $args, $offset, $this->page_size); |
| 324 |
|
} |
| 325 |
|
else { |
| 326 |
|
$this->result = db_query($query, $args); |
| 327 |
|
} |
| 328 |
|
} |
| 329 |
|
|
| 330 |
|
$this->executed = TRUE; |
| 331 |
|
} |
| 332 |
|
|
| 333 |
|
/** |
| 334 |
* Render this view for display. |
* Render this view for display. |
| 335 |
*/ |
*/ |
| 336 |
function render($display_id = NULL) { |
function render($display_id = NULL) { |
| 337 |
if (empty($this->built)) { |
// Check for cached output. |
| 338 |
$this->build($display_id); |
// TODO: Implement this |
| 339 |
|
|
| 340 |
|
// Make sure the query has already executed. |
| 341 |
|
if (empty($this->executed)) { |
| 342 |
|
$this->execute($display_id); |
| 343 |
} |
} |
| 344 |
|
|
| 345 |
// Check to see if the build failed. |
// Check to see if the build failed. |
| 346 |
|
if (empty($this->result)) { |
| 347 |
|
return; |
| 348 |
|
} |
| 349 |
|
|
| 350 |
// Check for cached output. |
return $this->display_handler->render(); |
| 351 |
} |
} |
| 352 |
|
|
| 353 |
function get_title($context) { } |
function get_title($context) { } |
| 354 |
function get_url() { } |
function get_url($args = NULL) { } |
| 355 |
function is_cacheable() { } |
function is_cacheable() { } |
| 356 |
|
|
| 357 |
/** |
/** |
| 379 |
*/ |
*/ |
| 380 |
function _load_row($key) { |
function _load_row($key) { |
| 381 |
$object_name = "views_$key"; |
$object_name = "views_$key"; |
| 382 |
$table = $object_name . 's'; |
$table = $object_name; |
| 383 |
$result = db_query("SELECT * FROM {$table} WHERE vid = %d ORDER BY position", $this->vid); |
$result = db_query("SELECT * FROM {$table} WHERE vid = %d ORDER BY position", $this->vid); |
| 384 |
|
|
| 385 |
while ($data = db_fetch_object($result)) { |
while ($data = db_fetch_object($result)) { |
| 398 |
if (!empty($this->vid)) { |
if (!empty($this->vid)) { |
| 399 |
// remove existing table entries |
// remove existing table entries |
| 400 |
foreach (views_objects_all() as $key) { |
foreach (views_objects_all() as $key) { |
| 401 |
db_query("DELETE from {views_" . $key . "s} WHERE vid = %d", $this->vid); |
db_query("DELETE from {views_" . $key . "} WHERE vid = %d", $this->vid); |
| 402 |
$this->_load_row($key); |
$this->_load_row($key); |
| 403 |
} |
} |
| 404 |
} |
} |
| 422 |
foreach ($this->$key as $position => $object) { |
foreach ($this->$key as $position => $object) { |
| 423 |
$object->position = $position; |
$object->position = $position; |
| 424 |
$object->vid = $this->vid; |
$object->vid = $this->vid; |
| 425 |
_views_save_query("views_" . $key . "s", $object); |
_views_save_query("views_" . $key, $object); |
| 426 |
} |
} |
| 427 |
} |
} |
| 428 |
|
|
| 437 |
db_query("DELETE FROM {views_view} WHERE vid = %d", $this->vid); |
db_query("DELETE FROM {views_view} WHERE vid = %d", $this->vid); |
| 438 |
// Delete from all of our subtables as well. |
// Delete from all of our subtables as well. |
| 439 |
foreach (views_objects_all() as $key) { |
foreach (views_objects_all() as $key) { |
| 440 |
db_query("DELETE from {views_" . $key . "s} WHERE vid = %d", $this->vid); |
db_query("DELETE from {views_" . $key . "} WHERE vid = %d", $this->vid); |
| 441 |
$this->_load_row($key); |
$this->_load_row($key); |
| 442 |
} |
} |
| 443 |
|
|