Projet

Général

Profil

Paste
Télécharger (6,96 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / flag / includes / views / flag_plugin_argument_validate_flaggability.inc @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the flaggability validator handler.
6
 */
7

    
8
/**
9
 * Validates whether an argument is a flaggable/flagged object.
10
 *
11
 * @ingroup views
12
 */
13
class flag_plugin_argument_validate_flaggability extends views_plugin_argument_validate {
14

    
15
  function construct() {
16
    parent::construct();
17
    $this->flag_type = $this->definition['flag type'];
18
  }
19

    
20
  function option_definition() {
21
    $options = parent::option_definition();
22
    $options['flag_name'] = array('default' => '*relationship*');
23
    $options['flag_test'] = array('default' => 'flaggable');
24
    $options['flag_id_type'] = array('default' => 'id');
25
    return $options;
26
  }
27

    
28
  function options_form(&$form, &$form_state) {
29
    // If there are no flag relationships set, skip this form of validation.
30
    $flags = flag_get_flags($this->flag_type);
31
    if (!$flags) {
32
      return array();
33
    }
34
    $options = $this->flags_options();
35

    
36
    $form['flag_name'] = array(
37
      '#type' => 'radios',
38
      '#title' => t('Flag'),
39
      '#options' => $options,
40
      '#default_value' => $this->options['flag_name'],
41
      '#description' => t('Select the flag to validate against.'),
42
    );
43
    if (!$options) {
44
      $form['flag_name']['#description'] = '<p class="warning">' . t('No %type flags exist. You must first <a href="@create-url">create a %type flag</a> before being able to use one.', array('%type' => $this->flag_type, '@create-url' => FLAG_ADMIN_PATH)) . '</p>';
45
    }
46

    
47
    $form['flag_test'] = array(
48
      '#type' => 'select',
49
      '#title' => t('Validate the @type only if', array('@type' => $this->flag_type)),
50
      '#options' => $this->tests_options(),
51
      '#default_value' => $this->options['flag_test'],
52
    );
53

    
54
    // This validator supports the "multiple IDs" syntax. It may not be
55
    // extremely useful, but similar validators do support this and it's a good
56
    // thing to be compatible.
57
    $form['flag_id_type'] = array(
58
      '#type' => 'select',
59
      '#title' => t('Argument type'),
60
      '#options' => array(
61
        'id' => t('ID'),
62
        'ids' => t('IDs separated by , or +'),
63
      ),
64
      '#default_value' => $this->options['flag_id_type'],
65
    );
66
  }
67

    
68
  /**
69
   * Returns form #options for the flags. Returns empty array if no flags were
70
   * found.
71
   */
72
  function flags_options() {
73
    $flags = flag_get_flags($this->flag_type);
74
    if (!$flags) {
75
      return array();
76
    }
77
    else {
78
      foreach ($flags as $flag) {
79
        $options[$flag->name] = $flag->get_title();
80
      }
81
      $options['*relationship*'] = t('<em>Pick the first flag mentioned in the relationships.</em>');
82
      return $options;
83
    }
84
  }
85

    
86
  /**
87
   * Migrate existing Views 2 options to Views 3.
88
   */
89
  function convert_options(&$options) {
90
    $prefix = 'validate_argument_' . $this->flag_type . '_';
91
    if (!isset($options['flag_name']) && !empty($this->argument->options[$prefix . 'flag_name'])) {
92
      $options['flag_name'] = $this->argument->options[$prefix . 'flag_name'];
93
      $options['flag_test'] = $this->argument->options[$prefix . 'flag_test'];
94
      $options['flag_id_type'] = $this->argument->options[$prefix . 'flag_id_type'];
95
    }
96
  }
97

    
98
  /**
99
   * Declares all tests. This scheme makes it easy for derived classes to add
100
   * and remove tests.
101
   */
102
  function tests_info($which = NULL) {
103
    return array(
104
      'flaggable' => array(
105
        'title' => t('It is flaggable'),
106
        'callback' => 'test_flaggable',
107
      ),
108
      'flagged' => array(
109
        'title' => t('It is flagged at least once'),
110
        'callback' => 'test_flagged',
111
      ),
112
      'flagged_by_current_user' => array(
113
        'title' => t('It is flagged by the current user'),
114
        'callback' => 'test_flagged_by_current_user',
115
      ),
116
    );
117
  }
118

    
119
  function tests_options() {
120
    $options = array();
121
    foreach ($this->tests_info() as $id => $info) {
122
      $options[$id] = $info['title'];
123
    }
124
    return $options;
125
  }
126

    
127
  function get_flag() {
128
    $flag_name = $this->options['flag_name'];
129

    
130
    if ($flag_name == '*relationship*') {
131
      // Pick the first flag mentioned in the relationships.
132
      foreach ($this->view->relationship as $id => $handler) {
133
        // Note: we can't do $handler->field, because the relationship handler's init() may overwrite it.
134
        if (strpos($handler->options['field'], 'flag') !== FALSE && !empty($handler->options['flag'])) {
135
          $flag = flag_get_flag($handler->options['flag']);
136
          if ($flag && $flag->entity_type == $this->flag_type) {
137
            return $flag;
138
          }
139
        }
140
      }
141
    }
142

    
143
    return flag_get_flag($flag_name);
144
  }
145

    
146
  /**
147
   * Tests whether the argument is flaggable, or flagged, or flagged by current
148
   * user. These are three possible tests, and which of the three to actually
149
   * carry out is determined by 'flag_test'.
150
   */
151
  function validate_argument($argument) {
152
    $flag_test = $this->options['flag_test'];
153
    $id_type   = $this->options['flag_id_type'];
154

    
155
    $flag = $this->get_flag();
156
    if (!$flag) {
157
      // Validator is misconfigured somehow.
158
      return TRUE;
159
    }
160

    
161
    if ($id_type == 'id') {
162
      if (!is_numeric($argument)) {
163
        // If a user is being smart and types several IDs where only one is
164
        // expected, we invalidate this.
165
        return FALSE;
166
      }
167
    }
168

    
169
    $ids = views_break_phrase($argument, $this);
170
    if ($ids->value == array(-1)) {
171
      // Malformed argument syntax. Invalidate.
172
      return FALSE;
173
    }
174
    $ids = $ids->value;
175

    
176
    // Delegate the testing to the particual test method. $passed then
177
    // holds the IDs that passed the test.
178
    $tests_info = $this->tests_info();
179
    $method = $tests_info[$flag_test]['callback'];
180
    if (method_exists($this, $method)) {
181
      $passed = $this->$method($ids, $flag);
182
    }
183
    else {
184
      $passed = array();
185
    }
186

    
187
    // If some IDs exist that haven't $passed our test then validation fails.
188
    $failed = array_diff($ids, $passed);
189
    return empty($failed);
190
  }
191

    
192
  //
193
  // The actual tests. They return the IDs that passed.
194
  //
195

    
196
  function test_flaggable($ids, $flag) {
197
    return array_filter($ids, array($flag, 'applies_to_entity_id'));
198
  }
199

    
200
  function test_flagged($ids, $flag) {
201
    // view_break_phrase() is guaranteed to return only integers, so this is SQL safe.
202
    $flattened_ids = implode(',', $ids);
203
    return $this->_test_by_sql("SELECT entity_id FROM {flag_counts} WHERE fid = :fid AND entity_id IN ($flattened_ids) AND count > 0", array(':fid' => $flag->fid));
204
  }
205

    
206
  function test_flagged_by_current_user($ids, $flag) {
207
    global $user;
208
    if (!$user->uid) {
209
      // Anonymous user
210
      return array();
211
    }
212
    $flattened_ids = implode(',', $ids);
213
    return $this->_test_by_sql("SELECT entity_id FROM {flagging} WHERE fid = :fid AND entity_id IN ($flattened_ids) AND uid = :uid", array(':fid' => $flag->fid, ':uid' => $user->uid));
214
  }
215

    
216
  // Helper: executes an SQL query and returns all the entity_id's.
217
  function _test_by_sql($sql, $args) {
218
    $passed = array();
219
    $result = db_query($sql, $args);
220
    foreach ($result as $row) {
221
      $passed[] = $row->entity_id;
222
    }
223
    return $passed;
224
  }
225
}
226