1
|
<?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
|
foreach($entity_types as $key => $entity_type) {
|
279
|
$default_relationships[] = array(
|
280
|
'description' => $entity_type['label'],
|
281
|
'entity_type' => $key,
|
282
|
'base_table' => $entity_type['base table'],
|
283
|
'entity_id_column' => $entity_type['entity keys']['id'],
|
284
|
'pseudo_vote' => 'votingapi_vote', // for legacy compatability w/RC1.
|
285
|
'pseudo_cache' => 'votingapi_cache', // for legacy compatability w/RC1.
|
286
|
);
|
287
|
}
|
288
|
|
289
|
foreach ($default_relationships as $data) {
|
290
|
$pseudo = str_replace(array(' ', '-', '.'), '_', $data['entity_type'] . '_' . $data['entity_id_column']);
|
291
|
$pseudo_vote = empty($data['pseudo_vote']) ? 'vapi_' . $pseudo : $data['pseudo_vote'];
|
292
|
$pseudo_cache = empty($data['pseudo_cache']) ? 'vapic_' . $pseudo : $data['pseudo_cache'];
|
293
|
|
294
|
$views_data[$data['base_table']][$pseudo_vote]['relationship'] = array(
|
295
|
'title' => 'Votes',
|
296
|
'help' => 'Votes cast by users on ' . $data['description'] . '.',
|
297
|
'base' => 'votingapi_vote',
|
298
|
'field' => 'entity_id',
|
299
|
'relationship field' => $data['entity_id_column'],
|
300
|
'handler' => 'votingapi_views_handler_relationship',
|
301
|
'extra' => array(
|
302
|
array(
|
303
|
'field' => 'entity_type',
|
304
|
'value' => $data['entity_type'],
|
305
|
'numeric' => FALSE
|
306
|
),
|
307
|
),
|
308
|
);
|
309
|
|
310
|
$views_data[$data['base_table']][$pseudo_cache]['relationship'] = array(
|
311
|
'title' => 'Vote results',
|
312
|
'help' => 'Aggregate results of votes cast on ' . $data['description'] . '.',
|
313
|
'base' => 'votingapi_cache',
|
314
|
'field' => 'entity_id',
|
315
|
'relationship field' => $data['entity_id_column'],
|
316
|
'handler' => 'votingapi_views_handler_relationship',
|
317
|
'extra' => array(
|
318
|
array(
|
319
|
'field' => 'entity_type',
|
320
|
'value' => $data['entity_type'],
|
321
|
'numeric' => FALSE
|
322
|
),
|
323
|
),
|
324
|
);
|
325
|
}
|
326
|
}
|
327
|
|
328
|
|
329
|
function _votingapi_views_sql_safe_entity_type($string) {
|
330
|
return str_replace(array(' ', '-', '.'), '_', $string);
|
331
|
}
|
332
|
|
333
|
|
334
|
/**
|
335
|
* Implements of hook_views_analyze().
|
336
|
*
|
337
|
* Warns admins if a VotingAPI relationship has been defined, but filters on the
|
338
|
* relationship haven't been set.
|
339
|
*/
|
340
|
function votingapi_views_analyze($view) {
|
341
|
$ret = array();
|
342
|
// Check for something other than the default display:
|
343
|
foreach ($view->display as $id => $display) {
|
344
|
if (!$display->handler->is_defaulted('access') || !$display->handler->is_defaulted('filters')) {
|
345
|
$relationships = $display->handler->get_option('relationships');
|
346
|
foreach ($relationships as $relationship) {
|
347
|
if ($relationship['field'] == 'votingapi_vote') {
|
348
|
if (empty($relationship['votingapi']['value_type']) ||
|
349
|
empty($relationship['votingapi']['tag'])) {
|
350
|
$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');
|
351
|
}
|
352
|
}
|
353
|
elseif ($relationship['field'] == 'votingapi_cache') {
|
354
|
if (empty($relationship['votingapi']['value_type']) ||
|
355
|
empty($relationship['votingapi']['tag']) ||
|
356
|
empty($relationship['votingapi']['function'])) {
|
357
|
$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');
|
358
|
}
|
359
|
}
|
360
|
}
|
361
|
}
|
362
|
}
|
363
|
|
364
|
return $ret;
|
365
|
}
|