1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
599a39cd
|
Assos Assos
|
|
3 |
85ad3d82
|
Assos Assos
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Date pager.
|
6 |
599a39cd
|
Assos Assos
|
*
|
7 |
|
|
* Works with a Date argument, the argument filters the view and the pager
|
8 |
|
|
* provides back/next navigation.
|
9 |
85ad3d82
|
Assos Assos
|
*
|
10 |
|
|
* USER NOTES:
|
11 |
|
|
*
|
12 |
|
|
* To use this, add a pager to a view, and choose the option to 'Page by date'.
|
13 |
|
|
* There are several settings:
|
14 |
599a39cd
|
Assos Assos
|
* - The pager id: Set an id to be used as the identifier in the URL for pager
|
15 |
|
|
* values, defaults to 'date'.
|
16 |
|
|
* - Pager position: Choose whether to display the date pager above, below, or
|
17 |
|
|
* both above and below the content.
|
18 |
|
|
* - Link format: Choose whether the pager links will be in the simple
|
19 |
|
|
* 'calendar/2011-12' format or the more complex 'calendar/?date=2011-12'
|
20 |
|
|
* pager format. The second one is more likely to work correctly if the pager
|
21 |
|
|
* is used in blocks and panels.
|
22 |
85ad3d82
|
Assos Assos
|
*
|
23 |
599a39cd
|
Assos Assos
|
* The pager works in combination with a Date argument and it will use the date
|
24 |
|
|
* fields and granularity set in that argument to create its back/next links. If
|
25 |
|
|
* the view has no Date argument, the pager can do nothing. The argument can
|
26 |
|
|
* either be a 'Date' argument that lets you select one or more date fields in
|
27 |
|
|
* the argument, or the simple 'Content' argument for an individual date field.
|
28 |
|
|
* It must be an argument that uses the date argument handler.
|
29 |
85ad3d82
|
Assos Assos
|
*
|
30 |
|
|
* DEVELOPER NOTES
|
31 |
|
|
*
|
32 |
599a39cd
|
Assos Assos
|
* The pager could technically create a query of its own rather than depending
|
33 |
|
|
* on the date argument to set the query, but it has only a limited set of tools
|
34 |
|
|
* to work with because it is a plugin, not a handler: it has no knowledge about
|
35 |
|
|
* relationships, it cannot use the ensure_my_table() function, plugins are not
|
36 |
|
|
* even invoked in pre_query(), so can't do anything there.
|
37 |
85ad3d82
|
Assos Assos
|
*
|
38 |
599a39cd
|
Assos Assos
|
* My conclusion was that the date pager simply is not powerful enough to create
|
39 |
|
|
* its own queries for date fields, which require very complex queries. Instead,
|
40 |
|
|
* we can combine this with a date argument and let the argument create the
|
41 |
|
|
* query and let the pager just provide the back/next links. If there is no date
|
42 |
|
|
* argument, the pager will do nothing.
|
43 |
85ad3d82
|
Assos Assos
|
*
|
44 |
599a39cd
|
Assos Assos
|
* There are still other problems. The pager is not even initialized until after
|
45 |
|
|
* all the handlers have created their queries, so it has no chance to alter
|
46 |
|
|
* values ahead of that. And the argument has no knowledge of the pager, so it
|
47 |
|
|
* can't check for pager values before the query is created.
|
48 |
85ad3d82
|
Assos Assos
|
*
|
49 |
599a39cd
|
Assos Assos
|
* The solution used here is to let the argument create the original query. The
|
50 |
|
|
* pager query runs after that, so the pager checks to see if there is a pager
|
51 |
|
|
* value that needs to be used in the query. The date argument has identified
|
52 |
|
|
* the placeholders it used in the query. So if a change is needed, we can swap
|
53 |
|
|
* the pager value into the query created by the date argument and adjust the
|
54 |
|
|
* $view->date_info values set by the argument accordingly so the theme will
|
55 |
|
|
* pick up the new information.
|
56 |
85ad3d82
|
Assos Assos
|
*/
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
599a39cd
|
Assos Assos
|
* Views pager plugin to page by month.
|
60 |
85ad3d82
|
Assos Assos
|
*/
|
61 |
|
|
class date_views_plugin_pager extends views_plugin_pager {
|
62 |
|
|
|
63 |
|
|
/**
|
64 |
599a39cd
|
Assos Assos
|
* {@inheritdoc}
|
65 |
85ad3d82
|
Assos Assos
|
*/
|
66 |
|
|
function use_count_query() {
|
67 |
|
|
return FALSE;
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
/**
|
71 |
599a39cd
|
Assos Assos
|
* {@inheritdoc}
|
72 |
85ad3d82
|
Assos Assos
|
*/
|
73 |
|
|
function has_more_records() {
|
74 |
|
|
return FALSE;
|
75 |
|
|
}
|
76 |
|
|
|
77 |
599a39cd
|
Assos Assos
|
/**
|
78 |
|
|
* {@inheritdoc}
|
79 |
85ad3d82
|
Assos Assos
|
*/
|
80 |
|
|
function summary_title() {
|
81 |
|
|
return t("Position: @position, format: @format.", array('@position' => $this->options['pager_position'], '@format' => $this->options['link_format']));
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
/**
|
85 |
599a39cd
|
Assos Assos
|
* {@inheritdoc}
|
86 |
85ad3d82
|
Assos Assos
|
*/
|
87 |
|
|
function option_definition() {
|
88 |
|
|
$options = parent::option_definition();
|
89 |
|
|
$options['date_id'] = array('default' => 'date');
|
90 |
|
|
$options['pager_position'] = array('default' => 'top');
|
91 |
|
|
$options['link_format'] = array('default' => 'pager');
|
92 |
|
|
$options['date_argument'] = array('default' => 'Unknown');
|
93 |
|
|
$options['granularity'] = array('default' => 'Unknown');
|
94 |
b720ea3e
|
Assos Assos
|
$options['skip_empty_pages'] = array('default' => FALSE);
|
95 |
85ad3d82
|
Assos Assos
|
return $options;
|
96 |
|
|
}
|
97 |
|
|
|
98 |
599a39cd
|
Assos Assos
|
/**
|
99 |
|
|
* {@inheritdoc}
|
100 |
85ad3d82
|
Assos Assos
|
*/
|
101 |
|
|
function options_form(&$form, &$form_state) {
|
102 |
|
|
$form['markup']['#markup'] = t('This pager works together with a Date or Content date field contextual filter. If a Date filter has been added to the view, this pager will provide back/next paging to match the granularity of that filter (i.e. paging by year, month, week, or day). The filter must also be configured to use a DATE default value. If there is no Date contextual filter on this view, or if it has not been set to use a default date, the pager will not appear.');
|
103 |
|
|
$form['date_id'] = array(
|
104 |
|
|
'#title' => t('Date identifier'),
|
105 |
|
|
'#type' => 'textfield',
|
106 |
|
|
'#description' => t('The query identifier to use when fetching date data from in the URL. Note that if you have more than one display in the same view that uses the date pager (like a page and a block), the pager id must be different for each one or both will change when the pager value changes.'),
|
107 |
|
|
'#default_value' => $this->options['date_id'],
|
108 |
|
|
'#required' => TRUE,
|
109 |
|
|
);
|
110 |
|
|
$form['pager_position'] = array(
|
111 |
|
|
'#title' => t('Pager position'),
|
112 |
|
|
'#type' => 'select',
|
113 |
|
|
'#options' => array('bottom' => t('Bottom'), 'top' => t('Top'), 'both' => t('Both')),
|
114 |
|
|
'#description' => t('Where to place the date pager, on the top, bottom, or both top and bottom of the content.'),
|
115 |
|
|
'#default_value' => $this->options['pager_position'],
|
116 |
|
|
'#required' => TRUE,
|
117 |
|
|
);
|
118 |
|
|
$form['link_format'] = array(
|
119 |
|
|
'#title' => t('Link format'),
|
120 |
|
|
'#type' => 'select',
|
121 |
|
|
'#options' => array('pager' => t('Pager'), 'clean' => t('Clean URL')),
|
122 |
|
|
'#description' => t("The format for pager link urls. With the Pager format, the links look like 'calendar/?date=2020-05'. The Clean URL format links look like 'calendar/2020-05'. The Clean format links look nicer but the Pager format links are likely to work better if the calendar is used in blocks or panels."),
|
123 |
|
|
'#default_value' => $this->options['link_format'],
|
124 |
|
|
'#required' => TRUE,
|
125 |
|
|
);
|
126 |
b720ea3e
|
Assos Assos
|
$form['skip_empty_pages'] = array(
|
127 |
|
|
'#title' => t('Skip empty pages'),
|
128 |
|
|
'#type' => 'checkbox',
|
129 |
|
|
'#description' => t('When selected, the pager will not display pages with no result for the given date. This causes a slight performance degradation because two additional queries need to be executed.'),
|
130 |
|
|
'#default_value' => $this->options['skip_empty_pages'],
|
131 |
|
|
);
|
132 |
85ad3d82
|
Assos Assos
|
$form['date_argument']['#type'] = 'hidden';
|
133 |
|
|
$form['date_argument']['#value'] = $this->options['date_argument'];
|
134 |
|
|
$form['granularity']['#type'] = 'hidden';
|
135 |
|
|
$form['granularity']['#value'] = $this->options['granularity'];
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
/**
|
139 |
599a39cd
|
Assos Assos
|
* {@inheritdoc}
|
140 |
85ad3d82
|
Assos Assos
|
*/
|
141 |
|
|
function query() {
|
142 |
599a39cd
|
Assos Assos
|
// By fetching our data from the exposed input, it is possible to feed
|
143 |
|
|
// pager data through some method other than $_GET.
|
144 |
85ad3d82
|
Assos Assos
|
$input = $this->view->get_exposed_input();
|
145 |
|
|
$value = NULL;
|
146 |
|
|
if (!empty($input) && !empty($input[$this->options['date_id']])) {
|
147 |
|
|
$value = $input[$this->options['date_id']];
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
// Bring the argument information into the view so our theme can access it.
|
151 |
|
|
$i = 0;
|
152 |
|
|
foreach ($this->view->argument as $id => &$argument) {
|
153 |
|
|
if (date_views_handler_is_date($argument, 'argument')) {
|
154 |
|
|
|
155 |
599a39cd
|
Assos Assos
|
// If the argument is empty, nothing to do. This could be from an
|
156 |
|
|
// argument that does not set a default value.
|
157 |
85ad3d82
|
Assos Assos
|
if (empty($argument->argument) || empty($argument->date_handler)) {
|
158 |
|
|
continue;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
599a39cd
|
Assos Assos
|
// Storing this information in the pager so it's available for summary
|
162 |
|
|
// info. The view argument information is not otherwise accessible to
|
163 |
|
|
// the pager. Not working right yet, tho.
|
164 |
85ad3d82
|
Assos Assos
|
$date_handler = $argument->date_handler;
|
165 |
|
|
$this->options['date_argument'] = $id;
|
166 |
|
|
$this->options['granularity'] = $argument->date_handler->granularity;
|
167 |
|
|
|
168 |
|
|
// Reset values set by argument if pager requires it.
|
169 |
|
|
if (!empty($value)) {
|
170 |
b720ea3e
|
Assos Assos
|
$this->set_argument_value($argument, $value);
|
171 |
85ad3d82
|
Assos Assos
|
}
|
172 |
|
|
|
173 |
|
|
// The pager value might move us into a forbidden range, so test it.
|
174 |
|
|
if ($this->date_forbid($argument)) {
|
175 |
|
|
$this->view->build_info['fail'] = TRUE;
|
176 |
|
|
return;
|
177 |
|
|
}
|
178 |
599a39cd
|
Assos Assos
|
|
179 |
|
|
// Write date_info to store information to be used in the theming
|
180 |
|
|
// functions.
|
181 |
b720ea3e
|
Assos Assos
|
if (empty($this->view->date_info)) {
|
182 |
|
|
$this->view->date_info = new stdClass();
|
183 |
|
|
}
|
184 |
85ad3d82
|
Assos Assos
|
$this->view->date_info->granularity = $argument->date_handler->granularity;
|
185 |
|
|
$format = $this->view->date_info->granularity == 'week' ? DATE_FORMAT_DATETIME : $argument->sql_format;
|
186 |
|
|
$this->view->date_info->placeholders = isset($argument->placeholders) ? $argument->placeholders : $argument->date_handler->placeholders;
|
187 |
|
|
$this->view->date_info->date_arg = $argument->argument;
|
188 |
|
|
$this->view->date_info->date_arg_pos = $i;
|
189 |
b720ea3e
|
Assos Assos
|
$this->view->date_info->limit = $argument->limit;
|
190 |
|
|
$this->view->date_info->url = $this->view->get_url();
|
191 |
|
|
$this->view->date_info->pager_id = $this->options['date_id'];
|
192 |
|
|
$this->view->date_info->date_pager_position = $this->options['pager_position'];
|
193 |
|
|
$this->view->date_info->date_pager_format = $this->options['link_format'];
|
194 |
|
|
$this->view->date_info->skip_empty_pages = $this->options['skip_empty_pages'] == 1;
|
195 |
599a39cd
|
Assos Assos
|
|
196 |
|
|
// Execute two additional queries to find the previous and next page
|
197 |
|
|
// with values.
|
198 |
b720ea3e
|
Assos Assos
|
if ($this->view->date_info->skip_empty_pages) {
|
199 |
|
|
$q = clone $argument->query;
|
200 |
|
|
$field = $argument->table_alias . '.' . $argument->real_field;
|
201 |
|
|
$fieldsql = $date_handler->sql_field($field);
|
202 |
|
|
$fieldsql = $date_handler->sql_format($format, $fieldsql);
|
203 |
|
|
$q->clear_fields();
|
204 |
|
|
$q->orderby = array();
|
205 |
|
|
$q->set_distinct(TRUE, TRUE);
|
206 |
|
|
// Date limits of this argument.
|
207 |
|
|
$datelimits = $argument->date_handler->arg_range($argument->limit[0] . '--' . $argument->limit[1]);
|
208 |
599a39cd
|
Assos Assos
|
// Find the first two dates between the minimum date and the upper
|
209 |
|
|
// bound of the current value.
|
210 |
b720ea3e
|
Assos Assos
|
$q->add_orderby(NULL, $fieldsql, 'DESC', 'date');
|
211 |
|
|
$this->set_argument_placeholders($this->view->date_info->placeholders, $datelimits[0], $argument->max_date, $q, $format);
|
212 |
|
|
|
213 |
|
|
$compiledquery = $q->query();
|
214 |
|
|
$compiledquery->range(0, 2);
|
215 |
|
|
$results = $compiledquery->execute()->fetchCol(0);
|
216 |
|
|
|
217 |
|
|
$prevdate = array_shift($results);
|
218 |
|
|
$prevdatealt = array_shift($results);
|
219 |
|
|
// Find the first two dates between the lower bound
|
220 |
|
|
// of the current value and the maximum date.
|
221 |
|
|
$q->add_orderby(NULL, $fieldsql, 'ASC', 'date');
|
222 |
|
|
$this->set_argument_placeholders($this->view->date_info->placeholders, $argument->min_date, $datelimits[1], $q, $format);
|
223 |
|
|
|
224 |
|
|
$compiledquery = $q->query();
|
225 |
|
|
$compiledquery->range(0, 2);
|
226 |
|
|
$results = $compiledquery->execute()->fetchCol(0);
|
227 |
|
|
|
228 |
|
|
$nextdate = array_shift($results);
|
229 |
|
|
$nextdatealt = array_shift($results);
|
230 |
|
|
|
231 |
|
|
// Set the default value of the query to $prevfirst or $nextfirst
|
232 |
|
|
// when there is no value and $prevsecond or $nextsecond is set.
|
233 |
|
|
if (empty($value)) {
|
234 |
599a39cd
|
Assos Assos
|
// @todo find out which of $prevdate or $nextdate is closest to the
|
235 |
b720ea3e
|
Assos Assos
|
// default argument date value and choose that one.
|
236 |
|
|
if ($prevdate && $prevdatealt) {
|
237 |
|
|
$this->set_argument_value($argument, $prevdate);
|
238 |
|
|
$value = $prevdate;
|
239 |
|
|
$prevdate = $prevdatealt;
|
240 |
|
|
// If the first next date is the same as the first previous date,
|
241 |
|
|
// move to the following next date.
|
242 |
|
|
if ($value == $nextdate) {
|
243 |
|
|
$nextdate = $nextdatealt;
|
244 |
|
|
$nextdatealt = NULL;
|
245 |
|
|
}
|
246 |
|
|
}
|
247 |
|
|
elseif ($nextdate && $nextdatealt) {
|
248 |
|
|
$this->set_argument_value($argument, $nextdate);
|
249 |
|
|
$value = $nextdate;
|
250 |
|
|
$nextdate = $nextdatealt;
|
251 |
|
|
// If the first previous date is the same as the first next date,
|
252 |
|
|
// move to the following previous date.
|
253 |
|
|
if ($value == $prevdate) {
|
254 |
|
|
$prevdate = $prevdatealt;
|
255 |
|
|
$prevdatealt = NULL;
|
256 |
|
|
}
|
257 |
|
|
}
|
258 |
|
|
}
|
259 |
|
|
else {
|
260 |
599a39cd
|
Assos Assos
|
// $prevdate and $nextdate are the same as $value, so move to the
|
261 |
|
|
// next values.
|
262 |
b720ea3e
|
Assos Assos
|
$prevdate = $prevdatealt;
|
263 |
|
|
$nextdate = $nextdatealt;
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
$this->view->date_info->prev_date = $prevdate ? new DateObject($prevdate, NULL, $format) : NULL;
|
267 |
|
|
$this->view->date_info->next_date = $nextdate ? new DateObject($nextdate, NULL, $format) : NULL;
|
268 |
|
|
}
|
269 |
|
|
else {
|
270 |
599a39cd
|
Assos Assos
|
$this->view->date_info->prev_date = clone $argument->min_date;
|
271 |
b720ea3e
|
Assos Assos
|
date_modify($this->view->date_info->prev_date, '-1 ' . $argument->date_handler->granularity);
|
272 |
599a39cd
|
Assos Assos
|
$this->view->date_info->next_date = clone $argument->min_date;
|
273 |
b720ea3e
|
Assos Assos
|
date_modify($this->view->date_info->next_date, '+1 ' . $argument->date_handler->granularity);
|
274 |
|
|
}
|
275 |
|
|
// Write the date_info properties that depend on the current value.
|
276 |
85ad3d82
|
Assos Assos
|
$this->view->date_info->year = date_format($argument->min_date, 'Y');
|
277 |
|
|
$this->view->date_info->month = date_format($argument->min_date, 'n');;
|
278 |
|
|
$this->view->date_info->day = date_format($argument->min_date, 'j');
|
279 |
|
|
$this->view->date_info->week = date_week(date_format($argument->min_date, DATE_FORMAT_DATE));
|
280 |
|
|
$this->view->date_info->date_range = $argument->date_range;
|
281 |
|
|
$this->view->date_info->min_date = $argument->min_date;
|
282 |
|
|
$this->view->date_info->max_date = $argument->max_date;
|
283 |
|
|
}
|
284 |
|
|
$i++;
|
285 |
|
|
}
|
286 |
|
|
|
287 |
599a39cd
|
Assos Assos
|
// Is this a view that needs to be altered based on a pager value? If there
|
288 |
|
|
// is pager input and the argument has set the placeholders, swap the pager
|
289 |
|
|
// value in for the placeholder set by the argument.
|
290 |
85ad3d82
|
Assos Assos
|
if (!empty($value) && !empty($this->view->date_info->placeholders)) {
|
291 |
b720ea3e
|
Assos Assos
|
$this->set_argument_placeholders($this->view->date_info->placeholders, $this->view->date_info->min_date, $this->view->date_info->max_date, $this->view->query, $format);
|
292 |
|
|
}
|
293 |
|
|
}
|
294 |
|
|
|
295 |
599a39cd
|
Assos Assos
|
/**
|
296 |
|
|
* {@inheritdoc}
|
297 |
|
|
*/
|
298 |
b720ea3e
|
Assos Assos
|
function set_argument_value($argument, $value) {
|
299 |
|
|
$argument->argument = $value;
|
300 |
|
|
$argument->date_range = $argument->date_handler->arg_range($value);
|
301 |
|
|
$argument->min_date = $argument->date_range[0];
|
302 |
|
|
$argument->max_date = $argument->date_range[1];
|
303 |
|
|
// $argument->is_default works correctly for normal arguments, but does not
|
304 |
|
|
// work correctly if we are swapping in a new value from the pager.
|
305 |
|
|
$argument->is_default = FALSE;
|
306 |
|
|
}
|
307 |
|
|
|
308 |
599a39cd
|
Assos Assos
|
/**
|
309 |
|
|
* {@inheritdoc}
|
310 |
|
|
*/
|
311 |
b720ea3e
|
Assos Assos
|
function set_argument_placeholders($placeholders, $mindate, $maxdate, $query, $format) {
|
312 |
|
|
$count = count($placeholders);
|
313 |
|
|
foreach ($query->where as $group => $data) {
|
314 |
|
|
foreach ($data['conditions'] as $delta => $condition) {
|
315 |
|
|
if (array_key_exists('value', $condition) && is_array($condition['value'])) {
|
316 |
|
|
foreach ($condition['value'] as $placeholder => $placeholder_value) {
|
317 |
|
|
if (array_key_exists($placeholder, $placeholders)) {
|
318 |
599a39cd
|
Assos Assos
|
// If we didn't get a match, this is a > $min < $max query that
|
319 |
|
|
// uses the view min and max dates as placeholders.
|
320 |
b720ea3e
|
Assos Assos
|
$date = ($count == 2) ? $mindate : $maxdate;
|
321 |
|
|
$next_placeholder = array_shift($placeholders);
|
322 |
|
|
$query->where[$group]['conditions'][$delta]['value'][$placeholder] = $date->format($format);
|
323 |
|
|
$count--;
|
324 |
85ad3d82
|
Assos Assos
|
}
|
325 |
|
|
}
|
326 |
|
|
}
|
327 |
|
|
}
|
328 |
|
|
}
|
329 |
|
|
}
|
330 |
|
|
|
331 |
|
|
/**
|
332 |
599a39cd
|
Assos Assos
|
* Determine if we have moved outside the valid date range for this argument.
|
333 |
85ad3d82
|
Assos Assos
|
*/
|
334 |
|
|
function date_forbid($argument) {
|
335 |
|
|
// See if we're outside the allowed date range for our argument.
|
336 |
|
|
$limit = date_range_years($argument->options['year_range']);
|
337 |
|
|
if (date_format($argument->min_date, 'Y') < $limit[0] || date_format($argument->max_date, 'Y') > $limit[1]) {
|
338 |
|
|
return TRUE;
|
339 |
|
|
}
|
340 |
|
|
return FALSE;
|
341 |
|
|
}
|
342 |
|
|
|
343 |
599a39cd
|
Assos Assos
|
/**
|
344 |
|
|
* {@inheritdoc}
|
345 |
|
|
*/
|
346 |
|
|
function pre_render(&$result) {
|
347 |
|
|
// Load theme functions for the pager.
|
348 |
|
|
module_load_include('inc', 'date_views', 'theme/theme');
|
349 |
|
|
}
|
350 |
85ad3d82
|
Assos Assos
|
|
351 |
599a39cd
|
Assos Assos
|
/**
|
352 |
|
|
* {@inheritdoc}
|
353 |
|
|
*/
|
354 |
85ad3d82
|
Assos Assos
|
function render($input) {
|
355 |
599a39cd
|
Assos Assos
|
// This adds all of our template suggestions based upon the view name and
|
356 |
|
|
// display id.
|
357 |
85ad3d82
|
Assos Assos
|
$pager_theme = views_theme_functions('date_views_pager', $this->view, $this->display);
|
358 |
|
|
return theme($pager_theme, array('plugin' => $this, 'input' => $input));
|
359 |
|
|
}
|
360 |
599a39cd
|
Assos Assos
|
|
361 |
b720ea3e
|
Assos Assos
|
} |