views_handler_argument_date.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 string $arg_format
  12. * The format string to use on the current time when creating a default date
  13. * 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. /**
  26. * @var string
  27. */
  28. public $option_name = 'default_argument_date';
  29. /**
  30. * @var string
  31. */
  32. public $arg_format = 'Y-m-d';
  33. /**
  34. * Add an option to set the default value to the current date.
  35. */
  36. public function default_argument_form(&$form, &$form_state) {
  37. parent::default_argument_form($form, $form_state);
  38. $form['default_argument_type']['#options'] += array('date' => t('Current date'));
  39. $form['default_argument_type']['#options'] += array('node_created' => t("Current node's creation time"));
  40. $form['default_argument_type']['#options'] += array('node_changed' => t("Current node's update time"));
  41. }
  42. /**
  43. * Set the empty argument value to the current date.
  44. *
  45. * Formatted appropriately for this argument.
  46. *
  47. * @return string
  48. * The default argument.
  49. */
  50. public function get_default_argument($raw = FALSE) {
  51. if (!$raw && $this->options['default_argument_type'] == 'date') {
  52. return date($this->arg_format, REQUEST_TIME);
  53. }
  54. elseif (!$raw && in_array($this->options['default_argument_type'], array('node_created', 'node_changed'))) {
  55. foreach (range(1, 3) as $i) {
  56. $node = menu_get_object('node', $i);
  57. if (!empty($node)) {
  58. continue;
  59. }
  60. }
  61. if (arg(0) == 'node' && is_numeric(arg(1))) {
  62. $node = node_load(arg(1));
  63. }
  64. if (empty($node)) {
  65. return parent::get_default_argument();
  66. }
  67. elseif ($this->options['default_argument_type'] == 'node_created') {
  68. return date($this->arg_format, $node->created);
  69. }
  70. elseif ($this->options['default_argument_type'] == 'node_changed') {
  71. return date($this->arg_format, $node->changed);
  72. }
  73. }
  74. return parent::get_default_argument($raw);
  75. }
  76. /**
  77. * Adapt the export mechanism.
  78. *
  79. * The date handler provides some default argument types, which aren't
  80. * argument default plugins.
  81. */
  82. public function export_plugin($indent, $prefix, $storage, $option, $definition, $parents) {
  83. // Only use a special behaviour for the special argument types, else just
  84. // use the default behaviour.
  85. if ($option == 'default_argument_type') {
  86. $type = 'argument default';
  87. $option_name = 'default_argument_options';
  88. $plugin = $this->get_plugin($type);
  89. $name = $this->options[$option];
  90. if (in_array($name, array('date', 'node_created', 'node_changed'))) {
  91. // Write which plugin to use.
  92. $output = $indent . $prefix . "['$option'] = '$name';\n";
  93. return $output;
  94. }
  95. }
  96. return parent::export_plugin($indent, $prefix, $storage, $option, $definition, $parents);
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function get_sort_name() {
  102. return t('Date', array(), array('context' => 'Sort order'));
  103. }
  104. }