views_handler_sort_date.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /**
  16. * {@inheritdoc}
  17. */
  18. public function option_definition() {
  19. $options = parent::option_definition();
  20. $options['granularity'] = array('default' => 'second');
  21. return $options;
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function options_form(&$form, &$form_state) {
  27. parent::options_form($form, $form_state);
  28. $form['granularity'] = array(
  29. '#type' => 'radios',
  30. '#title' => t('Granularity'),
  31. '#options' => array(
  32. 'second' => t('Second'),
  33. 'minute' => t('Minute'),
  34. 'hour' => t('Hour'),
  35. 'day' => t('Day'),
  36. 'month' => t('Month'),
  37. 'year' => t('Year'),
  38. ),
  39. '#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.'),
  40. '#default_value' => $this->options['granularity'],
  41. );
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function query() {
  47. $this->ensure_my_table();
  48. switch ($this->options['granularity']) {
  49. case 'second':
  50. default:
  51. $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
  52. return;
  53. case 'minute':
  54. $formula = views_date_sql_format('YmdHi', "$this->table_alias.$this->real_field");
  55. break;
  56. case 'hour':
  57. $formula = views_date_sql_format('YmdH', "$this->table_alias.$this->real_field");
  58. break;
  59. case 'day':
  60. $formula = views_date_sql_format('Ymd', "$this->table_alias.$this->real_field");
  61. break;
  62. case 'month':
  63. $formula = views_date_sql_format('Ym', "$this->table_alias.$this->real_field");
  64. break;
  65. case 'year':
  66. $formula = views_date_sql_format('Y', "$this->table_alias.$this->real_field");
  67. break;
  68. }
  69. // Add the field.
  70. $this->query->add_orderby(NULL, $formula, $this->options['order'], $this->table_alias . '_' . $this->field . '_' . $this->options['granularity']);
  71. }
  72. }