Projet

Général

Profil

Paste
Télécharger (7,78 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides support for the Views module.
6
 */
7

    
8
/**
9
 * Implements hook_views_plugins().
10
 */
11
function flag_views_plugins() {
12
  return array(
13
    'argument validator' => array(
14
      'flag_flaggable_node' => array(
15
        'title' => t('Flaggable node'),
16
        'flag type' => 'node',
17
        'handler' => 'flag_plugin_argument_validate_flaggability',
18
        'path' => drupal_get_path('module', 'flag') . '/includes',
19
      ),
20
      'flag_flaggable_user' => array(
21
        'title' => t('Flaggable user'),
22
        'flag type' => 'user',
23
        'handler' => 'flag_plugin_argument_validate_flaggability',
24
        'path' => drupal_get_path('module', 'flag') . '/includes',
25
      ),
26
      // A comment validator won't be very useful. Moreover, having it will
27
      // contribute to the argument object's $options polution, so let's skip
28
      // it.
29
    ),
30
  );
31
}
32
/**
33
 * Implements hook_views_data().
34
 */
35
function flag_views_data() {
36
  $data = array();
37

    
38
  $data['flagging']['table']['group'] = t('Flags');
39
  $data['flag_counts']['table']['group'] = t('Flags');
40

    
41
  // Notify views from flag 2.x of our changes to views data.
42
  // @see flag_update_7301().
43
  $data['flag_content']['moved to'] = 'flagging';
44
  // @see flag_update_7303().
45
  $data['flag_content']['content_id']['moved to'] = array('flagging', 'entity_id');
46

    
47
  $data['flagging']['uid'] = array(
48
    'title' => t('User uid'),
49
    'help' => t('The user that flagged an item. If you need more fields than the uid add the "Flags: User" relationship.'),
50
    'relationship' => array(
51
      'base' => 'users',
52
      'title' => t('User'),
53
      'help' => t('Relate an item to the user that flagged it.'),
54
      'handler' => 'views_handler_relationship',
55
      'label' => t('Flag user'),
56
    ),
57
    'filter' => array(
58
      'handler' => 'views_handler_filter_user_name',
59
    ),
60
    'argument' => array(
61
      'handler' => 'views_handler_argument_numeric',
62
    ),
63
    'field' => array(
64
      'handler' => 'views_handler_field_user',
65
    ),
66
  );
67

    
68
  $data['flagging']['timestamp'] = array(
69
    'title' => t('Flagged time'),
70
    'help' => t('Display the time the content was flagged by a user.'),
71
    'field' => array(
72
      'handler' => 'views_handler_field_date',
73
      'click sortable' => TRUE,
74
    ),
75
    'sort' => array(
76
      'handler' => 'views_handler_sort_date',
77
    ),
78
    'filter' => array(
79
      'handler' => 'views_handler_filter_date',
80
    ),
81
    'argument' => array(
82
      'handler' => 'views_handler_argument_date',
83
    ),
84
  );
85

    
86
  // Argument for content ID, used for "Who's flagged this" views.
87
  $data['flagging']['entity_id'] = array(
88
    'title' => t('Content ID'),
89
    'help' => t('The unique ID of the object that has been flagged.'),
90
    'argument' => array(
91
      'handler' => 'flag_handler_argument_entity_id',
92
    ),
93
  );
94

    
95
  // Specialized is null/is not null filter.
96
  $data['flagging']['flagged'] = array(
97
    'title' => t('Flagged'),
98
    'real field' => 'uid',
99
    'field' => array(
100
      'handler' => 'flag_handler_field_flagged',
101
      'label' => t('Flagged'),
102
      'help' => t('A boolean field to show whether the flag is set or not.'),
103
    ),
104
    'filter' => array(
105
      'handler' => 'flag_handler_filter_flagged',
106
      'label' => t('Flagged'),
107
      'help' => t('Filter to ensure content has or has not been flagged.'),
108
    ),
109
    'sort' => array(
110
      'handler' => 'flag_handler_sort_flagged',
111
      'label' => t('Flagged'),
112
      'help' => t('Sort by whether entities have or have not been flagged.'),
113
    ),
114
  );
115

    
116
  // Flag content links.
117
  $data['flagging']['ops'] = array(
118
    'title' => t('Flag link'),
119
    'help' => t('Display flag/unflag link.'),
120
    'field' => array(
121
      'handler' => 'flag_handler_field_ops',
122
    ),
123
  );
124

    
125
  $data['flag_counts']['count'] = array(
126
    'title' => t('Flag counter'),
127
    'help' => t('The number of times a piece of content is flagged by any user.'),
128
    'field' => array(
129
      'handler' => 'views_handler_field_numeric',
130
      'click sortable' => TRUE,
131
    ),
132
    'sort' => array(
133
      'handler' => 'views_handler_sort',
134
    ),
135
    'filter' => array(
136
      'handler' => 'views_handler_filter_numeric',
137
    ),
138
    'argument' => array(
139
      'handler' => 'views_handler_argument_numeric',
140
    ),
141
  );
142

    
143
  $data['flag_counts']['last_updated'] = array(
144
    'title' => t('Time last flagged'),
145
    'help' => t('The time a piece of content was most recently flagged by any user.'),
146
    'field' => array(
147
      'handler' => 'views_handler_field_date',
148
      'click sortable' => TRUE,
149
    ),
150
    'sort' => array(
151
      'handler' => 'views_handler_sort_date',
152
    ),
153
    'filter' => array(
154
      'handler' => 'views_handler_filter_date',
155
    ),
156
    'argument' => array(
157
      'handler' => 'views_handler_argument_date',
158
    ),
159
  );
160

    
161
  return $data;
162
}
163

    
164
/**
165
 * Implements hook_views_data_alter().
166
 */
167
function flag_views_data_alter(&$data) {
168

    
169
  foreach (array_keys(flag_fetch_definition()) as $flag_type) {
170
    $flag = flag_flag::factory_by_entity_type($flag_type);
171
    $info = $flag->get_views_info();
172

    
173
    if (!isset($info)) {
174
      continue;
175
    }
176

    
177
    if (!empty($info['join field'])) {
178
      // Add the flag relationship.
179
      $data[$info['views table']]['flag_content_rel'] = array(
180
        'group' => t('Flags'),
181
        'title' => $info['title'],
182
        'help' => $info['help'],
183
        'relationship' => array(
184
          'flag type' => $flag_type,
185
          'handler' => 'flag_handler_relationship_content',
186
          'label' => t('flag'),
187
          'base' => 'flagging',
188
          'base field' => 'entity_id',
189
          'relationship field' => $info['join field'],
190
        ),
191
      );
192

    
193
      // Add the flag counter relationship.
194
      $data[$info['views table']]['flag_count_rel'] = array(
195
        'group' => t('Flags'),
196
        'title' => $info['counter title'],
197
        'help' => $info['counter help'],
198
        'relationship' => array(
199
          'flag type' => $flag_type,
200
          'handler' => 'flag_handler_relationship_counts',
201
          'label' => t('counter'),
202
          'base' => 'flag_counts',
203
          'base field' => 'entity_id',
204
          'relationship field' => $info['join field'],
205
        ),
206
      );
207
    }
208
  }
209

    
210
  // Add a relationship for the user that flagged any type of content.
211
  $data['users']['flag_user_content_rel'] = array(
212
    'group' => t('Flags'),
213
    'title' => t("User's flaggings"),
214
    'help' => t('Relate users to the flaggings they have made on objects, using a particular flag.'),
215
    'relationship' => array(
216
      'base' => 'flagging',
217
      'base field' => 'uid',
218
      'relationship field' => 'uid',
219
      'handler' => 'flag_handler_relationship_user_content',
220
      'label' => t('user flagged content'),
221
    ),
222
  );
223
}
224

    
225
/**
226
 * Implements hook_views_query_substitutions().
227
 *
228
 * Allow replacement of current user's session id so we can cache these queries.
229
 */
230
function flag_views_query_substitutions() {
231
  return array(
232
    '***FLAG_CURRENT_USER_SID***' => flag_get_sid(),
233
  );
234
}
235
/**
236
 * A helper function that creates a radio list of available flags.
237
 *
238
 * This function is used to select the desired flag when setting up flag
239
 * relationships and fields.
240
 */
241
function flag_views_flag_config_form($form_type, $entity_type, $current_flag) {
242
  $flags = flag_get_flags($entity_type);
243

    
244
  $options = array();
245
  foreach ($flags as $flag) {
246
    $options[$flag->name] = $flag->get_title();
247
  }
248

    
249
  $form = array(
250
    '#type' => $form_type,
251
    '#title' => t('Flag'),
252
    '#options' => $options,
253
    '#default_value' => $current_flag,
254
    '#required' => TRUE,
255
  );
256

    
257
  return $form;
258
}
259

    
260
/**
261
 * Helper function that gets the first defined flag and returns its name.
262
 */
263
function flag_views_flag_default($entity_type) {
264
  $default_flag = &drupal_static(__FUNCTION__, array());
265

    
266
  if (!array_key_exists($entity_type, $default_flag)) {
267
    $flag = array_shift(flag_get_flags($entity_type));
268
    $default_flag[$entity_type] = $flag ? $flag->name : NULL;
269
  }
270

    
271
  return $default_flag[$entity_type];
272
}