1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Defines date-related Views data and plugins:.
|
6
|
*
|
7
|
* Date argument:
|
8
|
* A generic date argument that has an option to select one or more
|
9
|
* Views date fields to filter on, automatically adds them to the view,
|
10
|
* and then filters the view by the value of the selected field(s).
|
11
|
* The flexible argument will accept and evaluate most ISO date
|
12
|
* and period formats, like 2009-05-01, 2008-W25, P1W.
|
13
|
*
|
14
|
* Date filter:
|
15
|
* A generic date filter that has an option to select a
|
16
|
* Views date field to filter on, with a choice of a widget to use
|
17
|
* for the filter form and an option to set the default value to
|
18
|
* a set date or something like 'now +90 days' . If the operator is
|
19
|
* set to 'between' or 'not between' you can set a default value for
|
20
|
* both the start and end dates.
|
21
|
*
|
22
|
* Current date argument default
|
23
|
* Adds a default option to set the argument to the current date
|
24
|
* when the argument is empty.
|
25
|
*
|
26
|
* Date navigation attachment
|
27
|
* Navigation that can be attached to any display to create back/next
|
28
|
* links by date, requires the date argument and uses the current
|
29
|
* date argument default to set a starting point for the view.
|
30
|
*/
|
31
|
|
32
|
/**
|
33
|
* Implements hook_views_plugins().
|
34
|
*/
|
35
|
function date_views_views_plugins() {
|
36
|
$path = drupal_get_path('module', 'date_views');
|
37
|
$views_path = drupal_get_path('module', 'views');
|
38
|
module_load_include('inc', 'date_views', 'theme/theme');
|
39
|
|
40
|
return array(
|
41
|
// This just tells our themes are elsewhere.
|
42
|
'module' => 'date_views',
|
43
|
'display' => array(
|
44
|
// Display plugin for date navigation.
|
45
|
'date_nav' => array(
|
46
|
'title' => t('Date browser'),
|
47
|
'help' => t('Date back/next navigation to attach to other displays. Requires the Date argument.'),
|
48
|
'handler' => 'date_plugin_display_attachment',
|
49
|
'parent' => 'attachment',
|
50
|
'path' => "$path/includes",
|
51
|
'theme' => 'views_view',
|
52
|
'use ajax' => TRUE,
|
53
|
'admin' => t('Date browser'),
|
54
|
'help topic' => 'date-browser',
|
55
|
),
|
56
|
),
|
57
|
|
58
|
'pager' => array(
|
59
|
'date_views_pager' => array(
|
60
|
'title' => t('Page by date'),
|
61
|
'help' => t('Page using the value of a date field.'),
|
62
|
'handler' => 'date_views_plugin_pager',
|
63
|
'path' => "$path/includes",
|
64
|
'help topic' => 'date-views-pager',
|
65
|
'uses options' => TRUE,
|
66
|
),
|
67
|
),
|
68
|
|
69
|
'style' => array(
|
70
|
'date_nav' => array(
|
71
|
'title' => t('Date browser style'),
|
72
|
'help' => t('Creates back/next navigation.'),
|
73
|
'handler' => 'date_navigation_plugin_style',
|
74
|
'path' => "$path/includes",
|
75
|
'theme' => 'date_navigation',
|
76
|
'theme file' => 'theme.inc',
|
77
|
'theme path' => "$path/theme",
|
78
|
'uses row plugin' => FALSE,
|
79
|
'uses fields' => FALSE,
|
80
|
'uses options' => TRUE,
|
81
|
'type' => 'date_nav',
|
82
|
'even empty' => TRUE,
|
83
|
),
|
84
|
),
|
85
|
);
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Implements hook_views_data().
|
90
|
*/
|
91
|
function date_views_views_data() {
|
92
|
$data = array();
|
93
|
|
94
|
$tables = date_views_base_tables();
|
95
|
|
96
|
foreach ($tables as $base_table => $entity) {
|
97
|
// The flexible date argument.
|
98
|
$data[$base_table]['date_argument'] = array(
|
99
|
'group' => t('Date'),
|
100
|
'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
|
101
|
'help' => t('Filter any Views !base_table date field by a date argument, using any common ISO date/period format (i.e. YYYY, YYYY-MM, YYYY-MM-DD, YYYY-W99, YYYY-MM-DD--P3M, P90D, etc).', array('!base_table' => $base_table)),
|
102
|
'argument' => array(
|
103
|
'handler' => 'date_views_argument_handler',
|
104
|
'empty field name' => t('Undated'),
|
105
|
'is date' => TRUE,
|
106
|
// 'skip base' => $base_table,
|
107
|
),
|
108
|
);
|
109
|
// The flexible date filter.
|
110
|
$data[$base_table]['date_filter'] = array(
|
111
|
'group' => t('Date'),
|
112
|
'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
|
113
|
'help' => t('Filter any Views !base_table date field.', array('!base_table' => $base_table)),
|
114
|
'filter' => array(
|
115
|
'handler' => 'date_views_filter_handler',
|
116
|
'empty field name' => t('Undated'),
|
117
|
'is date' => TRUE,
|
118
|
// 'skip base' => $base_table,
|
119
|
),
|
120
|
);
|
121
|
}
|
122
|
|
123
|
return $data;
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* Implements hook_views_data_alter().
|
128
|
*/
|
129
|
function date_views_views_data_alter(&$data) {
|
130
|
// Mark all the core date handlers as date fields.
|
131
|
// This will identify all handlers that directly use the _date handlers,
|
132
|
// will not pick up any that extend those handlers.
|
133
|
foreach ($data as $base_table => &$table) {
|
134
|
foreach ($table as $id => &$field) {
|
135
|
foreach (array('field', 'sort', 'filter', 'argument') as $type) {
|
136
|
if (isset($field[$type]) && isset($field[$type]['handler']) && ($field[$type]['handler'] == 'views_handler_' . $type . '_date')) {
|
137
|
$field[$type]['is date'] = TRUE;
|
138
|
}
|
139
|
}
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* Central function for setting up the right timezone values.
|
146
|
*
|
147
|
* In the SQL date handler.
|
148
|
*
|
149
|
* The date handler will use this information to decide if the database value
|
150
|
* needs a timezone conversion.
|
151
|
*
|
152
|
* In Views, we will always be comparing to a local date value, so the goal is
|
153
|
* to convert the database value to the right value to compare to the local
|
154
|
* value.
|
155
|
*/
|
156
|
function date_views_set_timezone(&$date_handler, &$view, $field) {
|
157
|
switch ($field['tz_handling']) {
|
158
|
case 'date':
|
159
|
$date_handler->db_timezone = 'UTC';
|
160
|
$date_handler->local_timezone_field = $field['timezone_field'];
|
161
|
$date_handler->offset_field = $field['offset_field'];
|
162
|
break;
|
163
|
|
164
|
case 'none':
|
165
|
$date_handler->db_timezone = date_default_timezone();
|
166
|
$date_handler->local_timezone = date_default_timezone();
|
167
|
break;
|
168
|
|
169
|
case 'utc':
|
170
|
$date_handler->db_timezone = 'UTC';
|
171
|
$date_handler->local_timezone = 'UTC';
|
172
|
break;
|
173
|
|
174
|
default:
|
175
|
$date_handler->db_timezone = 'UTC';
|
176
|
$date_handler->local_timezone = date_default_timezone();
|
177
|
}
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* Helper function to generate a query string.
|
182
|
*
|
183
|
* @param object $view
|
184
|
* A View object.
|
185
|
* @param array $extra_params
|
186
|
* An extra parameters.
|
187
|
*
|
188
|
* @return null/string
|
189
|
* Return a query or NULL.
|
190
|
*/
|
191
|
function date_views_querystring($view, $extra_params = array()) {
|
192
|
$query_params = array_merge($_GET, $extra_params);
|
193
|
|
194
|
// Allow NULL params to be removed from the query string.
|
195
|
foreach ($extra_params as $key => $value) {
|
196
|
if (!isset($value)) {
|
197
|
unset($query_params[$key]);
|
198
|
}
|
199
|
}
|
200
|
|
201
|
// Filter the special "q" and "view" variables out of the query string.
|
202
|
$exclude = array('q');
|
203
|
|
204
|
// If we don't explicitly add a value for "view", filter it out.
|
205
|
if (empty($extra_params['view'])) {
|
206
|
$exclude[] = 'view';
|
207
|
}
|
208
|
|
209
|
// Clear out old date pager settings if not explicitly added.
|
210
|
if (!empty($view->date_info->pager_id) && empty($extra_params[$view->date_info->pager_id])) {
|
211
|
$exclude[] = $view->date_info->pager_id;
|
212
|
}
|
213
|
|
214
|
$query = drupal_get_query_parameters($query_params, $exclude);
|
215
|
|
216
|
// To prevent an empty query string from adding a "?" on to the end of a URL,
|
217
|
// we return NULL.
|
218
|
return !empty($query) ? $query : NULL;
|
219
|
}
|