View source
<?php
class view extends views_db_object {
var $db_table = 'views_view';
var $base_table = 'node';
var $built = FALSE;
var $executed = FALSE;
var $args = array();
var $build_info = array();
var $use_ajax = FALSE;
var $result = array();
var $pager = array(
'use_pager' => FALSE,
'items_per_page' => 10,
'element' => 0,
'offset' => 0,
'current_page' => 0,
);
var $attachment_before = '';
var $attachment_after = '';
var $exposed_data = array();
var $exposed_input = array();
var $old_view = array();
var $current_display;
var $substitutions = array();
function view() {
parent::init();
foreach ($this
->db_objects() as $object) {
$this->{$object} = array();
}
$this->query = new stdClass();
}
function display_objects() {
return array(
'argument',
'field',
'sort',
'filter',
'relationship',
);
}
public static function db_objects() {
return array(
'display',
);
}
function set_arguments($args) {
$this->args = $args;
}
function set_items_per_page($items_per_page) {
$this->pager['items_per_page'] = $items_per_page;
if (empty($items_per_page)) {
$this->pager['use_pager'] = FALSE;
}
}
function set_current_page($page) {
$this->pager['current_page'] = $page;
}
function set_use_pager($use_pager) {
$this->pager['use_pager'] = $use_pager;
}
function set_pager_element($pager_element) {
$this->pager['element'] = $pager_element;
}
function synchronize_pager() {
if (!empty($this->pager['use_pager'])) {
global $pager_page_array, $pager_total, $pager_total_items;
$pager_total_items[$this->pager['element']] = $this->total_rows;
$pager_total[$this->pager['element']] = ceil($pager_total_items[$this->pager['element']] / $this->pager['items_per_page']);
$pager_page_array = isset($_GET['page']) ? explode(',', $_GET['page']) : array();
if (!empty($pager_page_array[$this->pager['element']])) {
$page = intval($pager_page_array[$this->pager['element']]);
if ($page > 0 && $page < $pager_total[$this->pager['element']]) {
$this->pager['current_page'] = $page;
}
}
$pager_page_array[$this->pager['element']] = $this->pager['current_page'];
}
}
function set_offset($offset) {
$this->pager['offset'] = $offset;
}
function set_use_ajax($use_ajax) {
$this->use_ajax = $use_ajax;
}
function set_exposed_input($filters) {
$this->exposed_input = $filters;
}
function get_exposed_input() {
if (empty($this->exposed_input)) {
$this->exposed_input = $_GET;
foreach (array(
'page',
'q',
) as $key) {
if (isset($this->exposed_input[$key])) {
unset($this->exposed_input[$key]);
}
}
$display_id = $this->display_handler
->is_defaulted('filters') ? 'default' : $this->current_display;
if (empty($this->exposed_input) && !empty($_SESSION['views'][$this->name][$display_id])) {
$this->exposed_input = $_SESSION['views'][$this->name][$display_id];
}
}
return $this->exposed_input;
}
function init_display($reset = FALSE) {
if (isset($this->current_display)) {
return TRUE;
}
foreach (array_keys($this->display) as $id) {
if (!empty($this->display[$id]->handler)) {
$this->display[$id] = drupal_clone($this->display[$id]);
unset($this->display[$id]->handler);
}
$this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
if (!empty($this->display[$id]->handler)) {
$this->display[$id]->handler
->init($this, $this->display[$id]);
if ($id != 'default') {
$this->display[$id]->handler->default_display =& $this->display['default']->handler;
}
}
}
$this->current_display = 'default';
$this->display_handler =& $this->display['default']->handler;
return TRUE;
}
function choose_display($displays) {
if (!is_array($displays)) {
return $displays;
}
$this
->init_display();
foreach ($displays as $display_id) {
if ($this->display[$display_id]->handler
->access()) {
return $display_id;
}
}
return 'default';
}
function set_display($display_id = NULL) {
if (empty($this->current_display)) {
$this
->init_display();
if (empty($display_id)) {
$display_id = 'default';
}
}
$display_id = $this
->choose_display($display_id);
if (empty($display_id)) {
return FALSE;
}
if (empty($this->display[$display_id])) {
$display_id = 'default';
if (empty($this->display[$display_id])) {
vpr(t('set_display() called with invalid display id @display.', array(
'@display' => $display_id,
)));
return FALSE;
}
}
$this->current_display = $display_id;
if (empty($this->display[$display_id]->handler)) {
return FALSE;
}
$this->display_handler =& $this->display[$display_id]->handler;
return TRUE;
}
function init_style() {
if (isset($this->style_plugin)) {
return is_object($this->style_plugin);
}
if (!isset($this->plugin_name)) {
$this->plugin_name = $this->display_handler
->get_option('style_plugin');
$this->style_options = $this->display_handler
->get_option('style_options');
}
$this->style_plugin = views_get_plugin('style', $this->plugin_name);
if (empty($this->style_plugin)) {
return FALSE;
}
$this->style_plugin
->init($this, $this->display[$this->current_display], $this->style_options);
return TRUE;
}
function init_handlers() {
if (empty($this->inited)) {
foreach (views_object_types() as $key => $info) {
$this
->_init_handler($key, $info);
}
$this->inited = TRUE;
}
}
function get_base_tables() {
$base_tables = array(
$this->base_table => TRUE,
'#global' => TRUE,
);
foreach ($this->display_handler
->get_handlers('relationship') as $handler) {
$base_tables[$handler->definition['base']] = TRUE;
}
return $base_tables;
}
function _pre_query() {
foreach (views_object_types() as $key => $info) {
$handlers =& $this->{$key};
$position = 0;
foreach ($handlers as $id => $handler) {
$handlers[$id]->position = $position;
$handlers[$id]
->pre_query();
$position++;
}
}
}
function substitutions($reset = FALSE) {
if ($reset || empty($this->substitutions)) {
$this->substitutions = module_invoke_all('views_query_substitutions', $this);
}
return $this->substitutions;
}
function _init_handler($key, $info) {
$this->{$key} =& $this->display_handler
->get_handlers($key);
$handlers =& $this->{$key};
foreach ($handlers as $id => $handler) {
if (!$handler
->access()) {
unset($handlers[$id]);
}
}
}
function render_exposed_form($block = FALSE) {
$form_state = array(
'view' => &$this,
'display' => &$this->display_handler->display,
'method' => 'get',
'rerender' => TRUE,
'no_redirect' => TRUE,
);
if (!$this->display_handler
->displays_exposed() || !$block && $this->display_handler
->get_option('exposed_block')) {
unset($form_state['rerender']);
}
if (!empty($this->ajax)) {
$form_state['ajax'] = TRUE;
}
$output = drupal_build_form('views_exposed_form', $form_state);
if (!empty($form_state['js settings'])) {
$this->js_settings = $form_state['js settings'];
}
if (form_get_errors() && empty($form_state['rerender'])) {
return NULL;
}
return $output;
}
function _build_arguments() {
if (empty($this->argument)) {
return TRUE;
}
$position = -1;
$title = $this->display_handler
->get_option('title');
$this->build_info['breadcrumb'] = array();
$breadcrumb_args = array();
$substitutions = array();
$status = TRUE;
foreach ($this->argument as $id => $arg) {
$position++;
$argument =& $this->argument[$id];
if ($argument
->broken()) {
continue;
}
$argument
->set_relationship();
$arg = isset($this->args[$position]) ? $this->args[$position] : NULL;
$argument->position = $position;
if (isset($arg) || $argument
->has_default_argument()) {
if (!isset($arg)) {
$arg = $argument
->get_default_argument();
if (isset($arg)) {
$this->args[$position] = $arg;
}
}
if (!$argument
->set_argument($arg)) {
$status = $argument
->validate_fail($arg);
break;
}
if ($argument
->is_wildcard()) {
$arg_title = $argument
->wildcard_title();
}
else {
$arg_title = $argument
->get_title();
$argument
->query();
}
$substitutions['%' . ($position + 1)] = $arg_title;
if ($this->display_handler
->uses_breadcrumb() && $argument
->uses_breadcrumb()) {
$path = $this
->get_url($breadcrumb_args);
if (strpos($path, '%') === FALSE) {
$breadcrumb = !empty($argument->options['breadcrumb']) ? $argument->options['breadcrumb'] : $title;
$this->build_info['breadcrumb'][$path] = str_replace(array_keys($substitutions), $substitutions, $breadcrumb);
}
}
$argument
->set_breadcrumb($this->build_info['breadcrumb']);
if (!empty($argument->options['title'])) {
$title = $argument->options['title'];
}
$breadcrumb_args[] = $arg;
}
else {
$status = $argument
->default_action();
break;
}
unset($argument);
}
if (!empty($title)) {
$this->build_info['title'] = str_replace(array_keys($substitutions), $substitutions, $title);
}
$this->build_info['substitutions'] = $substitutions;
return $status;
}
function init_query() {
$views_data = views_fetch_data($this->base_table);
$this->base_field = $views_data['table']['base']['field'];
if (!empty($views_data['table']['base']['database'])) {
$this->base_database = $views_data['table']['base']['database'];
}
views_include('query');
$this->query = new views_query($this->base_table, $this->base_field);
}
function build($display_id = NULL) {
if (!empty($this->built)) {
return;
}
if (empty($this->current_display) || $display_id) {
if (!$this
->set_display($display_id)) {
return FALSE;
}
}
foreach (module_implements('views_pre_build') as $module) {
$function = $module . '_views_pre_build';
$function($this);
}
$start = views_microtime();
$this->build_info = array(
'query' => '',
'count_query' => '',
'query_args' => array(),
);
$this
->init_query();
$this
->init_handlers();
$this
->_pre_query();
if ($this->display_handler
->uses_exposed()) {
$this->exposed_widgets = $this
->render_exposed_form();
if (form_set_error() || !empty($this->build_info['abort'])) {
$this->built = TRUE;
$this->executed = TRUE;
return empty($this->build_info['fail']);
}
}
$this
->_build('relationship');
$this
->_build('filter');
$this->build_sort = TRUE;
if (!$this
->_build_arguments()) {
$this->build_time = views_microtime() - $start;
$this
->attach_displays();
return $this->built;
}
if (!$this
->init_style()) {
$this->build_info['fail'] = TRUE;
return FALSE;
}
if ($this->style_plugin
->uses_fields()) {
$this
->_build('field');
}
if (!empty($this->build_sort)) {
if ($this->style_plugin
->build_sort()) {
$this
->_build('sort');
}
$this->style_plugin
->build_sort_post();
}
$this->display_handler
->query();
$this->style_plugin
->query();
if (variable_get('views_sql_signature', FALSE)) {
$this->query
->add_field(NULL, "'" . $this->name . ':' . $this->current_display . "'", 'view_name');
}
foreach (module_implements('views_query_alter') as $module) {
$function = $module . '_views_query_alter';
$function($this, $this->query);
}
$this->build_info['query'] = $this->query
->query();
$this->build_info['count_query'] = $this->query
->query(TRUE);
$this->build_info['query_args'] = $this->query
->get_where_args();
$this->built = TRUE;
$this->build_time = views_microtime() - $start;
$this
->attach_displays();
foreach (module_implements('views_post_build') as $module) {
$function = $module . '_views_post_build';
$function($this);
}
return TRUE;
}
function _build($key) {
$handlers =& $this->{$key};
foreach ($handlers as $id => $data) {
if (!empty($handlers[$id]) && is_object($handlers[$id])) {
if (!empty($this->exposed_data)) {
$rc = $handlers[$id]
->accept_exposed_input($this->exposed_data);
$handlers[$id]
->store_exposed_input($this->exposed_data, $rc);
if (!$rc) {
continue;
}
}
$handlers[$id]
->set_relationship();
$handlers[$id]
->query();
}
}
}
function execute($display_id = NULL) {
if (empty($this->built)) {
if (!$this
->build($display_id)) {
return FALSE;
}
}
if (!empty($this->executed)) {
return TRUE;
}
foreach (module_implements('views_pre_execute') as $module) {
$function = $module . '_views_pre_execute';
$function($this);
}
$query = db_rewrite_sql($this->build_info['query'], $this->base_table, $this->base_field, array(
'view' => &$this,
));
$count_query = db_rewrite_sql($this->build_info['count_query'], $this->base_table, $this->base_field, array(
'view' => &$this,
));
$args = $this->build_info['query_args'];
vpr($query);
if (!empty($this->live_preview)) {
$cache = FALSE;
}
else {
$cache = $this->display_handler
->get_cache_plugin();
}
if ($cache && $cache
->cache_get('results')) {
$this
->synchronize_pager();
vpr('Used cached results');
}
else {
$items = array();
if ($query) {
$replacements = $this
->substitutions();
$query = str_replace(array_keys($replacements), $replacements, $query);
$count_query = 'SELECT COUNT(*) FROM (' . str_replace(array_keys($replacements), $replacements, $count_query) . ') count_alias';
if (is_array($args)) {
foreach ($args as $id => $arg) {
$args[$id] = str_replace(array_keys($replacements), $replacements, $arg);
}
}
if (isset($this->base_database)) {
db_set_active($this->base_database);
$external = TRUE;
}
$start = views_microtime();
if (!empty($this->pager['items_per_page'])) {
if (!empty($this->pager['use_pager']) || !empty($this->get_total_rows)) {
$this->total_rows = db_result(db_query($count_query, $args)) - $this->pager['offset'];
}
$this
->synchronize_pager();
$offset = $this->pager['current_page'] * $this->pager['items_per_page'] + $this->pager['offset'];
$result = db_query_range($query, $args, $offset, $this->pager['items_per_page']);
}
else {
$result = db_query($query, $args);
}
$this->result = array();
while ($item = db_fetch_object($result)) {
$this->result[] = $item;
}
if (empty($this->pager['items_per_page'])) {
$this->total_rows = count($this->result);
}
if (!empty($external)) {
db_set_active();
}
$this->execute_time = views_microtime() - $start;
}
if ($cache) {
$cache
->cache_set('results');
}
}
foreach (module_implements('views_post_execute') as $module) {
$function = $module . '_views_post_execute';
$function($this);
}
$this->executed = TRUE;
}
function render($display_id = NULL) {
$this
->execute($display_id);
if (!empty($this->build_info['fail'])) {
return;
}
init_theme();
$start = views_microtime();
if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
$this
->start_query_capture();
}
if (!empty($this->live_preview)) {
$cache = FALSE;
}
else {
$cache = $this->display_handler
->get_cache_plugin();
}
if ($cache && $cache
->cache_get('output')) {
vpr('Used cached output');
}
else {
if ($cache) {
$cache
->cache_start();
}
$this
->init_style();
$this->style_plugin
->pre_render($this->result);
foreach (module_implements('views_pre_render') as $module) {
$function = $module . '_views_pre_render';
$function($this);
}
$function = $GLOBALS['theme'] . '_views_pre_render';
if (function_exists($function)) {
$function($this);
}
if ($this->style_plugin
->uses_fields()) {
foreach ($this->field as $id => $handler) {
if (!empty($this->field[$id])) {
$this->field[$id]
->pre_render($this->result);
}
}
}
$this->display_handler->output = $this->display_handler
->render();
if ($cache) {
$cache
->cache_set('output');
}
}
if ($cache) {
$cache
->post_render($this->display_handler->output);
}
foreach (module_implements('views_post_render') as $module) {
$function = $module . '_views_post_render';
$function($this, $this->display_handler->output, $cache);
}
$function = $GLOBALS['theme'] . '_views_post_render';
if (function_exists($function)) {
$function($this, $this->display_handler->output, $cache);
}
if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
$this
->end_query_capture();
}
$this->render_time = views_microtime() - $start;
return $this->display_handler->output;
}
function render_field($field, $row) {
if (isset($this->field[$field]) && isset($this->result[$row])) {
return $this->field[$field]
->advanced_render($this->result[$row]);
}
}
function execute_display($display_id = NULL, $args = array()) {
if (empty($this->current_display) || $this->current_display != $this
->choose_display($display_id)) {
if (!$this
->set_display($display_id)) {
return FALSE;
}
}
$this
->pre_execute($args);
$output = $this->display_handler
->execute();
$this
->post_execute();
return $output;
}
function preview($display_id = NULL, $args = array()) {
if (empty($this->current_display) || !empty($display_id) && $this->current_display != $display_id) {
if (!$this
->set_display($display_id)) {
return FALSE;
}
}
$this->preview = TRUE;
$this
->pre_execute($args);
$output = $this->display_handler
->preview();
$this
->post_execute();
return $output;
}
function pre_execute($args = array()) {
$this->old_view[] = views_get_current_view();
views_set_current_view($this);
$display_id = $this->current_display;
foreach (module_implements('views_pre_view') as $module) {
$function = $module . '_views_pre_view';
$function($this, $display_id, $args);
}
if ($args) {
$this
->set_arguments($args);
}
$this->display_handler
->pre_execute();
}
function post_execute() {
if ($this->old_view) {
$old_view = array_pop($this->old_view);
}
views_set_current_view(isset($old_view) ? $old_view : FALSE);
}
function attach_displays() {
if (!empty($this->is_attachment)) {
return;
}
if (!$this->display_handler
->accept_attachments()) {
return;
}
$this->is_attachment = TRUE;
foreach ($this->display as $id => $display) {
if (!empty($this->display[$id]->handler)) {
$this->display[$id]->handler
->attach_to($this->current_display);
}
}
$this->is_attachment = FALSE;
}
function execute_hook_menu($display_id = NULL, $callbacks = array()) {
if (!$this
->set_display($display_id)) {
return FALSE;
}
if (isset($this->display_handler)) {
return $this->display_handler
->execute_hook_menu($callbacks);
}
}
function execute_hook_block($display_id = NULL) {
if (!$this
->set_display($display_id)) {
return FALSE;
}
if (isset($this->display_handler)) {
return $this->display_handler
->execute_hook_block();
}
}
function access($displays = NULL, $account = NULL) {
if (!empty($this->disabled)) {
return FALSE;
}
if (!isset($this->current_display)) {
$this
->init_display();
}
if (!$account) {
$account = $GLOBALS['user'];
}
$displays = (array) $displays;
foreach ($displays as $display_id) {
if (!empty($this->display[$display_id]->handler)) {
if ($this->display[$display_id]->handler
->access($account)) {
return TRUE;
}
}
}
return FALSE;
}
function get_title() {
if (empty($this->display_handler)) {
if (!$this
->set_display('default')) {
return FALSE;
}
}
if (!empty($this->build_info['title'])) {
$title = $this->build_info['title'];
}
else {
$title = $this->display_handler
->get_option('title');
}
return $title;
}
function build_title() {
$this
->init_display();
if (empty($this->built)) {
$this
->init_query();
}
$this
->init_handlers();
$this
->_build_arguments();
}
function get_url($args = NULL, $path = NULL) {
if (!isset($path)) {
$path = $this
->get_path();
}
if (!isset($args)) {
$args = $this->args;
}
if (empty($path) || empty($args) && strpos($path, '%') === FALSE) {
return $path;
}
$pieces = array();
$arguments = isset($arguments) ? $arguments : $this->display_handler
->get_option('arguments');
$argument_keys = isset($arguments) ? array_keys($arguments) : array();
$id = current($argument_keys);
foreach (explode('/', $path) as $piece) {
if ($piece != '%') {
$pieces[] = $piece;
}
else {
if (empty($args)) {
if ($id && !empty($arguments[$id]['wildcard'])) {
$pieces[] = $arguments[$id]['wildcard'];
}
else {
$pieces[] = '*';
}
}
else {
$pieces[] = array_shift($args);
}
if ($id) {
$id = next($argument_keys);
}
}
}
if (!empty($args)) {
$pieces = array_merge($pieces, $args);
}
return implode('/', $pieces);
}
function get_path() {
if (!empty($this->override_path)) {
return $this->override_path;
}
if (empty($this->display_handler)) {
if (!$this
->set_display('default')) {
return FALSE;
}
}
return $this->display_handler
->get_path();
}
function get_breadcrumb($set = FALSE) {
$base = TRUE;
$breadcrumb = array();
if (!empty($this->build_info['breadcrumb'])) {
foreach ($this->build_info['breadcrumb'] as $path => $title) {
if ($path == variable_get('site_frontpage', 'node')) {
$base = FALSE;
$title = t('Home');
}
if ($title) {
$breadcrumb[] = l($title, $path, array(
'html' => true,
));
}
}
if ($set) {
if ($base) {
$breadcrumb = array_merge(drupal_get_breadcrumb(), $breadcrumb);
}
drupal_set_breadcrumb($breadcrumb);
}
}
return $breadcrumb;
}
function is_cacheable() {
return $this->is_cacheable;
}
function start_query_capture() {
global $conf, $queries;
if (empty($conf['dev_query'])) {
$this->fix_dev_query = TRUE;
$conf['dev_query'] = TRUE;
}
$this->last_query_key = NULL;
if (!empty($queries)) {
$keys = array_keys($queries);
$this->last_query_key = array_pop($keys);
}
}
function end_query_capture() {
global $conf, $queries;
if (!empty($this->fix_dev_query)) {
$conf['dev_query'] = FALSE;
}
$temp = $queries;
if (isset($this->last_query_key)) {
while (list($id, $query) = each($queries)) {
if ($id == $this->last_query_key) {
break;
}
unset($temp[$id]);
}
}
$this->additional_queries = $temp;
}
public static function &load($arg, $reset = FALSE) {
static $cache = array();
if (!array_key_exists($arg, $cache) || $reset) {
$where = is_numeric($arg) ? "vid = %d" : "name = '%s'";
$data = db_fetch_object(db_query("SELECT * FROM {views_view} WHERE {$where}", $arg));
if (empty($data)) {
$cache[$arg] = NULL;
}
else {
$view = new view();
$view
->load_row($data);
$view->type = t('Normal');
foreach ($view
->db_objects() as $key) {
$object_name = "views_{$key}";
$result = db_query("SELECT * FROM {{$object_name}} WHERE vid = %d ORDER BY position", $view->vid);
while ($data = db_fetch_object($result)) {
$object = new $object_name(FALSE);
$object
->load_row($data);
$location =& $view->{$key};
if (!empty($object->id)) {
$location[$object->id] = $object;
}
else {
$location[] = $object;
}
}
}
$view->loaded = TRUE;
$cache[$arg] = $view;
}
}
return $cache[$arg];
}
public static function load_views() {
$result = db_query("SELECT DISTINCT v.* FROM {views_view} v");
$views = array();
$vids = array();
while ($data = db_fetch_object($result)) {
$view = new view();
$view
->load_row($data);
$view->loaded = TRUE;
$view->type = t('Normal');
$views[$view->name] = $view;
$names[$view->vid] = $view->name;
}
if (!$views) {
return array();
}
$vids = implode(', ', array_keys($names));
foreach (view::db_objects() as $key) {
$object_name = "views_{$key}";
$result = db_query("SELECT * FROM {{$object_name}} WHERE vid IN ({$vids}) ORDER BY vid, position");
while ($data = db_fetch_object($result)) {
$object = new $object_name(FALSE);
$object
->load_row($data);
$location =& $views[$names[$object->vid]]->{$key};
if (!empty($object->id)) {
$location[$object->id] = $object;
}
else {
$location[] = $object;
}
}
}
return $views;
}
function save() {
if ($this->vid == 'new') {
$this->vid = NULL;
}
if (!empty($this->vid)) {
foreach ($this
->db_objects() as $key) {
db_query("DELETE from {views_" . $key . "} WHERE vid = %d", $this->vid);
}
}
$this
->save_row(!empty($this->vid) ? 'vid' : FALSE);
foreach ($this
->db_objects() as $key) {
$this
->_save_rows($key);
}
cache_clear_all('views_urls', 'cache_views');
cache_clear_all();
}
function _save_rows($key) {
$count = 0;
foreach ($this->{$key} as $position => $object) {
$object->position = ++$count;
$object->vid = $this->vid;
$object
->save_row();
}
}
function delete($clear = TRUE) {
if (empty($this->vid)) {
return;
}
db_query("DELETE FROM {views_view} WHERE vid = %d", $this->vid);
foreach ($this
->db_objects() as $key) {
db_query("DELETE from {views_" . $key . "} WHERE vid = %d", $this->vid);
}
cache_clear_all('views_query:' . $this->name, 'cache_views');
if ($clear) {
cache_clear_all();
menu_rebuild();
}
}
function export($indent = '') {
$this
->init_display();
$output = '';
$output .= $this
->export_row('view', $indent);
$output .= $indent . '$view->api_version = \'' . views_api_version() . "';\n";
$output .= $indent . '$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */' . "\n";
foreach ($this->display as $id => $display) {
$output .= $indent . '$handler = $view->new_display(' . views_var_export($display->display_plugin, $indent) . ', ' . views_var_export($display->display_title, $indent) . ', \'' . $id . "');\n";
if (empty($display->handler)) {
continue;
}
foreach ($display->handler
->option_definition() as $option => $definition) {
switch ($option) {
case 'defaults':
break;
default:
if (!$display->handler
->is_defaulted($option)) {
$value = $display->handler
->get_option($option);
if ($id == 'default') {
$default = isset($definition['default']) ? $definition['default'] : NULL;
}
else {
$default = $this->display['default']->handler
->get_option($option);
}
if ($value !== $default) {
$output .= $indent . '$handler->override_option(\'' . $option . '\', ' . views_var_export($value, $indent) . ");\n";
}
}
}
}
}
return $output;
}
function copy() {
$code = $this
->export();
eval($code);
return $view;
}
function clone_view() {
$clone = version_compare(phpversion(), '5.0') < 0 ? $this : clone $this;
$keys = array(
'current_display',
'display_handler',
'build_info',
'built',
'executed',
'attachment_before',
'attachment_after',
'field',
'argument',
'filter',
'sort',
'relationship',
'query',
'inited',
'style_plugin',
'plugin_name',
'exposed_data',
'exposed_input',
'exposed_widgets',
'many_to_one_tables',
'feed_icon',
);
foreach ($keys as $key) {
if (isset($clone->{$key})) {
unset($clone->{$key});
}
}
$clone->built = $clone->executed = FALSE;
$clone->build_info = array();
$clone->attachment_before = '';
$clone->attachment_after = '';
$clone->result = array();
$displays = array();
foreach ($clone->display as $id => $display) {
$displays[$id] = drupal_clone($display);
if (isset($displays[$id]->handler)) {
unset($displays[$id]->handler);
}
}
$clone->display = $displays;
return $clone;
}
function destroy() {
foreach (array_keys($this->display) as $display_id) {
if (isset($this->display[$display_id]->handler) && is_object($this->display[$display_id]->handler)) {
$this->display[$display_id]->handler
->destroy();
unset($this->display[$display_id]->handler);
}
}
foreach (views_object_types() as $type => $info) {
if (isset($this->{$type})) {
$handlers =& $this->{$type};
foreach ($handlers as $id => $item) {
$handlers[$id]
->destroy();
}
unset($handlers);
}
}
if (isset($this->style_plugin)) {
$this->style_plugin
->destroy();
unset($this->style_plugin);
}
if (isset($this->display_handler)) {
unset($this->display_handler);
}
if (isset($this->current_display)) {
unset($this->current_display);
}
if (isset($this->query)) {
unset($this->query);
}
}
function validate() {
$this
->init_display();
$errors = array();
$current_display = $this->current_display;
foreach ($this->display as $id => $display) {
if ($display->handler) {
if (!empty($display->deleted)) {
continue;
}
$this
->set_display($id);
$result = $this->display[$id]->handler
->validate();
if (!empty($result) && is_array($result)) {
$errors = array_merge($errors, $result);
}
}
}
$this
->set_display($current_display);
return $errors ? $errors : TRUE;
}
}
class views_db_object {
function init($init = TRUE) {
if (is_array($init)) {
return $this
->load_row($init);
}
if (!$init) {
return;
}
$schema = drupal_get_schema($this->db_table);
if (!$schema) {
return;
}
foreach ($schema['fields'] as $field => $info) {
if ($info['type'] == 'serial') {
$this->{$field} = NULL;
}
if (!isset($this->{$field})) {
if (!empty($info['serialize']) && isset($info['serialized default'])) {
$this->{$field} = unserialize($info['serialized default']);
}
else {
if (isset($info['default'])) {
$this->{$field} = $info['default'];
}
else {
$this->{$field} = '';
}
}
}
}
}
function save_row($update = NULL) {
$schema = drupal_get_schema($this->db_table);
$fields = $defs = $values = $serials = array();
foreach ($schema['fields'] as $field => $info) {
if ($info['type'] == 'serial') {
$serials[] = $field;
continue;
}
$fields[] = $field;
switch ($info['type']) {
case 'blob':
$defs[] = '%b';
break;
case 'int':
$defs[] = '%d';
break;
case 'float':
case 'numeric':
$defs[] = '%f';
break;
default:
$defs[] = "'%s'";
}
if (empty($info['serialize'])) {
$values[] = $this->{$field};
}
else {
$values[] = serialize($this->{$field});
}
}
$query = '';
if (!$update) {
$query = "INSERT INTO {" . $this->db_table . "} (" . implode(', ', $fields) . ') VALUES (' . implode(', ', $defs) . ')';
}
else {
$query = '';
foreach ($fields as $id => $field) {
if ($query) {
$query .= ', ';
}
$query .= $field . ' = ' . $defs[$id];
}
$query = "UPDATE {" . $this->db_table . "} SET " . $query . " WHERE {$update} = " . $this->{$update};
}
db_query($query, $values);
if ($serials && !$update) {
foreach ($serials as $field) {
$this->{$field} = db_last_insert_id($this->db_table, $field);
}
}
}
function load_row($data) {
$schema = drupal_get_schema($this->db_table);
foreach ($schema['fields'] as $field => $info) {
$this->{$field} = empty($info['serialize']) ? $data->{$field} : unserialize(db_decode_blob($data->{$field}));
}
}
function export_row($identifier = NULL, $indent = '') {
if (!$identifier) {
$identifier = $this->db_table;
}
$schema = drupal_get_schema($this->db_table);
$output = $indent . '$' . $identifier . ' = new ' . get_class($this) . ";\n";
foreach ($schema['fields'] as $field => $info) {
if (!empty($info['no export'])) {
continue;
}
if (!isset($this->{$field})) {
if (isset($info['default'])) {
$this->{$field} = $info['default'];
}
else {
$this->{$field} = '';
}
if (isset($info['serialize'])) {
$this->{$field} = unserialize(db_decode_blob($this->{$field}));
}
}
$value = $this->{$field};
if ($info['type'] == 'int') {
$value = $info['size'] == 'tiny' ? (bool) $value : (int) $value;
}
$output .= $indent . '$' . $identifier . '->' . $field . ' = ' . views_var_export($value, $indent) . ";\n";
}
return $output;
}
function add_display($type = 'page', $title = NULL, $id = NULL) {
if (empty($type)) {
return FALSE;
}
$plugin = views_fetch_plugin_data('display', $type);
if (empty($plugin)) {
$plugin['title'] = t('Broken');
}
if (empty($id)) {
$title = $plugin['title'];
$id = $this
->generate_display_id($type);
$count = str_replace($type . '_', '', $id);
if (empty($title)) {
$title = $plugin['title'] . ' ' . $count;
}
}
$display = new views_display();
$display
->options($type, $id, $title);
$this->display[$id] = $display;
return $id;
}
function generate_display_id($type) {
if ($type == 'default') {
return 'default';
}
$id = $type . '_1';
$count = 1;
while (!empty($this->display[$id])) {
$id = $type . '_' . ++$count;
}
return $id;
}
function &new_display($type = 'page', $title = NULL, $id = NULL) {
$id = $this
->add_display($type, $title, $id);
$this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
if (empty($this->display[$id]->handler)) {
$this->display[$id]->handler = views_get_plugin('display', 'default');
}
if (!empty($this->display[$id]->handler)) {
$this->display[$id]->handler
->init($this, $this->display[$id]);
if ($id != 'default') {
$this->display[$id]->handler->default_display =& $this->display['default']->handler;
}
}
return $this->display[$id]->handler;
}
function add_item($display_id, $type, $table, $field, $options = array(), $id = NULL) {
$types = views_object_types();
$this
->set_display($display_id);
$fields = $this->display[$display_id]->handler
->get_option($types[$type]['plural']);
if (empty($id)) {
$count = 0;
$id = $field;
while (!empty($fields[$id])) {
$id = $field . '_' . ++$count;
}
}
$new_item = array(
'id' => $id,
'table' => $table,
'field' => $field,
) + $options;
$handler = views_get_handler($table, $field, $type);
$fields[$id] = $new_item;
$this->display[$display_id]->handler
->set_option($types[$type]['plural'], $fields);
return $id;
}
function get_items($type, $display_id = NULL) {
$this
->set_display($display_id);
if (!isset($display_id)) {
$display_id = $this->current_display;
}
$types = views_object_types();
return $this->display[$display_id]->handler
->get_option($types[$type]['plural']);
}
function get_item($display_id, $type, $id) {
$types = views_object_types();
$this
->set_display($display_id);
$fields = $this->display[$display_id]->handler
->get_option($types[$type]['plural']);
return isset($fields[$id]) ? $fields[$id] : NULL;
}
function set_item($display_id, $type, $id, $item) {
$types = views_object_types();
$this
->set_display($display_id);
$fields = $this->display[$display_id]->handler
->get_option($types[$type]['plural']);
if (isset($item)) {
$fields[$id] = $item;
}
else {
unset($fields[$id]);
}
$this->display[$display_id]->handler
->set_option($types[$type]['plural'], $fields);
}
function set_item_option($display_id, $type, $id, $option, $value) {
$item = $this
->get_item($display_id, $type, $id);
$item[$option] = $value;
$this
->set_item($display_id, $type, $id, $item);
}
}
class views_display extends views_db_object {
var $db_table = 'views_display';
function views_display($init = TRUE) {
parent::init($init);
}
function options($type, $id, $title) {
$this->display_plugin = $type;
$this->id = $id;
$this->display_title = $title;
}
}
function views_object_types() {
static $retval = NULL;
if (!isset($retval)) {
$retval = array(
'field' => array(
'title' => t('Fields'),
'ltitle' => t('fields'),
'stitle' => t('Field'),
'lstitle' => t('field'),
'plural' => 'fields',
),
'argument' => array(
'title' => t('Arguments'),
'ltitle' => t('arguments'),
'stitle' => t('Argument'),
'lstitle' => t('Argument'),
'plural' => 'arguments',
),
'sort' => array(
'title' => t('Sort criteria'),
'ltitle' => t('sort criteria'),
'stitle' => t('Sort criterion'),
'lstitle' => t('sort criterion'),
'plural' => 'sorts',
),
'filter' => array(
'title' => t('Filters'),
'ltitle' => t('filters'),
'stitle' => t('Filter'),
'lstitle' => t('filter'),
'plural' => 'filters',
'options' => 'views_ui_config_filters_form',
),
'relationship' => array(
'title' => t('Relationships'),
'ltitle' => t('relationships'),
'stitle' => t('Relationship'),
'lstitle' => t('Relationship'),
'plural' => 'relationships',
),
);
}
return $retval;
}