date_views.module 19 KB

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