1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Date API views argument handler.
|
6
|
*
|
7
|
* This argument combines multiple date arguments into a single argument where
|
8
|
* all fields are controlled by the same date and can be combined with either
|
9
|
* AND or OR.
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Date API argument handler.
|
14
|
*/
|
15
|
class date_views_argument_handler extends date_views_argument_handler_simple {
|
16
|
|
17
|
/**
|
18
|
* {@inheritdoc}
|
19
|
*/
|
20
|
function init(&$view, &$options) {
|
21
|
parent::init($view, $options);
|
22
|
|
23
|
if (empty($this->view->date_info)) {
|
24
|
$this->view->date_info = new stdClass();
|
25
|
}
|
26
|
if (empty($this->view->date_info->date_fields)) {
|
27
|
$this->view->date_info->date_fields = array();
|
28
|
}
|
29
|
$this->view->date_info->date_fields = array_merge($this->view->date_info->date_fields, $this->options['date_fields']);
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* {@inheritdoc}
|
34
|
*/
|
35
|
function option_definition() {
|
36
|
$options = parent::option_definition();
|
37
|
$options['date_fields'] = array('default' => array());
|
38
|
$options['date_method'] = array('default' => 'OR');
|
39
|
$options['date_group'] = array('default' => 'date');
|
40
|
return $options;
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
* {@inheritdoc}
|
45
|
*/
|
46
|
function options_form(&$form, &$form_state) {
|
47
|
parent::options_form($form, $form_state);
|
48
|
$fields = date_views_fields($this->base_table);
|
49
|
$options = array();
|
50
|
foreach ($fields['name'] as $name => $field) {
|
51
|
$options[$name] = $field['label'];
|
52
|
}
|
53
|
|
54
|
$form['date_fields'] = array(
|
55
|
'#title' => t('Date field(s)'),
|
56
|
'#type' => 'checkboxes',
|
57
|
'#options' => $options,
|
58
|
'#default_value' => $this->options['date_fields'],
|
59
|
'#multiple' => TRUE,
|
60
|
'#description' => t("Select one or more date fields to filter with this argument."),
|
61
|
);
|
62
|
$form['date_method'] = array(
|
63
|
'#title' => t('Method'),
|
64
|
'#type' => 'radios',
|
65
|
'#options' => array('OR' => t('OR'), 'AND' => t('AND')),
|
66
|
'#default_value' => $this->options['date_method'],
|
67
|
'#description' => t('Method of handling multiple date fields in the same query. Return items that have any matching date field (date = field_1 OR field_2), or only those with matches in all selected date fields (date = field_1 AND field_2). '),
|
68
|
);
|
69
|
$form['date_group'] = array(
|
70
|
'#type' => 'hidden',
|
71
|
'#value' => $this->options['date_group'],
|
72
|
);
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* {@inheritdoc}
|
77
|
*/
|
78
|
function options_validate(&$form, &$form_state) {
|
79
|
// Views will whine if we don't have something for the these values even
|
80
|
// though we removed the option for summaries.
|
81
|
$form_state['values']['options']['summary']['format'] = 'none';
|
82
|
$form_state['values']['options']['summary']['options']['none'] = array();
|
83
|
|
84
|
// It is very important to call the parent function here:
|
85
|
parent::options_validate($form, $form_state);
|
86
|
|
87
|
if ($form_state['values']['form_id'] == 'views_ui_config_item_form') {
|
88
|
$check_fields = array_filter($form_state['values']['options']['date_fields']);
|
89
|
if (empty($check_fields)) {
|
90
|
form_error($form['date_fields'], t('You must select at least one date field for this argument.'));
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* {@inheritdoc}
|
97
|
*/
|
98
|
function options_submit(&$form, &$form_state) {
|
99
|
// It is very important to call the parent function here:
|
100
|
parent::options_submit($form, $form_state);
|
101
|
if ($form_state['values']['form_id'] == 'views_ui_config_item_form') {
|
102
|
$form_state['values']['options']['date_fields'] = array_filter($form_state['values']['options']['date_fields']);
|
103
|
}
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* {@inheritdoc}
|
108
|
*/
|
109
|
function admin_summary() {
|
110
|
$fields = date_views_fields($this->base_table);
|
111
|
if (!empty($this->options['date_fields'])) {
|
112
|
$output = array();
|
113
|
foreach ($this->options['date_fields'] as $field) {
|
114
|
if (array_key_exists($field, $fields['name'])) {
|
115
|
$output[] = $fields['name'][$field]['label'];
|
116
|
}
|
117
|
}
|
118
|
return implode('<br />' . $this->options['date_method'] . ' ', $output);
|
119
|
}
|
120
|
else {
|
121
|
return parent::admin_summary();
|
122
|
}
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* {@inheritdoc}
|
127
|
*/
|
128
|
function default_actions($which = NULL) {
|
129
|
$defaults = parent::default_actions();
|
130
|
|
131
|
// There is no easy way to do summary queries on multiple fields, so remove
|
132
|
// that option.
|
133
|
unset($defaults['summary']);
|
134
|
|
135
|
if ($which) {
|
136
|
if (!empty($defaults[$which])) {
|
137
|
return $defaults[$which];
|
138
|
}
|
139
|
}
|
140
|
else {
|
141
|
return $defaults;
|
142
|
}
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* {@inheritdoc}
|
147
|
*/
|
148
|
function query($group_by = FALSE) {
|
149
|
// @todo Not doing anything with $group_by yet, need to figure out what has
|
150
|
// to be done.
|
151
|
if ($this->date_forbid()) {
|
152
|
return;
|
153
|
}
|
154
|
$this->get_query_fields();
|
155
|
$this->query->set_where_group($this->options['date_method'], $this->options['date_group']);
|
156
|
$this->granularity = $this->date_handler->arg_granularity($this->argument);
|
157
|
$format = $this->date_handler->views_formats($this->granularity, 'sql');
|
158
|
|
159
|
$this->placeholders = array();
|
160
|
|
161
|
if (!empty($this->query_fields)) {
|
162
|
// Use set_where_group() with the selected date_method of 'AND' or 'OR'
|
163
|
// to create the where clause.
|
164
|
foreach ($this->query_fields as $count => $query_field) {
|
165
|
$field = $query_field['field'];
|
166
|
$this->date_handler = $query_field['date_handler'];
|
167
|
$this->field = $field['field_name'];
|
168
|
$this->real_field = $field['field_name'];
|
169
|
$this->table = $field['table_name'];
|
170
|
$this->original_table = $field['table_name'];
|
171
|
if ($field['table_name'] != $this->table || !empty($this->relationship)) {
|
172
|
$this->table = $this->query->ensure_table($field['table_name'], $this->relationship);
|
173
|
}
|
174
|
// $this->table_alias gets set when the first field is processed if
|
175
|
// otherwise empty. For subsequent fields, we need to be sure it is
|
176
|
// emptied again.
|
177
|
elseif (empty($this->relationship)) {
|
178
|
$this->table_alias = NULL;
|
179
|
}
|
180
|
parent::query($group_by);
|
181
|
|
182
|
$this->placeholders = array_merge($this->placeholders, $this->date_handler->placeholders);
|
183
|
}
|
184
|
}
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* {@inheritdoc}
|
189
|
*/
|
190
|
function get_query_fields() {
|
191
|
$fields = date_views_fields($this->base_table);
|
192
|
$fields = $fields['name'];
|
193
|
$this->query_fields = array();
|
194
|
foreach ($this->options['date_fields'] as $delta => $name) {
|
195
|
if (array_key_exists($name, $fields) && $field = $fields[$name]) {
|
196
|
$date_handler = new date_sql_handler($field['sql_type'], date_default_timezone());
|
197
|
$date_handler->granularity = $this->options['granularity'];
|
198
|
$date_handler->db_timezone = date_get_timezone_db($field['tz_handling']);
|
199
|
$date_handler->local_timezone = date_get_timezone($field['tz_handling']);
|
200
|
date_views_set_timezone($date_handler, $this, $field);
|
201
|
$this->query_fields[] = array('field' => $field, 'date_handler' => $date_handler);
|
202
|
}
|
203
|
}
|
204
|
}
|
205
|
|
206
|
}
|