Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / modules / node / views_plugin_argument_validate_node.inc @ 7547bb19

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the 'node' argument validator plugin.
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
  function option_definition() {
13
    $options = parent::option_definition();
14
    $options['types'] = array('default' => array());
15
    $options['access'] = array('default' => FALSE, 'bool' => TRUE);
16
    $options['access_op'] = array('default' => 'view');
17
    $options['nid_type'] = array('default' => 'nid');
18

    
19
    return $options;
20
  }
21

    
22
  function options_form(&$form, &$form_state) {
23
    $types = node_type_get_types();
24
    $options = array();
25
    foreach ($types as $type => $info) {
26
      $options[$type] = check_plain(t($info->name));
27
    }
28

    
29
    $form['types'] = array(
30
      '#type' => 'checkboxes',
31
      '#title' => t('Content types'),
32
      '#options' => $options,
33
      '#default_value' => $this->options['types'],
34
      '#description' => t('Choose one or more content types to validate with.'),
35
    );
36

    
37
    $form['access'] = array(
38
      '#type' => 'checkbox',
39
      '#title' => t('Validate user has access to the content'),
40
      '#default_value' => $this->options['access'],
41
    );
42
    $form['access_op'] = array(
43
      '#type' => 'radios',
44
      '#title' => t('Access operation to check'),
45
      '#options' => array('view' => t('View'), 'update' => t('Edit'), 'delete' => t('Delete')),
46
      '#default_value' => $this->options['access_op'],
47
      '#dependency' => array('edit-options-validate-options-node-access' => array(TRUE)),
48
    );
49

    
50
    $form['nid_type'] = array(
51
      '#type' => 'select',
52
      '#title' => t('Filter value format'),
53
      '#options' => array(
54
        'nid' => t('Node ID'),
55
        'nids' => t('Node IDs separated by , or +'),
56
      ),
57
      '#default_value' => $this->options['nid_type'],
58
    );
59
  }
60

    
61
  function options_submit(&$form, &$form_state, &$options = array()) {
62
    // filter trash out of the options so we don't store giant unnecessary arrays
63
    $options['types'] = array_filter($options['types']);
64
  }
65

    
66
  function convert_options(&$options) {
67
    if (!isset($options['types']) && !empty($this->argument->options['validate_argument_node_type'])) {
68
      $options['types'] = isset($this->argument->options['validate_argument_node_type']) ? $this->argument->options['validate_argument_node_type'] : array();
69
      $options['access'] = !empty($this->argument->options['validate_argument_node_access']);
70
      $options['access_op'] = isset($this->argument->options['validate_argument_node_access_op']) ? $this->argument->options['validate_argument_node_access_op'] : 'view';
71
      $options['nid_type'] = isset($this->argument->options['validate_argument_nid_type']) ? $this->argument->options['validate_argument_nid_type'] : array();
72
    }
73
  }
74

    
75
  function validate_argument($argument) {
76
    $types = $this->options['types'];
77

    
78
    switch ($this->options['nid_type']) {
79
      case 'nid':
80
        if (!is_numeric($argument)) {
81
          return FALSE;
82
        }
83
        $node = node_load($argument);
84
        if (!$node) {
85
          return FALSE;
86
        }
87

    
88
        if (!empty($this->options['access'])) {
89
          if (!node_access($this->options['access_op'], $node)) {
90
            return FALSE;
91
          }
92
        }
93

    
94
        // Save the title() handlers some work.
95
        $this->argument->validated_title = check_plain($node->title);
96

    
97
        if (empty($types)) {
98
          return TRUE;
99
        }
100

    
101
        return isset($types[$node->type]);
102
      break;
103
      case 'nids':
104
        $nids = new stdClass();
105
        $nids->value = array($argument);
106
        $nids = views_break_phrase($argument, $nids);
107
        if ($nids->value == array(-1)) {
108
          return FALSE;
109
        }
110

    
111
        $test = drupal_map_assoc($nids->value);
112
        $titles = array();
113

    
114
        $result = db_query("SELECT * FROM {node} WHERE nid IN (:nids)", array(':nids' =>  $nids->value));
115
        foreach ($result as $node) {
116
          if ($types && empty($types[$node->type])) {
117
            return FALSE;
118
          }
119

    
120
          if (!empty($this->options['access'])) {
121
            if (!node_access($this->options['access_op'], $node)) {
122
              return FALSE;
123
            }
124
          }
125

    
126
          $titles[] = check_plain($node->title);
127
          unset($test[$node->nid]);
128
        }
129

    
130
        $this->argument->validated_title = implode($nids->operator == 'or' ? ' + ' : ', ', $titles);
131
        // If this is not empty, we did not find a nid.
132
        return empty($test);
133
    }
134
  }
135
}