date_views.views.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * @file
  4. * Defines date-related Views data and plugins:
  5. *
  6. * Date argument:
  7. * A generic date argument that has an option to select one or more
  8. * Views date fields to filter on, automatically adds them to the view,
  9. * and then filters the view by the value of the selected field(s).
  10. * The flexible argument will accept and evaluate most ISO date
  11. * and period formats, like 2009-05-01, 2008-W25, P1W.
  12. *
  13. * Date filter:
  14. * A generic date filter that has an option to select a
  15. * Views date field to filter on, with a choice of a widget to use
  16. * for the filter form and an option to set the default value to
  17. * a set date or something like 'now +90 days' . If the operator is
  18. * set to 'between' or 'not between' you can set a default value for
  19. * both the start and end dates.
  20. *
  21. * Current date argument default
  22. * Adds a default option to set the argument to the current date
  23. * when the argument is empty.
  24. *
  25. * Date navigation attachment
  26. * Navigation that can be attached to any display to create back/next
  27. * links by date, requires the date argument and uses the current
  28. * date argument default to set a starting point for the view.
  29. */
  30. /**
  31. * Implements hook_views_plugins
  32. */
  33. function date_views_views_plugins() {
  34. $path = drupal_get_path('module', 'date_views');
  35. $views_path = drupal_get_path('module', 'views');
  36. module_load_include('inc', 'date_views', 'theme/theme');
  37. return array(
  38. 'module' => 'date_views', // This just tells our themes are elsewhere.
  39. 'display' => array(
  40. // Display plugin for date navigation.
  41. 'date_nav' => array(
  42. 'title' => t('Date browser'),
  43. 'help' => t('Date back/next navigation to attach to other displays. Requires the Date argument.'),
  44. 'handler' => 'date_plugin_display_attachment',
  45. 'parent' => 'attachment',
  46. 'path' => "$path/includes",
  47. 'theme' => 'views_view',
  48. 'use ajax' => TRUE,
  49. 'admin' => t('Date browser'),
  50. 'help topic' => 'date-browser',
  51. ),
  52. ),
  53. 'pager' => array(
  54. 'date_views_pager' => array(
  55. 'title' => t('Page by date'),
  56. 'help' => t('Page using the value of a date field.'),
  57. 'handler' => 'date_views_plugin_pager',
  58. 'path' => "$path/includes",
  59. 'help topic' => 'date-views-pager',
  60. 'uses options' => TRUE,
  61. ),
  62. ),
  63. 'style' => array(
  64. 'date_nav' => array(
  65. 'title' => t('Date browser style'),
  66. 'help' => t('Creates back/next navigation.'),
  67. 'handler' => 'date_navigation_plugin_style',
  68. 'path' => "$path/includes",
  69. 'theme' => 'date_navigation',
  70. 'theme file' => 'theme.inc',
  71. 'theme path' => "$path/theme",
  72. 'uses row plugin' => FALSE,
  73. 'uses fields' => FALSE,
  74. 'uses options' => TRUE,
  75. 'type' => 'date_nav',
  76. 'even empty' => TRUE,
  77. ),
  78. ),
  79. );
  80. }
  81. /**
  82. * Implements hook_views_data()
  83. */
  84. function date_views_views_data() {
  85. $data = array();
  86. $tables = date_views_base_tables();
  87. foreach ($tables as $base_table => $entity) {
  88. // The flexible date argument.
  89. $data[$base_table]['date_argument'] = array(
  90. 'group' => t('Date'),
  91. 'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
  92. 'help' => t('Filter any Views !base_table date field by a date argument, using any common ISO date/period format (i.e. YYYY, YYYY-MM, YYYY-MM-DD, YYYY-W99, YYYY-MM-DD--P3M, P90D, etc). ', array('!base_table' => $base_table)),
  93. 'argument' => array(
  94. 'handler' => 'date_views_argument_handler',
  95. 'empty field name' => t('Undated'),
  96. 'is date' => TRUE,
  97. //'skip base' => $base_table,
  98. ),
  99. );
  100. // The flexible date filter.
  101. $data[$base_table]['date_filter'] = array(
  102. 'group' => t('Date'),
  103. 'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
  104. 'help' => t('Filter any Views !base_table date field.', array('!base_table' => $base_table)),
  105. 'filter' => array(
  106. 'handler' => 'date_views_filter_handler',
  107. 'empty field name' => t('Undated'),
  108. 'is date' => TRUE,
  109. //'skip base' => $base_table,
  110. ),
  111. );
  112. }
  113. return $data;
  114. }
  115. /**
  116. * Implements hook_views_data_alter().
  117. */
  118. function date_views_views_data_alter(&$data) {
  119. // Mark all the core date handlers as date fields.
  120. // This will identify all handlers that directly use the _date handlers,
  121. // will not pick up any that extend those handlers.
  122. foreach ($data as $base_table => &$table) {
  123. foreach ($table as $id => &$field) {
  124. foreach (array('field', 'sort', 'filter', 'argument') as $type) {
  125. if (isset($field[$type]) && isset($field[$type]['handler']) && ($field[$type]['handler'] == 'views_handler_' . $type . '_date')) {
  126. $field[$type]['is date'] = TRUE;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Central function for setting up the right timezone values
  134. * in the SQL date handler.
  135. *
  136. * The date handler will use this information to decide if the
  137. * database value needs a timezone conversion.
  138. *
  139. * In Views, we will always be comparing to a local date value,
  140. * so the goal is to convert the database value to the right
  141. * value to compare to the local value.
  142. */
  143. function date_views_set_timezone(&$date_handler, &$view, $field) {
  144. switch ($field['tz_handling']) {
  145. case 'date' :
  146. $date_handler->db_timezone = 'UTC';
  147. $date_handler->local_timezone_field = $field['timezone_field'];
  148. $date_handler->offset_field = $field['offset_field'];
  149. break;
  150. case 'none':
  151. $date_handler->db_timezone = date_default_timezone();
  152. $date_handler->local_timezone = date_default_timezone();
  153. break;
  154. case 'utc':
  155. $date_handler->db_timezone = 'UTC';
  156. $date_handler->local_timezone = 'UTC';
  157. break;
  158. default :
  159. $date_handler->db_timezone = 'UTC';
  160. $date_handler->local_timezone = date_default_timezone();
  161. break;
  162. }
  163. }
  164. function date_views_querystring($view, $extra_params = array()) {
  165. $query_params = array_merge($_GET, $extra_params);
  166. // Allow NULL params to be removed from the query string.
  167. foreach ($extra_params AS $key => $value) {
  168. if (!isset($value)) {
  169. unset($query_params[$key]);
  170. }
  171. }
  172. // Filter the special "q" and "view" variables out of the query string.
  173. $exclude = array('q');
  174. // If we don't explicitly add a value for "view", filter it out.
  175. if (empty($extra_params['view'])) {
  176. $exclude[] = 'view';
  177. }
  178. // Clear out old date pager settings if not explicitly added.
  179. if (!empty($view->date_info->pager_id) && empty($extra_params[$view->date_info->pager_id])) {
  180. $exclude[] = $view->date_info->pager_id;
  181. }
  182. $query = drupal_get_query_parameters($query_params, $exclude);
  183. // To prevent an empty query string from adding a "?" on to the end of a URL,
  184. // we return NULL.
  185. return !empty($query) ? $query : NULL;
  186. }