date_views_argument_handler.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @file
  4. * Date API views argument handler.
  5. * This argument combines multiple date arguments into a single argument
  6. * where all fields are controlled by the same date and can be combined with either AND or OR.
  7. */
  8. /**
  9. * Date API argument handler.
  10. */
  11. class date_views_argument_handler extends date_views_argument_handler_simple {
  12. /**
  13. * Get granularity and use it to create the formula and a format
  14. * for the results.
  15. */
  16. function init(&$view, &$options) {
  17. parent::init($view, $options);
  18. if (empty($this->view->date_info)) {
  19. $this->view->date_info = new stdClass();
  20. }
  21. if (empty($this->view->date_info->date_fields)) {
  22. $this->view->date_info->date_fields = array();
  23. }
  24. $this->view->date_info->date_fields = array_merge($this->view->date_info->date_fields, $this->options['date_fields']);
  25. }
  26. /**
  27. * Default value for the date_fields option.
  28. */
  29. function option_definition() {
  30. $options = parent::option_definition();
  31. $options['date_fields'] = array('default' => array());
  32. $options['date_method'] = array('default' => 'OR');
  33. $options['date_group'] = array('default' => 'date');
  34. return $options;
  35. }
  36. /**
  37. * Add a form element to select date_fields for this argument.
  38. */
  39. function options_form(&$form, &$form_state) {
  40. parent::options_form($form, $form_state);
  41. $fields = date_views_fields($this->base_table);
  42. $options = array();
  43. foreach ($fields['name'] as $name => $field) {
  44. $options[$name] = $field['label'];
  45. }
  46. $form['date_fields'] = array(
  47. '#title' => t('Date field(s)'),
  48. '#type' => 'checkboxes',
  49. '#options' => $options,
  50. '#default_value' => $this->options['date_fields'],
  51. '#multiple' => TRUE,
  52. '#description' => t("Select one or more date fields to filter with this argument."),
  53. );
  54. $form['date_method'] = array(
  55. '#title' => t('Method'),
  56. '#type' => 'radios',
  57. '#options' => array('OR' => t('OR'), 'AND' => t('AND')),
  58. '#default_value' => $this->options['date_method'],
  59. '#description' => t('Method of handling multiple date fields in the same query. Return items that have any matching date field (date = field_1 OR field_2), or only those with matches in all selected date fields (date = field_1 AND field_2). '),
  60. );
  61. $form['date_group'] = array(
  62. '#type' => 'hidden',
  63. '#value' => $this->options['date_group'],
  64. );
  65. }
  66. function options_validate(&$form, &$form_state) {
  67. // Views will whine if we don't have something for the these values even though we removed the option for summaries.
  68. $form_state['values']['options']['summary']['format'] = 'none';
  69. $form_state['values']['options']['summary']['options']['none'] = array();
  70. // It is very important to call the parent function here:
  71. parent::options_validate($form, $form_state);
  72. if ($form_state['values']['form_id'] == 'views_ui_config_item_form') {
  73. $check_fields = array_filter($form_state['values']['options']['date_fields']);
  74. if (empty($check_fields)) {
  75. form_error($form['date_fields'], t('You must select at least one date field for this argument.'));
  76. }
  77. }
  78. }
  79. function options_submit(&$form, &$form_state) {
  80. // It is very important to call the parent function here:
  81. parent::options_submit($form, $form_state);
  82. if ($form_state['values']['form_id'] == 'views_ui_config_item_form') {
  83. $form_state['values']['options']['date_fields'] = array_filter($form_state['values']['options']['date_fields']);
  84. }
  85. }
  86. // Update the summary values to show selected granularity.
  87. function admin_summary() {
  88. $fields = date_views_fields($this->base_table);
  89. if (!empty($this->options['date_fields'])) {
  90. $output = array();
  91. foreach ($this->options['date_fields'] as $field) {
  92. if (array_key_exists($field, $fields['name'])) {
  93. $output[] = $fields['name'][$field]['label'];
  94. }
  95. }
  96. return implode('<br />' . $this->options['date_method'] . ' ', $output);
  97. }
  98. else {
  99. return parent::admin_summary();
  100. }
  101. }
  102. /**
  103. * Provide a list of default behaviors for this argument if the argument
  104. * is not present.
  105. *
  106. * Override this method to provide additional (or fewer) default behaviors.
  107. */
  108. function default_actions($which = NULL) {
  109. $defaults = parent::default_actions();
  110. // There is no easy way to do summary queries on multiple fields, so remove that option.
  111. unset($defaults['summary']);
  112. if ($which) {
  113. if (!empty($defaults[$which])) {
  114. return $defaults[$which];
  115. }
  116. }
  117. else {
  118. return $defaults;
  119. }
  120. }
  121. /**
  122. * Set up the query for this argument.
  123. *
  124. * The argument sent may be found at $this->argument.
  125. */
  126. function query($group_by = FALSE) {
  127. // @TODO Not doing anything with $group_by yet, need to figure out what has to be done.
  128. if ($this->date_forbid()) {
  129. return;
  130. }
  131. $this->get_query_fields();
  132. $this->query->set_where_group($this->options['date_method'], $this->options['date_group']);
  133. $this->granularity = $this->date_handler->arg_granularity($this->argument);
  134. $format = $this->date_handler->views_formats($this->granularity, 'sql');
  135. $this->placeholders = array();
  136. if (!empty($this->query_fields)) {
  137. // Use set_where_group() with the selected date_method
  138. // of 'AND' or 'OR' to create the where clause.
  139. foreach ($this->query_fields as $count => $query_field) {
  140. $field = $query_field['field'];
  141. $this->date_handler = $query_field['date_handler'];
  142. $this->field = $field['field_name'];
  143. $this->real_field = $field['field_name'];
  144. $this->table = $field['table_name'];
  145. $this->original_table = $field['table_name'];
  146. if ($field['table_name'] != $this->table || !empty($this->relationship)) {
  147. $this->table = $this->query->ensure_table($field['table_name'], $this->relationship);
  148. }
  149. // $this->table_alias gets set when the first field is processed if otherwise empty.
  150. // For subsequent fields, we need to be sure it is emptied again.
  151. elseif (empty($this->relationship)) {
  152. $this->table_alias = NULL;
  153. }
  154. parent::query($group_by);
  155. $this->placeholders = array_merge($this->placeholders, $this->date_handler->placeholders);
  156. }
  157. }
  158. }
  159. /**
  160. * Collect information about our fields we will need to create the right query.
  161. */
  162. function get_query_fields() {
  163. $fields = date_views_fields($this->base_table);
  164. $fields = $fields['name'];
  165. $this->query_fields = array();
  166. foreach ($this->options['date_fields'] as $delta => $name) {
  167. if (array_key_exists($name, $fields) && $field = $fields[$name]) {
  168. $date_handler = new date_sql_handler($field['sql_type'], date_default_timezone());
  169. $date_handler->granularity = $this->options['granularity'];
  170. $date_handler->db_timezone = date_get_timezone_db($field['tz_handling']);
  171. $date_handler->local_timezone = date_get_timezone($field['tz_handling']);
  172. date_views_set_timezone($date_handler, $this, $field);
  173. $this->query_fields[] = array('field' => $field, 'date_handler' => $date_handler);
  174. }
  175. }
  176. }
  177. }