Newer
Older
//$Id$
/**
* @file
* Provide views data and handlers for user.module
*/
/**
* @defgroup views_user_module user.module handlers
*
* @{
*/
/**
* Implementation of hook_views_data()
*/
// ----------------------------------------------------------------
// users table
// Define the base group of this table. Fields that don't
// have a group defined will go into this field by default.
$data['users']['table']['group'] = t('User');
$data['users']['table']['base'] = array(
'field' => 'uid',
'title' => t('User'),
'help' => t('Users who have created accounts on your site.'),
Earl Miles
committed
$data['users']['table']['join'] = array(
'node' => array(
'left_field' => 'uid',
'field' => 'uid',
),
// This will be the revision author, not the node author.
'node_revisions' => array(
'left_field' => 'uid',
'field' => 'uid',
),
);
// uid
$data['users']['uid'] = array(
'title' => t('Uid'),
'help' => t('The user ID'), // The help that appears on the UI,
'field' => array(
'handler' => 'views_handler_field_user',
'click sortable' => TRUE,
),
'argument' => array(
'handler' => 'views_handler_argument_user_uid',
'name field' => 'name', // display this field in the summary
),
'filter' => array(
'title' => t('Name'),
'handler' => 'views_handler_filter_user_name',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
// uid
$data['users']['uid_current'] = array(
'real field' => 'uid',
'title' => t('Current'),
'help' => t('Filter the view to the currently logged in user.'),
'filter' => array(
'handler' => 'views_handler_filter_user_current',
),
);
// name
$data['users']['name'] = array(
'title' => t('Name'), // The item it appears as on the UI,
'help' => t('The user or author name.'), // The help that appears on the UI,
'field' => array(
'handler' => 'views_handler_field_user_name',
Earl Miles
committed
'click sortable' => TRUE,
'sort' => array(
'handler' => 'views_handler_sort',
),
Earl Miles
committed
'argument' => array(
'handler' => 'views_handler_argument_string',
),
// mail
Earl Miles
committed
// Note that this field implements field level access control.
$data['users']['mail'] = array(
'title' => t('E-mail'), // The item it appears as on the UI,
'help' => t('Email address for a given user. Only accessible to users with <em>administer users</em> permission'), // The help that appears on the UI,
'field' => array(
'handler' => 'views_handler_field_user_mail',
'click sortable' => TRUE,
Earl Miles
committed
'access callback' => 'user_access',
'access arguments' => array('administer users'),
Earl Miles
committed
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
);
Earl Miles
committed
// picture
$data['users']['picture'] = array(
'title' => t('Picture'),
'help' => t("The user's picture, if allowed."), // The help that appears on the UI,
Earl Miles
committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Information for displaying the uid
'field' => array(
'handler' => 'views_handler_field_user_picture',
'click sortable' => TRUE,
),
);
// created field
$data['users']['created'] = array(
'title' => t('Created date'), // The item it appears as on the UI,
'help' => t('The date the user was created.'), // The help that appears on the UI,
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
);
// access field
$data['users']['access'] = array(
'title' => t('Last access'), // The item it appears as on the UI,
'help' => t("The user's last access date."), // The help that appears on the UI,
Earl Miles
committed
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
);
// login field
$data['users']['login'] = array(
'title' => t('Last login'), // The item it appears as on the UI,
'help' => t("The user's last login date."), // The help that appears on the UI,
Earl Miles
committed
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
);
Earl Miles
committed
// published status
$data['users']['status'] = array(
'title' => t('Active'), // The item it appears as on the UI,
'help' => t('Whether a user is active or blocked.'), // The help that appears on the UI,
// Information for displaying a title as a field
'field' => array(
'handler' => 'views_handler_field_boolean',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_boolean_operator',
'label' => t('Active'),
Earl Miles
committed
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
// log field
$data['users']['signature'] = array(
'title' => t('Signature'), // The item it appears as on the UI,
'help' => t("The user's signature."), // The help that appears on the UI,
Earl Miles
committed
// Information for displaying a title as a field
'field' => array(
'handler' => 'views_handler_field_xss',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
);
$data['users']['edit_node'] = array(
'field' => array(
'title' => t('Edit link'),
'help' => t('Provide a simple link to edit the user.'),
'handler' => 'views_handler_field_user_link_edit',
),
);
$data['users']['delete_node'] = array(
'field' => array(
'title' => t('Delete link'),
'help' => t('Provide a simple link to delete the user.'),
'handler' => 'views_handler_field_user_link_delete',
),
);
Earl Miles
committed
// ----------------------------------------------------------------------
Earl Miles
committed
$data['users_roles']['table']['group'] = t('User');
// Explain how this table joins to others.
$data['users_roles']['table']['join'] = array(
// Directly links to users table.
'users' => array(
'left_field' => 'uid',
'field' => 'uid',
Earl Miles
committed
),
Earl Miles
committed
'node' => array(
'table' => 'users',
'left_field' => 'uid',
'field' => 'uid',
Earl Miles
committed
),
Earl Miles
committed
'node_revisions' => array(
'table' => 'users',
'left_field' => 'uid',
'field' => 'uid',
),
);
$data['users_roles']['rid'] = array(
Earl Miles
committed
'title' => t('Roles'),
Earl Miles
committed
'help' => t('Roles that a user belongs to.'),
Earl Miles
committed
'field' => array(
'handler' => 'views_handler_field_user_roles',
),
'filter' => array(
'handler' => 'views_handler_filter_user_roles',
'numeric' => TRUE,
),
'argument' => array(
'handler' => 'views_handler_argument_users_roles_rid',
'name table' => 'role',
'name field' => 'name',
Earl Miles
committed
'empty field name' => t('No role'),
'numeric' => TRUE,
),
);
// ----------------------------------------------------------------------
// role table
$data['role']['table']['join'] = array(
// Directly links to users table.
'users' => array(
'left_table' => 'users_roles',
'left_field' => 'rid',
'field' => 'rid',
),
Earl Miles
committed
// needed for many to one helper sometimes
'users_roles' => array(
'left_field' => 'rid',
'field' => 'rid',
),
'node' => array(
'left_table' => 'users_roles',
'left_field' => 'rid',
'field' => 'rid',
),
'node_revisions' => array(
'left_table' => 'users_roles',
'left_field' => 'rid',
'field' => 'rid',
),
Earl Miles
committed
);
}
/**
* Field handler to provide simple renderer that allows linking to a user.
*
* @ingroup views_field_handlers
*/
class views_handler_field_user extends views_handler_field {
/**
* Override init function to provide generic option to link to user.
*/
function init(&$view, &$data) {
parent::init($view, $data);
if (!empty($this->options['link_to_user'])) {
$this->additional_fields['uid'] = 'uid';
function options(&$options) {
parent::options($options);
$options['link_to_user'] = TRUE;
/**
* Provide link to node option
*/
Earl Miles
committed
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['link_to_user'] = array(
'#title' => t('Link this field to its user'),
'#type' => 'checkbox',
'#default_value' => $this->options['link_to_user'],
);
}
function render_link($data, $values) {
Earl Miles
committed
if (!empty($this->options['link_to_user']) && user_access('access user profiles') && $values->{$this->aliases['uid']}) {
return l($data, "user/" . $values->{$this->aliases['uid']}, array('html' => TRUE));
}
else {
return $data;
}
}
function render($values) {
return $this->render_link(check_plain($values->{$this->field_alias}), $values);
}
}
Earl Miles
committed
/**
* Field handler to provide simple renderer that allows using a themed user link
*
* @ingroup views_field_handlers
*/
class views_handler_field_user_name extends views_handler_field_user {
function render_link($data, $values) {
if (!empty($this->options['link_to_user'])) {
$account = new stdClass();
$account->name = $values->{$this->field_alias};
$account->uid = $values->{$this->aliases['uid']};
return theme('username', $account);
}
else {
return $data;
}
}
}
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
/**
* Field handler to present a link to the user.
*
* @ingroup views_field_handlers
*/
class views_handler_field_user_link extends views_handler_field {
function construct() {
parent::construct();
$this->additional_fields['uid'] = 'uid';
}
function options(&$options) {
parent::options($options);
$options['text'] = '';
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['text'] = array(
'#type' => 'textfield',
'#title' => t('Text to display'),
'#default_value' => $this->options['text'],
);
}
// An example of field level access control.
function access() {
return user_access('access user profiles');
}
function query() {
$this->ensure_my_table();
$this->add_additional_fields();
}
function render($values) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('view');
$uid = $values->{$this->aliases['uid']};
return l($text, "user/$uid");
}
}
/**
* Field handler to present a link to user edit.
*
* @ingroup views_field_handlers
*/
class views_handler_field_user_link_edit extends views_handler_field_user_link {
// An example of field level access control.
function access() {
return user_access('administer users');
}
function render($values) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('edit');
$uid = $values->{$this->aliases['uid']};
return l($text, "user/$uid/edit", array('query' => drupal_get_destination()));
}
}
/**
* Field handler to present a link to user delete.
*
* @ingroup views_field_handlers
*/
class views_handler_field_user_link_delete extends views_handler_field_user_link {
// An example of field level access control.
function access() {
return user_access('administer users');
}
function render($values) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('delete');
$uid = $values->{$this->aliases['uid']};
return l($text, "user/$uid/delete", array('query' => drupal_get_destination()));
}
}
/**
* Field handler to provide acess control for the email field
*
* @ingroup views_field_handlers
*/
Earl Miles
committed
class views_handler_field_user_mail extends views_handler_field_user {
function options(&$options) {
parent::options($options);
$options['link_to_user'] = 'mailto';
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['link_to_user'] = array(
'#title' => t('Link this field'),
'#type' => 'radios',
'#options' => array(
0 => t('No link'),
'user' => t('To the user'),
'mailto' => t("With a mailto:"),
),
'#default_value' => $this->options['link_to_user'],
);
}
Earl Miles
committed
function render($values) {
if ($this->options['link_to_user'] == 'mailto') {
return l($values->{$this->field_alias}, "mailto:" . $values->{$this->field_alias});
}
return $this->render_link(check_plain($values->{$this->field_alias}), $values);
}
}
/**
* Field handler to provide simple renderer that allows using a themed user link
*
* @ingroup views_field_handlers
*/
Earl Miles
committed
class views_handler_field_user_picture extends views_handler_field {
function construct() {
parent::construct();
$this->additional_fields['uid'] = 'uid';
$this->additional_fields['name'] = 'name';
Earl Miles
committed
}
function render($values) {
// Fake an account object.
$account = new stdClass();
$account->uid = $values->{$this->aliases['uid']};
$account->name = $values->{$this->aliases['name']};
$account->picture = $values->{$this->field_alias};
return theme('user_picture', $account);
}
}
/**
* Field handler to provide a list of roles.
*
* @ingroup views_field_handlers
*/
class views_handler_field_user_roles extends views_handler_field_prerender_list {
Earl Miles
committed
function construct() {
parent::construct();
$this->additional_fields['uid'] = array('table' => 'users', 'field' => 'uid');
}
function query() {
$this->add_additional_fields();
$this->field_alias = $this->aliases['uid'];
Earl Miles
committed
}
Earl Miles
committed
$uids = array();
$this->items = array();
Earl Miles
committed
Earl Miles
committed
$uids[] = $result->{$this->aliases['uid']};
Earl Miles
committed
if ($uids) {
$result = db_query("SELECT u.uid, u.rid, r.name FROM {role} r INNER JOIN {users_roles} u ON u.rid = r.rid WHERE u.uid IN (" . implode(', ', $uids) . ") ORDER BY r.name");
while ($role = db_fetch_object($result)) {
$this->items[$role->uid][$role->rid] = check_plain($role->name);
Earl Miles
committed
}
}
}
}
/**
* Argument handler to accept a user id.
*
* @ingroup views_argument_handlers
*/
class views_handler_argument_user_uid extends views_handler_argument {
/**
* Override the behavior of title(). Get the name of the user.
*/
function title() {
if (!$this->argument) {
$title = variable_get('anonymous', t('Anonymous'));
}
else {
$title = db_result(db_query("SELECT u.name FROM {users} u WHERE u.uid = %d", $this->argument));
return t('No user');
}
return check_plain($title);
}
}
Earl Miles
committed
/**
* Filter handler for usernames
*
* @ingroup views_filter_handlers
*/
class views_handler_filter_user_name extends views_handler_filter_in_operator {
function value_form(&$form, &$form_state) {
$values = array();
if ($this->value) {
$result = db_query("SELECT * FROM {users} u WHERE uid IN (" . implode(', ', $this->value) . ")");
while ($account = db_fetch_object($result)) {
if ($account->uid) {
$values[] = $account->name;
}
else {
$values[] = 'Anonymous'; // Intentionally NOT translated.
}
}
}
sort($values);
$form['value'] = array(
'#type' => 'textfield',
'#title' => t('Usernames'),
'#description' => t('Enter a comma separated list of user names.'),
Earl Miles
committed
'#default_value' => implode(', ', $values),
'#autocomplete_path' => 'admin/views/ajax/autocomplete/user',
Earl Miles
committed
);
}
function value_validate(&$form, &$form_state) {
$values = drupal_explode_tags($form_state['values']['options']['value']);
$uids = array();
$placeholders = array();
$args = array();
$results = array();
foreach ($values as $value) {
if (strtolower($value) == 'anonymous') {
$uids[] = 0;
}
else {
$missing[strtolower($value)] = TRUE;
$args[] = $value;
$placeholders[] = "'%s'";
}
}
Earl Miles
committed
if (!$args) {
Earl Miles
committed
if ($uids) {
$form_state['values']['options']['value'] = $uids;
}
Earl Miles
committed
return;
}
Earl Miles
committed
$result = db_query("SELECT * FROM {users} WHERE name IN (" . implode(', ', $placeholders) . ")", $args);
while ($account = db_fetch_object($result)) {
unset($missing[strtolower($account->name)]);
$uids[] = $account->uid;
}
if ($missing) {
form_error($form['value'], t('Unable to find user(s): @users', array('@users', implode(', ', $missing))));
}
$form_state['values']['options']['value'] = $uids;
}
function value_submit() {
// prevent array filter from removing our anonymous user.
}
// Override to do nothing.
function get_value_options() { }
Earl Miles
committed
function admin_summary() {
// set up $this->value_options for the parent summary
$this->value_options = array();
if ($this->value) {
$result = db_query("SELECT * FROM {users} u WHERE uid IN (" . implode(', ', $this->value) . ")");
while ($account = db_fetch_object($result)) {
if ($account->uid) {
$this->value_options[$account->uid] = $account->name;
}
else {
$this->value_options[$account->uid] = 'Anonymous'; // Intentionally NOT translated.
}
}
}
Earl Miles
committed
return parent::admin_summary();
}
}
/**
* Filter handler for the current user
*
* @ingroup views_filter_handlers
*/
class views_handler_filter_user_current extends views_handler_filter_boolean_operator {
function construct() {
parent::construct();
$this->value_value = t('Is the logged in user');
}
function query() {
$this->ensure_my_table();
$this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field " . (empty($this->value) ? '!=' : '=') . " ***CURRENT_USER***");
}
}
Earl Miles
committed
/**
* Filter handler for user roles
*
* @ingroup views_filter_handlers
*/
class views_handler_filter_user_roles extends views_handler_filter_many_to_one {
function get_value_options() {
Earl Miles
committed
$this->value_options = user_roles(TRUE);
unset($this->value_options[DRUPAL_AUTHENTICATED_RID]);
}
}
Earl Miles
committed
/**
* Allow replacement of current userid so we can cache these queries
*/
function user_views_query_substitutions($view) {
global $user;
return array('***CURRENT_USER***' => intval($user->uid));
}
/**
* Allow role ID(s) as argument
*
* @ingroup views_argument_handlers
*/
class views_handler_argument_users_roles_rid extends views_handler_argument_many_to_one {
function title_query() {
$titles = array();
$placeholders = implode(', ', array_fill(0, sizeof($this->value), '%d'));
$result = db_query("SELECT name FROM {role} WHERE rid IN ($placeholders)", $this->value);
while ($term = db_fetch_object($result)) {
$titles[] = check_plain($term->name);
}
return $titles;
}
}