Newer
Older
Earl Miles
committed
<?php
/**
* This include file implements views functionality on behalf of profile.module
*/
function profile_views_tables() {
// add all profile fields as possible display fields and filters
$profile_fields = profile_views_get_fields();
foreach ($profile_fields as $field) {
$tables["$field->name"] = views_new_table('profile_values', 'internal', 'users', 'uid', 'uid', array( 'fid' => $field->fid ) );
profile_views_add_field($tables["$field->name"], $field);
profile_views_add_sort($tables["$field->name"], $field);
profile_views_add_filter($tables["$field->name"], $field);
}
return $tables;
Earl Miles
committed
}
function profile_views_arguments() {
}
function profile_views_default_views() {
}
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Get all profile fields
*/
function profile_views_get_fields() {
$fields = array();
$results = db_query("SELECT * FROM {profile_fields} ORDER BY category, weight");
while ($row = db_fetch_object($results)) {
// don't include private fields
if (user_access('administer users') || $row->visibility != PROFILE_PRIVATE) {
// make this work for default and modified profile.module
//$row->options = unserialize($row->options);
if( !empty($row->options) ){
if( unserialize($row->options) == false ){
// unserialized fields default version
$options = $row->options;
unset($row->options);
$row->options['selection'] = $options;
}
else {
// serialized fields or modified version
$row->options = unserialize($row->options);
}
}
$fields[] = $row;
}
}
return $fields;
}
/**
* Add profile fields to view table
*/
function profile_views_add_field(&$table, $field) {
$name = 'value';
$label = t('Profile: @field-name', array('@field-name' => $field->title));
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
switch ($field->type) {
case 'vocabulary':
$help = t('This will display all options of the profile field %field-name. ', array('%field-name' => $field ->title));
$others = array(
'sortable' => true,
'handler' => 'views_handler_field_profile_vocabulary',
);
break;
case 'selection':
$help = t('This will display all options of the profile field %field-name. ', array('%field-name' => $field ->title));
$others = array(
'sortable' => true,
'handler' => 'views_handler_field_profile_selection',
);
break;
case 'date':
$help = t('Display the date of the %field-name field.', array('%field-name' => $field ->title));
$others = array(
'sortable' => false,
'handler' => 'views_handler_field_profile_date',
);
break;
case 'checkbox':
$help = t('Checkbox based profile field help');
$others = array(
'sortable' => true,
'handler' => 'views_handler_field_profile_checkbox',
);
break;
default:
$help = t('Other types based profile field help');
$others = array(
'sortable' => true,
'handler' => 'views_handler_field_profile_default',
);
}
views_table_add_field($table, $name, $label, $help, $others);
}
/**
* Add profile fields to sort table
*/
function profile_views_add_sort(&$table, $field) {
$name = 'value';
$label = t('Profile: @field-name', array('@field-name' => $field->title));
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
$others = array();
switch ($field->type) {
case 'vocabulary':
$help = t('This allows you to sort by vocabulary terms');
break;
case 'selection':
$help = t('This allows you to sort by selection options');
break;
case 'date':
$other = array(
'handler' => 'views_handler_sort_profile_date',
);
$help = t('This allows you to sort by date');
break;
case 'checkbox':
$help = t('This allows you to sort yes/no');
break;
default:
$help = '';
}
views_table_add_sort($table, $name, $label, $help, $others);
}
/**
* Add profile filters to view table
*/
function profile_views_add_filter(&$table, $field) {
$name = 'value';
$label = t('Profile: @field-name', array('@field-name' => $field->title));
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
switch ($field->type) {
case 'vocabulary':
$help = t('Taxonomy based profile field help');
$others = array(
'list' => 'views_handler_filter_tid_by_voc',
'value-type' => 'array',
'option' => 'string',
'operator' => 'views_handler_operator_andor',
'handler' => 'views_handler_filter_profile_andor',
'vocabulary' => $field->options['vocabulary'],
);
break;
case 'selection':
$help = t('Selection based profile field help');
$others = array(
'list' => 'views_handler_filter_profile_selection',
'operator' => 'views_handler_operator_andor',
'options' => $field->options['selection'],
'handler' => 'views_handler_filter_profile_andor',
);
break;
case 'date':
$help = t('This filter allows nodes to be filtered by their creation date. Enter dates in the format: CCYY-MM-DD HH:MM:SS. Enter \'now\' to use the current time. You may enter a delta (in seconds) to the option that will be added to the time; this is most useful when combined with now. If you have the jscalendar module from jstools installed, you can use a popup date picker here.');
$others = array(
'operator' => 'views_handler_operator_gtlt',
//'value' => views_handler_filter_date_value_form(),
'value' => views_handler_filter_profile_date_value_form(),
'handler' => 'views_handler_filter_profile_date',
'value-type' => 'array',
'fid' => $field->fid,
);
break;
case 'checkbox':
$help = t('Checkbox based profile field help');
$others = array(
'operator' => array('=' => 'Equals'),
'list' => 'views_handler_operator_yesno',
//'value-type' => 'array',
//'list-type' => 'select',
);
break;
default:
$help = t('Other types based profile field help');
$others = array(
'list-type' => 'list',
'operator' => 'views_handler_operator_like',
'handler' => 'views_handler_filter_like',
);
}
views_table_add_filter($table, $name, $label, $help, $others);
}
/* FIELD HANDLER METHODS */
/*
* Format as a field as a email address.
*/
function views_handler_field_email($fieldinfo, $fielddata, $value, $data) {
return l($value, 'mailto:' . $value);
}
/**
* Display a profile field of type 'vocabulary'
*/
function views_handler_field_profile_vocabulary($fieldinfo, $fielddata, $value, $data) {
return _profile_field_vocabulary_getlist( unserialize($value) , 'taxonomy/term/' );
}
/**
* Display a profile field of type 'selection'
*/
function views_handler_field_profile_selection($fieldinfo, $fielddata, $value, $data) {
$value = (unserialize($value) === false) ? $value : unserialize($value);
if ( !is_array($value)) { $value = array($value); } // handles cases where values were set before this new profile module was put in place
return check_plain(implode(', ',$value));
}
/**
* Default display method for a profile field
*/
function views_handler_field_profile_default($fieldinfo, $fielddata, $value, $data) {
$value = (unserialize($value) === false) ? $value : unserialize($value);
return check_plain($value);
}
/**
* Display a profile field of type 'checkbox', view as tick mark
*/
function views_handler_field_profile_checkbox($fieldinfo, $fielddata, $value, $data) {
$value = (unserialize($value) === false) ? $value : unserialize($value);
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
return (check_plain($value) == '0') ? '✗' : '✓';
}
/**
* Display a profile field of type 'date'
*/
function views_handler_field_profile_date($fieldinfo, $fielddata, $value, $data) {
$format = substr(variable_get('date_format_short', 'm/d/Y - H:i'), 0, 5);
// Note: Avoid PHP's date() because it does not handle dates before
// 1970 on Windows. This would make the date field useless for e.g.
// birthdays.
$value = unserialize($value);
$replace = array('d' => sprintf('%02d', $value['day']),
'j' => $value['day'],
'm' => sprintf('%02d', $value['month']),
'M' => map_month($value['month']),
'Y' => $value['year'],
'H:i' => null,
'g:ia' => null);
return strtr($format, $replace);
}
/* SORT HANDLER METHODS */
function views_handler_sort_profile_date($action, &$query, $sortinfo, $sort) {
/*
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$query->add_orderby('', "rand()", "ASC");
break;
case 'pgsql':
$query->add_orderby('', "random()", "ASC");
break;
}
*/
}
/* FILTER LIST METHODS */
function views_handler_filter_profile_selection($op, $filter) {
$options = array();
foreach ( explode("\r", check_plain($filter['options'])) as $option) {
$options[trim($option)] = trim($option);
}
return $options;
}
/* FILTER HANDLER FUNCTIONS */
/**
* Handle a profile date fild.
*
* The problem here is that profile date fields are stored as serialized data.
* Doing any other operation that matching the date is difficult.
* possibilities include:
* 1.) hack profile.module to store date always with same number of digits,
* then user mysql STR_TO_DATE() function.
* 2.) Fetch the date here, unserialize, filter out users that match criteria
*/
function views_handler_filter_profile_date($op, $filter, &$filterinfo, &$query) {
// Approach 2:
// - read out date, unserialize
// - compare to input date
// - filter out matching users: WHERE users.uid IN(match1, match2, match3)
if( empty($filter['value']) ) return;
// CAREFUL! this may result in negative timestamp...
if( is_array($filter['value']) ){
// custom selection boxes for date entry were used
$value = mktime(0, 0, 0, $filter['value']['month'], $filter['value']['day'], $filter['value']['year']);
}
else {
// compatibility with old method of entering dates in a textfield
$value = $filter['value'] == 'now' ? time() : strtotime($filter['value']);
}
$users = array();
$results = db_query("SELECT uid, value FROM {profile_values} WHERE fid = %d", $filterinfo['fid']);
while ($profile = db_fetch_object($results)) {
$date = unserialize($profile->value);
// Epoch problem again...
$time = mktime(0, 0, 0, $date['month'], $date['day'], $date['year']);
switch($filter['operator']){
case '>':
if($time > $value) $users[] = $profile->uid;
break;
case '>=':
if($time >= $value) $users[] = $profile->uid;
break;
case '=':
if($time == $value) $users[] = $profile->uid;
break;
case '!=':
if($time != $value) $users[] = $profile->uid;
break;
case '<=':
if($time <= $value) $users[] = $profile->uid;
break;
case '<':
if($time < $value) $users[] = $profile->uid;
break;
}
}
if( empty($users) ) $users[] = 'NULL';
// TODO: make this owrk for date entry into textfield
if( checkdate($filter['value']['month'], $filter['value']['day'], $filter['value']['year']) ){
$query->ensure_table('users');
$query->add_where('users.uid IN(' . implode(', ', $users) .')');
}
}
/**
* Custom filter for andor operator for serialized profile fields.
* Note: This sunstring matching method may not be the most elegant method,
* yet working, and anyone is free to point out a better approach to me..
*/
function views_handler_filter_profile_andor($op, $filter, $filterinfo, &$query) {
if( empty($filter['value']) ) return;
switch ($op) {
case 'handler':
$table = $filterinfo['table'];
$column = $filterinfo['field'];
if (empty($column)) {
$fieldbits = explode('.', $filter['field']);
$column = $fieldbits[1];
}
$field = "$table.$column";
$query->ensure_table($table);
$where = array();
$args = array();
$operator = ($filter['operator'] == 'NOR') ? 'NOT LIKE' : 'LIKE';
// for 'force single' convert to array
if( !is_array($filter['value']) ) $filter['value'] = array($filter['value']);
foreach($filter['value'] as $value){
//$where[] = "%s $operator '%%\"%s\"%%'"; // use this line with modified version of profile.module
$where[] = "%s $operator '%s'"; // use this line with original version of profile.module
$args[] = $field;
$args[] = $value;
}
$operator = ($filter['operator'] == 'NOR') ? 'AND' : $filter['operator'];
$where = implode(" $operator ", $where);
$query->add_where("$where", $args);
break;
}
}
/**
* Drupal builtin date type does not allow to not have a
* value, hence build custom date field here
*/
function views_handler_filter_profile_date_value_form() {
$form['#tree'] = true;
$form['day'] = array(
'#prefix' => '<div class="container-inline">',
'#type' => 'select',
'#options' => array('0' => '--') + drupal_map_assoc(range(1,31)),
);
$form['month'] = array(
'#type' => 'select',
'#options' => array('0' => '--') + drupal_map_assoc(range(1,12), 'map_month'),
);
$form['year'] = array(
'#type' => 'select',
'#options' => array('0' => '--') + drupal_map_assoc(range(2006,2050)),
'#suffix' => '</div>',
);
$form['#after_build'] = array('views_handler_filter_profile_after_build');
return $form;
}
function views_handler_filter_profile_after_build($form_element) {
// get rid of 'edit' in name
$form_element['year']['#name'] = $form_element['#name'] . '[year]';
$form_element['month']['#name'] = $form_element['#name'] . '[month]';
$form_element['day']['#name'] = $form_element['#name'] . '[day]';
// set the select elements to the previously chosen date
$form_element['year']['#value'] = $form_element['#default_value']['year'];
$form_element['month']['#value'] = $form_element['#default_value']['month'];
$form_element['day']['#value'] = $form_element['#default_value']['day'];
return $form_element;
}