| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
/**
|
| 4 |
* An argument handler for use in fields that have a many to one relationship
|
| 5 |
* with the table(s) to the left. This adds a bunch of options that are
|
| 6 |
* reasonably common with this type of relationship.
|
| 7 |
* Definition terms:
|
| 8 |
* - numeric: If true, the field will be considered numeric. Probably should
|
| 9 |
* always be set TRUE as views_handler_argument_string has many to one
|
| 10 |
* capabilities.
|
| 11 |
*
|
| 12 |
* @ingroup views_argument_handlers
|
| 13 |
*/
|
| 14 |
class views_handler_argument_many_to_one extends views_handler_argument {
|
| 15 |
function init(&$view, &$options) {
|
| 16 |
parent::init($view, $options);
|
| 17 |
$this->helper = new views_many_to_one_helper($this);
|
| 18 |
|
| 19 |
// Ensure defaults for these, during summaries and stuff:
|
| 20 |
$this->operator = 'or';
|
| 21 |
$this->value = array();
|
| 22 |
}
|
| 23 |
|
| 24 |
function option_definition() {
|
| 25 |
$options = parent::option_definition();
|
| 26 |
|
| 27 |
if (!empty($this->definition['numeric'])) {
|
| 28 |
$options['break_phrase'] = array('default' => FALSE);
|
| 29 |
}
|
| 30 |
|
| 31 |
$options['add_table'] = array('default' => FALSE);
|
| 32 |
$options['require_value'] = array('default' => FALSE);
|
| 33 |
|
| 34 |
views_many_to_one_helper::option_definition($options);
|
| 35 |
return $options;
|
| 36 |
}
|
| 37 |
|
| 38 |
function options_form(&$form, &$form_state) {
|
| 39 |
parent::options_form($form, $form_state);
|
| 40 |
|
| 41 |
// allow + for or, , for and
|
| 42 |
if (!empty($this->definition['numeric'])) {
|
| 43 |
$form['break_phrase'] = array(
|
| 44 |
'#type' => 'checkbox',
|
| 45 |
'#title' => t('Allow multiple terms per argument.'),
|
| 46 |
'#description' => t('If selected, users can enter multiple arguments in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
|
| 47 |
'#default_value' => !empty($this->options['break_phrase']),
|
| 48 |
);
|
| 49 |
}
|
| 50 |
|
| 51 |
$form['add_table'] = array(
|
| 52 |
'#type' => 'checkbox',
|
| 53 |
'#title' => t('Allow multiple arguments to work together.'),
|
| 54 |
'#description' => t('If selected, multiple instances of this argument can work together, as though multiple terms were supplied to the same argument. This setting is not compatible with the "Reduce duplicates" setting.'),
|
| 55 |
'#default_value' => !empty($this->options['add_table']),
|
| 56 |
);
|
| 57 |
|
| 58 |
$form['require_value'] = array(
|
| 59 |
'#type' => 'checkbox',
|
| 60 |
'#title' => t('Do not display items with no value in summary'),
|
| 61 |
'#default_value' => !empty($this->options['require_value']),
|
| 62 |
);
|
| 63 |
|
| 64 |
$this->helper->options_form($form, $form_state);
|
| 65 |
}
|
| 66 |
|
| 67 |
/**
|
| 68 |
* Override ensure_my_table so we can control how this joins in.
|
| 69 |
* The operator actually has influence over joining.
|
| 70 |
*/
|
| 71 |
function ensure_my_table() {
|
| 72 |
$this->helper->ensure_my_table();
|
| 73 |
}
|
| 74 |
|
| 75 |
function query() {
|
| 76 |
if (empty($this->argument)) {
|
| 77 |
parent::ensure_my_table();
|
| 78 |
$this->query->add_where(0, "$this->table_alias.$this->real_field IS NULL");
|
| 79 |
return;
|
| 80 |
}
|
| 81 |
|
| 82 |
if (!empty($this->options['break_phrase'])) {
|
| 83 |
views_break_phrase($this->argument, $this);
|
| 84 |
}
|
| 85 |
else {
|
| 86 |
$this->value = array($this->argument);
|
| 87 |
$this->operator = 'or';
|
| 88 |
}
|
| 89 |
|
| 90 |
$this->helper->add_filter();
|
| 91 |
}
|
| 92 |
|
| 93 |
function title() {
|
| 94 |
if (!$this->argument) {
|
| 95 |
return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
|
| 96 |
}
|
| 97 |
|
| 98 |
if (!empty($this->options['break_phrase'])) {
|
| 99 |
views_break_phrase($this->argument, $this);
|
| 100 |
}
|
| 101 |
else {
|
| 102 |
$this->value = array($this->argument);
|
| 103 |
$this->operator = 'or';
|
| 104 |
}
|
| 105 |
|
| 106 |
// @todo -- both of these should check definition for alternate keywords.
|
| 107 |
|
| 108 |
if (empty($this->value)) {
|
| 109 |
return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
|
| 110 |
}
|
| 111 |
|
| 112 |
if ($this->value === array(-1)) {
|
| 113 |
return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
|
| 114 |
}
|
| 115 |
|
| 116 |
return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
|
| 117 |
}
|
| 118 |
|
| 119 |
function summary_query() {
|
| 120 |
$field = $this->table . '.' . $this->field;
|
| 121 |
$join = $this->get_join();
|
| 122 |
|
| 123 |
if (!empty($this->options['require_value'])) {
|
| 124 |
$join->type = 'INNER';
|
| 125 |
}
|
| 126 |
|
| 127 |
if (empty($this->options['add_table']) || empty($this->view->many_to_one_tables[$field])) {
|
| 128 |
$this->table_alias = $this->query->ensure_table($this->table, $this->relationship, $join);
|
| 129 |
}
|
| 130 |
else {
|
| 131 |
$this->table_alias = $this->helper->summary_join();
|
| 132 |
}
|
| 133 |
|
| 134 |
// Add the field.
|
| 135 |
$this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
|
| 136 |
|
| 137 |
$this->summary_name_field();
|
| 138 |
|
| 139 |
return $this->summary_basics();
|
| 140 |
}
|
| 141 |
|
| 142 |
function summary_argument($data) {
|
| 143 |
$value = $data->{$this->base_alias};
|
| 144 |
if (empty($value)) {
|
| 145 |
$value = 0;
|
| 146 |
}
|
| 147 |
|
| 148 |
return $value;
|
| 149 |
}
|
| 150 |
|
| 151 |
/**
|
| 152 |
* Override for specific title lookups.
|
| 153 |
*/
|
| 154 |
function title_query() {
|
| 155 |
return $this->value;
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|