Projet

Général

Profil

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

root / drupal7 / sites / all / modules / votingapi / views / votingapi.views.inc @ 5a4b9049

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Provide views data for votingapi.module.
6
 */
7
8
/**
9
 * @defgroup views_votingapi_module votingapi.module handlers.
10
 *
11
 * @{
12
 */
13
14
15
/**
16
 * Implements of hook_views_handlers().
17
 */
18
function votingapi_views_handlers() {
19
  return array(
20
    'handlers' => array(
21
      'votingapi_views_handler_field_value' => array(
22
        'parent' => 'views_handler_field_numeric',
23
        'path' => drupal_get_path('module', 'votingapi') . '/views',
24
      ),
25
      'votingapi_views_handler_relationship' => array(
26
        'parent' => 'views_handler_relationship',
27
        'path' => drupal_get_path('module', 'votingapi') . '/views',
28
      ),
29
      'votingapi_views_handler_sort_nullable' => array(
30
        'parent' => 'views_handler_sort',
31
        'path' => drupal_get_path('module', 'votingapi') . '/views',
32
      ),
33
    ),
34
  );
35
}
36
37
38
/**
39
 * Implements of hook_views_data().
40
 */
41
function votingapi_views_data() {
42
  // Basic table information.
43
44
  // Define the base group of this table. Fields that don't
45
  // have a group defined will go into this field by default.
46
  $data['votingapi_vote']['table']['group']  = t('Votes');
47
  $data['votingapi_cache']['table']['group']  = t('Vote results');
48
49
  // ----------------------------------------------------------------
50
  // Fields
51
52
  // value
53
  $data['votingapi_vote']['value'] = array(
54
    'title' => t('Value'), // The item it appears as on the UI,
55
    'help' => t('The value of an individual cast vote.'), // The help that appears on the UI,
56
     // Information for displaying a title as a field
57
    'field' => array(
58
      'handler' => 'votingapi_views_handler_field_value',
59
      'click sortable' => TRUE,
60
    ),
61
    'filter' => array(
62
      'handler' => 'views_handler_filter_numeric',
63
      'allow empty' => TRUE,
64
    ),
65
    'sort' => array(
66
      'handler' => 'votingapi_views_handler_sort_nullable',
67
    ),
68
  );
69
70
  // value type
71
  $data['votingapi_vote']['value_type'] = array(
72
    'title' => t('Value type'), // The item it appears as on the UI,
73
    'help' => t('The nature of the vote being cast (points, percentage, etc).'), // The help that appears on the UI,
74
     // Information for displaying a title as a field
75
    'field' => array(
76
      'handler' => 'views_handler_field',
77
      'click sortable' => TRUE,
78
    ),
79
    'filter' => array(
80
      'handler' => 'views_handler_filter_string',
81
    ),
82
    'sort' => array(
83
      'handler' => 'views_handler_sort',
84
    ),
85
  );
86
87
  // tag
88
  $data['votingapi_vote']['tag'] = array(
89
    'title' => t('Tag'), // The item it appears as on the UI,
90
    'help' => t('An optional tag to group multi-criteria votes.'), // The help that appears on the UI,
91
     // Information for displaying a title as a field
92
    'field' => array(
93
      'handler' => 'views_handler_field',
94
      'click sortable' => TRUE,
95
    ),
96
    'filter' => array(
97
      'handler' => 'views_handler_filter_string',
98
    ),
99
    'sort' => array(
100
      'handler' => 'views_handler_sort',
101
    ),
102
  );
103
104
  // uid
105
  $data['votingapi_vote']['uid'] = array(
106
    'title' => t('User'), // The item it appears as on the UI,
107
    'help' => t('The user who cast the vote.'), // The help that appears on the UI,
108
     // Information for displaying a title as a field
109
    'field' => array(
110
      'handler' => 'views_handler_field_user',
111
      'click sortable' => TRUE,
112
    ),
113
    'filter' => array(
114
      'handler' => 'views_handler_filter_user_name',
115
    ),
116
    'sort' => array(
117
      'handler' => 'views_handler_sort',
118
    ),
119
    'argument' => array(
120
      'handler' => 'views_handler_argument_user_uid',
121
    ),
122
    'relationship' => array(
123
      'handler' => 'views_handler_relationship',
124
      'base' => 'users',
125
      'field' => 'uid',
126
      'label' => t('Individual voter'),
127
    ),
128
  );
129
130
  // uid
131
  $data['votingapi_vote']['current_uid'] = array(
132
    'title' => t('Current user'), // The item it appears as on the UI,
133
    'help' => t('Restrict votes to those cast by the current user.'), // The help that appears on the UI,
134
    'real field' => 'uid',
135
    'filter' => array(
136
      'handler' => 'views_handler_filter_user_current',
137
    ),
138
  );
139
140
  // vote_source
141
  $data['votingapi_vote']['vote_source'] = array(
142
    'title' => t('IP Address'), // The item it appears as on the UI,
143
    'help' => t('The IP address of the user who cast the vote.'), // The help that appears on the UI,
144
     // Information for displaying a title as a field
145
    'field' => array(
146
      'handler' => 'views_handler_field',
147
      'click sortable' => TRUE,
148
    ),
149
    'filter' => array(
150
      'handler' => 'views_handler_filter_string',
151
    ),
152
    'sort' => array(
153
      'handler' => 'views_handler_sort',
154
    ),
155
  );
156
157
158
  // timestamp
159
  $data['votingapi_vote']['timestamp'] = array(
160
    'title' => t('Timestamp'), // The item it appears as on the UI,
161
    'help' => t('The time the vote was cast.'), // The help that appears on the UI,
162
     // Information for displaying a title as a field
163
    'field' => array(
164
      'handler' => 'views_handler_field_date',
165
      'click sortable' => TRUE,
166
    ),
167
    'filter' => array(
168
      'handler' => 'views_handler_filter_date',
169
    ),
170
    'sort' => array(
171
      'handler' => 'views_handler_sort',
172
    ),
173
  );
174
175
176
  // Cache table fields
177
178
  // value
179
  $data['votingapi_cache']['value'] = array(
180
    'title' => t('Value'), // The item it appears as on the UI,
181
    'help' => t('The value of an individual cast vote.'), // The help that appears on the UI,
182
     // Information for displaying a title as a field
183
    'field' => array(
184
      'handler' => 'votingapi_views_handler_field_value',
185
      'click sortable' => TRUE,
186
    ),
187
    'filter' => array(
188
      'handler' => 'views_handler_filter_numeric',
189
      'allow empty' => TRUE,
190
    ),
191
    'sort' => array(
192
      'handler' => 'votingapi_views_handler_sort_nullable',
193
    ),
194
  );
195
196
  // value type
197
  $data['votingapi_cache']['value_type'] = array(
198
    'title' => t('Value type'), // The item it appears as on the UI,
199
    'help' => t('The nature of the results in question (points, percentage, etc).'), // The help that appears on the UI,
200
     // Information for displaying a title as a field
201
    'field' => array(
202
      'handler' => 'views_handler_field',
203
      'click sortable' => TRUE,
204
    ),
205
    'filter' => array(
206
      'handler' => 'views_handler_filter_string',
207
    ),
208
    'sort' => array(
209
      'handler' => 'views_handler_sort',
210
    ),
211
  );
212
213
  // tag
214
  $data['votingapi_cache']['tag'] = array(
215
    'title' => t('Tag'), // The item it appears as on the UI,
216
    'help' => t('An optional tag to group multi-criteria results.'), // The help that appears on the UI,
217
     // Information for displaying a title as a field
218
    'field' => array(
219
      'handler' => 'views_handler_field',
220
      'click sortable' => TRUE,
221
    ),
222
    'filter' => array(
223
      'handler' => 'views_handler_filter_string',
224
    ),
225
    'sort' => array(
226
      'handler' => 'views_handler_sort',
227
    ),
228
  );
229
230
231
  // function
232
  $data['votingapi_cache']['function'] = array(
233
    'title' => t('Function'), // The item it appears as on the UI,
234
    'help' => t('The aggregate function used to calculate the result.'), // The help that appears on the UI,
235
     // Information for displaying a title as a field
236
    'field' => array(
237
      'handler' => 'views_handler_field',
238
      'click sortable' => TRUE,
239
    ),
240
    'filter' => array(
241
      'handler' => 'views_handler_filter_string',
242
    ),
243
    'sort' => array(
244
      'handler' => 'views_handler_sort',
245
    ),
246
  );
247
248
249
  // timestamp
250
  $data['votingapi_cache']['timestamp'] = array(
251
    'title' => t('Timestamp'), // The item it appears as on the UI,
252
    'help' => t('The time the results were calculated.'), // The help that appears on the UI,
253
     // Information for displaying a title as a field
254
    'field' => array(
255
      'handler' => 'views_handler_field_date',
256
      'click sortable' => TRUE,
257
    ),
258
    'filter' => array(
259
      'handler' => 'views_handler_filter_date',
260
    ),
261
    'sort' => array(
262
      'handler' => 'views_handler_sort',
263
    ),
264
  );
265
  return $data;
266
}
267
268
/**
269
 * @}
270
 */
271
272
function votingapi_views_data_alter(&$views_data) {
273
  // Add relationship handlers for both tables, for any base tables currently
274
  // available to Views.
275
276
  //Get all entity types in the system and register as relationship.
277
  $entity_types = entity_get_info();
278 7942932f Assos Assos
  $default_relationships = array();
279 85ad3d82 Assos Assos
  foreach($entity_types as $key => $entity_type) {
280 7942932f Assos Assos
    if (isset($entity_type['base table'])) {
281
      $default_relationships[] = array(
282
        'description' => $entity_type['label'],
283
        'entity_type' => $key,
284
        'base_table' => $entity_type['base table'],
285
        'entity_id_column' => $entity_type['entity keys']['id'],
286
        'pseudo_vote' => 'votingapi_vote',    // for legacy compatability w/RC1.
287
        'pseudo_cache' => 'votingapi_cache',  // for legacy compatability w/RC1.
288
      );
289
    }
290 85ad3d82 Assos Assos
  }
291
292
  foreach ($default_relationships as $data) {
293
    $pseudo = str_replace(array(' ', '-', '.'), '_', $data['entity_type'] . '_' . $data['entity_id_column']);
294
    $pseudo_vote = empty($data['pseudo_vote']) ? 'vapi_' . $pseudo : $data['pseudo_vote'];
295
    $pseudo_cache = empty($data['pseudo_cache']) ? 'vapic_' . $pseudo : $data['pseudo_cache'];
296
297
    $views_data[$data['base_table']][$pseudo_vote]['relationship'] = array(
298
      'title' => 'Votes',
299
      'help' => 'Votes cast by users on ' . $data['description'] . '.',
300
      'base' => 'votingapi_vote',
301
      'field' => 'entity_id',
302
      'relationship field' => $data['entity_id_column'],
303
      'handler' => 'votingapi_views_handler_relationship',
304
      'extra' => array(
305
        array(
306
          'field' => 'entity_type',
307
          'value' => $data['entity_type'],
308
          'numeric' => FALSE
309
        ),
310
      ),
311
    );
312
313
    $views_data[$data['base_table']][$pseudo_cache]['relationship'] = array(
314
      'title' => 'Vote results',
315
      'help' => 'Aggregate results of votes cast on ' . $data['description'] . '.',
316
      'base' => 'votingapi_cache',
317
      'field' => 'entity_id',
318
      'relationship field' => $data['entity_id_column'],
319
      'handler' => 'votingapi_views_handler_relationship',
320
      'extra' => array(
321
        array(
322
          'field' => 'entity_type',
323
          'value' => $data['entity_type'],
324
          'numeric' => FALSE
325
        ),
326
      ),
327
    );
328
  }
329
}
330
331
332
function _votingapi_views_sql_safe_entity_type($string) {
333
  return str_replace(array(' ', '-', '.'), '_', $string);
334
}
335
336
337
/**
338
 * Implements of hook_views_analyze().
339
 *
340
 * Warns admins if a VotingAPI relationship has been defined, but filters on the
341
 * relationship haven't been set.
342
 */
343
function votingapi_views_analyze($view) {
344
  $ret = array();
345
  // Check for something other than the default display:
346
  foreach ($view->display as $id => $display) {
347
    if (!$display->handler->is_defaulted('access') || !$display->handler->is_defaulted('filters')) {
348
      $relationships = $display->handler->get_option('relationships');
349
      foreach ($relationships as $relationship) {
350
        if ($relationship['field'] == 'votingapi_vote') {
351
          if (empty($relationship['votingapi']['value_type']) ||
352
              empty($relationship['votingapi']['tag'])) {
353
            $ret[] = views_ui_analysis(t('Display %display defines a VotingAPI vote relationship, but does not restrict the relationship data. This may result in duplicate entries in the resulting view.', array('%display' => $display->display_title)), 'warning');
354
          }
355
        }
356
        elseif ($relationship['field'] == 'votingapi_cache') {
357
          if (empty($relationship['votingapi']['value_type']) ||
358
              empty($relationship['votingapi']['tag']) ||
359
              empty($relationship['votingapi']['function'])) {
360
            $ret[] = views_ui_analysis(t('Display %display defines a VotingAPI results relationship, but does not restrict the relationship data. This may result in duplicate entries in the resulting view.', array('%display' => $display->display_title)), 'warning');
361
          }
362
        }
363
      }
364
    }
365
  }
366
367
  return $ret;
368
}