View source
<?php
class views_handler_argument extends views_handler {
var $name_field = NULL;
function construct() {
parent::construct();
if (!empty($this->definition['name field'])) {
$this->name_field = $this->definition['name field'];
}
if (!empty($this->definition['name table'])) {
$this->name_table = $this->definition['name table'];
}
}
function init(&$view, $options) {
parent::init($view, $options);
}
function set_breadcrumb(&$breadcrumb) {
}
function uses_breadcrumb() {
$info = $this
->default_actions($this->options['default_action']);
return !empty($info['breadcrumb']);
}
function is_wildcard($arg = NULL) {
if (!isset($arg)) {
$arg = $this->argument;
}
return !empty($this->options['wildcard']) && $this->options['wildcard'] === $arg;
}
function wildcard_title() {
return $this->options['wildcard_substitution'];
}
function needs_style_plugin() {
$info = $this
->default_actions($this->options['default_action']);
$validate_info = $this
->default_actions($this->options['validate_fail']);
return !empty($info['style plugin']) || !empty($validate_info['style plugin']);
}
function option_definition() {
$options = parent::option_definition();
$options['default_action'] = array(
'default' => 'ignore',
);
$options['style_plugin'] = array(
'default' => 'default_summary',
'export' => 'export_style',
);
$options['style_options'] = array(
'default' => array(),
'export' => FALSE,
);
$options['wildcard'] = array(
'default' => 'all',
);
$options['wildcard_substitution'] = array(
'default' => t('All'),
'translatable' => TRUE,
);
$options['title'] = array(
'default' => '',
'translatable' => TRUE,
);
$options['breadcrumb'] = array(
'default' => '',
'translatable' => TRUE,
);
$options['default_argument_type'] = array(
'default' => 'fixed',
'export' => 'export_plugin',
);
$options['default_argument_options'] = array(
'default' => array(),
'export' => FALSE,
);
$options['default_argument_skip_url'] = array(
'default' => FALSE,
);
$options['validate_type'] = array(
'default' => 'none',
'export' => 'export_plugin',
);
$options['validate_options'] = array(
'default' => array(),
'export' => FALSE,
);
$options['validate_fail'] = array(
'default' => 'not found',
);
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$defaults = $this
->default_actions();
$form['title'] = array(
'#prefix' => '<div class="clear-block">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $this->options['title'],
'#description' => t('The title to use when this argument is present. It will override the title of the view and titles from previous arguments. You can use percent substitution here to replace with argument titles. Use "%1" for the first argument, "%2" for the second, etc.'),
);
$form['breadcrumb'] = array(
'#prefix' => '<div class="clear-block">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Breadcrumb'),
'#default_value' => $this->options['breadcrumb'],
'#description' => t('The Breadcrumb title to use when this argument is present. If no breadcrumb is set here, default Title values will be used, see "Title" for percent substitutions.'),
);
$form['clear_start'] = array(
'#value' => '<div class="clear-block">',
);
$form['defaults_start'] = array(
'#value' => '<div class="views-left-50">',
);
$form['default_action'] = array(
'#type' => 'radios',
'#title' => t('Action to take if argument is not present'),
'#default_value' => $this->options['default_action'],
);
$form['defaults_stop'] = array(
'#value' => '</div>',
);
$form['wildcard'] = array(
'#prefix' => '<div class="views-right-50">',
'#type' => 'textfield',
'#title' => t('Wildcard'),
'#size' => 20,
'#default_value' => $this->options['wildcard'],
'#description' => t('If this value is received as an argument, the argument will be ignored; i.e, "all values"'),
);
$form['wildcard_substitution'] = array(
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Wildcard title'),
'#size' => 20,
'#default_value' => $this->options['wildcard_substitution'],
'#description' => t('The title to use for the wildcard in substitutions elsewhere.'),
);
$form['clear_stop'] = array(
'#value' => '</div>',
);
$options = array();
$validate_options = array();
foreach ($defaults as $id => $info) {
$options[$id] = $info['title'];
if (empty($info['default only'])) {
$validate_options[$id] = $info['title'];
}
if (!empty($info['form method'])) {
$this
->{$info['form method']}($form, $form_state);
}
}
$form['default_action']['#options'] = $options;
$form['validate_options_div_prefix'] = array(
'#id' => 'views-validator-options',
'#value' => '<fieldset id="views-validator-options"><legend>' . t('Validator options') . '</legend>',
);
$form['validate_type'] = array(
'#type' => 'select',
'#title' => t('Validator'),
'#default_value' => $this->options['validate_type'],
);
$validate_types = array(
'none' => t('- Basic validation -'),
);
$plugins = views_fetch_plugin_data('argument validator');
foreach ($plugins as $id => $info) {
if (!empty($info['no ui'])) {
continue;
}
$valid = TRUE;
if (!empty($info['type'])) {
$valid = FALSE;
if (empty($this->definition['validate type'])) {
continue;
}
foreach ((array) $info['type'] as $type) {
if ($type == $this->definition['validate type']) {
$valid = TRUE;
break;
}
}
}
if ($valid) {
$plugin = $this
->get_plugin('argument validator', $id);
if ($plugin) {
if ($plugin
->access() || $this->options['validate_type'] == $id) {
$form['argument_validate'][$id] = array(
'#type' => 'item',
'#input' => TRUE,
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-validate-type' => array(
$id,
),
),
);
$plugin
->options_form($form['argument_validate'][$id], $form_state);
$validate_types[$id] = $info['title'];
}
}
}
}
asort($validate_types);
$form['validate_type']['#options'] = $validate_types;
$form['validate_fail'] = array(
'#type' => 'select',
'#title' => t('Action to take if argument does not validate'),
'#default_value' => $this->options['validate_fail'],
'#options' => $validate_options,
);
$form['validate_options_div_suffix'] = array(
'#value' => '</fieldset>',
);
}
function options_validate(&$form, &$form_state) {
if (empty($form_state['values']['options'])) {
return;
}
$default_id = $form_state['values']['options']['default_argument_type'];
$plugin = $this
->get_plugin('argument default', $default_id);
if ($plugin) {
$plugin
->options_validate($form['argument_default'][$default_id], $form_state, $form_state['values']['options']['argument_default'][$default_id]);
}
$validate_id = $form_state['values']['options']['validate_type'];
$plugin = $this
->get_plugin('argument validator', $validate_id);
if ($plugin) {
$plugin
->options_validate($form['argument_validate'][$default_id], $form_state, $form_state['values']['options']['argument_validate'][$validate_id]);
}
}
function options_submit($form, &$form_state) {
if (empty($form_state['values']['options'])) {
return;
}
$default_id = $form_state['values']['options']['default_argument_type'];
$plugin = $this
->get_plugin('argument default', $default_id);
if ($plugin) {
$options =& $form_state['values']['options']['argument_default'][$default_id];
$plugin
->options_submit($form['argument_default'][$default_id], $form_state, $options);
$form_state['values']['options']['default_argument_options'] = $options;
}
$validate_id = $form_state['values']['options']['validate_type'];
$plugin = $this
->get_plugin('argument validator', $validate_id);
if ($plugin) {
$options =& $form_state['values']['options']['argument_validate'][$validate_id];
$plugin
->options_submit($form['argument_validate'][$validate_id], $form_state, $options);
$form_state['values']['options']['validate_options'] = $options;
}
}
function default_actions($which = NULL) {
$defaults = array(
'ignore' => array(
'title' => t('Display all values'),
'method' => 'default_ignore',
'breadcrumb' => TRUE,
),
'not found' => array(
'title' => t('Hide view / Page not found (404)'),
'method' => 'default_not_found',
'hard fail' => TRUE,
),
'empty' => array(
'title' => t('Display empty text'),
'method' => 'default_empty',
'breadcrumb' => TRUE,
),
'summary asc' => array(
'title' => t('Summary, sorted ascending'),
'method' => 'default_summary',
'method args' => array(
'asc',
),
'style plugin' => TRUE,
'breadcrumb' => TRUE,
),
'summary desc' => array(
'title' => t('Summary, sorted descending'),
'method' => 'default_summary',
'method args' => array(
'desc',
),
'style plugin' => TRUE,
'breadcrumb' => TRUE,
),
'summary asc by count' => array(
'title' => t('Summary, sorted by number of records ascending'),
'method' => 'default_summary',
'method args' => array(
'asc',
'num_records',
),
'style plugin' => TRUE,
'breadcrumb' => TRUE,
),
'summary desc by count' => array(
'title' => t('Summary, sorted by number of records descending'),
'method' => 'default_summary',
'method args' => array(
'desc',
'num_records',
),
'style plugin' => TRUE,
'breadcrumb' => TRUE,
),
'default' => array(
'title' => t('Provide default argument'),
'method' => 'default_default',
'form method' => 'default_argument_form',
'has default argument' => TRUE,
'default only' => TRUE,
),
);
if ($which) {
if (!empty($defaults[$which])) {
return $defaults[$which];
}
}
else {
return $defaults;
}
}
function default_argument_form(&$form, &$form_state) {
$plugins = views_fetch_plugin_data('argument default');
$options = array();
$form['default_options_div_prefix'] = array(
'#type' => 'hidden',
'#id' => 'views-default-options',
'#prefix' => '<div><fieldset id="views-default-options"><legend>' . t('Provide default argument options') . '</legend>',
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'radio:options[default_action]' => array(
'default',
),
),
);
$form['default_argument_skip_url'] = array(
'#type' => 'checkbox',
'#title' => t('Skip default argument for view URL'),
'#default_value' => $this->options['default_argument_skip_url'],
'#description' => t('Select whether to include this default argument when constructing the URL for this view. Skipping default arguments is useful e.g. in the case of feeds.'),
);
$form['default_argument_type'] = array(
'#prefix' => '<div id="edit-options-default-argument-type-wrapper">',
'#suffix' => '</div>',
'#type' => 'radios',
'#id' => 'edit-options-default-argument-type',
'#title' => t('Default argument type'),
'#default_value' => $this->options['default_argument_type'],
);
foreach ($plugins as $id => $info) {
if (!empty($info['no ui'])) {
continue;
}
$plugin = $this
->get_plugin('argument default', $id);
if ($plugin) {
if ($plugin
->access() || $this->options['default_argument_type'] == $id) {
$form['argument_default'][$id] = array(
'#type' => 'item',
'#input' => TRUE,
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'radio:options[default_action]' => array(
'default',
),
'radio:options[default_argument_type]' => array(
$id,
),
),
'#dependency_count' => 2,
);
$options[$id] = $info['title'];
$plugin
->options_form($form['argument_default'][$id], $form_state);
}
}
}
$form['default_options_div_suffix'] = array(
'#value' => '</fieldset></div>',
);
asort($options);
$form['default_argument_type']['#options'] = $options;
}
function default_action($info = NULL) {
if (!isset($info)) {
$info = $this
->default_actions($this->options['default_action']);
}
if (!$info) {
return FALSE;
}
if (!empty($info['method args'])) {
return call_user_func_array(array(
&$this,
$info['method'],
), $info['method args']);
}
else {
return $this
->{$info['method']}();
}
}
function validate_fail() {
$info = $this
->default_actions($this->options['validate_fail']);
return $this
->default_action($info);
}
function default_ignore() {
return TRUE;
}
function default_not_found() {
$this->view->build_info['fail'] = TRUE;
return FALSE;
}
function default_empty() {
$this->view->built = TRUE;
$this->view->executed = TRUE;
$this->view->result = array();
return FALSE;
}
function default_default() {
return TRUE;
}
function has_default_argument() {
$info = $this
->default_actions($this->options['default_action']);
return !empty($info['has default argument']);
}
function get_default_argument() {
$plugin = $this
->get_plugin('argument default');
if ($plugin) {
return $plugin
->get_argument();
}
}
function default_summary($order, $by = NULL) {
$this->view->build_info['summary'] = TRUE;
$this->view->build_info['summary_level'] = $this->options['id'];
$this->view->plugin_name = $this->options['style_plugin'];
$this->view->style_options = $this->options['style_options'];
$this->query
->clear_fields();
$this
->summary_query();
$this
->summary_sort($order, $by);
$this->view->build_sort = $this->view->build_fields = FALSE;
return TRUE;
}
function summary_query() {
$this
->ensure_my_table();
$this->base_alias = $this->query
->add_field($this->table_alias, $this->real_field);
$this
->summary_name_field();
return $this
->summary_basics();
}
function summary_name_field() {
if (isset($this->name_table)) {
if ($this->table_alias != $this->name_table) {
$j = views_get_table_join($this->name_table, $this->table);
if ($j) {
$join = drupal_clone($j);
$join->left_table = $this->table_alias;
$this->name_table_alias = $this->query
->add_table($this->name_table, $this->relationship, $join);
}
}
else {
$this->name_table_alias = $this->query
->ensure_table($this->name_table, $this->relationship);
}
}
else {
$this->name_table_alias = $this->table_alias;
}
if (isset($this->name_field)) {
$this->name_alias = $this->query
->add_field($this->name_table_alias, $this->name_field);
}
else {
$this->name_alias = $this->base_alias;
}
}
function summary_basics($count_field = TRUE) {
$field = $this->query->base_table . '.' . $this->query->base_field;
$distinct = $this->view->display_handler
->get_option('distinct') && empty($this->query->no_distinct);
$count_alias = $this->query
->add_field(NULL, $field, 'num_records', array(
'count' => TRUE,
'distinct' => $distinct,
));
$this->query
->add_groupby($this->name_alias);
if ($count_field) {
$this->query
->set_count_field($this->table_alias, $this->real_field);
}
$this->count_alias = $count_alias;
}
function summary_sort($order, $by = NULL) {
$this->query
->add_orderby(NULL, NULL, $order, !empty($by) ? $by : $this->name_alias);
}
function summary_argument($data) {
return $data->{$this->base_alias};
}
function summary_name($data) {
$value = $data->{$this->name_alias};
if (empty($value) && !empty($this->definition['empty field name'])) {
$value = $this->definition['empty field name'];
}
return check_plain($value);
}
function query() {
$this
->ensure_my_table();
$placeholder = empty($this->definition['numeric']) ? "'%s'" : '%d';
$this->query
->add_where(0, "{$this->table_alias}.{$this->real_field} = {$placeholder}", $this->argument);
}
function title() {
return check_plain($this->argument);
}
function get_title() {
if (isset($this->validated_title)) {
return $this->validated_title;
}
else {
return $this
->title();
}
}
function validate_arg($arg) {
if (isset($this->argument_validated)) {
return $this->argument_validated;
}
if ($this
->is_wildcard($arg)) {
return $this->argument_validated = TRUE;
}
if ($this->options['validate_type'] == 'none') {
return $this->argument_validated = $this
->validate_argument_basic($arg);
}
$plugin = $this
->get_plugin('argument validator');
if ($plugin) {
return $this->argument_validated = $plugin
->validate_argument($arg);
}
return $this->argument_validated = $this
->validate_argument_basic($arg);
}
function validate_argument($arg) {
$validate_info = $this
->default_actions($this->options['validate_fail']);
if (empty($validate_info['hard fail'])) {
return TRUE;
}
$rc = $this
->validate_arg($arg);
$validate_info = $this
->default_actions($this->options['validate_fail']);
if (empty($validate_info['hard fail'])) {
return TRUE;
}
return $rc;
}
function validate_argument_basic($arg) {
if (!isset($arg) || $arg === '') {
return FALSE;
}
if (!empty($this->definition['numeric']) && !isset($this->options['break_phrase']) && !is_numeric($arg)) {
return FALSE;
}
return TRUE;
}
function set_argument($arg) {
$this->argument = $arg;
return $this
->validate_arg($arg);
}
function get_value() {
if (isset($this->argument)) {
return $this->argument;
}
$value = NULL;
$position = 0;
foreach ($this->view->argument as $id => $argument) {
if ($id == $this->options['id']) {
break;
}
$position++;
}
$arg = isset($this->view->args[$position]) ? $this->view->args[$position] : NULL;
$this->position = $position;
$argument = drupal_clone($this);
if (!isset($arg) && $argument
->has_default_argument()) {
$arg = $argument
->get_default_argument();
$this->is_default = TRUE;
}
if ($argument
->set_argument($arg)) {
$value = $argument->argument;
}
unset($argument);
return $value;
}
function export_style($indent, $prefix, $storage, $option, $definition, $parents) {
$output = '';
$name = $storage[$option];
$options = $storage['style_options'];
$plugin = views_get_plugin('style', $name);
if ($plugin) {
$plugin
->init($this->view, $this->display, $options);
$output .= $indent . $prefix . "['{$option}'] = '{$name}';\n";
$output .= $plugin
->export_options($indent, $prefix . "['style_options']");
}
return $output;
}
function export_plugin($indent, $prefix, $storage, $option, $definition, $parents) {
$output = '';
if ($option == 'default_argument_type') {
$type = 'argument default';
$option_name = 'default_argument_options';
}
else {
$type = 'argument validator';
$option_name = 'validate_options';
}
$plugin = $this
->get_plugin($type);
$name = $this->options[$option];
if ($plugin) {
$output .= $indent . $prefix . "['{$option}'] = '{$name}';\n";
$output .= $plugin
->export_options($indent, $prefix . "['{$option_name}']");
}
return $output;
}
function get_plugin($type = 'argument default', $name = NULL) {
$options = array();
switch ($type) {
case 'argument default':
$plugin_name = 'default_argument_type';
$options_name = 'default_argument_options';
break;
case 'argument validator':
$plugin_name = 'validate_type';
$options_name = 'validate_options';
}
if (!$name) {
$name = $this->options[$plugin_name];
}
if ($name == $this->options[$plugin_name]) {
$options = $this->options[$options_name];
}
$plugin = views_get_plugin($type, $name);
if ($plugin) {
$plugin
->init($this->view, $this, $options);
return $plugin;
}
}
}
class views_handler_argument_broken extends views_handler_argument {
function ui_name($short = FALSE) {
return t('Broken/missing handler');
}
function ensure_my_table() {
}
function query() {
}
function options_form(&$form, &$form_state) {
$form['markup'] = array(
'#prefix' => '<div class="form-item description">',
'#value' => t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.'),
);
}
function broken() {
return TRUE;
}
}