views_handler_argument_date.inc

  1. 3.x handlers/views_handler_argument_date.inc
  2. 2.x handlers/views_handler_argument_date.inc

Definition of views_handler_argument_date.

File

handlers/views_handler_argument_date.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_argument_date.
  5. */
  6. /**
  7. * Abstract argument handler for dates.
  8. *
  9. * Adds an option to set a default argument based on the current date.
  10. *
  11. * @param $arg_format
  12. * The format string to use on the current time when
  13. * creating a default date argument.
  14. *
  15. * Definitions terms:
  16. * - many to one: If true, the "many to one" helper will be used.
  17. * - invalid input: A string to give to the user for obviously invalid input.
  18. * This is deprecated in favor of argument validators.
  19. *
  20. * @see views_many_to_one_helper()
  21. *
  22. * @ingroup views_argument_handlers
  23. */
  24. class views_handler_argument_date extends views_handler_argument_formula {
  25. var $option_name = 'default_argument_date';
  26. var $arg_format = 'Y-m-d';
  27. /**
  28. * Add an option to set the default value to the current date.
  29. */
  30. function default_argument_form(&$form, &$form_state) {
  31. parent::default_argument_form($form, $form_state);
  32. $form['default_argument_type']['#options'] += array('date' => t('Current date'));
  33. $form['default_argument_type']['#options'] += array('node_created' => t("Current node's creation time"));
  34. $form['default_argument_type']['#options'] += array('node_changed' => t("Current node's update time")); }
  35. /**
  36. * Set the empty argument value to the current date,
  37. * formatted appropriately for this argument.
  38. */
  39. function get_default_argument($raw = FALSE) {
  40. if (!$raw && $this->options['default_argument_type'] == 'date') {
  41. return date($this->arg_format, REQUEST_TIME);
  42. }
  43. else if (!$raw && in_array($this->options['default_argument_type'], array('node_created', 'node_changed'))) {
  44. foreach (range(1, 3) as $i) {
  45. $node = menu_get_object('node', $i);
  46. if (!empty($node)) {
  47. continue;
  48. }
  49. }
  50. if (arg(0) == 'node' && is_numeric(arg(1))) {
  51. $node = node_load(arg(1));
  52. }
  53. if (empty($node)) {
  54. return parent::get_default_argument();
  55. }
  56. elseif ($this->options['default_argument_type'] == 'node_created') {
  57. return date($this->arg_format, $node->created);
  58. }
  59. elseif ($this->options['default_argument_type'] == 'node_changed') {
  60. return date($this->arg_format, $node->changed);
  61. }
  62. }
  63. return parent::get_default_argument($raw);
  64. }
  65. /**
  66. * The date handler provides some default argument types, which aren't argument default plugins,
  67. * so addapt the export mechanism.
  68. */
  69. function export_plugin($indent, $prefix, $storage, $option, $definition, $parents) {
  70. // Only use a special behaviour for the special argument types, else just
  71. // use the default behaviour.
  72. if ($option == 'default_argument_type') {
  73. $type = 'argument default';
  74. $option_name = 'default_argument_options';
  75. $plugin = $this->get_plugin($type);
  76. $name = $this->options[$option];
  77. if (in_array($name, array('date', 'node_created', 'node_changed'))) {
  78. // Write which plugin to use.
  79. $output = $indent . $prefix . "['$option'] = '$name';\n";
  80. return $output;
  81. }
  82. }
  83. return parent::export_plugin($indent, $prefix, $storage, $option, $definition, $parents);
  84. }
  85. function get_sort_name() {
  86. return t('Date', array(), array('context' => 'Sort order'));
  87. }
  88. }