Project

General

Profile

Paste
Download (3.42 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_argument_date.inc @ 8be7bf84

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_argument_date.
6
 */
7

    
8
/**
9
 * Abstract argument handler for dates.
10
 *
11
 * Adds an option to set a default argument based on the current date.
12
 *
13
 * @param string $arg_format
14
 *   The format string to use on the current time when creating a default date
15
 *   argument.
16
 *
17
 * Definitions terms:
18
 * - many to one: If true, the "many to one" helper will be used.
19
 * - invalid input: A string to give to the user for obviously invalid input.
20
 *   This is deprecated in favor of argument validators.
21
 *
22
 * @see views_many_to_one_helper()
23
 *
24
 * @ingroup views_argument_handlers
25
 */
26
class views_handler_argument_date extends views_handler_argument_formula {
27

    
28
  /**
29
   * @var string
30
   */
31
  public $option_name = 'default_argument_date';
32

    
33
  /**
34
   * @var string
35
   */
36
  public $arg_format = 'Y-m-d';
37

    
38
  /**
39
   * Add an option to set the default value to the current date.
40
   */
41
  public function default_argument_form(&$form, &$form_state) {
42
    parent::default_argument_form($form, $form_state);
43
    $form['default_argument_type']['#options'] += array('date' => t('Current date'));
44
    $form['default_argument_type']['#options'] += array('node_created' => t("Current node's creation time"));
45
    $form['default_argument_type']['#options'] += array('node_changed' => t("Current node's update time"));
46
  }
47

    
48
  /**
49
   * Set the empty argument value to the current date.
50
   *
51
   * Formatted appropriately for this argument.
52
   *
53
   * @return string
54
   *   The default argument.
55
   */
56
  public function get_default_argument($raw = FALSE) {
57
    if (!$raw && $this->options['default_argument_type'] == 'date') {
58
      return date($this->arg_format, REQUEST_TIME);
59
    }
60
    elseif (!$raw && in_array($this->options['default_argument_type'], array('node_created', 'node_changed'))) {
61
      foreach (range(1, 3) as $i) {
62
        $node = menu_get_object('node', $i);
63
        if (!empty($node)) {
64
          continue;
65
        }
66
      }
67

    
68
      if (arg(0) == 'node' && is_numeric(arg(1))) {
69
        $node = node_load(arg(1));
70
      }
71

    
72
      if (empty($node)) {
73
        return parent::get_default_argument();
74
      }
75
      elseif ($this->options['default_argument_type'] == 'node_created') {
76
        return date($this->arg_format, $node->created);
77
      }
78
      elseif ($this->options['default_argument_type'] == 'node_changed') {
79
        return date($this->arg_format, $node->changed);
80
      }
81
    }
82

    
83
    return parent::get_default_argument($raw);
84
  }
85

    
86
  /**
87
   * Adapt the export mechanism.
88
   *
89
   * The date handler provides some default argument types, which aren't
90
   * argument default plugins.
91
   */
92
  public function export_plugin($indent, $prefix, $storage, $option, $definition, $parents) {
93
    // Only use a special behaviour for the special argument types, else just
94
    // use the default behaviour.
95
    if ($option == 'default_argument_type') {
96
      $type = 'argument default';
97
      $option_name = 'default_argument_options';
98

    
99
      $plugin = $this->get_plugin($type);
100
      $name = $this->options[$option];
101
      if (in_array($name, array('date', 'node_created', 'node_changed'))) {
102

    
103
        // Write which plugin to use.
104
        $output = $indent . $prefix . "['$option'] = '$name';\n";
105
        return $output;
106
      }
107
    }
108
    return parent::export_plugin($indent, $prefix, $storage, $option, $definition, $parents);
109
  }
110

    
111
  /**
112
   * {@inheritdoc}
113
   */
114
  public function get_sort_name() {
115
    return t('Date', array(), array('context' => 'Sort order'));
116
  }
117

    
118
}