views_handler_sort_date.inc

  1. 3.x handlers/views_handler_sort_date.inc
  2. 2.x handlers/views_handler_sort_date.inc

Definition of views_handler_sort_date.

File

handlers/views_handler_sort_date.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_sort_date.
  5. */
  6. /**
  7. * Basic sort handler for dates.
  8. *
  9. * This handler enables granularity, which is the ability to make dates
  10. * equivalent based upon nearness.
  11. *
  12. * @ingroup views_sort_handlers
  13. */
  14. class views_handler_sort_date extends views_handler_sort {
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['granularity'] = array('default' => 'second');
  18. return $options;
  19. }
  20. function options_form(&$form, &$form_state) {
  21. parent::options_form($form, $form_state);
  22. $form['granularity'] = array(
  23. '#type' => 'radios',
  24. '#title' => t('Granularity'),
  25. '#options' => array(
  26. 'second' => t('Second'),
  27. 'minute' => t('Minute'),
  28. 'hour' => t('Hour'),
  29. 'day' => t('Day'),
  30. 'month' => t('Month'),
  31. 'year' => t('Year'),
  32. ),
  33. '#description' => t('The granularity is the smallest unit to use when determining whether two dates are the same; for example, if the granularity is "Year" then all dates in 1999, regardless of when they fall in 1999, will be considered the same date.'),
  34. '#default_value' => $this->options['granularity'],
  35. );
  36. }
  37. /**
  38. * Called to add the sort to a query.
  39. */
  40. function query() {
  41. $this->ensure_my_table();
  42. switch ($this->options['granularity']) {
  43. case 'second':
  44. default:
  45. $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
  46. return;
  47. case 'minute':
  48. $formula = views_date_sql_format('YmdHi', "$this->table_alias.$this->real_field");
  49. break;
  50. case 'hour':
  51. $formula = views_date_sql_format('YmdH', "$this->table_alias.$this->real_field");
  52. break;
  53. case 'day':
  54. $formula = views_date_sql_format('Ymd', "$this->table_alias.$this->real_field");
  55. break;
  56. case 'month':
  57. $formula = views_date_sql_format('Ym', "$this->table_alias.$this->real_field");
  58. break;
  59. case 'year':
  60. $formula = views_date_sql_format('Y', "$this->table_alias.$this->real_field");
  61. break;
  62. }
  63. // Add the field.
  64. $this->query->add_orderby(NULL, $formula, $this->options['order'], $this->table_alias . '_' . $this->field . '_' . $this->options['granularity']);
  65. }
  66. }