date_views.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /**
  3. * Implements hook_views_api().
  4. *
  5. * This one is used as the base to reduce errors when updating.
  6. */
  7. function date_views_theme() {
  8. $path = drupal_get_path('module', 'date_views');
  9. $base = array(
  10. 'file' => 'theme.inc',
  11. 'path' => "$path/theme",
  12. );
  13. return array(
  14. 'date_nav_title' => $base + array('variables' => array('granularity' => NULL, 'view' => NULL, 'link' => NULL, 'format' => NULL)),
  15. 'date_views_filter_form' => $base + array('template' => 'date-views-filter-form', 'render element' => 'form'),
  16. 'date_calendar_day' => $base + array('variables' => array('date' => NULL)),
  17. 'date_views_pager' => $base + array(
  18. 'variables' => array('plugin' => NULL, 'input' => NULL),
  19. // Register a pattern so that it can work like all views templates.
  20. 'pattern' => 'date_views_pager__',
  21. 'template' => 'date-views-pager',
  22. ),
  23. );
  24. }
  25. function date_views_views_api() {
  26. return array(
  27. 'api' => 3,
  28. 'path' => drupal_get_path('module', 'date_views') . '/includes',
  29. );
  30. }
  31. /**
  32. * Wrapper function to make sure this function will always work.
  33. */
  34. function date_views_views_fetch_fields($base, $type) {
  35. if (!module_exists('views')) {
  36. return array();
  37. }
  38. module_load_include('inc', 'views', 'includes/admin');
  39. return views_fetch_fields($base, $type);
  40. }
  41. /**
  42. * Identify all potential date/timestamp fields and cache the data.
  43. */
  44. function date_views_fields($base = 'node', $reset = FALSE) {
  45. static $fields = array();
  46. $empty = array('name' => array(), 'alias' => array());
  47. module_load_include('inc', 'date_views', 'includes/date_views_fields');
  48. if (empty($fields[$base]) || $reset) {
  49. $cid = 'date_views_fields_' . $base;
  50. if (!$reset && $cached = cache_get($cid, 'cache_views')) {
  51. $fields[$base] = $cached->data;
  52. }
  53. else {
  54. $fields[$base] = _date_views_fields($base);
  55. }
  56. }
  57. // Make sure that empty values will be arrays in he expected format.
  58. return !empty($fields) && !empty($fields[$base]) ? $fields[$base] : $empty;
  59. }
  60. /**
  61. * Implements hook_date_views_entities().
  62. * Map extra Views tables to the entity that holds its date fields,
  63. * needed for Views tables other than the primary tables identified in entity_info().
  64. */
  65. function date_views_date_views_extra_tables() {
  66. return array(
  67. 'node_revision' => 'node',
  68. );
  69. }
  70. /**
  71. * Helper function to map entity types to the Views base table they use,
  72. * to make it easier to infer the entity type from a base table.
  73. *
  74. * Views has a new handler called views_handler_field_entity() that loads
  75. * entities, and you can use something like the following to get the
  76. * entity type from a view, but not all our base tables contain the
  77. * entity information we need, (i.e. revisions) so it won't work here
  78. * and we resort to creating information from entity_get_info().
  79. *
  80. * // A method to get the entity type for a base table.
  81. * $table_data = views_fetch_data($base_table);
  82. * if (!isset($table_data['table']['base']['entity type'])) {
  83. * return FALSE;
  84. * }
  85. * $entity_type = $table_data['table']['base']['entity type'];
  86. */
  87. function date_views_base_tables() {
  88. $base_tables = &drupal_static(__FILE__, array());
  89. if (empty($base_tables)) {
  90. // First we get the base tables we can learn about from entity_info.
  91. $entity_info = entity_get_info();
  92. foreach ($entity_info as $entity_type => $info) {
  93. if (!empty($info['base table'])) {
  94. $base_tables[$info['base table']] = $entity_type;
  95. }
  96. if (!empty($info['revision table'])) {
  97. $base_tables[$info['revision table']] = $entity_type;
  98. }
  99. }
  100. // Then we let other modules tell us about other entity tables that hold date fields.
  101. $base_tables += module_invoke_all('date_views_extra_tables');
  102. }
  103. return $base_tables;
  104. }
  105. /**
  106. * Implements hook_date_views_fields().
  107. *
  108. * All modules that create custom fields that use the
  109. * 'views_handler_field_date' handler can provide
  110. * additional information here about the type of
  111. * date they create so the date can be used by
  112. * the Date API views date argument and date filter.
  113. */
  114. function date_views_date_views_fields($field) {
  115. $values = array(
  116. // The type of date: DATE_UNIX, DATE_ISO, DATE_DATETIME.
  117. 'sql_type' => DATE_UNIX,
  118. // Timezone handling options: 'none', 'site', 'date', 'utc' .
  119. 'tz_handling' => 'site',
  120. // Needed only for dates that use 'date' tz_handling.
  121. 'timezone_field' => '',
  122. // Needed only for dates that use 'date' tz_handling.
  123. 'offset_field' => '',
  124. // Array of "table.field" values for related fields that should be
  125. // loaded automatically in the Views SQL.
  126. 'related_fields' => array(),
  127. // Granularity of this date field's db data.
  128. 'granularity' => array('year', 'month', 'day', 'hour', 'minute', 'second'),
  129. );
  130. switch ($field) {
  131. case 'users.created':
  132. case 'users.access':
  133. case 'users.login':
  134. case 'node.created':
  135. case 'node.changed':
  136. case 'node_revision.timestamp':
  137. case 'file_managed.timestamp':
  138. case 'comment.timestamp':
  139. return $values;
  140. }
  141. }
  142. /**
  143. * A version of date_real_url that formats links correctly for the new Date pager.
  144. */
  145. function date_pager_url($view, $date_type = NULL, $date_arg = NULL, $force_view_url = FALSE, $absolute = TRUE) {
  146. // If someone adds a pager without a matching argument, there is not date information to work with.
  147. if (empty($view->date_info) || !isset($view->date_info->date_arg_pos)) {
  148. return '';
  149. }
  150. $args = $view->args;
  151. $pos = $view->date_info->date_arg_pos;
  152. // The View arguments array is indexed numerically but is not necessarily
  153. // in numerical order. Sort the arguments to ensure the correct order.
  154. ksort($args);
  155. // If there are empty arguments before the date argument,
  156. // pad them with the wildcard so the date argument will be in
  157. // the right position.
  158. if (count($args) < $pos) {
  159. foreach ($view->argument as $name => $argument) {
  160. if ($argument->position == $pos) {
  161. break;
  162. }
  163. $args[] = $argument->options['exception']['value'];
  164. }
  165. }
  166. if (!empty($date_type)) {
  167. switch ($date_type) {
  168. case 'year':
  169. $args[$pos] = date_pad($view->date_info->year, 4);
  170. break;
  171. case 'week':
  172. $args[$pos] = date_pad($view->date_info->year, 4) . '-W' . date_pad($view->date_info->week);
  173. break;
  174. case 'day':
  175. $args[$pos] = date_pad($view->date_info->year, 4) . '-' . date_pad($view->date_info->month) . '-' . date_pad($view->date_info->day);
  176. break;
  177. default:
  178. $args[$pos] = date_pad($view->date_info->year, 4) . '-' . date_pad($view->date_info->month);
  179. break;
  180. }
  181. }
  182. elseif (!empty($date_arg)) {
  183. $args[$pos] = $date_arg;
  184. }
  185. else {
  186. $args = $view->args;
  187. }
  188. // Is this an embedded or a block view?
  189. // Return the pager query value.
  190. if (!$force_view_url &&
  191. (!empty($view->preview) || !empty($view->date_info->block_identifier))) {
  192. $url = $args[$pos];
  193. $key = date_block_identifier($view);
  194. if (!empty($key)) {
  195. return url($_GET['q'], array(
  196. 'query' => date_views_querystring($view, array($key => $url)),
  197. 'absolute' => $absolute));
  198. }
  199. }
  200. // Normal views may need querystrings appended to them
  201. // if they use exposed filters.
  202. return url($view->get_url($args), array(
  203. 'query' => date_views_querystring($view),
  204. 'absolute' => $absolute));
  205. }
  206. function date_block_identifier($view) {
  207. if (!empty($view->block_identifier)) {
  208. return $view->block_identifier;
  209. }
  210. return isset($view->date_info->block_identifier) ? $view->date_info->block_identifier : NULL;
  211. }
  212. /**
  213. * Implements hook_field_views_data_alter().
  214. *
  215. * Create a Views field for each date column we care about
  216. * to supplement the generic 'entity_id' and 'revision_id'
  217. * fields that are automatically created.
  218. *
  219. * Also use friendlier labels to distinguish the start date
  220. * and end date in listings (for fields that use both).
  221. */
  222. function date_views_field_views_data_alter(&$result, $field, $module) {
  223. if ($module == 'date') {
  224. $has_end_date = !empty($field['settings']['todate']);
  225. if ($has_end_date) {
  226. $labels = field_views_field_label($field['field_name']);
  227. $label = array_shift($labels);
  228. }
  229. foreach ($result as $table => $data) {
  230. $additional = array();
  231. $field_name = $field['field_name'];
  232. foreach ($data as $column => $value) {
  233. // The old 'entity_id' and 'revision_id' values got rewritten in Views.
  234. // The old values are still there with a 'moved to' key, so ignore them.
  235. if (array_key_exists('field', $value) && !array_key_exists('moved to', $value['field'])) {
  236. $result[$table][$column]['field']['is date'] = TRUE;
  237. // Not sure yet if we still need a custom field handler in D7 now that custom formatters are available.
  238. // Might still need it to handle grouping of multiple value dates.
  239. //$result[$table][$column]['field']['handler'] = 'date_handler_field_date';
  240. //$result[$table][$column]['field']['add fields to query'] = TRUE;
  241. }
  242. // For filters, arguments, and sorts, determine if this column is for
  243. // the start date ('value') or the end date ('value2').
  244. $this_column = NULL;
  245. foreach (array_keys($field['columns']) as $candidate_column) {
  246. if ($column == $field['field_name'] . '_' . $candidate_column) {
  247. $this_column = $candidate_column;
  248. break;
  249. }
  250. }
  251. // Only alter the date fields, not timezone, rrule, offset, etc.
  252. if ($this_column != 'value' && $this_column != 'value2') {
  253. continue;
  254. }
  255. // We will replace the label with a friendlier name in the case of
  256. // arguments, filters, and sorts (but only if this field uses an end
  257. // date).
  258. $replace_label = FALSE;
  259. if (array_key_exists('argument', $value)) {
  260. $result[$table][$column]['argument']['handler'] = 'date_views_argument_handler_simple';
  261. $result[$table][$column]['argument']['empty field name'] = t('Undated');
  262. $result[$table][$column]['argument']['is date'] = TRUE;
  263. $replace_label = $has_end_date;
  264. }
  265. if (array_key_exists('filter', $value)) {
  266. $result[$table][$column]['filter']['handler'] = 'date_views_filter_handler_simple';
  267. $result[$table][$column]['filter']['empty field name'] = t('Undated');
  268. $result[$table][$column]['filter']['is date'] = TRUE;
  269. $replace_label = $has_end_date;
  270. }
  271. if (array_key_exists('sort', $value)) {
  272. $result[$table][$column]['sort']['is date'] = TRUE;
  273. $replace_label = $has_end_date;
  274. }
  275. if ($replace_label) {
  276. // Determine if this column is for the start date ('value') or the
  277. // end date ('value2').
  278. $this_column = NULL;
  279. foreach (array_keys($field['columns']) as $candidate_column) {
  280. if ($column == $field['field_name'] . '_' . $candidate_column) {
  281. $this_column = $candidate_column;
  282. break;
  283. }
  284. }
  285. // Insert the phrase "start date" or "end date" after the label, so
  286. // users can distinguish them in listings (compared to the default
  287. // behavior of field_views_field_default_views_data(), which only
  288. // uses the 'value2' column name to distinguish them).
  289. switch ($this_column) {
  290. case 'value':
  291. // Insert a deliberate double space before 'start date' in the
  292. // translatable string. This is a hack to get it to appear right
  293. // before 'end date' in the listing (i.e., in a non-alphabetical,
  294. // but more user friendly, order).
  295. $result[$table][$column]['title'] = t('@label - start date (!name)', array('@label' => $label, '!name' => $field['field_name']));
  296. $result[$table][$column]['title short'] = t('@label - start date', array('@label' => $label));
  297. break;
  298. case 'value2':
  299. $result[$table][$column]['title'] = t('@label - end date (!name:!column)', array('@label' => $label, '!name' => $field['field_name'], '!column' => $this_column));
  300. $result[$table][$column]['title short'] = t('@label - end date:!column', array('@label' => $label, '!column' => $this_column));
  301. break;
  302. }
  303. }
  304. }
  305. }
  306. }
  307. }
  308. /**
  309. * Implements hook_form_FORM_ID_alter() for views_ui_edit_form().
  310. */
  311. function date_views_form_views_ui_edit_form_alter(&$form, &$form_state, $form_id) {
  312. // This CSS is needed for the configuration form provided by the Date filter
  313. // (date_views_filter_handler_simple), but we have to add it here so that
  314. // it's already on the edit form the first time a Date filter is being added
  315. // to the View. See http://drupal.org/node/1239228#comment-4885288.
  316. $form['#attached']['css'][] = drupal_get_path('module', 'date_views') . '/css/date_views.css';
  317. }
  318. /**
  319. * The instanceof function makes this work for any handler that was derived
  320. * from 'views_handler_filter_date' or 'views_handler_argument_date',
  321. * which includes core date fields like the node updated field.
  322. *
  323. * The test for $handler->min_date tells us that this is an argument that
  324. * not only is derived from the views date handler but also has been processed
  325. * by the Date Views filter or argument code.
  326. */
  327. function date_views_handler_is_date($handler, $type = 'argument') {
  328. switch ($type) {
  329. case 'filter':
  330. return $handler instanceof views_handler_filter_date && !empty($handler->min_date);
  331. case 'argument':
  332. return $handler instanceof views_handler_argument_date && !empty($handler->min_date);
  333. }
  334. return FALSE;
  335. }
  336. /**
  337. * Validation hook for exposed filters that use the select widget.
  338. * This is to ensure the the user completes all parts of the date
  339. * not just some parts. Only needed for the select widget.
  340. */
  341. function date_views_select_validate(&$form, &$form_state) {
  342. // If there are no values just return.
  343. if (empty($form['value']) && empty($form['min'])) {
  344. return;
  345. }
  346. $granularity = (!empty($form['min']['#date_format'])) ? date_format_order($form['min']['#date_format']) : date_format_order($form['value']['#date_format']);
  347. $filled = array();
  348. $value = drupal_array_get_nested_value($form_state['input'], $form['#parents']);
  349. foreach ($granularity as $part) {
  350. if (!empty($value['value'][$part])) {
  351. $filled[] = $part;
  352. }
  353. }
  354. if (!empty($filled) && count($filled) != count($granularity)) {
  355. $unfilled = array_diff($granularity, $filled);
  356. foreach ($unfilled as $part) {
  357. switch ($part) {
  358. case 'year':
  359. form_error($form['value'][$part], t('Please choose a year.'), $form_state);
  360. break;
  361. case 'month':
  362. form_error($form['value'][$part], t('Please choose a month.'), $form_state);
  363. break;
  364. case 'day':
  365. form_error($form['value'][$part], t('Please choose a day.'), $form_state);
  366. break;
  367. case 'hour':
  368. form_error($form['value'][$part], t('Please choose an hour.'), $form_state);
  369. break;
  370. case 'minute':
  371. form_error($form['value'][$part], t('Please choose a minute.'), $form_state);
  372. break;
  373. case 'second':
  374. form_error($form['value'][$part], t('Please choose a second.'), $form_state);
  375. break;
  376. }
  377. }
  378. }
  379. }
  380. /**
  381. * Implements hook_date_formatter_view_alter().
  382. *
  383. * If we are displaying a date from a view, see if we have information about
  384. * which multiple value to display. If so, set the date_id in the entity.
  385. */
  386. function date_views_date_formatter_pre_view_alter(&$entity, &$variables) {
  387. // Some views have no row index.
  388. if (!empty($entity->view) && isset($entity->view->row_index)) {
  389. $field = $variables['field'];
  390. $date_id = 'date_id_' . $field['field_name'];
  391. $date_delta = 'date_delta_' . $field['field_name'];
  392. $date_item = $entity->view->result[$entity->view->row_index];
  393. if (!empty($date_item->$date_id)) {
  394. $entity->date_id = 'date.' . $date_item->$date_id . '.' . $field['field_name'] . '.' . $date_item->$date_delta . '.0';
  395. }
  396. }
  397. }