Projet

Général

Profil

Paste
Télécharger (8,86 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / flag / includes / views / flag_handler_relationships.inc @ 76e2e7c3

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains various relationship handlers.
6
 */
7

    
8
/**
9
 * Base class for all our relationship classes.
10
 *
11
 * @ingroup views
12
 */
13
abstract class flag_handler_relationship extends views_handler_relationship {
14

    
15
  /**
16
   * Every relationship has a 'flag' option.
17
   */
18
  function option_definition() {
19
    $options = parent::option_definition();
20
    $options['flag'] = array('default' => NULL);
21
    $options['required'] = array('default' => 1);
22
    return $options;
23
  }
24

    
25
  /**
26
   * Make sure the flag exists.
27
   *
28
   * When importing views, or when deleting flags, inconsistent views may
29
   * result. This validator is called by Views before saving or previewing a
30
   * view.
31
   */
32
  function validate() {
33
    $errors = array();
34
    $tokens = array(
35
      '@relationship-name' => $this->ui_name() . ' ' . $this->admin_summary(),
36
      '@flag-name' => $this->options['flag'],
37
    );
38
    if (!$this->options['flag']) {
39
      $errors[] = t('You must pick a flag to use for the relationship "@relationship-name".', $tokens);
40
    }
41
    elseif (!flag_get_flag($this->options['flag'])) {
42
      $errors[] = t('This view is looking for a flag by the name "@flag-name", but there is no such flag. Perhaps it was deleted. Please update the relationship "@relationship-name" in this view to use an existing flag.', $tokens);
43
    }
44
    return $errors;
45
  }
46

    
47
  function get_flag_type() {
48
    return isset($this->definition['flag type']) ? $this->definition['flag type'] : NULL;
49
  }
50

    
51
  /**
52
   * Returns the flag object.
53
   */
54
  function get_flag() {
55

    
56
    // Backward compatibility: There may exist old views on the system whose
57
    // 'flag' option isn't set. (This happens if the admin had skippped
58
    // clicking the 'Update' button.) When run, these views should behave as
59
    // if the first flag was selected.
60
    if (!isset($this->options['flag'])) {
61
      $this->options['flag'] = flag_views_flag_default($this->get_flag_type());
62
    }
63

    
64
    // Validation: Since validate() is called only when in Views's
65
    // administrative UI, we need to do validation at "run time" ourselves.
66
    if (($errors = $this->validate())) {
67
      foreach ($errors as $error) {
68
        drupal_set_message($error, 'error');
69
      }
70
    }
71

    
72
    return flag_get_flag($this->options['flag']);
73
  }
74

    
75
  // @todo: It's logical that this class should also implement options_form(),
76
  // to show the flag selector, and query(), to filter on the flag.
77
}
78

    
79
/**
80
 * Specialized relationship handler associating flags and content.
81
 *
82
 * @ingroup views
83
 */
84
class flag_handler_relationship_content extends flag_handler_relationship {
85

    
86
  function option_definition() {
87
    $options = parent::option_definition();
88
    $options['user_scope'] = array('default' => 'current');
89
    return $options;
90
  }
91

    
92
  function options_form(&$form, &$form_state) {
93
    parent::options_form($form, $form_state);
94
    $entity_type = $this->definition['flag type'];
95
    $form['label']['#description'] .= ' ' . t('The name of the selected flag makes a good label.');
96
    $form['flag'] = flag_views_flag_config_form('radios', $entity_type, $this->options['flag']);
97

    
98
    $form['user_scope'] = array(
99
      '#type' => 'radios',
100
      '#title' => t('By'),
101
      '#options' => array('current' => t('Current user'), 'any' => t('Any user')),
102
      '#default_value' => $this->options['user_scope'],
103
    );
104

    
105
    $form['required']['#title'] = t('Include only flagged content');
106
    $form['required']['#description'] = t('If checked, only content that has this flag will be included. Leave unchecked to include all content; or, in combination with the <em>Flagged</em> filter, <a href="@unflagged-url">to limit the results to specifically unflagged content</a>.', array('@unflagged-url' => 'http://drupal.org/node/299335'));
107

    
108
    if (!$form['flag']['#options']) {
109
      $form = array(
110
        'error' => array(
111
          '#markup' => '<p class="error form-item">' . t('No %type flags exist. You must first <a href="@create-url">create a %type flag</a> before being able to use this relationship type.', array('%type' => $entity_type, '@create-url' => url(FLAG_ADMIN_PATH))) . '</p>',
112
        ),
113
      );
114
      $form_state['no flags exist'] = TRUE;
115
    }
116
    if (module_exists('session_api')) {
117
      $form['session_warning'] = array(
118
        '#markup' => '<p class="warning form-item">' . t('<strong>Warning</strong>: Adding this relationship for any flag that contains <strong>anonymous flagging access</strong> will disable page caching for anonymous users when this view is executed. (But this is only true when the relationship is constrained to "Current user", not to "Any user".) It is recommended to create a dedicated page for views containing anonymous user data.') . '</p>',
119
      );
120
    }
121
  }
122

    
123
  function options_validate(&$form, &$form_state) {
124
    if (!empty($form_state['no flags exist'])) {
125
      form_error($form, t('You must first create a flag'));
126
    }
127
  }
128

    
129
  function admin_summary() {
130
    return $this->options['user_scope'] == 'current' ? t('by current user') : t('by any user');
131
  }
132

    
133
  function ui_name($short = FALSE) {
134
    // We put the bookmark name in the UI string to save space.
135
    return t('!group: !title', array('!group' => $this->definition['group'], '!title' => empty($this->options['flag']) ? t('(Please select a flag)') : $this->options['flag']));
136
  }
137

    
138
  /**
139
   * Called to implement a relationship in a query.
140
   */
141
  function query() {
142
    if (!($flag = $this->get_flag())) {
143
      return;
144
    }
145

    
146
    $this->definition['extra'][] = array(
147
      'field' => 'fid',
148
      'value' => $flag->fid,
149
      'numeric' => TRUE,
150
    );
151
    if ($this->options['user_scope'] == 'current' && !$flag->global) {
152
      $this->definition['extra'][] = array(
153
        'field' => 'uid',
154
        'value' => '***CURRENT_USER***',
155
        'numeric' => TRUE,
156
      );
157
      $flag_roles = user_roles(FALSE, "flag $flag->name");
158
      if (isset($flag_roles[DRUPAL_ANONYMOUS_RID])) {
159
        // Disable page caching for anonymous users.
160
        drupal_page_is_cacheable(FALSE);
161

    
162
        // Add in the SID from Session API for anonymous users.
163
        $this->definition['extra'][] = array(
164
          'field' => 'sid',
165
          'value' => '***FLAG_CURRENT_USER_SID***',
166
          'numeric' => TRUE,
167
        );
168
      }
169
    }
170
    parent::query();
171
  }
172
}
173

    
174
/**
175
 * Specialized relationship handler associating flag counts and content.
176
 *
177
 * @ingroup views
178
 */
179
class flag_handler_relationship_counts extends flag_handler_relationship {
180

    
181
  function options_form(&$form, &$form_state) {
182
    parent::options_form($form, $form_state);
183
    $entity_type = $this->definition['flag type'];
184
    $form['flag'] = flag_views_flag_config_form('radios', $entity_type, $this->options['flag']);
185

    
186
    $form['required']['#title'] = t('Include only flagged content');
187
    $form['required']['#description'] = t('If checked, only content that is flagged will be included.');
188
  }
189

    
190
  function admin_summary() {
191
    // Nothing to show.
192
  }
193

    
194
  function ui_name($short = FALSE) {
195
    // We put the bookmark name in the UI string to save space.
196
    return t('!group: !title counter', array('!group' => $this->definition['group'], '!title' => $this->options['flag']));
197
  }
198

    
199
  /**
200
   * Called to implement a relationship in a query.
201
   */
202
  function query() {
203
    if (!($flag = $this->get_flag())) {
204
      return;
205
    }
206

    
207
    $this->definition['extra'][] = array(
208
      'field' => 'fid',
209
      'value' => $flag->fid,
210
      'numeric' => TRUE,
211
    );
212
    if (!empty($this->options['required'])) {
213
      // Unfortunately, we may have zeros in our table, so having
214
      // parent::query() do INNER JOIN doesn't suffice. We need to filter these
215
      // zeros out.
216
      // @todo Make sure zero records aren't written in the first place, and
217
      // remove this code.
218
      $this->definition['extra'][] = array(
219
        'field' => 'count',
220
        'operator' => '>',
221
        'value' => '0',
222
        'numeric' => TRUE,
223
      );
224
    }
225
    parent::query();
226
  }
227
}
228

    
229
/**
230
 * Specialized relationship handler associating flags and users.
231
 *
232
 * @ingroup views
233
 */
234
class flag_handler_relationship_user_content extends flag_handler_relationship {
235

    
236
  function options_form(&$form, &$form_state) {
237
    parent::options_form($form, $form_state);
238
    $form['label']['#description'] .= ' ' . t('Including name of the selected flag helps identify this relationship.');
239

    
240
    $form['flag'] = flag_views_flag_config_form('radios', NULL, $this->options['flag']);
241
    $form['flag']['#title'] = t('Flagged');
242

    
243
    $form['required']['#title'] = t('Include only users who have flagged content.');
244
    $form['required']['#description'] = t('If checked, only users that have flagged any content with this flag will be included.');
245
  }
246

    
247
  function admin_summary() {
248
    return $this->options['flag'];
249
  }
250

    
251
  /**
252
   * Called to implement a relationship in a query.
253
   */
254
  function query() {
255
    if (!($flag = $this->get_flag())) {
256
      return;
257
    }
258

    
259
    $this->definition['extra'][] = array(
260
      'field' => 'fid',
261
      'value' => $flag->fid,
262
      'numeric' => TRUE,
263
    );
264
    parent::query();
265
  }
266
}