first import
This commit is contained in:
133
sites/all/modules/views/handlers/views_handler_area.inc
Normal file
133
sites/all/modules/views/handlers/views_handler_area.inc
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Views area handlers.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup views_area_handlers Views area handlers
|
||||
* @{
|
||||
* Handlers to tell Views what can display in header, footer
|
||||
* and empty text in a view.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base class for area handlers.
|
||||
*
|
||||
* @ingroup views_area_handlers
|
||||
*/
|
||||
class views_handler_area extends views_handler {
|
||||
|
||||
/**
|
||||
* Overrides views_handler::init().
|
||||
*
|
||||
* Make sure that no result area handlers are set to be shown when the result
|
||||
* is empty.
|
||||
*/
|
||||
function init(&$view, &$options) {
|
||||
parent::init($view, $options);
|
||||
if ($this->handler_type == 'empty') {
|
||||
$this->options['empty'] = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this field's label.
|
||||
*/
|
||||
function label() {
|
||||
if (!isset($this->options['label'])) {
|
||||
return $this->ui_name();
|
||||
}
|
||||
return $this->options['label'];
|
||||
}
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$this->definition['field'] = !empty($this->definition['field']) ? $this->definition['field'] : '';
|
||||
$label = !empty($this->definition['label']) ? $this->definition['label'] : $this->definition['field'];
|
||||
$options['label'] = array('default' => $label, 'translatable' => TRUE);
|
||||
$options['empty'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide extra data to the administration form
|
||||
*/
|
||||
function admin_summary() {
|
||||
return $this->label();
|
||||
}
|
||||
|
||||
/**
|
||||
* Default options form that provides the label widget that all fields
|
||||
* should have.
|
||||
*/
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
$form['label'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Label'),
|
||||
'#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
|
||||
'#description' => t('The label for this area that will be displayed only administratively.'),
|
||||
);
|
||||
|
||||
if ($form_state['type'] != 'empty') {
|
||||
$form['empty'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Display even if view has no result'),
|
||||
'#default_value' => isset($this->options['empty']) ? $this->options['empty'] : 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't run a query
|
||||
*/
|
||||
function query() { }
|
||||
|
||||
/**
|
||||
* Render the area
|
||||
*/
|
||||
function render($empty = FALSE) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Area handlers shouldn't have groupby.
|
||||
*/
|
||||
function use_group_by() {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A special handler to take the place of missing or broken handlers.
|
||||
*
|
||||
* @ingroup views_area_handlers
|
||||
*/
|
||||
class views_handler_area_broken extends views_handler_area {
|
||||
function ui_name($short = FALSE) {
|
||||
return t('Broken/missing handler');
|
||||
}
|
||||
|
||||
function ensure_my_table() { /* No table to ensure! */ }
|
||||
function query($group_by = FALSE) { /* No query to run */ }
|
||||
function render($empty = FALSE) { return ''; }
|
||||
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.'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the handler is considered 'broken'
|
||||
*/
|
||||
function broken() { return TRUE; }
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_area_result.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Views area handler to display some configurable result summary.
|
||||
*
|
||||
* @ingroup views_area_handlers
|
||||
*/
|
||||
class views_handler_area_result extends views_handler_area {
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['content'] = array(
|
||||
'default' => 'Displaying @start - @end of @total',
|
||||
'translatable' => TRUE,
|
||||
);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
$variables = array(
|
||||
'items' => array(
|
||||
'@start -- the initial record number in the set',
|
||||
'@end -- the last record number in the set',
|
||||
'@total -- the total records in the set',
|
||||
'@name -- the human-readable name of the view',
|
||||
'@per_page -- the number of items per page',
|
||||
'@current_page -- the current page number',
|
||||
'@current_record_count -- the current page record count',
|
||||
'@page_count -- the total page count',
|
||||
),
|
||||
);
|
||||
$list = theme('item_list', $variables);
|
||||
$form['content'] = array(
|
||||
'#title' => t('Display'),
|
||||
'#type' => 'textarea',
|
||||
'#rows' => 3,
|
||||
'#default_value' => $this->options['content'],
|
||||
'#description' => t('You may use HTML code in this field. The following tokens are supported:') . $list,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Find out the information to render.
|
||||
*/
|
||||
function render($empty = FALSE) {
|
||||
// Must have options and does not work on summaries.
|
||||
if (!isset($this->options['content']) || $this->view->plugin_name == 'default_summary') {
|
||||
return;
|
||||
}
|
||||
$output = '';
|
||||
$format = $this->options['content'];
|
||||
// Calculate the page totals.
|
||||
$current_page = (int) $this->view->get_current_page() + 1;
|
||||
$per_page = (int) $this->view->get_items_per_page();
|
||||
$count = count($this->view->result);
|
||||
// @TODO: Maybe use a possible is views empty functionality.
|
||||
// Not every view has total_rows set, use view->result instead.
|
||||
$total = isset($this->view->total_rows) ? $this->view->total_rows : count($this->view->result);
|
||||
$name = check_plain($this->view->human_name);
|
||||
if ($per_page === 0) {
|
||||
$page_count = 1;
|
||||
$start = 1;
|
||||
$end = $total;
|
||||
}
|
||||
else {
|
||||
$page_count = (int) ceil($total / $per_page);
|
||||
$total_count = $current_page * $per_page;
|
||||
if ($total_count > $total) {
|
||||
$total_count = $total;
|
||||
}
|
||||
$start = ($current_page - 1) * $per_page + 1;
|
||||
$end = $total_count;
|
||||
}
|
||||
$current_record_count = ($end - $start) + 1;
|
||||
// Get the search information.
|
||||
$items = array('start', 'end', 'total', 'name', 'per_page', 'current_page', 'current_record_count', 'page_count');
|
||||
$replacements = array();
|
||||
foreach ($items as $item) {
|
||||
$replacements["@$item"] = ${$item};
|
||||
}
|
||||
// Send the output.
|
||||
if (!empty($total)) {
|
||||
$output .= filter_xss_admin(str_replace(array_keys($replacements), array_values($replacements), $format));
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
}
|
110
sites/all/modules/views/handlers/views_handler_area_text.inc
Normal file
110
sites/all/modules/views/handlers/views_handler_area_text.inc
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_area_text.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Views area text handler.
|
||||
*
|
||||
* @ingroup views_area_handlers
|
||||
*/
|
||||
class views_handler_area_text extends views_handler_area {
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
$options['content'] = array('default' => '', 'translatable' => TRUE, 'format_key' => 'format');
|
||||
$options['format'] = array('default' => NULL);
|
||||
$options['tokenize'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
$form['content'] = array(
|
||||
'#type' => 'text_format',
|
||||
'#default_value' => $this->options['content'],
|
||||
'#rows' => 6,
|
||||
'#format' => isset($this->options['format']) ? $this->options['format'] : filter_default_format(),
|
||||
'#wysiwyg' => FALSE,
|
||||
);
|
||||
|
||||
// @TODO: Refactor token handling into a base class.
|
||||
$form['tokenize'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Use replacement tokens from the first row'),
|
||||
'#default_value' => $this->options['tokenize'],
|
||||
);
|
||||
|
||||
// Get a list of the available fields and arguments for token replacement.
|
||||
$options = array();
|
||||
foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
|
||||
$options[t('Fields')]["[$field]"] = $handler->ui_name();
|
||||
}
|
||||
|
||||
$count = 0; // This lets us prepare the key as we want it printed.
|
||||
foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
|
||||
$options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
|
||||
$options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
|
||||
}
|
||||
|
||||
if (!empty($options)) {
|
||||
$output = '<p>' . t('The following tokens are available. If you would like to have the characters \'[\' and \']\' please use the html entity codes \'%5B\' or \'%5D\' or they will get replaced with empty space.' . '</p>');
|
||||
foreach (array_keys($options) as $type) {
|
||||
if (!empty($options[$type])) {
|
||||
$items = array();
|
||||
foreach ($options[$type] as $key => $value) {
|
||||
$items[] = $key . ' == ' . $value;
|
||||
}
|
||||
$output .= theme('item_list',
|
||||
array(
|
||||
'items' => $items,
|
||||
'type' => $type
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
$form['token_help'] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => t('Replacement patterns'),
|
||||
'#collapsible' => TRUE,
|
||||
'#collapsed' => TRUE,
|
||||
'#value' => $output,
|
||||
'#id' => 'edit-options-token-help',
|
||||
'#dependency' => array(
|
||||
'edit-options-tokenize' => array(1),
|
||||
),
|
||||
'#prefix' => '<div>',
|
||||
'#suffix' => '</div>',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function options_submit(&$form, &$form_state) {
|
||||
$form_state['values']['options']['format'] = $form_state['values']['options']['content']['format'];
|
||||
$form_state['values']['options']['content'] = $form_state['values']['options']['content']['value'];
|
||||
parent::options_submit($form, $form_state);
|
||||
}
|
||||
|
||||
function render($empty = FALSE) {
|
||||
$format = isset($this->options['format']) ? $this->options['format'] : filter_default_format();
|
||||
if (!$empty || !empty($this->options['empty'])) {
|
||||
return $this->render_textarea($this->options['content'], $format);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a text area, using the proper format.
|
||||
*/
|
||||
function render_textarea($value, $format) {
|
||||
if ($value) {
|
||||
if ($this->options['tokenize']) {
|
||||
$value = $this->view->style_plugin->tokenize_value($value, 0);
|
||||
}
|
||||
return check_markup($value, $format, '', FALSE);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_area_text_custom.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Views area text custom handler.
|
||||
*
|
||||
* @ingroup views_area_handlers
|
||||
*/
|
||||
class views_handler_area_text_custom extends views_handler_area_text {
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
unset($options['format']);
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
// Alter the form element, to be a regular text area.
|
||||
$form['content']['#type'] = 'textarea';
|
||||
unset($form['content']['#format']);
|
||||
unset($form['content']['#wysiwyg']);
|
||||
|
||||
// @TODO: Use the token refactored base class.
|
||||
}
|
||||
|
||||
// Empty, so we don't inherit options_submit from the parent.
|
||||
function options_submit(&$form, &$form_state) {
|
||||
}
|
||||
|
||||
function render($empty = FALSE) {
|
||||
if (!$empty || !empty($this->options['empty'])) {
|
||||
return $this->render_textarea_custom($this->options['content']);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a text area with filter_xss_admin.
|
||||
*/
|
||||
function render_textarea_custom($value) {
|
||||
if ($value) {
|
||||
if ($this->options['tokenize']) {
|
||||
$value = $this->view->style_plugin->tokenize_value($value, 0);
|
||||
}
|
||||
return $this->sanitize_value($value, 'xss_admin');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
83
sites/all/modules/views/handlers/views_handler_area_view.inc
Normal file
83
sites/all/modules/views/handlers/views_handler_area_view.inc
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_area_view.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Views area handlers. Insert a view inside of an area.
|
||||
*
|
||||
* @ingroup views_area_handlers
|
||||
*/
|
||||
class views_handler_area_view extends views_handler_area {
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['view_to_insert'] = array('default' => '');
|
||||
$options['inherit_arguments'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default options form that provides the label widget that all fields
|
||||
* should have.
|
||||
*/
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
$view_display = $this->view->name . ':' . $this->view->current_display;
|
||||
|
||||
$options = array('' => t('-Select-'));
|
||||
$options += views_get_views_as_options(FALSE, 'all', $view_display, FALSE, TRUE);
|
||||
$form['view_to_insert'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('View to insert'),
|
||||
'#default_value' => $this->options['view_to_insert'],
|
||||
'#description' => t('The view to insert into this area.'),
|
||||
'#options' => $options,
|
||||
);
|
||||
|
||||
$form['inherit_arguments'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Inherit contextual filters'),
|
||||
'#default_value' => $this->options['inherit_arguments'],
|
||||
'#description' => t('If checked, this view will receive the same contextual filters as its parent.'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the area
|
||||
*/
|
||||
function render($empty = FALSE) {
|
||||
if (!empty($this->options['view_to_insert'])) {
|
||||
list($view_name, $display_id) = explode(':', $this->options['view_to_insert']);
|
||||
|
||||
$view = views_get_view($view_name);
|
||||
if (empty($view) || !$view->access($display_id)) {
|
||||
return;
|
||||
}
|
||||
$view->set_display($display_id);
|
||||
|
||||
// Avoid recursion
|
||||
$view->parent_views += $this->view->parent_views;
|
||||
$view->parent_views[] = "$view_name:$display_id";
|
||||
|
||||
// Check if the view is part of the parent views of this view
|
||||
$search = "$view_name:$display_id";
|
||||
if (in_array($search, $this->view->parent_views)) {
|
||||
drupal_set_message(t("Recursion detected in view @view display @display.", array('@view' => $view_name, '@display' => $display_id)), 'error');
|
||||
}
|
||||
else {
|
||||
if (!empty($this->options['inherit_arguments']) && !empty($this->view->args)) {
|
||||
return $view->preview($display_id, $this->view->args);
|
||||
}
|
||||
else {
|
||||
return $view->preview($display_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
1240
sites/all/modules/views/handlers/views_handler_argument.inc
Normal file
1240
sites/all/modules/views/handlers/views_handler_argument.inc
Normal file
File diff suppressed because it is too large
Load Diff
101
sites/all/modules/views/handlers/views_handler_argument_date.inc
Normal file
101
sites/all/modules/views/handlers/views_handler_argument_date.inc
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_argument_date.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Abstract argument handler for dates.
|
||||
*
|
||||
* Adds an option to set a default argument based on the current date.
|
||||
*
|
||||
* @param $arg_format
|
||||
* The format string to use on the current time when
|
||||
* creating a default date argument.
|
||||
*
|
||||
* Definitions terms:
|
||||
* - many to one: If true, the "many to one" helper will be used.
|
||||
* - invalid input: A string to give to the user for obviously invalid input.
|
||||
* This is deprecated in favor of argument validators.
|
||||
*
|
||||
* @see views_many_to_one_helper()
|
||||
*
|
||||
* @ingroup views_argument_handlers
|
||||
*/
|
||||
class views_handler_argument_date extends views_handler_argument_formula {
|
||||
var $option_name = 'default_argument_date';
|
||||
var $arg_format = 'Y-m-d';
|
||||
|
||||
/**
|
||||
* Add an option to set the default value to the current date.
|
||||
*/
|
||||
function default_argument_form(&$form, &$form_state) {
|
||||
parent::default_argument_form($form, $form_state);
|
||||
$form['default_argument_type']['#options'] += array('date' => t('Current date'));
|
||||
$form['default_argument_type']['#options'] += array('node_created' => t("Current node's creation time"));
|
||||
$form['default_argument_type']['#options'] += array('node_changed' => t("Current node's update time")); }
|
||||
|
||||
/**
|
||||
* Set the empty argument value to the current date,
|
||||
* formatted appropriately for this argument.
|
||||
*/
|
||||
function get_default_argument($raw = FALSE) {
|
||||
if (!$raw && $this->options['default_argument_type'] == 'date') {
|
||||
return date($this->arg_format, REQUEST_TIME);
|
||||
}
|
||||
else if (!$raw && in_array($this->options['default_argument_type'], array('node_created', 'node_changed'))) {
|
||||
foreach (range(1, 3) as $i) {
|
||||
$node = menu_get_object('node', $i);
|
||||
if (!empty($node)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (arg(0) == 'node' && is_numeric(arg(1))) {
|
||||
$node = node_load(arg(1));
|
||||
}
|
||||
|
||||
if (empty($node)) {
|
||||
return parent::get_default_argument();
|
||||
}
|
||||
elseif ($this->options['default_argument_type'] == 'node_created') {
|
||||
return date($this->arg_format, $node->created);
|
||||
}
|
||||
elseif ($this->options['default_argument_type'] == 'node_changed') {
|
||||
return date($this->arg_format, $node->changed);
|
||||
}
|
||||
}
|
||||
|
||||
return parent::get_default_argument($raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* The date handler provides some default argument types, which aren't argument default plugins,
|
||||
* so addapt the export mechanism.
|
||||
*/
|
||||
function export_plugin($indent, $prefix, $storage, $option, $definition, $parents) {
|
||||
|
||||
// Only use a special behaviour for the special argument types, else just
|
||||
// use the default behaviour.
|
||||
if ($option == 'default_argument_type') {
|
||||
$type = 'argument default';
|
||||
$option_name = 'default_argument_options';
|
||||
|
||||
$plugin = $this->get_plugin($type);
|
||||
$name = $this->options[$option];
|
||||
if (in_array($name, array('date', 'node_created', 'node_changed'))) {
|
||||
|
||||
// Write which plugin to use.
|
||||
$output = $indent . $prefix . "['$option'] = '$name';\n";
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
return parent::export_plugin($indent, $prefix, $storage, $option, $definition, $parents);
|
||||
}
|
||||
|
||||
|
||||
function get_sort_name() {
|
||||
return t('Date', array(), array('context' => 'Sort order'));
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_argument_formula.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Abstract argument handler for simple formulae.
|
||||
*
|
||||
* Child classes of this object should implement summary_argument, at least.
|
||||
*
|
||||
* Definition terms:
|
||||
* - formula: The formula to use for this handler.
|
||||
*
|
||||
* @ingroup views_argument_handlers
|
||||
*/
|
||||
class views_handler_argument_formula extends views_handler_argument {
|
||||
var $formula = NULL;
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
function construct() {
|
||||
parent::construct();
|
||||
|
||||
if (!empty($this->definition['formula'])) {
|
||||
$this->formula = $this->definition['formula'];
|
||||
}
|
||||
}
|
||||
|
||||
function get_formula() {
|
||||
return str_replace('***table***', $this->table_alias, $this->formula);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the summary query based on a formula
|
||||
*/
|
||||
function summary_query() {
|
||||
$this->ensure_my_table();
|
||||
// Now that our table is secure, get our formula.
|
||||
$formula = $this->get_formula();
|
||||
|
||||
// Add the field.
|
||||
$this->base_alias = $this->name_alias = $this->query->add_field(NULL, $formula, $this->field);
|
||||
$this->query->set_count_field(NULL, $formula, $this->field);
|
||||
|
||||
return $this->summary_basics(FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the query based upon the formula
|
||||
*/
|
||||
function query($group_by = FALSE) {
|
||||
$this->ensure_my_table();
|
||||
// Now that our table is secure, get our formula.
|
||||
$placeholder = $this->placeholder();
|
||||
$formula = $this->get_formula() .' = ' . $placeholder;
|
||||
$placeholders = array(
|
||||
$placeholder => $this->argument,
|
||||
);
|
||||
$this->query->add_where(0, $formula, $placeholders, 'formula');
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_argument_group_by_numeric.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple handler for arguments using group by.
|
||||
*
|
||||
* @ingroup views_argument_handlers
|
||||
*/
|
||||
class views_handler_argument_group_by_numeric extends views_handler_argument {
|
||||
function query($group_by = FALSE) {
|
||||
$this->ensure_my_table();
|
||||
$field = $this->get_field();
|
||||
$placeholder = $this->placeholder();
|
||||
|
||||
$this->query->add_having_expression(0, "$field = $placeholder", array($placeholder => $this->argument));
|
||||
}
|
||||
|
||||
function ui_name($short = FALSE) {
|
||||
return $this->get_field(parent::ui_name($short));
|
||||
}
|
||||
|
||||
function get_sort_name() {
|
||||
return t('Numerical', array(), array('context' => 'Sort order'));
|
||||
}
|
||||
}
|
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_argument_many_to_one.
|
||||
*/
|
||||
|
||||
/**
|
||||
* An argument handler for use in fields that have a many to one relationship
|
||||
* with the table(s) to the left. This adds a bunch of options that are
|
||||
* reasonably common with this type of relationship.
|
||||
* Definition terms:
|
||||
* - numeric: If true, the field will be considered numeric. Probably should
|
||||
* always be set TRUE as views_handler_argument_string has many to one
|
||||
* capabilities.
|
||||
* - zero is null: If true, a 0 will be handled as empty, so for example
|
||||
* a default argument can be provided or a summary can be shown.
|
||||
*
|
||||
* @ingroup views_argument_handlers
|
||||
*/
|
||||
class views_handler_argument_many_to_one extends views_handler_argument {
|
||||
function init(&$view, &$options) {
|
||||
parent::init($view, $options);
|
||||
$this->helper = new views_many_to_one_helper($this);
|
||||
|
||||
// Ensure defaults for these, during summaries and stuff:
|
||||
$this->operator = 'or';
|
||||
$this->value = array();
|
||||
}
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
if (!empty($this->definition['numeric'])) {
|
||||
$options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
}
|
||||
|
||||
$options['add_table'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
$options['require_value'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
|
||||
if (isset($this->helper)) {
|
||||
$this->helper->option_definition($options);
|
||||
}
|
||||
else {
|
||||
$helper = new views_many_to_one_helper($this);
|
||||
$helper->option_definition($options);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
// allow + for or, , for and
|
||||
if (!empty($this->definition['numeric'])) {
|
||||
$form['break_phrase'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Allow multiple values'),
|
||||
'#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
|
||||
'#default_value' => !empty($this->options['break_phrase']),
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
}
|
||||
|
||||
$form['add_table'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Allow multiple filter values to work together'),
|
||||
'#description' => t('If selected, multiple instances of this filter can work together, as though multiple values were supplied to the same filter. This setting is not compatible with the "Reduce duplicates" setting.'),
|
||||
'#default_value' => !empty($this->options['add_table']),
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
|
||||
$form['require_value'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Do not display items with no value in summary'),
|
||||
'#default_value' => !empty($this->options['require_value']),
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
|
||||
$this->helper->options_form($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override ensure_my_table so we can control how this joins in.
|
||||
* The operator actually has influence over joining.
|
||||
*/
|
||||
function ensure_my_table() {
|
||||
$this->helper->ensure_my_table();
|
||||
}
|
||||
|
||||
function query($group_by = FALSE) {
|
||||
$empty = FALSE;
|
||||
if (isset($this->definition['zero is null']) && $this->definition['zero is null']) {
|
||||
if (empty($this->argument)) {
|
||||
$empty = TRUE;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!isset($this->argument)) {
|
||||
$empty = TRUE;
|
||||
}
|
||||
}
|
||||
if ($empty) {
|
||||
parent::ensure_my_table();
|
||||
$this->query->add_where(0, "$this->table_alias.$this->real_field", NULL, 'IS NULL');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($this->options['break_phrase'])) {
|
||||
views_break_phrase($this->argument, $this);
|
||||
}
|
||||
else {
|
||||
$this->value = array($this->argument);
|
||||
$this->operator = 'or';
|
||||
}
|
||||
|
||||
$this->helper->add_filter();
|
||||
}
|
||||
|
||||
function title() {
|
||||
if (!$this->argument) {
|
||||
return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
|
||||
}
|
||||
|
||||
if (!empty($this->options['break_phrase'])) {
|
||||
views_break_phrase($this->argument, $this);
|
||||
}
|
||||
else {
|
||||
$this->value = array($this->argument);
|
||||
$this->operator = 'or';
|
||||
}
|
||||
|
||||
// @todo -- both of these should check definition for alternate keywords.
|
||||
|
||||
if (empty($this->value)) {
|
||||
return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
|
||||
}
|
||||
|
||||
if ($this->value === array(-1)) {
|
||||
return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
|
||||
}
|
||||
|
||||
return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
|
||||
}
|
||||
|
||||
function summary_query() {
|
||||
$field = $this->table . '.' . $this->field;
|
||||
$join = $this->get_join();
|
||||
|
||||
if (!empty($this->options['require_value'])) {
|
||||
$join->type = 'INNER';
|
||||
}
|
||||
|
||||
if (empty($this->options['add_table']) || empty($this->view->many_to_one_tables[$field])) {
|
||||
$this->table_alias = $this->query->ensure_table($this->table, $this->relationship, $join);
|
||||
}
|
||||
else {
|
||||
$this->table_alias = $this->helper->summary_join();
|
||||
}
|
||||
|
||||
// Add the field.
|
||||
$this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
|
||||
|
||||
$this->summary_name_field();
|
||||
|
||||
return $this->summary_basics();
|
||||
}
|
||||
|
||||
function summary_argument($data) {
|
||||
$value = $data->{$this->base_alias};
|
||||
if (empty($value)) {
|
||||
$value = 0;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override for specific title lookups.
|
||||
*/
|
||||
function title_query() {
|
||||
return $this->value;
|
||||
}
|
||||
}
|
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_argument_null.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Argument handler that ignores the argument.
|
||||
*
|
||||
* @ingroup views_argument_handlers
|
||||
*/
|
||||
class views_handler_argument_null extends views_handler_argument {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
$options['must_not_be'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override options_form() so that only the relevant options
|
||||
* are displayed to the user.
|
||||
*/
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
$form['must_not_be'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Fail basic validation if any argument is given'),
|
||||
'#default_value' => !empty($this->options['must_not_be']),
|
||||
'#description' => t('By checking this field, you can use this to make sure views with more arguments than necessary fail validation.'),
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
|
||||
unset($form['exception']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override default_actions() to remove actions that don't
|
||||
* make sense for a null argument.
|
||||
*/
|
||||
function default_actions($which = NULL) {
|
||||
if ($which) {
|
||||
if (in_array($which, array('ignore', 'not found', 'empty', 'default'))) {
|
||||
return parent::default_actions($which);
|
||||
}
|
||||
return;
|
||||
}
|
||||
$actions = parent::default_actions();
|
||||
unset($actions['summary asc']);
|
||||
unset($actions['summary desc']);
|
||||
return $actions;
|
||||
}
|
||||
|
||||
function validate_argument_basic($arg) {
|
||||
if (!empty($this->options['must_not_be'])) {
|
||||
return !isset($arg);
|
||||
}
|
||||
|
||||
return parent::validate_argument_basic($arg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the behavior of query() to prevent the query
|
||||
* from being changed in any way.
|
||||
*/
|
||||
function query($group_by = FALSE) {}
|
||||
}
|
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_argument_numeric.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Basic argument handler for arguments that are numeric. Incorporates
|
||||
* break_phrase.
|
||||
*
|
||||
* @ingroup views_argument_handlers
|
||||
*/
|
||||
class views_handler_argument_numeric extends views_handler_argument {
|
||||
/**
|
||||
* The operator used for the query: or|and.
|
||||
* @var string
|
||||
*/
|
||||
var $operator;
|
||||
|
||||
/**
|
||||
* The actual value which is used for querying.
|
||||
* @var array
|
||||
*/
|
||||
var $value;
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
$options['not'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
// allow + for or, , for and
|
||||
$form['break_phrase'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Allow multiple values'),
|
||||
'#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
|
||||
'#default_value' => !empty($this->options['break_phrase']),
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
|
||||
$form['not'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Exclude'),
|
||||
'#description' => t('If selected, the numbers entered for the filter will be excluded rather than limiting the view.'),
|
||||
'#default_value' => !empty($this->options['not']),
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
}
|
||||
|
||||
function title() {
|
||||
if (!$this->argument) {
|
||||
return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
|
||||
}
|
||||
|
||||
if (!empty($this->options['break_phrase'])) {
|
||||
views_break_phrase($this->argument, $this);
|
||||
}
|
||||
else {
|
||||
$this->value = array($this->argument);
|
||||
$this->operator = 'or';
|
||||
}
|
||||
|
||||
if (empty($this->value)) {
|
||||
return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
|
||||
}
|
||||
|
||||
if ($this->value === array(-1)) {
|
||||
return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
|
||||
}
|
||||
|
||||
return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
|
||||
}
|
||||
|
||||
/**
|
||||
* Override for specific title lookups.
|
||||
* @return array
|
||||
* Returns all titles, if it's just one title it's an array with one entry.
|
||||
*/
|
||||
function title_query() {
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
function query($group_by = FALSE) {
|
||||
$this->ensure_my_table();
|
||||
|
||||
if (!empty($this->options['break_phrase'])) {
|
||||
views_break_phrase($this->argument, $this);
|
||||
}
|
||||
else {
|
||||
$this->value = array($this->argument);
|
||||
}
|
||||
|
||||
$placeholder = $this->placeholder();
|
||||
$null_check = empty($this->options['not']) ? '' : "OR $this->table_alias.$this->real_field IS NULL";
|
||||
|
||||
if (count($this->value) > 1) {
|
||||
$operator = empty($this->options['not']) ? 'IN' : 'NOT IN';
|
||||
$this->query->add_where_expression(0, "$this->table_alias.$this->real_field $operator($placeholder) $null_check", array($placeholder => $this->value));
|
||||
}
|
||||
else {
|
||||
$operator = empty($this->options['not']) ? '=' : '!=';
|
||||
$this->query->add_where_expression(0, "$this->table_alias.$this->real_field $operator $placeholder $null_check", array($placeholder => $this->argument));
|
||||
}
|
||||
}
|
||||
|
||||
function get_sort_name() {
|
||||
return t('Numerical', array(), array('context' => 'Sort order'));
|
||||
}
|
||||
}
|
@@ -0,0 +1,274 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_argument_string.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Basic argument handler to implement string arguments that may have length
|
||||
* limits.
|
||||
*
|
||||
* @ingroup views_argument_handlers
|
||||
*/
|
||||
class views_handler_argument_string extends views_handler_argument {
|
||||
function init(&$view, &$options) {
|
||||
parent::init($view, $options);
|
||||
if (!empty($this->definition['many to one'])) {
|
||||
$this->helper = new views_many_to_one_helper($this);
|
||||
|
||||
// Ensure defaults for these, during summaries and stuff:
|
||||
$this->operator = 'or';
|
||||
$this->value = array();
|
||||
}
|
||||
}
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['glossary'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
$options['limit'] = array('default' => 0);
|
||||
$options['case'] = array('default' => 'none');
|
||||
$options['path_case'] = array('default' => 'none');
|
||||
$options['transform_dash'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
$options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
|
||||
if (!empty($this->definition['many to one'])) {
|
||||
$options['add_table'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
$options['require_value'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
$form['glossary'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Glossary mode'),
|
||||
'#description' => t('Glossary mode applies a limit to the number of characters used in the filter value, which allows the summary view to act as a glossary.'),
|
||||
'#default_value' => $this->options['glossary'],
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
|
||||
$form['limit'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Character limit'),
|
||||
'#description' => t('How many characters of the filter value to filter against. If set to 1, all fields starting with the first letter in the filter value would be matched.'),
|
||||
'#default_value' => $this->options['limit'],
|
||||
'#dependency' => array('edit-options-glossary' => array(TRUE)),
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
|
||||
$form['case'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Case'),
|
||||
'#description' => t('When printing the title and summary, how to transform the case of the filter value.'),
|
||||
'#options' => array(
|
||||
'none' => t('No transform'),
|
||||
'upper' => t('Upper case'),
|
||||
'lower' => t('Lower case'),
|
||||
'ucfirst' => t('Capitalize first letter'),
|
||||
'ucwords' => t('Capitalize each word'),
|
||||
),
|
||||
'#default_value' => $this->options['case'],
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
|
||||
$form['path_case'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Case in path'),
|
||||
'#description' => t('When printing url paths, how to transform the case of the filter value. Do not use this unless with Postgres as it uses case sensitive comparisons.'),
|
||||
'#options' => array(
|
||||
'none' => t('No transform'),
|
||||
'upper' => t('Upper case'),
|
||||
'lower' => t('Lower case'),
|
||||
'ucfirst' => t('Capitalize first letter'),
|
||||
'ucwords' => t('Capitalize each word'),
|
||||
),
|
||||
'#default_value' => $this->options['path_case'],
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
|
||||
$form['transform_dash'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Transform spaces to dashes in URL'),
|
||||
'#default_value' => $this->options['transform_dash'],
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
|
||||
if (!empty($this->definition['many to one'])) {
|
||||
$form['add_table'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Allow multiple filter values to work together'),
|
||||
'#description' => t('If selected, multiple instances of this filter can work together, as though multiple values were supplied to the same filter. This setting is not compatible with the "Reduce duplicates" setting.'),
|
||||
'#default_value' => !empty($this->options['add_table']),
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
|
||||
$form['require_value'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Do not display items with no value in summary'),
|
||||
'#default_value' => !empty($this->options['require_value']),
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
}
|
||||
|
||||
// allow + for or, , for and
|
||||
$form['break_phrase'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Allow multiple values'),
|
||||
'#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
|
||||
'#default_value' => !empty($this->options['break_phrase']),
|
||||
'#fieldset' => 'more',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the summary query based on a string
|
||||
*/
|
||||
function summary_query() {
|
||||
if (empty($this->definition['many to one'])) {
|
||||
$this->ensure_my_table();
|
||||
}
|
||||
else {
|
||||
$this->table_alias = $this->helper->summary_join();
|
||||
}
|
||||
|
||||
if (empty($this->options['glossary'])) {
|
||||
// Add the field.
|
||||
$this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
|
||||
$this->query->set_count_field($this->table_alias, $this->real_field);
|
||||
}
|
||||
else {
|
||||
// Add the field.
|
||||
$formula = $this->get_formula();
|
||||
$this->base_alias = $this->query->add_field(NULL, $formula, $this->field . '_truncated');
|
||||
$this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
|
||||
}
|
||||
|
||||
$this->summary_name_field();
|
||||
return $this->summary_basics(FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the formula for this argument.
|
||||
*
|
||||
* $this->ensure_my_table() MUST have been called prior to this.
|
||||
*/
|
||||
function get_formula() {
|
||||
return "SUBSTRING($this->table_alias.$this->real_field, 1, " . intval($this->options['limit']) . ")";
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the query based upon the formula
|
||||
*/
|
||||
function query($group_by = FALSE) {
|
||||
$argument = $this->argument;
|
||||
if (!empty($this->options['transform_dash'])) {
|
||||
$argument = strtr($argument, '-', ' ');
|
||||
}
|
||||
|
||||
if (!empty($this->options['break_phrase'])) {
|
||||
views_break_phrase_string($argument, $this);
|
||||
}
|
||||
else {
|
||||
$this->value = array($argument);
|
||||
$this->operator = 'or';
|
||||
}
|
||||
|
||||
if (!empty($this->definition['many to one'])) {
|
||||
if (!empty($this->options['glossary'])) {
|
||||
$this->helper->formula = TRUE;
|
||||
}
|
||||
$this->helper->ensure_my_table();
|
||||
$this->helper->add_filter();
|
||||
return;
|
||||
}
|
||||
|
||||
$this->ensure_my_table();
|
||||
$formula = FALSE;
|
||||
if (empty($this->options['glossary'])) {
|
||||
$field = "$this->table_alias.$this->real_field";
|
||||
}
|
||||
else {
|
||||
$formula = TRUE;
|
||||
$field = $this->get_formula();
|
||||
}
|
||||
|
||||
if (count($this->value) > 1) {
|
||||
$operator = 'IN';
|
||||
$argument = $this->value;
|
||||
}
|
||||
else {
|
||||
$operator = '=';
|
||||
}
|
||||
|
||||
if ($formula) {
|
||||
$placeholder = $this->placeholder();
|
||||
if ($operator == 'IN') {
|
||||
$field .= " IN($placeholder)";
|
||||
}
|
||||
else {
|
||||
$field .= ' = ' . $placeholder;
|
||||
}
|
||||
$placeholders = array(
|
||||
$placeholder => $argument,
|
||||
);
|
||||
$this->query->add_where_expression(0, $field, $placeholders);
|
||||
}
|
||||
else {
|
||||
$this->query->add_where(0, $field, $argument, $operator);
|
||||
}
|
||||
}
|
||||
|
||||
function summary_argument($data) {
|
||||
$value = $this->case_transform($data->{$this->base_alias}, $this->options['path_case']);
|
||||
if (!empty($this->options['transform_dash'])) {
|
||||
$value = strtr($value, ' ', '-');
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
function get_sort_name() {
|
||||
return t('Alphabetical', array(), array('context' => 'Sort order'));
|
||||
}
|
||||
|
||||
function title() {
|
||||
$this->argument = $this->case_transform($this->argument, $this->options['case']);
|
||||
if (!empty($this->options['transform_dash'])) {
|
||||
$this->argument = strtr($this->argument, '-', ' ');
|
||||
}
|
||||
|
||||
if (!empty($this->options['break_phrase'])) {
|
||||
views_break_phrase_string($this->argument, $this);
|
||||
}
|
||||
else {
|
||||
$this->value = array($this->argument);
|
||||
$this->operator = 'or';
|
||||
}
|
||||
|
||||
if (empty($this->value)) {
|
||||
return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
|
||||
}
|
||||
|
||||
if ($this->value === array(-1)) {
|
||||
return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
|
||||
}
|
||||
|
||||
return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
|
||||
}
|
||||
|
||||
/**
|
||||
* Override for specific title lookups.
|
||||
*/
|
||||
function title_query() {
|
||||
return drupal_map_assoc($this->value, 'check_plain');
|
||||
}
|
||||
|
||||
function summary_name($data) {
|
||||
return $this->case_transform(parent::summary_name($data), $this->options['case']);
|
||||
}
|
||||
|
||||
}
|
1630
sites/all/modules/views/handlers/views_handler_field.inc
Normal file
1630
sites/all/modules/views/handlers/views_handler_field.inc
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_boolean.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A handler to provide proper displays for booleans.
|
||||
*
|
||||
* Allows for display of true/false, yes/no, on/off, enabled/disabled.
|
||||
*
|
||||
* Definition terms:
|
||||
* - output formats: An array where the first entry is displayed on boolean true
|
||||
* and the second is displayed on boolean false. An example for sticky is:
|
||||
* @code
|
||||
* 'output formats' => array(
|
||||
* 'sticky' => array(t('Sticky'), ''),
|
||||
* ),
|
||||
* @endcode
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_boolean extends views_handler_field {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
$options['type'] = array('default' => 'yes-no');
|
||||
$options['not'] = array('definition bool' => 'reverse');
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function init(&$view, &$options) {
|
||||
parent::init($view, $options);
|
||||
|
||||
$default_formats = array(
|
||||
'yes-no' => array(t('Yes'), t('No')),
|
||||
'true-false' => array(t('True'), t('False')),
|
||||
'on-off' => array(t('On'), t('Off')),
|
||||
'enabled-disabled' => array(t('Enabled'), t('Disabled')),
|
||||
'boolean' => array(1, 0),
|
||||
'unicode-yes-no' => array('✔', '✖'),
|
||||
);
|
||||
$output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array();
|
||||
$this->formats = array_merge($default_formats, $output_formats);
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
foreach ($this->formats as $key => $item) {
|
||||
$options[$key] = implode('/', $item);
|
||||
}
|
||||
|
||||
$form['type'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Output format'),
|
||||
'#options' => $options,
|
||||
'#default_value' => $this->options['type'],
|
||||
);
|
||||
$form['not'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Reverse'),
|
||||
'#description' => t('If checked, true will be displayed as false.'),
|
||||
'#default_value' => $this->options['not'],
|
||||
);
|
||||
parent::options_form($form, $form_state);
|
||||
}
|
||||
|
||||
function render($values) {
|
||||
$value = $this->get_value($values);
|
||||
if (!empty($this->options['not'])) {
|
||||
$value = !$value;
|
||||
}
|
||||
|
||||
if (isset($this->formats[$this->options['type']])) {
|
||||
return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
|
||||
}
|
||||
else {
|
||||
return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_contextual_links.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Provides a handler that adds contextual links.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_contextual_links extends views_handler_field {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['fields'] = array('default' => array());
|
||||
$options['destination'] = array('default' => TRUE, 'bool' => TRUE);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
$all_fields = $this->view->display_handler->get_field_labels();
|
||||
// Offer to include only those fields that follow this one.
|
||||
$field_options = array_slice($all_fields, 0, array_search($this->options['id'], array_keys($all_fields)));
|
||||
$form['fields'] = array(
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => t('Fields'),
|
||||
'#description' => t('Fields to be included as contextual links.'),
|
||||
'#options' => $field_options,
|
||||
'#default_value' => $this->options['fields'],
|
||||
);
|
||||
$form['destination'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Include destination'),
|
||||
'#description' => t('Include a "destination" parameter in the link to return the user to the original view upon completing the contextual action.'),
|
||||
'#default_value' => $this->options['destination'],
|
||||
);
|
||||
}
|
||||
|
||||
function pre_render(&$values) {
|
||||
// Add a row plugin css class for the contextual link.
|
||||
$class = 'contextual-links-region';
|
||||
if (!empty($this->view->style_plugin->options['row_class'])) {
|
||||
$this->view->style_plugin->options['row_class'] .= " $class";
|
||||
}
|
||||
else {
|
||||
$this->view->style_plugin->options['row_class'] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the contextual fields.
|
||||
*/
|
||||
function render($values) {
|
||||
$links = array();
|
||||
foreach ($this->options['fields'] as $field) {
|
||||
if (empty($this->view->style_plugin->rendered_fields[$this->view->row_index][$field])) {
|
||||
continue;
|
||||
}
|
||||
$title = $this->view->field[$field]->last_render_text;
|
||||
$path = '';
|
||||
if (!empty($this->view->field[$field]->options['alter']['path'])) {
|
||||
$path = $this->view->field[$field]->options['alter']['path'];
|
||||
}
|
||||
if (!empty($title) && !empty($path)) {
|
||||
// Make sure that tokens are replaced for this paths as well.
|
||||
$tokens = $this->get_render_tokens(array());
|
||||
$path = strip_tags(decode_entities(strtr($path, $tokens)));
|
||||
|
||||
$links[$field] = array(
|
||||
'href' => $path,
|
||||
'title' => $title,
|
||||
);
|
||||
if (!empty($this->options['destination'])) {
|
||||
$links[$field]['query'] = drupal_get_destination();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($links)) {
|
||||
$build = array(
|
||||
'#prefix' => '<div class="contextual-links-wrapper">',
|
||||
'#suffix' => '</div>',
|
||||
'#theme' => 'links__contextual',
|
||||
'#links' => $links,
|
||||
'#attributes' => array('class' => array('contextual-links')),
|
||||
'#attached' => array(
|
||||
'library' => array(array('contextual', 'contextual-links')),
|
||||
),
|
||||
'#access' => user_access('access contextual links'),
|
||||
);
|
||||
return drupal_render($build);
|
||||
}
|
||||
else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function query() { }
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_counter.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Field handler to show a counter of the current row.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_counter extends views_handler_field {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
$options['counter_start'] = array('default' => 1);
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
$form['counter_start'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Starting value'),
|
||||
'#default_value' => $this->options['counter_start'],
|
||||
'#description' => t('Specify the number the counter should start at.'),
|
||||
'#size' => 2,
|
||||
);
|
||||
|
||||
parent::options_form($form, $form_state);
|
||||
}
|
||||
|
||||
function query() {
|
||||
// do nothing -- to override the parent query.
|
||||
}
|
||||
|
||||
function render($values) {
|
||||
// Note: 1 is subtracted from the counter start value below because the
|
||||
// counter value is incremented by 1 at the end of this function.
|
||||
$count = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] - 1 : 0;
|
||||
$pager = $this->view->query->pager;
|
||||
// Get the base count of the pager.
|
||||
if ($pager->use_pager()) {
|
||||
$count += ($pager->get_items_per_page() * $pager->get_current_page() + $pager->get_offset());
|
||||
}
|
||||
// Add the counter for the current site.
|
||||
$count += $this->view->row_index + 1;
|
||||
|
||||
return $count;
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_custom.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A handler to provide a field that is completely custom by the administrator.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_custom extends views_handler_field {
|
||||
function query() {
|
||||
// do nothing -- to override the parent query.
|
||||
}
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
// Override the alter text option to always alter the text.
|
||||
$options['alter']['contains']['alter_text'] = array('default' => TRUE, 'bool' => TRUE);
|
||||
$options['hide_alter_empty'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
// Remove the checkbox
|
||||
unset($form['alter']['alter_text']);
|
||||
unset($form['alter']['text']['#dependency']);
|
||||
unset($form['alter']['text']['#process']);
|
||||
unset($form['alter']['help']['#dependency']);
|
||||
unset($form['alter']['help']['#process']);
|
||||
$form['#pre_render'][] = 'views_handler_field_custom_pre_render_move_text';
|
||||
}
|
||||
|
||||
function render($values) {
|
||||
// Return the text, so the code never thinks the value is empty.
|
||||
return $this->options['alter']['text'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prerender function to move the textarea to the top.
|
||||
*/
|
||||
function views_handler_field_custom_pre_render_move_text($form) {
|
||||
$form['text'] = $form['alter']['text'];
|
||||
$form['help'] = $form['alter']['help'];
|
||||
unset($form['alter']['text']);
|
||||
unset($form['alter']['help']);
|
||||
|
||||
return $form;
|
||||
}
|
101
sites/all/modules/views/handlers/views_handler_field_date.inc
Normal file
101
sites/all/modules/views/handlers/views_handler_field_date.inc
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_date.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A handler to provide proper displays for dates.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_date extends views_handler_field {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['date_format'] = array('default' => 'small');
|
||||
$options['custom_date_format'] = array('default' => '');
|
||||
$options['timezone'] = array('default' => '');
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
|
||||
$date_formats = array();
|
||||
$date_types = system_get_date_types();
|
||||
foreach ($date_types as $key => $value) {
|
||||
$date_formats[$value['type']] = t('@date_format format', array('@date_format' => $value['title'])) . ': ' . format_date(REQUEST_TIME, $value['type']);
|
||||
}
|
||||
|
||||
$form['date_format'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Date format'),
|
||||
'#options' => $date_formats + array(
|
||||
'custom' => t('Custom'),
|
||||
'raw time ago' => t('Time ago'),
|
||||
'time ago' => t('Time ago (with "ago" appended)'),
|
||||
'raw time hence' => t('Time hence'),
|
||||
'time hence' => t('Time hence (with "hence" appended)'),
|
||||
'raw time span' => t('Time span (future dates have "-" prepended)'),
|
||||
'inverse time span' => t('Time span (past dates have "-" prepended)'),
|
||||
'time span' => t('Time span (with "ago/hence" appended)'),
|
||||
),
|
||||
'#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
|
||||
);
|
||||
$form['custom_date_format'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Custom date format'),
|
||||
'#description' => t('If "Custom", see the <a href="@url" target="_blank">PHP manual</a> for date formats. Otherwise, enter the number of different time units to display, which defaults to 2.', array('@url' => 'http://php.net/manual/function.date.php')),
|
||||
'#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '',
|
||||
'#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span')),
|
||||
);
|
||||
$form['timezone'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Timezone'),
|
||||
'#description' => t('Timezone to be used for date output.'),
|
||||
'#options' => array('' => t('- Default site/user timezone -')) + system_time_zones(FALSE),
|
||||
'#default_value' => $this->options['timezone'],
|
||||
'#dependency' => array('edit-options-date-format' => array_merge(array('custom'), array_keys($date_formats))),
|
||||
);
|
||||
|
||||
parent::options_form($form, $form_state);
|
||||
}
|
||||
|
||||
function render($values) {
|
||||
$value = $this->get_value($values);
|
||||
$format = $this->options['date_format'];
|
||||
if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'))) {
|
||||
$custom_format = $this->options['custom_date_format'];
|
||||
}
|
||||
|
||||
if ($value) {
|
||||
$timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
|
||||
$time_diff = REQUEST_TIME - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
|
||||
switch ($format) {
|
||||
case 'raw time ago':
|
||||
return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
|
||||
case 'time ago':
|
||||
return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
|
||||
case 'raw time hence':
|
||||
return format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2);
|
||||
case 'time hence':
|
||||
return t('%time hence', array('%time' => format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2)));
|
||||
case 'raw time span':
|
||||
return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
|
||||
case 'inverse time span':
|
||||
return ($time_diff > 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
|
||||
case 'time span':
|
||||
return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
|
||||
case 'custom':
|
||||
if ($custom_format == 'r') {
|
||||
return format_date($value, $format, $custom_format, $timezone, 'en');
|
||||
}
|
||||
return format_date($value, $format, $custom_format, $timezone);
|
||||
default:
|
||||
return format_date($value, $format, '', $timezone);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
104
sites/all/modules/views/handlers/views_handler_field_entity.inc
Normal file
104
sites/all/modules/views/handlers/views_handler_field_entity.inc
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_entity.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A handler to display data from entity objects.
|
||||
*
|
||||
* Fields based upon this handler work with all query-backends if the tables
|
||||
* used by the query backend have an 'entity type' specified. In order to
|
||||
* make fields based upon this handler automatically available to all compatible
|
||||
* query backends, the views field can be defined in the table
|
||||
* @code views_entity_{ENTITY_TYPE} @endcode.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_entity extends views_handler_field {
|
||||
|
||||
/**
|
||||
* Stores the entity type which is loaded by this field.
|
||||
*/
|
||||
public $entity_type;
|
||||
|
||||
/**
|
||||
* Stores all entites which are in the result.
|
||||
*/
|
||||
public $entities;
|
||||
|
||||
/**
|
||||
* The base field of the entity type assosiated with this field.
|
||||
*/
|
||||
public $base_field;
|
||||
|
||||
/**
|
||||
* Initialize the entity type.
|
||||
*/
|
||||
public function init(&$view, &$options) {
|
||||
parent::init($view, $options);
|
||||
|
||||
// Initialize the entity-type used.
|
||||
$table_data = views_fetch_data($this->table);
|
||||
$this->entity_type = $table_data['table']['entity type'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Overriden to add the field for the entity id.
|
||||
*/
|
||||
function query() {
|
||||
$this->table_alias = $base_table = $this->view->base_table;
|
||||
$this->base_field = $this->view->base_field;
|
||||
|
||||
if (!empty($this->relationship)) {
|
||||
foreach ($this->view->relationship as $relationship) {
|
||||
if ($relationship->alias == $this->relationship) {
|
||||
$base_table = $relationship->definition['base'];
|
||||
$this->table_alias = $relationship->alias;
|
||||
|
||||
$table_data = views_fetch_data($base_table);
|
||||
$this->base_field = empty($relationship->definition['base field']) ? $table_data['table']['base']['field'] : $relationship->definition['base field'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add the field if the query back-end implements an add_field() method,
|
||||
// just like the default back-end.
|
||||
if (method_exists($this->query, 'add_field')) {
|
||||
$this->field_alias = $this->query->add_field($this->table_alias, $this->base_field, '');
|
||||
}
|
||||
|
||||
$this->add_additional_fields();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the entities for all rows that are about to be displayed.
|
||||
*/
|
||||
function pre_render(&$values) {
|
||||
if (!empty($values)) {
|
||||
list($this->entity_type, $this->entities) = $this->query->get_result_entities($values, !empty($this->relationship) ? $this->relationship : NULL, $this->field_alias);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden to return the entity object, or a certain property of the entity.
|
||||
*/
|
||||
function get_value($values, $field = NULL) {
|
||||
if (isset($this->entities[$this->view->row_index])) {
|
||||
$entity = $this->entities[$this->view->row_index];
|
||||
// Support to get a certain part of the entity.
|
||||
if (isset($field) && isset($entity->{$field})) {
|
||||
return $entity->{$field};
|
||||
}
|
||||
// Support to get a part of the values as the normal get_value.
|
||||
elseif (isset($field) && isset($values->{$this->aliases[$field]})) {
|
||||
return $values->{$this->aliases[$field]};
|
||||
}
|
||||
else {
|
||||
return $entity;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
}
|
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_machine_name.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Field handler whichs allows to show machine name content as human name.
|
||||
* @ingroup views_field_handlers
|
||||
*
|
||||
* Definition items:
|
||||
* - options callback: The function to call in order to generate the value options. If omitted, the options 'Yes' and 'No' will be used.
|
||||
* - options arguments: An array of arguments to pass to the options callback.
|
||||
*/
|
||||
class views_handler_field_machine_name extends views_handler_field {
|
||||
/**
|
||||
* @var array Stores the available options.
|
||||
*/
|
||||
var $value_options;
|
||||
|
||||
function get_value_options() {
|
||||
if (isset($this->value_options)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
|
||||
if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) {
|
||||
$this->value_options = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']);
|
||||
}
|
||||
else {
|
||||
$this->value_options = call_user_func($this->definition['options callback']);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$this->value_options = array();
|
||||
}
|
||||
}
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
$options['machine_name'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
$form['machine_name'] = array(
|
||||
'#title' => t('Output machine name'),
|
||||
'#description' => t('Display field as machine name.'),
|
||||
'#type' => 'checkbox',
|
||||
'#default_value' => !empty($this->options['machine_name']),
|
||||
);
|
||||
}
|
||||
|
||||
function pre_render(&$values) {
|
||||
$this->get_value_options();
|
||||
}
|
||||
|
||||
function render($values) {
|
||||
$value = $values->{$this->field_alias};
|
||||
if (!empty($this->options['machine_name']) || !isset($this->value_options[$value])) {
|
||||
$result = check_plain($value);
|
||||
}
|
||||
else {
|
||||
$result = $this->value_options[$value];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_markup.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A handler to run a field through check_markup, using a companion
|
||||
* format field.
|
||||
*
|
||||
* - format: (REQUIRED) Either a string format id to use for this field or an
|
||||
* array('field' => {$field}) where $field is the field in this table
|
||||
* used to control the format such as the 'format' field in the node,
|
||||
* which goes with the 'body' field.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_markup extends views_handler_field {
|
||||
/**
|
||||
* Constructor; calls to base object constructor.
|
||||
*/
|
||||
function construct() {
|
||||
parent::construct();
|
||||
|
||||
$this->format = $this->definition['format'];
|
||||
|
||||
$this->additional_fields = array();
|
||||
if (is_array($this->format)) {
|
||||
$this->additional_fields['format'] = $this->format;
|
||||
}
|
||||
}
|
||||
|
||||
function render($values) {
|
||||
$value = $this->get_value($values);
|
||||
if (is_array($this->format)) {
|
||||
$format = $this->get_value($values, 'format');
|
||||
}
|
||||
else {
|
||||
$format = $this->format;
|
||||
}
|
||||
if ($value) {
|
||||
$value = str_replace('<!--break-->', '', $value);
|
||||
return check_markup($value, $format, '');
|
||||
}
|
||||
}
|
||||
|
||||
function element_type($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) {
|
||||
if ($inline) {
|
||||
return 'span';
|
||||
}
|
||||
|
||||
if (isset($this->definition['element type'])) {
|
||||
return $this->definition['element type'];
|
||||
}
|
||||
|
||||
return 'div';
|
||||
}
|
||||
}
|
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_math.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Render a mathematical expression as a numeric value
|
||||
*
|
||||
* Definition terms:
|
||||
* - float: If true this field contains a decimal value. If unset this field
|
||||
* will be assumed to be integer.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_math extends views_handler_field_numeric {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
$options['expression'] = array('default' => '');
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
$form['expression'] = array(
|
||||
'#type' => 'textarea',
|
||||
'#title' => t('Expression'),
|
||||
'#description' => t('Enter mathematical expressions such as 2 + 2 or sqrt(5). You may assign variables and create mathematical functions and evaluate them. Use the ; to separate these. For example: f(x) = x + 2; f(2).'),
|
||||
'#default_value' => $this->options['expression'],
|
||||
);
|
||||
|
||||
// Create a place for the help
|
||||
$form['expression_help'] = array();
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
// Then move the existing help:
|
||||
$form['expression_help'] = $form['alter']['help'];
|
||||
unset($form['expression_help']['#dependency']);
|
||||
unset($form['alter']['help']);
|
||||
}
|
||||
|
||||
function render($values) {
|
||||
ctools_include('math-expr');
|
||||
$tokens = array_map('floatval', $this->get_render_tokens(array()));
|
||||
$value = strtr($this->options['expression'], $tokens);
|
||||
$expressions = explode(';', $value);
|
||||
$math = new ctools_math_expr;
|
||||
foreach ($expressions as $expression) {
|
||||
if ($expression !== '') {
|
||||
$value = $math->evaluate($expression);
|
||||
}
|
||||
}
|
||||
|
||||
// The rest is directly from views_handler_field_numeric but because it
|
||||
// does not allow the value to be passed in, it is copied.
|
||||
if (!empty($this->options['set_precision'])) {
|
||||
$value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
|
||||
}
|
||||
else {
|
||||
$remainder = abs($value) - intval(abs($value));
|
||||
$value = $value > 0 ? floor($value) : ceil($value);
|
||||
$value = number_format($value, 0, '', $this->options['separator']);
|
||||
if ($remainder) {
|
||||
// The substr may not be locale safe.
|
||||
$value .= $this->options['decimal'] . substr($remainder, 2);
|
||||
}
|
||||
}
|
||||
|
||||
// Check to see if hiding should happen before adding prefix and suffix.
|
||||
if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Should we format as a plural.
|
||||
if (!empty($this->options['format_plural']) && ($value != 0 || !$this->options['empty_zero'])) {
|
||||
$value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
|
||||
}
|
||||
|
||||
return $this->sanitize_value($this->options['prefix'] . $value . $this->options['suffix']);
|
||||
}
|
||||
|
||||
function query() { }
|
||||
}
|
137
sites/all/modules/views/handlers/views_handler_field_numeric.inc
Normal file
137
sites/all/modules/views/handlers/views_handler_field_numeric.inc
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_numeric.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Render a field as a numeric value
|
||||
*
|
||||
* Definition terms:
|
||||
* - float: If true this field contains a decimal value. If unset this field
|
||||
* will be assumed to be integer.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_numeric extends views_handler_field {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['set_precision'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
$options['precision'] = array('default' => 0);
|
||||
$options['decimal'] = array('default' => '.', 'translatable' => TRUE);
|
||||
$options['separator'] = array('default' => ',', 'translatable' => TRUE);
|
||||
$options['format_plural'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
$options['format_plural_singular'] = array('default' => '1');
|
||||
$options['format_plural_plural'] = array('default' => '@count');
|
||||
$options['prefix'] = array('default' => '', 'translatable' => TRUE);
|
||||
$options['suffix'] = array('default' => '', 'translatable' => TRUE);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
if (!empty($this->definition['float'])) {
|
||||
$form['set_precision'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Round'),
|
||||
'#description' => t('If checked, the number will be rounded.'),
|
||||
'#default_value' => $this->options['set_precision'],
|
||||
);
|
||||
$form['precision'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Precision'),
|
||||
'#default_value' => $this->options['precision'],
|
||||
'#description' => t('Specify how many digits to print after the decimal point.'),
|
||||
'#dependency' => array('edit-options-set-precision' => array(TRUE)),
|
||||
'#size' => 2,
|
||||
);
|
||||
$form['decimal'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Decimal point'),
|
||||
'#default_value' => $this->options['decimal'],
|
||||
'#description' => t('What single character to use as a decimal point.'),
|
||||
'#size' => 2,
|
||||
);
|
||||
}
|
||||
$form['separator'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Thousands marker'),
|
||||
'#options' => array(
|
||||
'' => t('- None -'),
|
||||
',' => t('Comma'),
|
||||
' ' => t('Space'),
|
||||
'.' => t('Decimal'),
|
||||
'\'' => t('Apostrophe'),
|
||||
),
|
||||
'#default_value' => $this->options['separator'],
|
||||
'#description' => t('What single character to use as the thousands separator.'),
|
||||
'#size' => 2,
|
||||
);
|
||||
$form['format_plural'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Format plural'),
|
||||
'#description' => t('If checked, special handling will be used for plurality.'),
|
||||
'#default_value' => $this->options['format_plural'],
|
||||
);
|
||||
$form['format_plural_singular'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Singular form'),
|
||||
'#default_value' => $this->options['format_plural_singular'],
|
||||
'#description' => t('Text to use for the singular form.'),
|
||||
'#dependency' => array('edit-options-format-plural' => array(TRUE)),
|
||||
);
|
||||
$form['format_plural_plural'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Plural form'),
|
||||
'#default_value' => $this->options['format_plural_plural'],
|
||||
'#description' => t('Text to use for the plural form, @count will be replaced with the value.'),
|
||||
'#dependency' => array('edit-options-format-plural' => array(TRUE)),
|
||||
);
|
||||
$form['prefix'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Prefix'),
|
||||
'#default_value' => $this->options['prefix'],
|
||||
'#description' => t('Text to put before the number, such as currency symbol.'),
|
||||
);
|
||||
$form['suffix'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Suffix'),
|
||||
'#default_value' => $this->options['suffix'],
|
||||
'#description' => t('Text to put after the number, such as currency symbol.'),
|
||||
);
|
||||
|
||||
parent::options_form($form, $form_state);
|
||||
}
|
||||
|
||||
function render($values) {
|
||||
$value = $this->get_value($values);
|
||||
if (!empty($this->options['set_precision'])) {
|
||||
$value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
|
||||
}
|
||||
else {
|
||||
$remainder = abs($value) - intval(abs($value));
|
||||
$value = $value > 0 ? floor($value) : ceil($value);
|
||||
$value = number_format($value, 0, '', $this->options['separator']);
|
||||
if ($remainder) {
|
||||
// The substr may not be locale safe.
|
||||
$value .= $this->options['decimal'] . substr($remainder, 2);
|
||||
}
|
||||
}
|
||||
|
||||
// Check to see if hiding should happen before adding prefix and suffix.
|
||||
if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Should we format as a plural.
|
||||
if (!empty($this->options['format_plural'])) {
|
||||
$value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
|
||||
}
|
||||
|
||||
return $this->sanitize_value($this->options['prefix'], 'xss')
|
||||
. $this->sanitize_value($value)
|
||||
. $this->sanitize_value($this->options['suffix'], 'xss');
|
||||
}
|
||||
}
|
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_prerender_list.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Field handler to provide a list of items.
|
||||
*
|
||||
* The items are expected to be loaded by a child object during pre_render,
|
||||
* and 'my field' is expected to be the pointer to the items in the list.
|
||||
*
|
||||
* Items to render should be in a list in $this->items
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_prerender_list extends views_handler_field {
|
||||
/**
|
||||
* Stores all items which are used to render the items.
|
||||
* It should be keyed first by the id of the base table, for example nid.
|
||||
* The second key is the id of the thing which is displayed multiple times
|
||||
* per row, for example the tid.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
var $items = array();
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['type'] = array('default' => 'separator');
|
||||
$options['separator'] = array('default' => ', ');
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
$form['type'] = array(
|
||||
'#type' => 'radios',
|
||||
'#title' => t('Display type'),
|
||||
'#options' => array(
|
||||
'ul' => t('Unordered list'),
|
||||
'ol' => t('Ordered list'),
|
||||
'separator' => t('Simple separator'),
|
||||
),
|
||||
'#default_value' => $this->options['type'],
|
||||
);
|
||||
|
||||
$form['separator'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Separator'),
|
||||
'#default_value' => $this->options['separator'],
|
||||
'#dependency' => array('radio:options[type]' => array('separator')),
|
||||
);
|
||||
parent::options_form($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the field.
|
||||
*
|
||||
* This function is deprecated, but left in for older systems that have not
|
||||
* yet or won't update their prerender list fields. If a render_item method
|
||||
* exists, this will not get used by advanced_render.
|
||||
*/
|
||||
function render($values) {
|
||||
$field = $this->get_value($values);
|
||||
if (!empty($this->items[$field])) {
|
||||
if ($this->options['type'] == 'separator') {
|
||||
return implode($this->sanitize_value($this->options['separator']), $this->items[$field]);
|
||||
}
|
||||
else {
|
||||
return theme('item_list',
|
||||
array(
|
||||
'items' => $this->items[$field],
|
||||
'title' => NULL,
|
||||
'type' => $this->options['type']
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render all items in this field together.
|
||||
*
|
||||
* When using advanced render, each possible item in the list is rendered
|
||||
* individually. Then the items are all pasted together.
|
||||
*/
|
||||
function render_items($items) {
|
||||
if (!empty($items)) {
|
||||
if ($this->options['type'] == 'separator') {
|
||||
return implode($this->sanitize_value($this->options['separator'], 'xss_admin'), $items);
|
||||
}
|
||||
else {
|
||||
return theme('item_list',
|
||||
array(
|
||||
'items' => $items,
|
||||
'title' => NULL,
|
||||
'type' => $this->options['type']
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of items for the field.
|
||||
*
|
||||
* Items should be stored in the result array, if possible, as an array
|
||||
* with 'value' as the actual displayable value of the item, plus
|
||||
* any items that might be found in the 'alter' options array for
|
||||
* creating links, such as 'path', 'fragment', 'query' etc, such a thing
|
||||
* is to be made. Additionally, items that might be turned into tokens
|
||||
* should also be in this array.
|
||||
*/
|
||||
function get_items($values) {
|
||||
// Only the parent get_value returns a single field.
|
||||
$field = parent::get_value($values);
|
||||
if (!empty($this->items[$field])) {
|
||||
return $this->items[$field];
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value that's supposed to be rendered.
|
||||
*
|
||||
* @param $values
|
||||
* An object containing all retrieved values.
|
||||
* @param $field
|
||||
* Optional name of the field where the value is stored.
|
||||
* @param $raw
|
||||
* Use the raw data and not the data defined in pre_render
|
||||
*/
|
||||
function get_value($values, $field = NULL, $raw = FALSE) {
|
||||
if ($raw) {
|
||||
return parent::get_value($values, $field);
|
||||
}
|
||||
$item = $this->get_items($values);
|
||||
$item = (array) $item;
|
||||
if (isset($field) && isset($item[$field])) {
|
||||
return $item[$field];
|
||||
}
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if advanced rendering is allowed.
|
||||
*
|
||||
* By default, advanced rendering will NOT be allowed if the class
|
||||
* inheriting from this does not implement a 'render_items' method.
|
||||
*/
|
||||
function allow_advanced_render() {
|
||||
// Note that the advanced render bits also use the presence of
|
||||
// this method to determine if it needs to render items as a list.
|
||||
return method_exists($this, 'render_item');
|
||||
}
|
||||
}
|
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_serialized.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Field handler to show data of serialized fields.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_serialized extends views_handler_field {
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
$options['format'] = array('default' => 'unserialized');
|
||||
$options['key'] = array('default' => '');
|
||||
return $options;
|
||||
}
|
||||
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
$form['format'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Display format'),
|
||||
'#description' => t('How should the serialized data be displayed. You can choose a custom array/object key or a print_r on the full output.'),
|
||||
'#options' => array(
|
||||
'unserialized' => t('Full data (unserialized)'),
|
||||
'serialized' => t('Full data (serialized)'),
|
||||
'key' => t('A certain key'),
|
||||
),
|
||||
'#default_value' => $this->options['format'],
|
||||
);
|
||||
$form['key'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Which key should be displayed'),
|
||||
'#default_value' => $this->options['key'],
|
||||
'#dependency' => array('edit-options-format' => array('key')),
|
||||
);
|
||||
}
|
||||
|
||||
function options_validate(&$form, &$form_state) {
|
||||
// Require a key if the format is key.
|
||||
if ($form_state['values']['options']['format'] == 'key' && $form_state['values']['options']['key'] == '') {
|
||||
form_error($form['key'], t('You have to enter a key if you want to display a key of the data.'));
|
||||
}
|
||||
}
|
||||
|
||||
function render($values) {
|
||||
$value = $values->{$this->field_alias};
|
||||
|
||||
if ($this->options['format'] == 'unserialized') {
|
||||
return check_plain(print_r(unserialize($value), TRUE));
|
||||
}
|
||||
elseif ($this->options['format'] == 'key' && !empty($this->options['key'])) {
|
||||
$value = (array) unserialize($value);
|
||||
return check_plain($value[$this->options['key']]);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_time_interval.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A handler to provide proper displays for time intervals.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_time_interval extends views_handler_field {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['granularity'] = array('default' => 2);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
$form['granularity'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Granularity'),
|
||||
'#description' => t('How many different units to display in the string.'),
|
||||
'#default_value' => $this->options['granularity'],
|
||||
);
|
||||
}
|
||||
|
||||
function render($values) {
|
||||
$value = $values->{$this->field_alias};
|
||||
return format_interval($value, isset($this->options['granularity']) ? $this->options['granularity'] : 2);
|
||||
}
|
||||
}
|
46
sites/all/modules/views/handlers/views_handler_field_url.inc
Normal file
46
sites/all/modules/views/handlers/views_handler_field_url.inc
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_field_url.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Field handler to provide simple renderer that turns a URL into a clickable link.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_field_url extends views_handler_field {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['display_as_link'] = array('default' => TRUE, 'bool' => TRUE);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide link to the page being visited.
|
||||
*/
|
||||
function options_form(&$form, &$form_state) {
|
||||
$form['display_as_link'] = array(
|
||||
'#title' => t('Display as link'),
|
||||
'#type' => 'checkbox',
|
||||
'#default_value' => !empty($this->options['display_as_link']),
|
||||
);
|
||||
parent::options_form($form, $form_state);
|
||||
}
|
||||
|
||||
function render($values) {
|
||||
$value = $this->get_value($values);
|
||||
if (!empty($this->options['display_as_link'])) {
|
||||
$this->options['alter']['make_link'] = TRUE;
|
||||
$this->options['alter']['path'] = $value;
|
||||
$text = !empty($this->options['text']) ? $this->sanitize_value($this->options['text']) : $this->sanitize_value($value, 'url');
|
||||
return $text;
|
||||
}
|
||||
else {
|
||||
return $this->sanitize_value($value, 'url');
|
||||
}
|
||||
}
|
||||
}
|
1410
sites/all/modules/views/handlers/views_handler_filter.inc
Normal file
1410
sites/all/modules/views/handlers/views_handler_filter.inc
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_filter_boolean_operator.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple filter to handle matching of boolean values
|
||||
*
|
||||
* Definition items:
|
||||
* - label: (REQUIRED) The label for the checkbox.
|
||||
* - type: For basic 'true false' types, an item can specify the following:
|
||||
* - true-false: True/false (this is the default)
|
||||
* - yes-no: Yes/No
|
||||
* - on-off: On/Off
|
||||
* - enabled-disabled: Enabled/Disabled
|
||||
* - accept null: Treat a NULL value as false.
|
||||
* - use equal: If you use this flag the query will use = 1 instead of <> 0.
|
||||
* This might be helpful for performance reasons.
|
||||
*
|
||||
* @ingroup views_filter_handlers
|
||||
*/
|
||||
class views_handler_filter_boolean_operator extends views_handler_filter {
|
||||
// exposed filter options
|
||||
var $always_multiple = TRUE;
|
||||
// Don't display empty space where the operator would be.
|
||||
var $no_operator = TRUE;
|
||||
// Whether to accept NULL as a false value or not
|
||||
var $accept_null = FALSE;
|
||||
|
||||
function construct() {
|
||||
$this->value_value = t('True');
|
||||
if (isset($this->definition['label'])) {
|
||||
$this->value_value = $this->definition['label'];
|
||||
}
|
||||
if (isset($this->definition['accept null'])) {
|
||||
$this->accept_null = (bool) $this->definition['accept null'];
|
||||
}
|
||||
else if (isset($this->definition['accept_null'])) {
|
||||
$this->accept_null = (bool) $this->definition['accept_null'];
|
||||
}
|
||||
$this->value_options = NULL;
|
||||
parent::construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the possible options for this filter.
|
||||
*
|
||||
* Child classes should override this function to set the possible values
|
||||
* for the filter. Since this is a boolean filter, the array should have
|
||||
* two possible keys: 1 for "True" and 0 for "False", although the labels
|
||||
* can be whatever makes sense for the filter. These values are used for
|
||||
* configuring the filter, when the filter is exposed, and in the admin
|
||||
* summary of the filter. Normally, this should be static data, but if it's
|
||||
* dynamic for some reason, child classes should use a guard to reduce
|
||||
* database hits as much as possible.
|
||||
*/
|
||||
function get_value_options() {
|
||||
if (isset($this->definition['type'])) {
|
||||
if ($this->definition['type'] == 'yes-no') {
|
||||
$this->value_options = array(1 => t('Yes'), 0 => t('No'));
|
||||
}
|
||||
if ($this->definition['type'] == 'on-off') {
|
||||
$this->value_options = array(1 => t('On'), 0 => t('Off'));
|
||||
}
|
||||
if ($this->definition['type'] == 'enabled-disabled') {
|
||||
$this->value_options = array(1 => t('Enabled'), 0 => t('Disabled'));
|
||||
}
|
||||
}
|
||||
|
||||
// Provide a fallback if the above didn't set anything.
|
||||
if (!isset($this->value_options)) {
|
||||
$this->value_options = array(1 => t('True'), 0 => t('False'));
|
||||
}
|
||||
}
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['value']['default'] = FALSE;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function operator_form(&$form, &$form_state) {
|
||||
$form['operator'] = array();
|
||||
}
|
||||
|
||||
function value_form(&$form, &$form_state) {
|
||||
if (empty($this->value_options)) {
|
||||
// Initialize the array of possible values for this filter.
|
||||
$this->get_value_options();
|
||||
}
|
||||
if (!empty($form_state['exposed'])) {
|
||||
// Exposed filter: use a select box to save space.
|
||||
$filter_form_type = 'select';
|
||||
}
|
||||
else {
|
||||
// Configuring a filter: use radios for clarity.
|
||||
$filter_form_type = 'radios';
|
||||
}
|
||||
$form['value'] = array(
|
||||
'#type' => $filter_form_type,
|
||||
'#title' => $this->value_value,
|
||||
'#options' => $this->value_options,
|
||||
'#default_value' => $this->value,
|
||||
);
|
||||
if (!empty($this->options['exposed'])) {
|
||||
$identifier = $this->options['expose']['identifier'];
|
||||
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
|
||||
$form_state['input'][$identifier] = $this->value;
|
||||
}
|
||||
// If we're configuring an exposed filter, add an <Any> option.
|
||||
if (empty($form_state['exposed']) || empty($this->options['expose']['required'])) {
|
||||
$any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? '<Any>' : t('- Any -');
|
||||
if ($form['value']['#type'] != 'select') {
|
||||
$any_label = check_plain($any_label);
|
||||
}
|
||||
$form['value']['#options'] = array('All' => $any_label) + $form['value']['#options'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function value_validate($form, &$form_state) {
|
||||
if ($form_state['values']['options']['value'] == 'All' && !empty($form_state['values']['options']['expose']['required'])) {
|
||||
form_set_error('value', t('You must select a value unless this is an non-required exposed filter.'));
|
||||
}
|
||||
}
|
||||
|
||||
function admin_summary() {
|
||||
if ($this->is_a_group()) {
|
||||
return t('grouped');
|
||||
}
|
||||
if (!empty($this->options['exposed'])) {
|
||||
return t('exposed');
|
||||
}
|
||||
if (empty($this->value_options)) {
|
||||
$this->get_value_options();
|
||||
}
|
||||
// Now that we have the valid options for this filter, just return the
|
||||
// human-readable label based on the current value. The value_options
|
||||
// array is keyed with either 0 or 1, so if the current value is not
|
||||
// empty, use the label for 1, and if it's empty, use the label for 0.
|
||||
return $this->value_options[!empty($this->value)];
|
||||
}
|
||||
|
||||
function expose_options() {
|
||||
parent::expose_options();
|
||||
$this->options['expose']['operator_id'] = '';
|
||||
$this->options['expose']['label'] = $this->value_value;
|
||||
$this->options['expose']['required'] = TRUE;
|
||||
}
|
||||
|
||||
function query() {
|
||||
$this->ensure_my_table();
|
||||
$field = "$this->table_alias.$this->real_field";
|
||||
|
||||
if (empty($this->value)) {
|
||||
if ($this->accept_null) {
|
||||
$or = db_or()
|
||||
->condition($field, 0, '=')
|
||||
->condition($field, NULL, 'IS NULL');
|
||||
$this->query->add_where($this->options['group'], $or);
|
||||
}
|
||||
else {
|
||||
$this->query->add_where($this->options['group'], $field, 0, '=');
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!empty($this->definition['use equal'])) {
|
||||
$this->query->add_where($this->options['group'], $field, 1, '=');
|
||||
}
|
||||
else {
|
||||
$this->query->add_where($this->options['group'], $field, 0, '<>');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_filter_boolean_operator_string.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple filter to handle matching of boolean values.
|
||||
*
|
||||
* This handler checks to see if a string field is empty (equal to '') or not.
|
||||
* It is otherwise identical to the parent operator.
|
||||
*
|
||||
* Definition items:
|
||||
* - label: (REQUIRED) The label for the checkbox.
|
||||
*
|
||||
* @ingroup views_filter_handlers
|
||||
*/
|
||||
class views_handler_filter_boolean_operator_string extends views_handler_filter_boolean_operator {
|
||||
function query() {
|
||||
$this->ensure_my_table();
|
||||
$where = "$this->table_alias.$this->real_field ";
|
||||
|
||||
if (empty($this->value)) {
|
||||
$where .= "= ''";
|
||||
if ($this->accept_null) {
|
||||
$where = '(' . $where . " OR $this->table_alias.$this->real_field IS NULL)";
|
||||
}
|
||||
}
|
||||
else {
|
||||
$where .= "<> ''";
|
||||
}
|
||||
$this->query->add_where($this->options['group'], $where);
|
||||
}
|
||||
}
|
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_filter_combine.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Filter handler which allows to search on multiple fields.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*/
|
||||
class views_handler_filter_combine extends views_handler_filter_string {
|
||||
/**
|
||||
* @var views_plugin_query_default
|
||||
*/
|
||||
public $query;
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
$options['fields'] = array('default' => array());
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
$this->view->init_style();
|
||||
|
||||
// Allow to choose all fields as possible.
|
||||
if ($this->view->style_plugin->uses_fields()) {
|
||||
$options = array();
|
||||
foreach ($this->view->display_handler->get_handlers('field') as $name => $field) {
|
||||
$options[$name] = $field->ui_name(TRUE);
|
||||
}
|
||||
if ($options) {
|
||||
$form['fields'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Choose fields to combine for filtering'),
|
||||
'#description' => t("This filter doesn't work for very special field handlers."),
|
||||
'#multiple' => TRUE,
|
||||
'#options' => $options,
|
||||
'#default_value' => $this->options['fields'],
|
||||
);
|
||||
}
|
||||
else {
|
||||
form_set_error('', t('You have to add some fields to be able to use this filter.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function query() {
|
||||
$this->view->_build('field');
|
||||
$fields = array();
|
||||
// Only add the fields if they have a proper field and table alias.
|
||||
foreach ($this->options['fields'] as $id) {
|
||||
$field = $this->view->field[$id];
|
||||
// Always add the table of the selected fields to be sure a table alias
|
||||
// exists.
|
||||
$field->ensure_my_table();
|
||||
if (!empty($field->field_alias) && !empty($field->field_alias)) {
|
||||
$fields[] = "$field->table_alias.$field->real_field";
|
||||
}
|
||||
}
|
||||
if ($fields) {
|
||||
$count = count($fields);
|
||||
$separated_fields = array();
|
||||
foreach ($fields as $key => $field) {
|
||||
$separated_fields[] = $field;
|
||||
if ($key < $count - 1) {
|
||||
$separated_fields[] = "' '";
|
||||
}
|
||||
}
|
||||
$expression = implode(', ', $separated_fields);
|
||||
$expression = "CONCAT_WS(' ', $expression)";
|
||||
|
||||
$info = $this->operators();
|
||||
if (!empty($info[$this->operator]['method'])) {
|
||||
$this->{$info[$this->operator]['method']}($expression);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// By default things like op_equal uses add_where, that doesn't support
|
||||
// complex expressions, so override all operators.
|
||||
function op_equal($field) {
|
||||
$placeholder = $this->placeholder();
|
||||
$operator = $this->operator();
|
||||
$this->query->add_where_expression($this->options['group'], "$field $operator $placeholder", array($placeholder => $this->value));
|
||||
}
|
||||
|
||||
function op_contains($field) {
|
||||
$placeholder = $this->placeholder();
|
||||
$this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
|
||||
}
|
||||
|
||||
function op_word($field) {
|
||||
$where = $this->operator == 'word' ? db_or() : db_and();
|
||||
|
||||
// Don't filter on empty strings.
|
||||
if (empty($this->value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->value, $matches, PREG_SET_ORDER);
|
||||
foreach ($matches as $match) {
|
||||
$phrase = FALSE;
|
||||
// Strip off phrase quotes.
|
||||
if ($match[2]{0} == '"') {
|
||||
$match[2] = substr($match[2], 1, -1);
|
||||
$phrase = TRUE;
|
||||
}
|
||||
$words = trim($match[2], ',?!();:-');
|
||||
$words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
|
||||
$placeholder = $this->placeholder();
|
||||
foreach ($words as $word) {
|
||||
$where->where($field . " LIKE $placeholder", array($placeholder => '%' . db_like(trim($word, " ,!?")) . '%'));
|
||||
}
|
||||
}
|
||||
|
||||
if (!$where) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Previously this was a call_user_func_array() but that's unnecessary
|
||||
// as views will unpack an array that is a single arg.
|
||||
$this->query->add_where($this->options['group'], $where);
|
||||
}
|
||||
|
||||
function op_starts($field) {
|
||||
$placeholder = $this->placeholder();
|
||||
$this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
|
||||
}
|
||||
|
||||
function op_not_starts($field) {
|
||||
$placeholder = $this->placeholder();
|
||||
$this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
|
||||
}
|
||||
|
||||
function op_ends($field) {
|
||||
$placeholder = $this->placeholder();
|
||||
$this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
|
||||
}
|
||||
|
||||
function op_not_ends($field) {
|
||||
$placeholder = $this->placeholder();
|
||||
$this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
|
||||
}
|
||||
|
||||
function op_not($field) {
|
||||
$placeholder = $this->placeholder();
|
||||
$this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
|
||||
}
|
||||
|
||||
function op_regex($field) {
|
||||
$placeholder = $this->placeholder();
|
||||
$this->query->add_where_expression($this->options['group'], "$field RLIKE $placeholder", array($placeholder => $this->value));
|
||||
}
|
||||
|
||||
function op_empty($field) {
|
||||
if ($this->operator == 'empty') {
|
||||
$operator = "IS NULL";
|
||||
}
|
||||
else {
|
||||
$operator = "IS NOT NULL";
|
||||
}
|
||||
|
||||
$this->query->add_where_expression($this->options['group'], "$field $operator");
|
||||
}
|
||||
}
|
183
sites/all/modules/views/handlers/views_handler_filter_date.inc
Normal file
183
sites/all/modules/views/handlers/views_handler_filter_date.inc
Normal file
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_filter_date.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Filter to handle dates stored as a timestamp.
|
||||
*
|
||||
* @ingroup views_filter_handlers
|
||||
*/
|
||||
class views_handler_filter_date extends views_handler_filter_numeric {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
// value is already set up properly, we're just adding our new field to it.
|
||||
$options['value']['contains']['type']['default'] = 'date';
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a type selector to the value form
|
||||
*/
|
||||
function value_form(&$form, &$form_state) {
|
||||
if (empty($form_state['exposed'])) {
|
||||
$form['value']['type'] = array(
|
||||
'#type' => 'radios',
|
||||
'#title' => t('Value type'),
|
||||
'#options' => array(
|
||||
'date' => t('A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred.'),
|
||||
'offset' => t('An offset from the current time such as "!example1" or "!example2"', array('!example1' => '+1 day', '!example2' => '-2 hours -30 minutes')),
|
||||
),
|
||||
'#default_value' => !empty($this->value['type']) ? $this->value['type'] : 'date',
|
||||
);
|
||||
}
|
||||
parent::value_form($form, $form_state);
|
||||
}
|
||||
|
||||
function options_validate(&$form, &$form_state) {
|
||||
parent::options_validate($form, $form_state);
|
||||
|
||||
if (!empty($this->options['exposed']) && empty($form_state['values']['options']['expose']['required'])) {
|
||||
// Who cares what the value is if it's exposed and non-required.
|
||||
return;
|
||||
}
|
||||
|
||||
$this->validate_valid_time($form['value'], $form_state['values']['options']['operator'], $form_state['values']['options']['value']);
|
||||
}
|
||||
|
||||
function exposed_validate(&$form, &$form_state) {
|
||||
if (empty($this->options['exposed'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($this->options['expose']['required'])) {
|
||||
// Who cares what the value is if it's exposed and non-required.
|
||||
return;
|
||||
}
|
||||
|
||||
$value = &$form_state['values'][$this->options['expose']['identifier']];
|
||||
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
|
||||
$operator = $form_state['values'][$this->options['expose']['operator_id']];
|
||||
}
|
||||
else {
|
||||
$operator = $this->operator;
|
||||
}
|
||||
|
||||
$this->validate_valid_time($this->options['expose']['identifier'], $operator, $value);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the time values convert to something usable.
|
||||
*/
|
||||
function validate_valid_time(&$form, $operator, $value) {
|
||||
$operators = $this->operators();
|
||||
|
||||
if ($operators[$operator]['values'] == 1) {
|
||||
$convert = strtotime($value['value']);
|
||||
if (!empty($form['value']) && ($convert == -1 || $convert === FALSE)) {
|
||||
form_error($form['value'], t('Invalid date format.'));
|
||||
}
|
||||
}
|
||||
elseif ($operators[$operator]['values'] == 2) {
|
||||
$min = strtotime($value['min']);
|
||||
if ($min == -1 || $min === FALSE) {
|
||||
form_error($form['min'], t('Invalid date format.'));
|
||||
}
|
||||
$max = strtotime($value['max']);
|
||||
if ($max == -1 || $max === FALSE) {
|
||||
form_error($form['max'], t('Invalid date format.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the build group options form.
|
||||
*/
|
||||
function build_group_validate($form, &$form_state) {
|
||||
// Special case to validate grouped date filters, this is because the
|
||||
// $group['value'] array contains the type of filter (date or offset)
|
||||
// and therefore the number of items the comparission has to be done
|
||||
// against 'one' instead of 'zero'.
|
||||
foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
|
||||
if (empty($group['remove'])) {
|
||||
// Check if the title is defined but value wasn't defined.
|
||||
if (!empty($group['title'])) {
|
||||
if ((!is_array($group['value']) && empty($group['value'])) || (is_array($group['value']) && count(array_filter($group['value'])) == 1)) {
|
||||
form_error($form['group_info']['group_items'][$id]['value'], t('The value is required if title for this item is defined.'));
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the value is defined but title wasn't defined.
|
||||
if ((!is_array($group['value']) && !empty($group['value'])) || (is_array($group['value']) && count(array_filter($group['value'])) > 1)) {
|
||||
if (empty($group['title'])) {
|
||||
form_error($form['group_info']['group_items'][$id]['title'], t('The title is required if value for this item is defined.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function accept_exposed_input($input) {
|
||||
if (empty($this->options['exposed'])) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// Store this because it will get overwritten.
|
||||
$type = $this->value['type'];
|
||||
$rc = parent::accept_exposed_input($input);
|
||||
|
||||
// Don't filter if value(s) are empty.
|
||||
$operators = $this->operators();
|
||||
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
|
||||
$operator = $input[$this->options['expose']['operator_id']];
|
||||
}
|
||||
else {
|
||||
$operator = $this->operator;
|
||||
}
|
||||
|
||||
if ($operators[$operator]['values'] == 1) {
|
||||
if ($this->value['value'] == '') {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($this->value['min'] == '' || $this->value['max'] == '') {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
// restore what got overwritten by the parent.
|
||||
$this->value['type'] = $type;
|
||||
return $rc;
|
||||
}
|
||||
|
||||
function op_between($field) {
|
||||
$a = intval(strtotime($this->value['min'], 0));
|
||||
$b = intval(strtotime($this->value['max'], 0));
|
||||
|
||||
if ($this->value['type'] == 'offset') {
|
||||
$a = '***CURRENT_TIME***' . sprintf('%+d', $a); // keep sign
|
||||
$b = '***CURRENT_TIME***' . sprintf('%+d', $b); // keep sign
|
||||
}
|
||||
// This is safe because we are manually scrubbing the values.
|
||||
// It is necessary to do it this way because $a and $b are formulas when using an offset.
|
||||
$operator = strtoupper($this->operator);
|
||||
$this->query->add_where_expression($this->options['group'], "$field $operator $a AND $b");
|
||||
}
|
||||
|
||||
function op_simple($field) {
|
||||
$value = intval(strtotime($this->value['value'], 0));
|
||||
if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
|
||||
$value = '***CURRENT_TIME***' . sprintf('%+d', $value); // keep sign
|
||||
}
|
||||
// This is safe because we are manually scrubbing the value.
|
||||
// It is necessary to do it this way because $value is a formula when using an offset.
|
||||
$this->query->add_where_expression($this->options['group'], "$field $this->operator $value");
|
||||
}
|
||||
}
|
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_filter_entity_bundle
|
||||
*/
|
||||
|
||||
/**
|
||||
* Filter class which allows to filter by certain bundles of an entity.
|
||||
*
|
||||
* This class provides workarounds for taxonomy and comment.
|
||||
*
|
||||
* @ingroup views_filter_handlers
|
||||
*/
|
||||
class views_handler_filter_entity_bundle extends views_handler_filter_in_operator {
|
||||
/**
|
||||
* Stores the entity type on which the filter filters.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $entity_type;
|
||||
|
||||
function init(&$view, &$options) {
|
||||
parent::init($view, $options);
|
||||
|
||||
$this->get_entity_type();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set and returns the entity_type.
|
||||
*
|
||||
* @return string
|
||||
* The entity type on the filter.
|
||||
*/
|
||||
function get_entity_type() {
|
||||
if (!isset($this->entity_type)) {
|
||||
$data = views_fetch_data($this->table);
|
||||
if (isset($data['table']['entity type'])) {
|
||||
$this->entity_type = $data['table']['entity type'];
|
||||
}
|
||||
|
||||
// If the current filter is under a relationship you can't be sure that the
|
||||
// entity type of the view is the entity type of the current filter
|
||||
// For example a filter from a node author on a node view does have users as entity type.
|
||||
if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
|
||||
$relationships = $this->view->display_handler->get_option('relationships');
|
||||
if (!empty($relationships[$this->options['relationship']])) {
|
||||
$options = $relationships[$this->options['relationship']];
|
||||
$data = views_fetch_data($options['table']);
|
||||
$this->entity_type = $data['table']['entity type'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->entity_type;
|
||||
}
|
||||
|
||||
|
||||
function get_value_options() {
|
||||
if (!isset($this->value_options)) {
|
||||
$info = entity_get_info($this->entity_type);
|
||||
$types = $info['bundles'];
|
||||
$this->value_title = t('@entity types', array('@entity' => $info['label']));
|
||||
|
||||
$options = array();
|
||||
foreach ($types as $type => $info) {
|
||||
$options[$type] = t($info['label']);
|
||||
}
|
||||
asort($options);
|
||||
$this->value_options = $options;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All entity types beside comment and taxonomy terms have a proper implement
|
||||
* bundle, though these two need an additional join to node/vocab table
|
||||
* to work as required.
|
||||
*/
|
||||
function query() {
|
||||
$this->ensure_my_table();
|
||||
|
||||
// Adjust the join for the comment case.
|
||||
if ($this->entity_type == 'comment') {
|
||||
$join = new views_join();
|
||||
$def = array(
|
||||
'table' => 'node',
|
||||
'field' => 'nid',
|
||||
'left_table' => $this->table_alias,
|
||||
'left_field' => 'nid',
|
||||
);
|
||||
$join->definition = $def;
|
||||
$join->construct();
|
||||
$join->adjusted = TRUE;
|
||||
$this->table_alias = $this->query->add_table('node', $this->relationship, $join);
|
||||
$this->real_field = 'type';
|
||||
|
||||
// Replace the value to match the node type column.
|
||||
foreach ($this->value as &$value) {
|
||||
$value = str_replace('comment_node_', '', $value);
|
||||
}
|
||||
}
|
||||
elseif ($this->entity_type == 'taxonomy_term') {
|
||||
$join = new views_join();
|
||||
$def = array(
|
||||
'table' => 'taxonomy_vocabulary',
|
||||
'field' => 'vid',
|
||||
'left_table' => $this->table_alias,
|
||||
'left_field' => 'vid',
|
||||
);
|
||||
$join->definition = $def;
|
||||
$join->construct();
|
||||
$join->adjusted = TRUE;
|
||||
$this->table_alias = $this->query->add_table('taxonomy_vocabulary', $this->relationship, $join);
|
||||
$this->real_field = 'machine_name';
|
||||
}
|
||||
else {
|
||||
$entity_info = entity_get_info($this->entity_type);
|
||||
$this->real_field = $entity_info['bundle keys']['bundle'];
|
||||
}
|
||||
parent::query();
|
||||
}
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_filter_equality.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple filter to handle equal to / not equal to filters
|
||||
*
|
||||
* @ingroup views_filter_handlers
|
||||
*/
|
||||
class views_handler_filter_equality extends views_handler_filter {
|
||||
// exposed filter options
|
||||
var $always_multiple = TRUE;
|
||||
|
||||
/**
|
||||
* Provide simple equality operator
|
||||
*/
|
||||
function operator_options() {
|
||||
return array(
|
||||
'=' => t('Is equal to'),
|
||||
'!=' => t('Is not equal to'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a simple textfield for equality
|
||||
*/
|
||||
function value_form(&$form, &$form_state) {
|
||||
$form['value'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Value'),
|
||||
'#size' => 30,
|
||||
'#default_value' => $this->value,
|
||||
);
|
||||
|
||||
if (!empty($form_state['exposed'])) {
|
||||
$identifier = $this->options['expose']['identifier'];
|
||||
if (!isset($form_state['input'][$identifier])) {
|
||||
$form_state['input'][$identifier] = $this->value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_filter_group_by_numeric.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple filter to handle greater than/less than filters
|
||||
*
|
||||
* @ingroup views_filter_handlers
|
||||
*/
|
||||
class views_handler_filter_group_by_numeric extends views_handler_filter_numeric {
|
||||
function query() {
|
||||
$this->ensure_my_table();
|
||||
$field = $this->get_field();
|
||||
|
||||
$info = $this->operators();
|
||||
if (!empty($info[$this->operator]['method'])) {
|
||||
$this->{$info[$this->operator]['method']}($field);
|
||||
}
|
||||
}
|
||||
function op_between($field) {
|
||||
$placeholder_min = $this->placeholder();
|
||||
$placeholder_max = $this->placeholder();
|
||||
if ($this->operator == 'between') {
|
||||
$this->query->add_having_expression($this->options['group'], "$field >= $placeholder_min", array($placeholder_min => $this->value['min']));
|
||||
$this->query->add_having_expression($this->options['group'], "$field <= $placeholder_max", array($placeholder_max => $this->value['max']));
|
||||
}
|
||||
else {
|
||||
$this->query->add_having_expression($this->options['group'], "$field <= $placeholder_min OR $field >= $placeholder_max", array($placeholder_min => $this->value['min'], $placeholder_max => $this->value['max']));
|
||||
}
|
||||
}
|
||||
|
||||
function op_simple($field) {
|
||||
$placeholder = $this->placeholder();
|
||||
$this->query->add_having_expression($this->options['group'], "$field $this->operator $placeholder", array($placeholder => $this->value['value']));
|
||||
}
|
||||
|
||||
function op_empty($field) {
|
||||
if ($this->operator == 'empty') {
|
||||
$operator = "IS NULL";
|
||||
}
|
||||
else {
|
||||
$operator = "IS NOT NULL";
|
||||
}
|
||||
|
||||
$this->query->add_having_expression($this->options['group'], "$field $operator");
|
||||
}
|
||||
|
||||
function ui_name($short = FALSE) {
|
||||
return $this->get_field(parent::ui_name($short));
|
||||
}
|
||||
|
||||
function can_group() { return FALSE; }
|
||||
}
|
@@ -0,0 +1,426 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_filter_in_operator.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple filter to handle matching of multiple options selectable via checkboxes
|
||||
*
|
||||
* Definition items:
|
||||
* - options callback: The function to call in order to generate the value options. If omitted, the options 'Yes' and 'No' will be used.
|
||||
* - options arguments: An array of arguments to pass to the options callback.
|
||||
*
|
||||
* @ingroup views_filter_handlers
|
||||
*/
|
||||
class views_handler_filter_in_operator extends views_handler_filter {
|
||||
var $value_form_type = 'checkboxes';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* Stores all operations which are available on the form.
|
||||
*/
|
||||
var $value_options = NULL;
|
||||
|
||||
function construct() {
|
||||
parent::construct();
|
||||
$this->value_title = t('Options');
|
||||
$this->value_options = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Child classes should be used to override this function and set the
|
||||
* 'value options', unless 'options callback' is defined as a valid function
|
||||
* or static public method to generate these values.
|
||||
*
|
||||
* This can use a guard to be used to reduce database hits as much as
|
||||
* possible.
|
||||
*
|
||||
* @return
|
||||
* Return the stored values in $this->value_options if someone expects it.
|
||||
*/
|
||||
function get_value_options() {
|
||||
if (isset($this->value_options)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
|
||||
if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) {
|
||||
$this->value_options = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']);
|
||||
}
|
||||
else {
|
||||
$this->value_options = call_user_func($this->definition['options callback']);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$this->value_options = array(t('Yes'), t('No'));
|
||||
}
|
||||
|
||||
return $this->value_options;
|
||||
}
|
||||
|
||||
function expose_options() {
|
||||
parent::expose_options();
|
||||
$this->options['expose']['reduce'] = FALSE;
|
||||
}
|
||||
|
||||
function expose_form(&$form, &$form_state) {
|
||||
parent::expose_form($form, $form_state);
|
||||
$form['expose']['reduce'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Limit list to selected items'),
|
||||
'#description' => t('If checked, the only items presented to the user will be the ones selected here.'),
|
||||
'#default_value' => !empty($this->options['expose']['reduce']), // safety
|
||||
);
|
||||
}
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['operator']['default'] = 'in';
|
||||
$options['value']['default'] = array();
|
||||
$options['expose']['contains']['reduce'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* This kind of construct makes it relatively easy for a child class
|
||||
* to add or remove functionality by overriding this function and
|
||||
* adding/removing items from this array.
|
||||
*/
|
||||
function operators() {
|
||||
$operators = array(
|
||||
'in' => array(
|
||||
'title' => t('Is one of'),
|
||||
'short' => t('in'),
|
||||
'short_single' => t('='),
|
||||
'method' => 'op_simple',
|
||||
'values' => 1,
|
||||
),
|
||||
'not in' => array(
|
||||
'title' => t('Is not one of'),
|
||||
'short' => t('not in'),
|
||||
'short_single' => t('<>'),
|
||||
'method' => 'op_simple',
|
||||
'values' => 1,
|
||||
),
|
||||
);
|
||||
// if the definition allows for the empty operator, add it.
|
||||
if (!empty($this->definition['allow empty'])) {
|
||||
$operators += array(
|
||||
'empty' => array(
|
||||
'title' => t('Is empty (NULL)'),
|
||||
'method' => 'op_empty',
|
||||
'short' => t('empty'),
|
||||
'values' => 0,
|
||||
),
|
||||
'not empty' => array(
|
||||
'title' => t('Is not empty (NOT NULL)'),
|
||||
'method' => 'op_empty',
|
||||
'short' => t('not empty'),
|
||||
'values' => 0,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return $operators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build strings from the operators() for 'select' options
|
||||
*/
|
||||
function operator_options($which = 'title') {
|
||||
$options = array();
|
||||
foreach ($this->operators() as $id => $info) {
|
||||
$options[$id] = $info[$which];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function operator_values($values = 1) {
|
||||
$options = array();
|
||||
foreach ($this->operators() as $id => $info) {
|
||||
if (isset($info['values']) && $info['values'] == $values) {
|
||||
$options[] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function value_form(&$form, &$form_state) {
|
||||
$form['value'] = array();
|
||||
$options = array();
|
||||
|
||||
if (empty($form_state['exposed'])) {
|
||||
// Add a select all option to the value form.
|
||||
$options = array('all' => t('Select all'));
|
||||
}
|
||||
|
||||
$this->get_value_options();
|
||||
$options += $this->value_options;
|
||||
$default_value = (array) $this->value;
|
||||
|
||||
$which = 'all';
|
||||
if (!empty($form['operator'])) {
|
||||
$source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
|
||||
}
|
||||
if (!empty($form_state['exposed'])) {
|
||||
$identifier = $this->options['expose']['identifier'];
|
||||
|
||||
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
|
||||
// exposed and locked.
|
||||
$which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
|
||||
}
|
||||
else {
|
||||
$source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
|
||||
}
|
||||
|
||||
if (!empty($this->options['expose']['reduce'])) {
|
||||
$options = $this->reduce_value_options();
|
||||
|
||||
if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
|
||||
$default_value = array();
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($this->options['expose']['multiple'])) {
|
||||
if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
|
||||
$default_value = 'All';
|
||||
}
|
||||
elseif (empty($default_value)) {
|
||||
$keys = array_keys($options);
|
||||
$default_value = array_shift($keys);
|
||||
}
|
||||
else {
|
||||
$copy = $default_value;
|
||||
$default_value = array_shift($copy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($which == 'all' || $which == 'value') {
|
||||
$form['value'] = array(
|
||||
'#type' => $this->value_form_type,
|
||||
'#title' => $this->value_title,
|
||||
'#options' => $options,
|
||||
'#default_value' => $default_value,
|
||||
// These are only valid for 'select' type, but do no harm to checkboxes.
|
||||
'#multiple' => TRUE,
|
||||
'#size' => count($options) > 8 ? 8 : count($options),
|
||||
);
|
||||
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
|
||||
$form_state['input'][$identifier] = $default_value;
|
||||
}
|
||||
|
||||
if ($which == 'all') {
|
||||
if (empty($form_state['exposed']) && (in_array($this->value_form_type, array('checkbox', 'checkboxes', 'radios', 'select')))) {
|
||||
$form['value']['#prefix'] = '<div id="edit-options-value-wrapper">';
|
||||
$form['value']['#suffix'] = '</div>';
|
||||
}
|
||||
$form['value']['#dependency'] = array($source => $this->operator_values(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* When using exposed filters, we may be required to reduce the set.
|
||||
*/
|
||||
function reduce_value_options($input = NULL) {
|
||||
if (!isset($input)) {
|
||||
$input = $this->value_options;
|
||||
}
|
||||
|
||||
// Because options may be an array of strings, or an array of mixed arrays
|
||||
// and strings (optgroups) or an array of objects, we have to
|
||||
// step through and handle each one individually.
|
||||
$options = array();
|
||||
foreach ($input as $id => $option) {
|
||||
if (is_array($option)) {
|
||||
$options[$id] = $this->reduce_value_options($option);
|
||||
continue;
|
||||
}
|
||||
elseif (is_object($option)) {
|
||||
$keys = array_keys($option->option);
|
||||
$key = array_shift($keys);
|
||||
if (isset($this->options['value'][$key])) {
|
||||
$options[$id] = $option;
|
||||
}
|
||||
}
|
||||
elseif (isset($this->options['value'][$id])) {
|
||||
$options[$id] = $option;
|
||||
}
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
function accept_exposed_input($input) {
|
||||
// A very special override because the All state for this type of
|
||||
// filter could have a default:
|
||||
if (empty($this->options['exposed'])) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// If this is non-multiple and non-required, then this filter will
|
||||
// participate, but using the default settings, *if* 'limit is true.
|
||||
if (empty($this->options['expose']['multiple']) && empty($this->options['expose']['required']) && !empty($this->options['expose']['limit'])) {
|
||||
$identifier = $this->options['expose']['identifier'];
|
||||
if ($input[$identifier] == 'All') {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::accept_exposed_input($input);
|
||||
}
|
||||
|
||||
function value_submit($form, &$form_state) {
|
||||
// Drupal's FAPI system automatically puts '0' in for any checkbox that
|
||||
// was not set, and the key to the checkbox if it is set.
|
||||
// Unfortunately, this means that if the key to that checkbox is 0,
|
||||
// we are unable to tell if that checkbox was set or not.
|
||||
|
||||
// Luckily, the '#value' on the checkboxes form actually contains
|
||||
// *only* a list of checkboxes that were set, and we can use that
|
||||
// instead.
|
||||
|
||||
$form_state['values']['options']['value'] = $form['value']['#value'];
|
||||
}
|
||||
|
||||
function admin_summary() {
|
||||
if ($this->is_a_group()) {
|
||||
return t('grouped');
|
||||
}
|
||||
if (!empty($this->options['exposed'])) {
|
||||
return t('exposed');
|
||||
}
|
||||
$info = $this->operators();
|
||||
|
||||
$this->get_value_options();
|
||||
|
||||
if (!is_array($this->value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$operator = check_plain($info[$this->operator]['short']);
|
||||
$values = '';
|
||||
if (in_array($this->operator, $this->operator_values(1))) {
|
||||
// Remove every element which is not known.
|
||||
foreach ($this->value as $value) {
|
||||
if (!isset($this->value_options[$value])) {
|
||||
unset($this->value[$value]);
|
||||
}
|
||||
}
|
||||
// Choose different kind of ouput for 0, a single and multiple values.
|
||||
if (count($this->value) == 0) {
|
||||
$values = t('Unknown');
|
||||
}
|
||||
else if (count($this->value) == 1) {
|
||||
// If any, use the 'single' short name of the operator instead.
|
||||
if (isset($info[$this->operator]['short_single'])) {
|
||||
$operator = check_plain($info[$this->operator]['short_single']);
|
||||
}
|
||||
|
||||
$keys = $this->value;
|
||||
$value = array_shift($keys);
|
||||
if (isset($this->value_options[$value])) {
|
||||
$values = check_plain($this->value_options[$value]);
|
||||
}
|
||||
else {
|
||||
$values = '';
|
||||
}
|
||||
}
|
||||
else {
|
||||
foreach ($this->value as $value) {
|
||||
if ($values !== '') {
|
||||
$values .= ', ';
|
||||
}
|
||||
if (drupal_strlen($values) > 8) {
|
||||
$values .= '...';
|
||||
break;
|
||||
}
|
||||
if (isset($this->value_options[$value])) {
|
||||
$values .= check_plain($this->value_options[$value]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $operator . (($values !== '') ? ' ' . $values : '');
|
||||
}
|
||||
|
||||
function query() {
|
||||
$info = $this->operators();
|
||||
if (!empty($info[$this->operator]['method'])) {
|
||||
$this->{$info[$this->operator]['method']}();
|
||||
}
|
||||
}
|
||||
|
||||
function op_simple() {
|
||||
if (empty($this->value)) {
|
||||
return;
|
||||
}
|
||||
$this->ensure_my_table();
|
||||
|
||||
// We use array_values() because the checkboxes keep keys and that can cause
|
||||
// array addition problems.
|
||||
$this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", array_values($this->value), $this->operator);
|
||||
}
|
||||
|
||||
function op_empty() {
|
||||
$this->ensure_my_table();
|
||||
if ($this->operator == 'empty') {
|
||||
$operator = "IS NULL";
|
||||
}
|
||||
else {
|
||||
$operator = "IS NOT NULL";
|
||||
}
|
||||
|
||||
$this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", NULL, $operator);
|
||||
}
|
||||
|
||||
function validate() {
|
||||
$this->get_value_options();
|
||||
$errors = array();
|
||||
|
||||
// If the operator is an operator which doesn't require a value, there is
|
||||
// no need for additional validation.
|
||||
if (in_array($this->operator, $this->operator_values(0))) {
|
||||
return array();
|
||||
}
|
||||
|
||||
if (!in_array($this->operator, $this->operator_values(1))) {
|
||||
$errors[] = t('The operator is invalid on filter: @filter.', array('@filter' => $this->ui_name(TRUE)));
|
||||
}
|
||||
if (is_array($this->value)) {
|
||||
if (!isset($this->value_options)) {
|
||||
// Don't validate if there are none value options provided, for example for special handlers.
|
||||
return $errors;
|
||||
}
|
||||
if ($this->options['exposed'] && !$this->options['expose']['required'] && empty($this->value)) {
|
||||
// Don't validate if the field is exposed and no default value is provided.
|
||||
return $errors;
|
||||
}
|
||||
|
||||
// Some filter_in_operator usage uses optgroups forms, so flatten it.
|
||||
$flat_options = form_options_flatten($this->value_options, TRUE);
|
||||
|
||||
// Remove every element which is not known.
|
||||
foreach ($this->value as $value) {
|
||||
if (!isset($flat_options[$value])) {
|
||||
unset($this->value[$value]);
|
||||
}
|
||||
}
|
||||
// Choose different kind of ouput for 0, a single and multiple values.
|
||||
if (count($this->value) == 0) {
|
||||
$errors[] = t('No valid values found on filter: @filter.', array('@filter' => $this->ui_name(TRUE)));
|
||||
}
|
||||
}
|
||||
elseif (!empty($this->value) && ($this->operator == 'in' || $this->operator == 'not in')) {
|
||||
$errors[] = t('The value @value is not an array for @operator on filter: @filter', array('@value' => views_var_export($this->value), '@operator' => $this->operator, '@filter' => $this->ui_name(TRUE)));
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
}
|
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_filter_many_to_one.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Complex filter to handle filtering for many to one relationships,
|
||||
* such as terms (many terms per node) or roles (many roles per user).
|
||||
*
|
||||
* The construct method needs to be overridden to provide a list of options;
|
||||
* alternately, the value_form and admin_summary methods need to be overriden
|
||||
* to provide something that isn't just a select list.
|
||||
*
|
||||
* @ingroup views_filter_handlers
|
||||
*/
|
||||
class views_handler_filter_many_to_one extends views_handler_filter_in_operator {
|
||||
/**
|
||||
* @var views_many_to_one_helper
|
||||
*
|
||||
* Stores the Helper object which handles the many_to_one complexity.
|
||||
*/
|
||||
var $helper = NULL;
|
||||
|
||||
function init(&$view, &$options) {
|
||||
parent::init($view, $options);
|
||||
$this->helper = new views_many_to_one_helper($this);
|
||||
}
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['operator']['default'] = 'or';
|
||||
$options['value']['default'] = array();
|
||||
|
||||
if (isset($this->helper)) {
|
||||
$this->helper->option_definition($options);
|
||||
}
|
||||
else {
|
||||
$helper = new views_many_to_one_helper($this);
|
||||
$helper->option_definition($options);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function operators() {
|
||||
$operators = array(
|
||||
'or' => array(
|
||||
'title' => t('Is one of'),
|
||||
'short' => t('or'),
|
||||
'short_single' => t('='),
|
||||
'method' => 'op_helper',
|
||||
'values' => 1,
|
||||
'ensure_my_table' => 'helper',
|
||||
),
|
||||
'and' => array(
|
||||
'title' => t('Is all of'),
|
||||
'short' => t('and'),
|
||||
'short_single' => t('='),
|
||||
'method' => 'op_helper',
|
||||
'values' => 1,
|
||||
'ensure_my_table' => 'helper',
|
||||
),
|
||||
'not' => array(
|
||||
'title' => t('Is none of'),
|
||||
'short' => t('not'),
|
||||
'short_single' => t('<>'),
|
||||
'method' => 'op_helper',
|
||||
'values' => 1,
|
||||
'ensure_my_table' => 'helper',
|
||||
),
|
||||
);
|
||||
// if the definition allows for the empty operator, add it.
|
||||
if (!empty($this->definition['allow empty'])) {
|
||||
$operators += array(
|
||||
'empty' => array(
|
||||
'title' => t('Is empty (NULL)'),
|
||||
'method' => 'op_empty',
|
||||
'short' => t('empty'),
|
||||
'values' => 0,
|
||||
),
|
||||
'not empty' => array(
|
||||
'title' => t('Is not empty (NOT NULL)'),
|
||||
'method' => 'op_empty',
|
||||
'short' => t('not empty'),
|
||||
'values' => 0,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return $operators;
|
||||
}
|
||||
|
||||
var $value_form_type = 'select';
|
||||
function value_form(&$form, &$form_state) {
|
||||
parent::value_form($form, $form_state);
|
||||
|
||||
if (empty($form_state['exposed'])) {
|
||||
$this->helper->options_form($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override ensure_my_table so we can control how this joins in.
|
||||
* The operator actually has influence over joining.
|
||||
*/
|
||||
function ensure_my_table() {
|
||||
// Defer to helper if the operator specifies it.
|
||||
$info = $this->operators();
|
||||
if (isset($info[$this->operator]['ensure_my_table']) && $info[$this->operator]['ensure_my_table'] == 'helper') {
|
||||
return $this->helper->ensure_my_table();
|
||||
}
|
||||
|
||||
return parent::ensure_my_table();
|
||||
}
|
||||
|
||||
function op_helper() {
|
||||
if (empty($this->value)) {
|
||||
return;
|
||||
}
|
||||
$this->helper->add_filter();
|
||||
}
|
||||
}
|
@@ -0,0 +1,325 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_filter_numeric.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple filter to handle greater than/less than filters
|
||||
*
|
||||
* @ingroup views_filter_handlers
|
||||
*/
|
||||
class views_handler_filter_numeric extends views_handler_filter {
|
||||
var $always_multiple = TRUE;
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['value'] = array(
|
||||
'contains' => array(
|
||||
'min' => array('default' => ''),
|
||||
'max' => array('default' => ''),
|
||||
'value' => array('default' => ''),
|
||||
),
|
||||
);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function operators() {
|
||||
$operators = array(
|
||||
'<' => array(
|
||||
'title' => t('Is less than'),
|
||||
'method' => 'op_simple',
|
||||
'short' => t('<'),
|
||||
'values' => 1,
|
||||
),
|
||||
'<=' => array(
|
||||
'title' => t('Is less than or equal to'),
|
||||
'method' => 'op_simple',
|
||||
'short' => t('<='),
|
||||
'values' => 1,
|
||||
),
|
||||
'=' => array(
|
||||
'title' => t('Is equal to'),
|
||||
'method' => 'op_simple',
|
||||
'short' => t('='),
|
||||
'values' => 1,
|
||||
),
|
||||
'!=' => array(
|
||||
'title' => t('Is not equal to'),
|
||||
'method' => 'op_simple',
|
||||
'short' => t('!='),
|
||||
'values' => 1,
|
||||
),
|
||||
'>=' => array(
|
||||
'title' => t('Is greater than or equal to'),
|
||||
'method' => 'op_simple',
|
||||
'short' => t('>='),
|
||||
'values' => 1,
|
||||
),
|
||||
'>' => array(
|
||||
'title' => t('Is greater than'),
|
||||
'method' => 'op_simple',
|
||||
'short' => t('>'),
|
||||
'values' => 1,
|
||||
),
|
||||
'between' => array(
|
||||
'title' => t('Is between'),
|
||||
'method' => 'op_between',
|
||||
'short' => t('between'),
|
||||
'values' => 2,
|
||||
),
|
||||
'not between' => array(
|
||||
'title' => t('Is not between'),
|
||||
'method' => 'op_between',
|
||||
'short' => t('not between'),
|
||||
'values' => 2,
|
||||
),
|
||||
);
|
||||
|
||||
// if the definition allows for the empty operator, add it.
|
||||
if (!empty($this->definition['allow empty'])) {
|
||||
$operators += array(
|
||||
'empty' => array(
|
||||
'title' => t('Is empty (NULL)'),
|
||||
'method' => 'op_empty',
|
||||
'short' => t('empty'),
|
||||
'values' => 0,
|
||||
),
|
||||
'not empty' => array(
|
||||
'title' => t('Is not empty (NOT NULL)'),
|
||||
'method' => 'op_empty',
|
||||
'short' => t('not empty'),
|
||||
'values' => 0,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Add regexp support for MySQL.
|
||||
if (Database::getConnection()->databaseType() == 'mysql') {
|
||||
$operators += array(
|
||||
'regular_expression' => array(
|
||||
'title' => t('Regular expression'),
|
||||
'short' => t('regex'),
|
||||
'method' => 'op_regex',
|
||||
'values' => 1,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return $operators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a list of all the numeric operators
|
||||
*/
|
||||
function operator_options($which = 'title') {
|
||||
$options = array();
|
||||
foreach ($this->operators() as $id => $info) {
|
||||
$options[$id] = $info[$which];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function operator_values($values = 1) {
|
||||
$options = array();
|
||||
foreach ($this->operators() as $id => $info) {
|
||||
if ($info['values'] == $values) {
|
||||
$options[] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
/**
|
||||
* Provide a simple textfield for equality
|
||||
*/
|
||||
function value_form(&$form, &$form_state) {
|
||||
$form['value']['#tree'] = TRUE;
|
||||
|
||||
// We have to make some choices when creating this as an exposed
|
||||
// filter form. For example, if the operator is locked and thus
|
||||
// not rendered, we can't render dependencies; instead we only
|
||||
// render the form items we need.
|
||||
$which = 'all';
|
||||
if (!empty($form['operator'])) {
|
||||
$source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
|
||||
}
|
||||
|
||||
if (!empty($form_state['exposed'])) {
|
||||
$identifier = $this->options['expose']['identifier'];
|
||||
|
||||
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
|
||||
// exposed and locked.
|
||||
$which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value';
|
||||
}
|
||||
else {
|
||||
$source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($which == 'all') {
|
||||
$form['value']['value'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => empty($form_state['exposed']) ? t('Value') : '',
|
||||
'#size' => 30,
|
||||
'#default_value' => $this->value['value'],
|
||||
'#dependency' => array($source => $this->operator_values(1)),
|
||||
);
|
||||
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['value'])) {
|
||||
$form_state['input'][$identifier]['value'] = $this->value['value'];
|
||||
}
|
||||
}
|
||||
elseif ($which == 'value') {
|
||||
// When exposed we drop the value-value and just do value if
|
||||
// the operator is locked.
|
||||
$form['value'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => empty($form_state['exposed']) ? t('Value') : '',
|
||||
'#size' => 30,
|
||||
'#default_value' => $this->value['value'],
|
||||
);
|
||||
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
|
||||
$form_state['input'][$identifier] = $this->value['value'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($which == 'all' || $which == 'minmax') {
|
||||
$form['value']['min'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => empty($form_state['exposed']) ? t('Min') : '',
|
||||
'#size' => 30,
|
||||
'#default_value' => $this->value['min'],
|
||||
);
|
||||
$form['value']['max'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => empty($form_state['exposed']) ? t('And max') : t('And'),
|
||||
'#size' => 30,
|
||||
'#default_value' => $this->value['max'],
|
||||
);
|
||||
if ($which == 'all') {
|
||||
$dependency = array(
|
||||
'#dependency' => array($source => $this->operator_values(2)),
|
||||
);
|
||||
$form['value']['min'] += $dependency;
|
||||
$form['value']['max'] += $dependency;
|
||||
}
|
||||
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['min'])) {
|
||||
$form_state['input'][$identifier]['min'] = $this->value['min'];
|
||||
}
|
||||
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['max'])) {
|
||||
$form_state['input'][$identifier]['max'] = $this->value['max'];
|
||||
}
|
||||
|
||||
if (!isset($form['value'])) {
|
||||
// Ensure there is something in the 'value'.
|
||||
$form['value'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => NULL
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function query() {
|
||||
$this->ensure_my_table();
|
||||
$field = "$this->table_alias.$this->real_field";
|
||||
|
||||
$info = $this->operators();
|
||||
if (!empty($info[$this->operator]['method'])) {
|
||||
$this->{$info[$this->operator]['method']}($field);
|
||||
}
|
||||
}
|
||||
|
||||
function op_between($field) {
|
||||
if ($this->operator == 'between') {
|
||||
$this->query->add_where($this->options['group'], $field, array($this->value['min'], $this->value['max']), 'BETWEEN');
|
||||
}
|
||||
else {
|
||||
$this->query->add_where($this->options['group'], db_or()->condition($field, $this->value['min'], '<=')->condition($field, $this->value['max'], '>='));
|
||||
}
|
||||
}
|
||||
|
||||
function op_simple($field) {
|
||||
$this->query->add_where($this->options['group'], $field, $this->value['value'], $this->operator);
|
||||
}
|
||||
|
||||
function op_empty($field) {
|
||||
if ($this->operator == 'empty') {
|
||||
$operator = "IS NULL";
|
||||
}
|
||||
else {
|
||||
$operator = "IS NOT NULL";
|
||||
}
|
||||
|
||||
$this->query->add_where($this->options['group'], $field, NULL, $operator);
|
||||
}
|
||||
|
||||
function op_regex($field) {
|
||||
$this->query->add_where($this->options['group'], $field, $this->value, 'RLIKE');
|
||||
}
|
||||
|
||||
function admin_summary() {
|
||||
if ($this->is_a_group()) {
|
||||
return t('grouped');
|
||||
}
|
||||
if (!empty($this->options['exposed'])) {
|
||||
return t('exposed');
|
||||
}
|
||||
|
||||
$options = $this->operator_options('short');
|
||||
$output = check_plain($options[$this->operator]);
|
||||
if (in_array($this->operator, $this->operator_values(2))) {
|
||||
$output .= ' ' . t('@min and @max', array('@min' => $this->value['min'], '@max' => $this->value['max']));
|
||||
}
|
||||
elseif (in_array($this->operator, $this->operator_values(1))) {
|
||||
$output .= ' ' . check_plain($this->value['value']);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do some minor translation of the exposed input
|
||||
*/
|
||||
function accept_exposed_input($input) {
|
||||
if (empty($this->options['exposed'])) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// rewrite the input value so that it's in the correct format so that
|
||||
// the parent gets the right data.
|
||||
if (!empty($this->options['expose']['identifier'])) {
|
||||
$value = &$input[$this->options['expose']['identifier']];
|
||||
if (!is_array($value)) {
|
||||
$value = array(
|
||||
'value' => $value,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$rc = parent::accept_exposed_input($input);
|
||||
|
||||
if (empty($this->options['expose']['required'])) {
|
||||
// We have to do some of our own checking for non-required filters.
|
||||
$info = $this->operators();
|
||||
if (!empty($info[$this->operator]['values'])) {
|
||||
switch ($info[$this->operator]['values']) {
|
||||
case 1:
|
||||
if ($value['value'] === '') {
|
||||
return FALSE;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if ($value['min'] === '' && $value['max'] === '') {
|
||||
return FALSE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $rc;
|
||||
}
|
||||
}
|
338
sites/all/modules/views/handlers/views_handler_filter_string.inc
Normal file
338
sites/all/modules/views/handlers/views_handler_filter_string.inc
Normal file
@@ -0,0 +1,338 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_filter_string.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Basic textfield filter to handle string filtering commands
|
||||
* including equality, like, not like, etc.
|
||||
*
|
||||
* @ingroup views_filter_handlers
|
||||
*/
|
||||
class views_handler_filter_string extends views_handler_filter {
|
||||
// exposed filter options
|
||||
var $always_multiple = TRUE;
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['expose']['contains']['required'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* This kind of construct makes it relatively easy for a child class
|
||||
* to add or remove functionality by overriding this function and
|
||||
* adding/removing items from this array.
|
||||
*/
|
||||
function operators() {
|
||||
$operators = array(
|
||||
'=' => array(
|
||||
'title' => t('Is equal to'),
|
||||
'short' => t('='),
|
||||
'method' => 'op_equal',
|
||||
'values' => 1,
|
||||
),
|
||||
'!=' => array(
|
||||
'title' => t('Is not equal to'),
|
||||
'short' => t('!='),
|
||||
'method' => 'op_equal',
|
||||
'values' => 1,
|
||||
),
|
||||
'contains' => array(
|
||||
'title' => t('Contains'),
|
||||
'short' => t('contains'),
|
||||
'method' => 'op_contains',
|
||||
'values' => 1,
|
||||
),
|
||||
'word' => array(
|
||||
'title' => t('Contains any word'),
|
||||
'short' => t('has word'),
|
||||
'method' => 'op_word',
|
||||
'values' => 1,
|
||||
),
|
||||
'allwords' => array(
|
||||
'title' => t('Contains all words'),
|
||||
'short' => t('has all'),
|
||||
'method' => 'op_word',
|
||||
'values' => 1,
|
||||
),
|
||||
'starts' => array(
|
||||
'title' => t('Starts with'),
|
||||
'short' => t('begins'),
|
||||
'method' => 'op_starts',
|
||||
'values' => 1,
|
||||
),
|
||||
'not_starts' => array(
|
||||
'title' => t('Does not start with'),
|
||||
'short' => t('not_begins'),
|
||||
'method' => 'op_not_starts',
|
||||
'values' => 1,
|
||||
),
|
||||
'ends' => array(
|
||||
'title' => t('Ends with'),
|
||||
'short' => t('ends'),
|
||||
'method' => 'op_ends',
|
||||
'values' => 1,
|
||||
),
|
||||
'not_ends' => array(
|
||||
'title' => t('Does not end with'),
|
||||
'short' => t('not_ends'),
|
||||
'method' => 'op_not_ends',
|
||||
'values' => 1,
|
||||
),
|
||||
'not' => array(
|
||||
'title' => t('Does not contain'),
|
||||
'short' => t('!has'),
|
||||
'method' => 'op_not',
|
||||
'values' => 1,
|
||||
),
|
||||
'shorterthan' => array(
|
||||
'title' => t('Length is shorter than'),
|
||||
'short' => t('shorter than'),
|
||||
'method' => 'op_shorter',
|
||||
'values' => 1,
|
||||
),
|
||||
'longerthan' => array(
|
||||
'title' => t('Length is longer than'),
|
||||
'short' => t('longer than'),
|
||||
'method' => 'op_longer',
|
||||
'values' => 1,
|
||||
),
|
||||
);
|
||||
// if the definition allows for the empty operator, add it.
|
||||
if (!empty($this->definition['allow empty'])) {
|
||||
$operators += array(
|
||||
'empty' => array(
|
||||
'title' => t('Is empty (NULL)'),
|
||||
'method' => 'op_empty',
|
||||
'short' => t('empty'),
|
||||
'values' => 0,
|
||||
),
|
||||
'not empty' => array(
|
||||
'title' => t('Is not empty (NOT NULL)'),
|
||||
'method' => 'op_empty',
|
||||
'short' => t('not empty'),
|
||||
'values' => 0,
|
||||
),
|
||||
);
|
||||
}
|
||||
// Add regexp support for MySQL.
|
||||
if (Database::getConnection()->databaseType() == 'mysql') {
|
||||
$operators += array(
|
||||
'regular_expression' => array(
|
||||
'title' => t('Regular expression'),
|
||||
'short' => t('regex'),
|
||||
'method' => 'op_regex',
|
||||
'values' => 1,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return $operators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build strings from the operators() for 'select' options
|
||||
*/
|
||||
function operator_options($which = 'title') {
|
||||
$options = array();
|
||||
foreach ($this->operators() as $id => $info) {
|
||||
$options[$id] = $info[$which];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function admin_summary() {
|
||||
if ($this->is_a_group()) {
|
||||
return t('grouped');
|
||||
}
|
||||
if (!empty($this->options['exposed'])) {
|
||||
return t('exposed');
|
||||
}
|
||||
|
||||
$options = $this->operator_options('short');
|
||||
$output = '';
|
||||
if(!empty($options[$this->operator])) {
|
||||
$output = check_plain($options[$this->operator]);
|
||||
}
|
||||
if (in_array($this->operator, $this->operator_values(1))) {
|
||||
$output .= ' ' . check_plain($this->value);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
function operator_values($values = 1) {
|
||||
$options = array();
|
||||
foreach ($this->operators() as $id => $info) {
|
||||
if (isset($info['values']) && $info['values'] == $values) {
|
||||
$options[] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a simple textfield for equality
|
||||
*/
|
||||
function value_form(&$form, &$form_state) {
|
||||
// We have to make some choices when creating this as an exposed
|
||||
// filter form. For example, if the operator is locked and thus
|
||||
// not rendered, we can't render dependencies; instead we only
|
||||
// render the form items we need.
|
||||
$which = 'all';
|
||||
if (!empty($form['operator'])) {
|
||||
$source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
|
||||
}
|
||||
if (!empty($form_state['exposed'])) {
|
||||
$identifier = $this->options['expose']['identifier'];
|
||||
|
||||
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
|
||||
// exposed and locked.
|
||||
$which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
|
||||
}
|
||||
else {
|
||||
$source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($which == 'all' || $which == 'value') {
|
||||
$form['value'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Value'),
|
||||
'#size' => 30,
|
||||
'#default_value' => $this->value,
|
||||
);
|
||||
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
|
||||
$form_state['input'][$identifier] = $this->value;
|
||||
}
|
||||
|
||||
if ($which == 'all') {
|
||||
$form['value'] += array(
|
||||
'#dependency' => array($source => $this->operator_values(1)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($form['value'])) {
|
||||
// Ensure there is something in the 'value'.
|
||||
$form['value'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => NULL
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function operator() {
|
||||
return $this->operator == '=' ? 'LIKE' : 'NOT LIKE';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add this filter to the query.
|
||||
*
|
||||
* Due to the nature of fapi, the value and the operator have an unintended
|
||||
* level of indirection. You will find them in $this->operator
|
||||
* and $this->value respectively.
|
||||
*/
|
||||
function query() {
|
||||
$this->ensure_my_table();
|
||||
$field = "$this->table_alias.$this->real_field";
|
||||
|
||||
$info = $this->operators();
|
||||
if (!empty($info[$this->operator]['method'])) {
|
||||
$this->{$info[$this->operator]['method']}($field);
|
||||
}
|
||||
}
|
||||
|
||||
function op_equal($field) {
|
||||
$this->query->add_where($this->options['group'], $field, $this->value, $this->operator());
|
||||
}
|
||||
|
||||
function op_contains($field) {
|
||||
$this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'LIKE');
|
||||
}
|
||||
|
||||
function op_word($field) {
|
||||
$where = $this->operator == 'word' ? db_or() : db_and();
|
||||
|
||||
// Don't filter on empty strings.
|
||||
if (empty($this->value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->value, $matches, PREG_SET_ORDER);
|
||||
foreach ($matches as $match) {
|
||||
$phrase = false;
|
||||
// Strip off phrase quotes
|
||||
if ($match[2]{0} == '"') {
|
||||
$match[2] = substr($match[2], 1, -1);
|
||||
$phrase = true;
|
||||
}
|
||||
$words = trim($match[2], ',?!();:-');
|
||||
$words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
|
||||
foreach ($words as $word) {
|
||||
$placeholder = $this->placeholder();
|
||||
$where->condition($field, '%' . db_like(trim($word, " ,!?")) . '%', 'LIKE');
|
||||
}
|
||||
}
|
||||
|
||||
if (!$where) {
|
||||
return;
|
||||
}
|
||||
|
||||
// previously this was a call_user_func_array but that's unnecessary
|
||||
// as views will unpack an array that is a single arg.
|
||||
$this->query->add_where($this->options['group'], $where);
|
||||
}
|
||||
|
||||
function op_starts($field) {
|
||||
$this->query->add_where($this->options['group'], $field, db_like($this->value) . '%', 'LIKE');
|
||||
}
|
||||
|
||||
function op_not_starts($field) {
|
||||
$this->query->add_where($this->options['group'], $field, db_like($this->value) . '%', 'NOT LIKE');
|
||||
}
|
||||
|
||||
function op_ends($field) {
|
||||
$this->query->add_where($this->options['group'], $field, '%' . db_like($this->value), 'LIKE');
|
||||
}
|
||||
|
||||
function op_not_ends($field) {
|
||||
$this->query->add_where($this->options['group'], $field, '%' . db_like($this->value), 'NOT LIKE');
|
||||
}
|
||||
|
||||
function op_not($field) {
|
||||
$this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'NOT LIKE');
|
||||
}
|
||||
|
||||
function op_shorter($field) {
|
||||
$placeholder = $this->placeholder();
|
||||
$this->query->add_where_expression($this->options['group'], "LENGTH($field) < $placeholder", array($placeholder => $this->value));
|
||||
}
|
||||
|
||||
function op_longer($field) {
|
||||
$placeholder = $this->placeholder();
|
||||
$this->query->add_where_expression($this->options['group'], "LENGTH($field) > $placeholder", array($placeholder => $this->value));
|
||||
}
|
||||
|
||||
function op_regex($field) {
|
||||
$this->query->add_where($this->options['group'], $field, $this->value, 'RLIKE');
|
||||
}
|
||||
|
||||
function op_empty($field) {
|
||||
if ($this->operator == 'empty') {
|
||||
$operator = "IS NULL";
|
||||
}
|
||||
else {
|
||||
$operator = "IS NOT NULL";
|
||||
}
|
||||
|
||||
$this->query->add_where($this->options['group'], $field, NULL, $operator);
|
||||
}
|
||||
|
||||
}
|
186
sites/all/modules/views/handlers/views_handler_relationship.inc
Normal file
186
sites/all/modules/views/handlers/views_handler_relationship.inc
Normal file
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Views' relationship handlers.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup views_relationship_handlers Views relationship handlers
|
||||
* @{
|
||||
* Handlers to tell Views how to create alternate relationships.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple relationship handler that allows a new version of the primary table
|
||||
* to be linked in.
|
||||
*
|
||||
* The base relationship handler can only handle a single join. Some relationships
|
||||
* are more complex and might require chains of joins; for those, you must
|
||||
* utilize a custom relationship handler.
|
||||
*
|
||||
* Definition items:
|
||||
* - base: The new base table this relationship will be adding. This does not
|
||||
* have to be a declared base table, but if there are no tables that
|
||||
* utilize this base table, it won't be very effective.
|
||||
* - base field: The field to use in the relationship; if left out this will be
|
||||
* assumed to be the primary field.
|
||||
* - relationship table: The actual table this relationship operates against.
|
||||
* This is analogous to using a 'table' override.
|
||||
* - relationship field: The actual field this relationship operates against.
|
||||
* This is analogous to using a 'real field' override.
|
||||
* - label: The default label to provide for this relationship, which is
|
||||
* shown in parentheses next to any field/sort/filter/argument that uses
|
||||
* the relationship.
|
||||
*
|
||||
* @ingroup views_relationship_handlers
|
||||
*/
|
||||
class views_handler_relationship extends views_handler {
|
||||
/**
|
||||
* Init handler to let relationships live on tables other than
|
||||
* the table they operate on.
|
||||
*/
|
||||
function init(&$view, &$options) {
|
||||
parent::init($view, $options);
|
||||
if (isset($this->definition['relationship table'])) {
|
||||
$this->table = $this->definition['relationship table'];
|
||||
}
|
||||
if (isset($this->definition['relationship field'])) {
|
||||
// Set both real_field and field so custom handler
|
||||
// can rely on the old field value.
|
||||
$this->real_field = $this->field = $this->definition['relationship field'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this field's label.
|
||||
*/
|
||||
function label() {
|
||||
if (!isset($this->options['label'])) {
|
||||
return $this->ui_name();
|
||||
}
|
||||
return $this->options['label'];
|
||||
}
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
|
||||
// Relationships definitions should define a default label, but if they aren't get another default value.
|
||||
if (!empty($this->definition['label'])) {
|
||||
$label = $this->definition['label'];
|
||||
}
|
||||
else {
|
||||
$label = !empty($this->definition['field']) ? $this->definition['field'] : $this->definition['base field'];
|
||||
}
|
||||
|
||||
$options['label'] = array('default' => $label, 'translatable' => TRUE);
|
||||
$options['required'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default options form that provides the label widget that all fields
|
||||
* should have.
|
||||
*/
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
$form['label'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Identifier'),
|
||||
'#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
|
||||
'#description' => t('Edit the administrative label displayed when referencing this relationship from filters, etc.'),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
|
||||
$form['required'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Require this relationship'),
|
||||
'#description' => t('Enable to hide items that do not contain this relationship'),
|
||||
'#default_value' => !empty($this->options['required']),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to implement a relationship in a query.
|
||||
*/
|
||||
function query() {
|
||||
// Figure out what base table this relationship brings to the party.
|
||||
$table_data = views_fetch_data($this->definition['base']);
|
||||
$base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
|
||||
|
||||
$this->ensure_my_table();
|
||||
|
||||
$def = $this->definition;
|
||||
$def['table'] = $this->definition['base'];
|
||||
$def['field'] = $base_field;
|
||||
$def['left_table'] = $this->table_alias;
|
||||
$def['left_field'] = $this->real_field;
|
||||
if (!empty($this->options['required'])) {
|
||||
$def['type'] = 'INNER';
|
||||
}
|
||||
|
||||
if (!empty($this->definition['extra'])) {
|
||||
$def['extra'] = $this->definition['extra'];
|
||||
}
|
||||
|
||||
if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
|
||||
$join = new $def['join_handler'];
|
||||
}
|
||||
else {
|
||||
$join = new views_join();
|
||||
}
|
||||
|
||||
$join->definition = $def;
|
||||
$join->options = $this->options;
|
||||
$join->construct();
|
||||
$join->adjusted = TRUE;
|
||||
|
||||
// use a short alias for this:
|
||||
$alias = $def['table'] . '_' . $this->table;
|
||||
|
||||
$this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
|
||||
|
||||
// Add access tags if the base table provide it.
|
||||
if (empty($this->query->options['disable_sql_rewrite']) && isset($table_data['table']['base']['access query tag'])) {
|
||||
$access_tag = $table_data['table']['base']['access query tag'];
|
||||
$this->query->add_tag($access_tag);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* You can't groupby a relationship.
|
||||
*/
|
||||
function use_group_by() {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A special handler to take the place of missing or broken handlers.
|
||||
*
|
||||
* @ingroup views_relationship_handlers
|
||||
*/
|
||||
class views_handler_relationship_broken extends views_handler_relationship {
|
||||
function ui_name($short = FALSE) {
|
||||
return t('Broken/missing handler');
|
||||
}
|
||||
|
||||
function ensure_my_table() { /* No table to ensure! */ }
|
||||
function query() { /* No query to run */ }
|
||||
function options_form(&$form, &$form_state) {
|
||||
$form['markup'] = array(
|
||||
'#markup' => '<div class="form-item description">' . 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.') . '</div>',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the handler is considered 'broken'
|
||||
*/
|
||||
function broken() { return TRUE; }
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
@@ -0,0 +1,382 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Relationship for groupwise maximum handler.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Relationship handler that allows a groupwise maximum of the linked in table.
|
||||
* For a definition, see:
|
||||
* http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html
|
||||
* In lay terms, instead of joining to get all matching records in the linked
|
||||
* table, we get only one record, a 'representative record' picked according
|
||||
* to a given criteria.
|
||||
*
|
||||
* Example:
|
||||
* Suppose we have a term view that gives us the terms: Horse, Cat, Aardvark.
|
||||
* We wish to show for each term the most recent node of that term.
|
||||
* What we want is some kind of relationship from term to node.
|
||||
* But a regular relationship will give us all the nodes for each term,
|
||||
* giving the view multiple rows per term. What we want is just one
|
||||
* representative node per term, the node that is the 'best' in some way:
|
||||
* eg, the most recent, the most commented on, the first in alphabetical order.
|
||||
*
|
||||
* This handler gives us that kind of relationship from term to node.
|
||||
* The method of choosing the 'best' implemented with a sort
|
||||
* that the user selects in the relationship settings.
|
||||
*
|
||||
* So if we want our term view to show the most commented node for each term,
|
||||
* add the relationship and in its options, pick the 'Comment count' sort.
|
||||
*
|
||||
* Relationship definition
|
||||
* - 'outer field': The outer field to substitute into the correlated subquery.
|
||||
* This must be the full field name, not the alias.
|
||||
* Eg: 'term_data.tid'.
|
||||
* - 'argument table',
|
||||
* 'argument field': These options define a views argument that the subquery
|
||||
* must add to itself to filter by the main view.
|
||||
* Example: the main view shows terms, this handler is being used to get to
|
||||
* the nodes base table. Your argument must be 'term_node', 'tid', as this
|
||||
* is the argument that should be added to a node view to filter on terms.
|
||||
*
|
||||
* A note on performance:
|
||||
* This relationship uses a correlated subquery, which is expensive.
|
||||
* Subsequent versions of this handler could also implement the alternative way
|
||||
* of doing this, with a join -- though this looks like it could be pretty messy
|
||||
* to implement. This is also an expensive method, so providing both methods and
|
||||
* allowing the user to choose which one works fastest for their data might be
|
||||
* the best way.
|
||||
* If your use of this relationship handler is likely to result in large
|
||||
* data sets, you might want to consider storing statistics in a separate table,
|
||||
* in the same way as node_comment_statistics.
|
||||
*
|
||||
* @ingroup views_relationship_handlers
|
||||
*/
|
||||
class views_handler_relationship_groupwise_max extends views_handler_relationship {
|
||||
|
||||
/**
|
||||
* Defines default values for options.
|
||||
*/
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['subquery_sort'] = array('default' => NULL);
|
||||
// Descending more useful.
|
||||
$options['subquery_order'] = array('default' => 'DESC');
|
||||
$options['subquery_regenerate'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
$options['subquery_view'] = array('default' => FALSE);
|
||||
$options['subquery_namespace'] = array('default' => FALSE);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extends the relationship's basic options, allowing the user to pick
|
||||
* a sort and an order for it.
|
||||
*/
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
// Get the sorts that apply to our base.
|
||||
$sorts = views_fetch_fields($this->definition['base'], 'sort');
|
||||
foreach ($sorts as $sort_id => $sort) {
|
||||
$sort_options[$sort_id] = "$sort[group]: $sort[title]";
|
||||
}
|
||||
$base_table_data = views_fetch_data($this->definition['base']);
|
||||
|
||||
$form['subquery_sort'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Representative sort criteria'),
|
||||
// Provide the base field as sane default sort option.
|
||||
'#default_value' => !empty($this->options['subquery_sort']) ? $this->options['subquery_sort'] : $this->definition['base'] . '.' . $base_table_data['table']['base']['field'],
|
||||
'#options' => $sort_options,
|
||||
'#description' => theme('advanced_help_topic', array('module' => 'views', 'topic' => 'relationship-representative')) .
|
||||
t("The sort criteria is applied to the data brought in by the relationship to determine how a representative item is obtained for each row. For example, to show the most recent node for each user, pick 'Content: Updated date'."),
|
||||
);
|
||||
|
||||
$form['subquery_order'] = array(
|
||||
'#type' => 'radios',
|
||||
'#title' => t('Representative sort order'),
|
||||
'#description' => t("The ordering to use for the sort criteria selected above."),
|
||||
'#options' => array('ASC' => t('Ascending'), 'DESC' => t('Descending')),
|
||||
'#default_value' => $this->options['subquery_order'],
|
||||
);
|
||||
|
||||
$form['subquery_namespace'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Subquery namespace'),
|
||||
'#description' => t('Advanced. Enter a namespace for the subquery used by this relationship.'),
|
||||
'#default_value' => $this->options['subquery_namespace'],
|
||||
);
|
||||
|
||||
|
||||
// WIP: This stuff doens't work yet: namespacing issues.
|
||||
// A list of suitable views to pick one as the subview.
|
||||
$views = array('' => '<none>');
|
||||
$all_views = views_get_all_views();
|
||||
foreach ($all_views as $view) {
|
||||
// Only get views that are suitable:
|
||||
// - base must the base that our relationship joins towards
|
||||
// - must have fields.
|
||||
if ($view->base_table == $this->definition['base'] && !empty($view->display['default']->display_options['fields'])) {
|
||||
// TODO: check the field is the correct sort?
|
||||
// or let users hang themselves at this stage and check later?
|
||||
if ($view->type == 'Default') {
|
||||
$views[t('Default Views')][$view->name] = $view->name;
|
||||
}
|
||||
else {
|
||||
$views[t('Existing Views')][$view->name] = $view->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$form['subquery_view'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Representative view'),
|
||||
'#default_value' => $this->options['subquery_view'],
|
||||
'#options' => $views,
|
||||
'#description' => t('Advanced. Use another view to generate the relationship subquery. This allows you to use filtering and more than one sort. If you pick a view here, the sort options above are ignored. Your view must have the ID of its base as its only field, and should have some kind of sorting.'),
|
||||
);
|
||||
|
||||
$form['subquery_regenerate'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Generate subquery each time view is run.'),
|
||||
'#default_value' => $this->options['subquery_regenerate'],
|
||||
'#description' => t('Will re-generate the subquery for this relationship every time the view is run, instead of only when these options are saved. Use for testing if you are making changes elsewhere. WARNING: seriously impairs performance.'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to create a pseudo view.
|
||||
*
|
||||
* We use this to obtain our subquery SQL.
|
||||
*/
|
||||
function get_temporary_view() {
|
||||
views_include('view');
|
||||
$view = new view();
|
||||
$view->vid = 'new'; // @todo: what's this?
|
||||
$view->base_table = $this->definition['base'];
|
||||
$view->add_display('default');
|
||||
return $view;
|
||||
}
|
||||
|
||||
/**
|
||||
* When the form is submitted, take sure to clear the subquery string cache.
|
||||
*/
|
||||
function options_form_submit(&$form, &$form_state) {
|
||||
$cid = 'views_relationship_groupwise_max:' . $this->view->name . ':' . $this->view->current_display . ':' . $this->options['id'];
|
||||
cache_clear_all($cid, 'cache_views_data');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a subquery given the user options, as set in the options.
|
||||
* These are passed in rather than picked up from the object because we
|
||||
* generate the subquery when the options are saved, rather than when the view
|
||||
* is run. This saves considerable time.
|
||||
*
|
||||
* @param $options
|
||||
* An array of options:
|
||||
* - subquery_sort: the id of a views sort.
|
||||
* - subquery_order: either ASC or DESC.
|
||||
* @return
|
||||
* The subquery SQL string, ready for use in the main query.
|
||||
*/
|
||||
function left_query($options) {
|
||||
// Either load another view, or create one on the fly.
|
||||
if ($options['subquery_view']) {
|
||||
$temp_view = views_get_view($options['subquery_view']);
|
||||
// Remove all fields from default display
|
||||
unset($temp_view->display['default']->display_options['fields']);
|
||||
}
|
||||
else {
|
||||
// Create a new view object on the fly, which we use to generate a query
|
||||
// object and then get the SQL we need for the subquery.
|
||||
$temp_view = $this->get_temporary_view();
|
||||
|
||||
// Add the sort from the options to the default display.
|
||||
// This is broken, in that the sort order field also gets added as a
|
||||
// select field. See http://drupal.org/node/844910.
|
||||
// We work around this further down.
|
||||
$sort = $options['subquery_sort'];
|
||||
list($sort_table, $sort_field) = explode('.', $sort);
|
||||
$sort_options = array('order' => $options['subquery_order']);
|
||||
$temp_view->add_item('default', 'sort', $sort_table, $sort_field, $sort_options);
|
||||
}
|
||||
|
||||
// Get the namespace string.
|
||||
$temp_view->namespace = (!empty($options['subquery_namespace'])) ? '_'. $options['subquery_namespace'] : '_INNER';
|
||||
$this->subquery_namespace = (!empty($options['subquery_namespace'])) ? '_'. $options['subquery_namespace'] : 'INNER';
|
||||
|
||||
// The value we add here does nothing, but doing this adds the right tables
|
||||
// and puts in a WHERE clause with a placeholder we can grab later.
|
||||
$temp_view->args[] = '**CORRELATED**';
|
||||
|
||||
// Add the base table ID field.
|
||||
$views_data = views_fetch_data($this->definition['base']);
|
||||
$base_field = $views_data['table']['base']['field'];
|
||||
$temp_view->add_item('default', 'field', $this->definition['base'], $this->definition['field']);
|
||||
|
||||
// Add the correct argument for our relationship's base
|
||||
// ie the 'how to get back to base' argument.
|
||||
// The relationship definition tells us which one to use.
|
||||
$temp_view->add_item(
|
||||
'default',
|
||||
'argument',
|
||||
$this->definition['argument table'], // eg 'term_node',
|
||||
$this->definition['argument field'] // eg 'tid'
|
||||
);
|
||||
|
||||
// Build the view. The creates the query object and produces the query
|
||||
// string but does not run any queries.
|
||||
$temp_view->build();
|
||||
|
||||
// Now take the SelectQuery object the View has built and massage it
|
||||
// somewhat so we can get the SQL query from it.
|
||||
$subquery = $temp_view->build_info['query'];
|
||||
|
||||
// Workaround until http://drupal.org/node/844910 is fixed:
|
||||
// Remove all fields from the SELECT except the base id.
|
||||
$fields =& $subquery->getFields();
|
||||
foreach (array_keys($fields) as $field_name) {
|
||||
// The base id for this subquery is stored in our definition.
|
||||
if ($field_name != $this->definition['field']) {
|
||||
unset($fields[$field_name]);
|
||||
}
|
||||
}
|
||||
|
||||
// Make every alias in the subquery safe within the outer query by
|
||||
// appending a namespace to it, '_inner' by default.
|
||||
$tables =& $subquery->getTables();
|
||||
foreach (array_keys($tables) as $table_name) {
|
||||
$tables[$table_name]['alias'] .= $this->subquery_namespace;
|
||||
// Namespace the join on every table.
|
||||
if (isset($tables[$table_name]['condition'])) {
|
||||
$tables[$table_name]['condition'] = $this->condition_namespace($tables[$table_name]['condition']);
|
||||
}
|
||||
}
|
||||
// Namespace fields.
|
||||
foreach (array_keys($fields) as $field_name) {
|
||||
$fields[$field_name]['table'] .= $this->subquery_namespace;
|
||||
$fields[$field_name]['alias'] .= $this->subquery_namespace;
|
||||
}
|
||||
// Namespace conditions.
|
||||
$where =& $subquery->conditions();
|
||||
$this->alter_subquery_condition($subquery, $where);
|
||||
// Not sure why, but our sort order clause doesn't have a table.
|
||||
// TODO: the call to add_item() above to add the sort handler is probably
|
||||
// wrong -- needs attention from someone who understands it.
|
||||
// In the meantime, this works, but with a leap of faith...
|
||||
$orders =& $subquery->getOrderBy();
|
||||
foreach ($orders as $order_key => $order) {
|
||||
// But if we're using a whole view, we don't know what we have!
|
||||
if ($options['subquery_view']) {
|
||||
list($sort_table, $sort_field) = explode('.', $order_key);
|
||||
}
|
||||
$orders[$sort_table . $this->subquery_namespace . '.' . $sort_field] = $order;
|
||||
unset($orders[$order_key]);
|
||||
}
|
||||
|
||||
// The query we get doesn't include the LIMIT, so add it here.
|
||||
$subquery->range(0, 1);
|
||||
|
||||
// Extract the SQL the temporary view built.
|
||||
$subquery_sql = $subquery->__toString();
|
||||
|
||||
// Replace the placeholder with the outer, correlated field.
|
||||
// Eg, change the placeholder ':users_uid' into the outer field 'users.uid'.
|
||||
// We have to work directly with the SQL, because putting a name of a field
|
||||
// into a SelectQuery that it does not recognize (because it's outer) just
|
||||
// makes it treat it as a string.
|
||||
$outer_placeholder = ':' . str_replace('.', '_', $this->definition['outer field']);
|
||||
$subquery_sql = str_replace($outer_placeholder, $this->definition['outer field'], $subquery_sql);
|
||||
|
||||
return $subquery_sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive helper to add a namespace to conditions.
|
||||
*
|
||||
* Similar to _views_query_tag_alter_condition().
|
||||
*
|
||||
* (Though why is the condition we get in a simple query 3 levels deep???)
|
||||
*/
|
||||
function alter_subquery_condition(QueryAlterableInterface $query, &$conditions) {
|
||||
foreach ($conditions as $condition_id => &$condition) {
|
||||
// Skip the #conjunction element.
|
||||
if (is_numeric($condition_id)) {
|
||||
if (is_string($condition['field'])) {
|
||||
$condition['field'] = $this->condition_namespace($condition['field']);
|
||||
}
|
||||
elseif (is_object($condition['field'])) {
|
||||
$sub_conditions =& $condition['field']->conditions();
|
||||
$this->alter_subquery_condition($query, $sub_conditions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to namespace query pieces.
|
||||
*
|
||||
* Turns 'foo.bar' into 'foo_NAMESPACE.bar'.
|
||||
*/
|
||||
function condition_namespace($string) {
|
||||
return str_replace('.', $this->subquery_namespace . '.', $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to implement a relationship in a query.
|
||||
* This is mostly a copy of our parent's query() except for this bit with
|
||||
* the join class.
|
||||
*/
|
||||
function query() {
|
||||
// Figure out what base table this relationship brings to the party.
|
||||
$table_data = views_fetch_data($this->definition['base']);
|
||||
$base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
|
||||
|
||||
$this->ensure_my_table();
|
||||
|
||||
$def = $this->definition;
|
||||
$def['table'] = $this->definition['base'];
|
||||
$def['field'] = $base_field;
|
||||
$def['left_table'] = $this->table_alias;
|
||||
$def['left_field'] = $this->field;
|
||||
if (!empty($this->options['required'])) {
|
||||
$def['type'] = 'INNER';
|
||||
}
|
||||
|
||||
if ($this->options['subquery_regenerate']) {
|
||||
// For testing only, regenerate the subquery each time.
|
||||
$def['left_query'] = $this->left_query($this->options);
|
||||
}
|
||||
else {
|
||||
// Get the stored subquery SQL string.
|
||||
$cid = 'views_relationship_groupwise_max:' . $this->view->name . ':' . $this->view->current_display . ':' . $this->options['id'];
|
||||
$cache = cache_get($cid, 'cache_views_data');
|
||||
if (isset($cache->data)) {
|
||||
$def['left_query'] = $cache->data;
|
||||
}
|
||||
else {
|
||||
$def['left_query'] = $this->left_query($this->options);
|
||||
cache_set($cid, $def['left_query'], 'cache_views_data');
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
|
||||
$join = new $def['join_handler'];
|
||||
}
|
||||
else {
|
||||
$join = new views_join_subquery();
|
||||
}
|
||||
|
||||
$join->definition = $def;
|
||||
$join->construct();
|
||||
$join->adjusted = TRUE;
|
||||
|
||||
// use a short alias for this:
|
||||
$alias = $def['table'] . '_' . $this->table;
|
||||
|
||||
$this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
|
||||
}
|
||||
}
|
240
sites/all/modules/views/handlers/views_handler_sort.inc
Normal file
240
sites/all/modules/views/handlers/views_handler_sort.inc
Normal file
@@ -0,0 +1,240 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @todo.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup views_sort_handlers Views sort handlers
|
||||
* @{
|
||||
* Handlers to tell Views how to sort queries.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base sort handler that has no options and performs a simple sort.
|
||||
*
|
||||
* @ingroup views_sort_handlers
|
||||
*/
|
||||
class views_handler_sort extends views_handler {
|
||||
|
||||
/**
|
||||
* Determine if a sort can be exposed.
|
||||
*/
|
||||
function can_expose() { return TRUE; }
|
||||
|
||||
/**
|
||||
* Called to add the sort to a query.
|
||||
*/
|
||||
function query() {
|
||||
$this->ensure_my_table();
|
||||
// Add the field.
|
||||
$this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
|
||||
}
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['order'] = array('default' => 'ASC');
|
||||
$options['exposed'] = array('default' => FALSE, 'bool' => TRUE);
|
||||
$options['expose'] = array(
|
||||
'contains' => array(
|
||||
'label' => array('default' => '', 'translatable' => TRUE),
|
||||
),
|
||||
);
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display whether or not the sort order is ascending or descending
|
||||
*/
|
||||
function admin_summary() {
|
||||
if (!empty($this->options['exposed'])) {
|
||||
return t('Exposed');
|
||||
}
|
||||
switch ($this->options['order']) {
|
||||
case 'ASC':
|
||||
case 'asc':
|
||||
default:
|
||||
return t('asc');
|
||||
break;
|
||||
case 'DESC';
|
||||
case 'desc';
|
||||
return t('desc');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic options for all sort criteria
|
||||
*/
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
if ($this->can_expose()) {
|
||||
$this->show_expose_button($form, $form_state);
|
||||
}
|
||||
$form['op_val_start'] = array('#value' => '<div class="clearfix">');
|
||||
$this->show_sort_form($form, $form_state);
|
||||
$form['op_val_end'] = array('#value' => '</div>');
|
||||
if ($this->can_expose()) {
|
||||
$this->show_expose_form($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut to display the expose/hide button.
|
||||
*/
|
||||
function show_expose_button(&$form, &$form_state) {
|
||||
$form['expose_button'] = array(
|
||||
'#prefix' => '<div class="views-expose clearfix">',
|
||||
'#suffix' => '</div>',
|
||||
// Should always come first
|
||||
'#weight' => -1000,
|
||||
);
|
||||
|
||||
// Add a checkbox for JS users, which will have behavior attached to it
|
||||
// so it can replace the button.
|
||||
$form['expose_button']['checkbox'] = array(
|
||||
'#theme_wrappers' => array('container'),
|
||||
'#attributes' => array('class' => array('js-only')),
|
||||
);
|
||||
$form['expose_button']['checkbox']['checkbox'] = array(
|
||||
'#title' => t('Expose this sort to visitors, to allow them to change it'),
|
||||
'#type' => 'checkbox',
|
||||
);
|
||||
|
||||
// Then add the button itself.
|
||||
if (empty($this->options['exposed'])) {
|
||||
$form['expose_button']['markup'] = array(
|
||||
'#markup' => '<div class="description exposed-description" style="float: left; margin-right:10px">' . t('This sort is not exposed. Expose it to allow the users to change it.') . '</div>',
|
||||
);
|
||||
$form['expose_button']['button'] = array(
|
||||
'#limit_validation_errors' => array(),
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Expose sort'),
|
||||
'#submit' => array('views_ui_config_item_form_expose'),
|
||||
);
|
||||
$form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
|
||||
}
|
||||
else {
|
||||
$form['expose_button']['markup'] = array(
|
||||
'#markup' => '<div class="description exposed-description">' . t('This sort is exposed. If you hide it, users will not be able to change it.') . '</div>',
|
||||
);
|
||||
$form['expose_button']['button'] = array(
|
||||
'#limit_validation_errors' => array(),
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Hide sort'),
|
||||
'#submit' => array('views_ui_config_item_form_expose'),
|
||||
);
|
||||
$form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple validate handler
|
||||
*/
|
||||
function options_validate(&$form, &$form_state) {
|
||||
$this->sort_validate($form, $form_state);
|
||||
if (!empty($this->options['exposed'])) {
|
||||
$this->expose_validate($form, $form_state);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple submit handler
|
||||
*/
|
||||
function options_submit(&$form, &$form_state) {
|
||||
unset($form_state['values']['expose_button']); // don't store this.
|
||||
$this->sort_submit($form, $form_state);
|
||||
if (!empty($this->options['exposed'])) {
|
||||
$this->expose_submit($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut to display the value form.
|
||||
*/
|
||||
function show_sort_form(&$form, &$form_state) {
|
||||
$options = $this->sort_options();
|
||||
if (!empty($options)) {
|
||||
$form['order'] = array(
|
||||
'#type' => 'radios',
|
||||
'#options' => $options,
|
||||
'#default_value' => $this->options['order'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function sort_validate(&$form, &$form_state) { }
|
||||
|
||||
function sort_submit(&$form, &$form_state) { }
|
||||
|
||||
/**
|
||||
* Provide a list of options for the default sort form.
|
||||
* Should be overridden by classes that don't override sort_form
|
||||
*/
|
||||
function sort_options() {
|
||||
return array(
|
||||
'ASC' => t('Sort ascending'),
|
||||
'DESC' => t('Sort descending'),
|
||||
);
|
||||
}
|
||||
|
||||
function expose_form(&$form, &$form_state) {
|
||||
// #flatten will move everything from $form['expose'][$key] to $form[$key]
|
||||
// prior to rendering. That's why the pre_render for it needs to run first,
|
||||
// so that when the next pre_render (the one for fieldsets) runs, it gets
|
||||
// the flattened data.
|
||||
array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
|
||||
$form['expose']['#flatten'] = TRUE;
|
||||
|
||||
$form['expose']['label'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#default_value' => $this->options['expose']['label'],
|
||||
'#title' => t('Label'),
|
||||
'#required' => TRUE,
|
||||
'#size' => 40,
|
||||
'#weight' => -1,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide default options for exposed sorts.
|
||||
*/
|
||||
function expose_options() {
|
||||
$this->options['expose'] = array(
|
||||
'order' => $this->options['order'],
|
||||
'label' => $this->definition['title'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A special handler to take the place of missing or broken handlers.
|
||||
*
|
||||
* @ingroup views_sort_handlers
|
||||
*/
|
||||
class views_handler_sort_broken extends views_handler_sort {
|
||||
function ui_name($short = FALSE) {
|
||||
return t('Broken/missing handler');
|
||||
}
|
||||
|
||||
function ensure_my_table() { /* No table to ensure! */ }
|
||||
function query($group_by = FALSE) { /* No query to run */ }
|
||||
function options_form(&$form, &$form_state) {
|
||||
$form['markup'] = array(
|
||||
'#markup' => '<div class="form-item description">' . 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.') . '</div>',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the handler is considered 'broken'
|
||||
*/
|
||||
function broken() { return TRUE; }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
74
sites/all/modules/views/handlers/views_handler_sort_date.inc
Normal file
74
sites/all/modules/views/handlers/views_handler_sort_date.inc
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_sort_date.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Basic sort handler for dates.
|
||||
*
|
||||
* This handler enables granularity, which is the ability to make dates
|
||||
* equivalent based upon nearness.
|
||||
*
|
||||
* @ingroup views_sort_handlers
|
||||
*/
|
||||
class views_handler_sort_date extends views_handler_sort {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
$options['granularity'] = array('default' => 'second');
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
$form['granularity'] = array(
|
||||
'#type' => 'radios',
|
||||
'#title' => t('Granularity'),
|
||||
'#options' => array(
|
||||
'second' => t('Second'),
|
||||
'minute' => t('Minute'),
|
||||
'hour' => t('Hour'),
|
||||
'day' => t('Day'),
|
||||
'month' => t('Month'),
|
||||
'year' => t('Year'),
|
||||
),
|
||||
'#description' => t('The granularity is the smallest unit to use when determining whether two dates are the same; for example, if the granularity is "Year" then all dates in 1999, regardless of when they fall in 1999, will be considered the same date.'),
|
||||
'#default_value' => $this->options['granularity'],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to add the sort to a query.
|
||||
*/
|
||||
function query() {
|
||||
$this->ensure_my_table();
|
||||
switch ($this->options['granularity']) {
|
||||
case 'second':
|
||||
default:
|
||||
$this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
|
||||
return;
|
||||
case 'minute':
|
||||
$formula = views_date_sql_format('YmdHi', "$this->table_alias.$this->real_field");
|
||||
break;
|
||||
case 'hour':
|
||||
$formula = views_date_sql_format('YmdH', "$this->table_alias.$this->real_field");
|
||||
break;
|
||||
case 'day':
|
||||
$formula = views_date_sql_format('Ymd', "$this->table_alias.$this->real_field");
|
||||
break;
|
||||
case 'month':
|
||||
$formula = views_date_sql_format('Ym', "$this->table_alias.$this->real_field");
|
||||
break;
|
||||
case 'year':
|
||||
$formula = views_date_sql_format('Y', "$this->table_alias.$this->real_field");
|
||||
break;
|
||||
}
|
||||
|
||||
// Add the field.
|
||||
$this->query->add_orderby(NULL, $formula, $this->options['order'], $this->table_alias . '_' . $this->field . '_' . $this->options['granularity']);
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_sort_group_by_numeric.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Handler for GROUP BY on simple numeric fields.
|
||||
*
|
||||
* @ingroup views_sort_handlers
|
||||
*/
|
||||
class views_handler_sort_group_by_numeric extends views_handler_sort {
|
||||
function init(&$view, &$options) {
|
||||
parent::init($view, $options);
|
||||
|
||||
// Initialize the original handler.
|
||||
$this->handler = views_get_handler($options['table'], $options['field'], 'sort');
|
||||
$this->handler->init($view, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to add the field to a query.
|
||||
*/
|
||||
function query() {
|
||||
$this->ensure_my_table();
|
||||
|
||||
$params = array(
|
||||
'function' => $this->options['group_type'],
|
||||
);
|
||||
|
||||
$this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order'], NULL, $params);
|
||||
}
|
||||
|
||||
function ui_name($short = FALSE) {
|
||||
return $this->get_field(parent::ui_name($short));
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_sort_menu_hierarchy.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sort in menu hierarchy order.
|
||||
*
|
||||
* Given a field name of 'p' this produces an ORDER BY on p1, p2, ..., p9;
|
||||
* and optionally injects multiple joins to {menu_links} to sort by weight
|
||||
* and title as well.
|
||||
*
|
||||
* This is only really useful for the {menu_links} table.
|
||||
*
|
||||
* @ingroup views_sort_handlers
|
||||
*/
|
||||
class views_handler_sort_menu_hierarchy extends views_handler_sort {
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
$options['sort_within_level'] = array('default' => FALSE);
|
||||
return $options;
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
$form['sort_within_level'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Sort within each hierarchy level'),
|
||||
'#description' => t('Enable this to sort the items within each level of the hierarchy by weight and title. Warning: this may produce a slow query.'),
|
||||
'#default_value' => $this->options['sort_within_level'],
|
||||
);
|
||||
}
|
||||
|
||||
function query() {
|
||||
$this->ensure_my_table();
|
||||
$max_depth = isset($this->definition['max depth']) ? $this->definition['max depth'] : MENU_MAX_DEPTH;
|
||||
for ($i = 1; $i <= $max_depth; ++$i) {
|
||||
if ($this->options['sort_within_level']) {
|
||||
$join = new views_join();
|
||||
$join->construct('menu_links', $this->table_alias, $this->field . $i, 'mlid');
|
||||
$menu_links = $this->query->add_table('menu_links', NULL, $join);
|
||||
$this->query->add_orderby($menu_links, 'weight', $this->options['order']);
|
||||
$this->query->add_orderby($menu_links, 'link_title', $this->options['order']);
|
||||
}
|
||||
|
||||
// We need this even when also sorting by weight and title, to make sure
|
||||
// that children of two parents with the same weight and title are
|
||||
// correctly separated.
|
||||
$this->query->add_orderby($this->table_alias, $this->field . $i, $this->options['order']);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of views_handler_sort_random.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Handle a random sort.
|
||||
*
|
||||
* @ingroup views_sort_handlers
|
||||
*/
|
||||
class views_handler_sort_random extends views_handler_sort {
|
||||
function query() {
|
||||
$this->query->add_orderby('rand');
|
||||
}
|
||||
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
$form['order']['#access'] = FALSE;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user