date_views.views.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. // This just tells our themes are elsewhere.
  39. 'module' => 'date_views',
  40. 'display' => array(
  41. // Display plugin for date navigation.
  42. 'date_nav' => array(
  43. 'title' => t('Date browser'),
  44. 'help' => t('Date back/next navigation to attach to other displays. Requires the Date argument.'),
  45. 'handler' => 'date_plugin_display_attachment',
  46. 'parent' => 'attachment',
  47. 'path' => "$path/includes",
  48. 'theme' => 'views_view',
  49. 'use ajax' => TRUE,
  50. 'admin' => t('Date browser'),
  51. 'help topic' => 'date-browser',
  52. ),
  53. ),
  54. 'pager' => array(
  55. 'date_views_pager' => array(
  56. 'title' => t('Page by date'),
  57. 'help' => t('Page using the value of a date field.'),
  58. 'handler' => 'date_views_plugin_pager',
  59. 'path' => "$path/includes",
  60. 'help topic' => 'date-views-pager',
  61. 'uses options' => TRUE,
  62. ),
  63. ),
  64. 'style' => array(
  65. 'date_nav' => array(
  66. 'title' => t('Date browser style'),
  67. 'help' => t('Creates back/next navigation.'),
  68. 'handler' => 'date_navigation_plugin_style',
  69. 'path' => "$path/includes",
  70. 'theme' => 'date_navigation',
  71. 'theme file' => 'theme.inc',
  72. 'theme path' => "$path/theme",
  73. 'uses row plugin' => FALSE,
  74. 'uses fields' => FALSE,
  75. 'uses options' => TRUE,
  76. 'type' => 'date_nav',
  77. 'even empty' => TRUE,
  78. ),
  79. ),
  80. );
  81. }
  82. /**
  83. * Implements hook_views_data().
  84. */
  85. function date_views_views_data() {
  86. $data = array();
  87. $tables = date_views_base_tables();
  88. foreach ($tables as $base_table => $entity) {
  89. // The flexible date argument.
  90. $data[$base_table]['date_argument'] = array(
  91. 'group' => t('Date'),
  92. 'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
  93. '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)),
  94. 'argument' => array(
  95. 'handler' => 'date_views_argument_handler',
  96. 'empty field name' => t('Undated'),
  97. 'is date' => TRUE,
  98. // 'skip base' => $base_table,
  99. ),
  100. );
  101. // The flexible date filter.
  102. $data[$base_table]['date_filter'] = array(
  103. 'group' => t('Date'),
  104. 'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
  105. 'help' => t('Filter any Views !base_table date field.', array('!base_table' => $base_table)),
  106. 'filter' => array(
  107. 'handler' => 'date_views_filter_handler',
  108. 'empty field name' => t('Undated'),
  109. 'is date' => TRUE,
  110. // 'skip base' => $base_table,
  111. ),
  112. );
  113. }
  114. return $data;
  115. }
  116. /**
  117. * Implements hook_views_data_alter().
  118. */
  119. function date_views_views_data_alter(&$data) {
  120. // Mark all the core date handlers as date fields.
  121. // This will identify all handlers that directly use the _date handlers,
  122. // will not pick up any that extend those handlers.
  123. foreach ($data as $base_table => &$table) {
  124. foreach ($table as $id => &$field) {
  125. foreach (array('field', 'sort', 'filter', 'argument') as $type) {
  126. if (isset($field[$type]) && isset($field[$type]['handler']) && ($field[$type]['handler'] == 'views_handler_' . $type . '_date')) {
  127. $field[$type]['is date'] = TRUE;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. /**
  134. * Central function for setting up the right timezone values.
  135. *
  136. * In the SQL date handler.
  137. *
  138. * The date handler will use this information to decide if the
  139. * database value needs a timezone conversion.
  140. *
  141. * In Views, we will always be comparing to a local date value,
  142. * so the goal is to convert the database value to the right
  143. * value to compare to the local value.
  144. */
  145. function date_views_set_timezone(&$date_handler, &$view, $field) {
  146. switch ($field['tz_handling']) {
  147. case 'date':
  148. $date_handler->db_timezone = 'UTC';
  149. $date_handler->local_timezone_field = $field['timezone_field'];
  150. $date_handler->offset_field = $field['offset_field'];
  151. break;
  152. case 'none':
  153. $date_handler->db_timezone = date_default_timezone();
  154. $date_handler->local_timezone = date_default_timezone();
  155. break;
  156. case 'utc':
  157. $date_handler->db_timezone = 'UTC';
  158. $date_handler->local_timezone = 'UTC';
  159. break;
  160. default:
  161. $date_handler->db_timezone = 'UTC';
  162. $date_handler->local_timezone = date_default_timezone();
  163. break;
  164. }
  165. }
  166. /**
  167. * Helper function to generate a query string.
  168. *
  169. * @param object $view
  170. * A View object.
  171. *
  172. * @param array $extra_params
  173. * An extra parameters.
  174. *
  175. * @return null/string
  176. * Return a query or NULL.
  177. */
  178. function date_views_querystring($view, $extra_params = array()) {
  179. $query_params = array_merge($_GET, $extra_params);
  180. // Allow NULL params to be removed from the query string.
  181. foreach ($extra_params as $key => $value) {
  182. if (!isset($value)) {
  183. unset($query_params[$key]);
  184. }
  185. }
  186. // Filter the special "q" and "view" variables out of the query string.
  187. $exclude = array('q');
  188. // If we don't explicitly add a value for "view", filter it out.
  189. if (empty($extra_params['view'])) {
  190. $exclude[] = 'view';
  191. }
  192. // Clear out old date pager settings if not explicitly added.
  193. if (!empty($view->date_info->pager_id) && empty($extra_params[$view->date_info->pager_id])) {
  194. $exclude[] = $view->date_info->pager_id;
  195. }
  196. $query = drupal_get_query_parameters($query_params, $exclude);
  197. // To prevent an empty query string from adding a "?" on to the end of a URL,
  198. // we return NULL.
  199. return !empty($query) ? $query : NULL;
  200. }