date_views_plugin_pager.inc 16 KB

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