Projet

Général

Profil

Paste
Télécharger (4,62 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / node / views_plugin_argument_validate_node.inc @ 5d12d676

1
<?php
2

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

    
8
/**
9
 * Validate whether an argument is an acceptable node.
10
 */
11
class views_plugin_argument_validate_node extends views_plugin_argument_validate {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public function option_definition() {
17
    $options = parent::option_definition();
18
    $options['types'] = array('default' => array());
19
    $options['access'] = array('default' => FALSE, 'bool' => TRUE);
20
    $options['access_op'] = array('default' => 'view');
21
    $options['nid_type'] = array('default' => 'nid');
22

    
23
    return $options;
24
  }
25

    
26
  /**
27
   * {@inheritdoc}
28
   */
29
  public function options_form(&$form, &$form_state) {
30
    $types = node_type_get_types();
31
    $options = array();
32
    foreach ($types as $type => $info) {
33
      $options[$type] = check_plain(t($info->name));
34
    }
35

    
36
    $form['types'] = array(
37
      '#type' => 'checkboxes',
38
      '#title' => t('Content types'),
39
      '#options' => $options,
40
      '#default_value' => $this->options['types'],
41
      '#description' => t('Choose one or more content types to validate with.'),
42
    );
43

    
44
    $form['access'] = array(
45
      '#type' => 'checkbox',
46
      '#title' => t('Validate user has access to the content'),
47
      '#default_value' => $this->options['access'],
48
    );
49
    $form['access_op'] = array(
50
      '#type' => 'radios',
51
      '#title' => t('Access operation to check'),
52
      '#options' => array('view' => t('View'), 'update' => t('Edit'), 'delete' => t('Delete')),
53
      '#default_value' => $this->options['access_op'],
54
      '#dependency' => array('edit-options-validate-options-node-access' => array(TRUE)),
55
    );
56

    
57
    $form['nid_type'] = array(
58
      '#type' => 'select',
59
      '#title' => t('Filter value format'),
60
      '#options' => array(
61
        'nid' => t('Node ID'),
62
        'nids' => t('Node IDs separated by , or +'),
63
      ),
64
      '#default_value' => $this->options['nid_type'],
65
    );
66
  }
67

    
68
  /**
69
   * {@inheritdoc}
70
   */
71
  public function options_submit(&$form, &$form_state, &$options = array()) {
72
    // Filter trash out of the options so we don't store giant unnecessary
73
    // arrays.
74
    $options['types'] = array_filter($options['types']);
75
  }
76

    
77
  /**
78
   * {@inheritdoc}
79
   */
80
  public function convert_options(&$options) {
81
    if (!isset($options['types']) && !empty($this->argument->options['validate_argument_node_type'])) {
82
      $options['types'] = isset($this->argument->options['validate_argument_node_type']) ? $this->argument->options['validate_argument_node_type'] : array();
83
      $options['access'] = !empty($this->argument->options['validate_argument_node_access']);
84
      $options['access_op'] = isset($this->argument->options['validate_argument_node_access_op']) ? $this->argument->options['validate_argument_node_access_op'] : 'view';
85
      $options['nid_type'] = isset($this->argument->options['validate_argument_nid_type']) ? $this->argument->options['validate_argument_nid_type'] : array();
86
    }
87
  }
88

    
89
  /**
90
   * {@inheritdoc}
91
   */
92
  public function validate_argument($argument) {
93
    $types = $this->options['types'];
94

    
95
    switch ($this->options['nid_type']) {
96
      case 'nid':
97
        if (!is_numeric($argument)) {
98
          return FALSE;
99
        }
100
        $node = node_load($argument);
101
        if (!$node) {
102
          return FALSE;
103
        }
104

    
105
        if (!empty($this->options['access'])) {
106
          if (!node_access($this->options['access_op'], $node)) {
107
            return FALSE;
108
          }
109
        }
110

    
111
        // Save the title() handlers some work.
112
        $this->argument->validated_title = check_plain($node->title);
113

    
114
        if (empty($types)) {
115
          return TRUE;
116
        }
117

    
118
        return isset($types[$node->type]);
119
        break;
120

    
121
      case 'nids':
122
        $nids = new stdClass();
123
        $nids->value = array($argument);
124
        $nids = views_break_phrase($argument, $nids);
125
        if ($nids->value == array(-1)) {
126
          return FALSE;
127
        }
128

    
129
        $test = drupal_map_assoc($nids->value);
130
        $titles = array();
131

    
132
        $result = db_query("SELECT * FROM {node} WHERE nid IN (:nids)", array(':nids' => $nids->value));
133
        foreach ($result as $node) {
134
          if ($types && empty($types[$node->type])) {
135
            return FALSE;
136
          }
137

    
138
          if (!empty($this->options['access'])) {
139
            if (!node_access($this->options['access_op'], $node)) {
140
              return FALSE;
141
            }
142
          }
143

    
144
          $titles[] = check_plain($node->title);
145
          unset($test[$node->nid]);
146
        }
147

    
148
        $this->argument->validated_title = implode($nids->operator == 'or' ? ' + ' : ', ', $titles);
149
        // If this is not empty, we did not find a nid.
150
        return empty($test);
151
    }
152
  }
153

    
154
}