user.views.inc 18.1 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//$Id$
/**
 * @file
 * Provide views data and handlers for user.module
 */

/**
 * @defgroup views_user_module user.module handlers
 *
 * @{
 */

/**
 * Implementation of hook_views_data()
 */
17
function user_views_data() {
18 19 20
  // ----------------------------------------------------------------
  // users table

21 22 23 24
  // 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');

25 26 27
  $data['users']['table']['base'] = array(
    'field' => 'uid',
    'title' => t('User'),
28
    'help' => t('Users who have created accounts on your site.'),
29 30
  );

31 32 33 34 35 36 37 38 39 40 41
  $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',
    ),
  );
42

43 44
  // name
  $data['users']['name'] = array(
45
    'title' => t('Name'), // The item it appears as on the UI,
46 47 48
    'help' => t('The user or author name.'), // The help that appears on the UI,
    'field' => array(
      'handler' => 'views_handler_field_user_name',
49
      'click sortable' => TRUE,
50
    ),
51 52 53
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
54 55 56 57 58 59
    'filter' => array(
      'handler' => 'views_handler_filter_user_name',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
60
  );
61 62

  // mail
63
  // Note that this field implements field level access control.
64 65 66 67 68 69 70
  $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,
    ),
71 72 73
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
74 75 76
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
77 78
  );

79 80 81 82 83 84 85
  // uid
  $data['users']['uid'] = array(
    'title' => t('Uid'),
    'help' => t('The user ID'), // The help that appears on the UI,
    // Information for displaying the uid
    'field' => array(
      'handler' => 'views_handler_field_user',
86
      'click sortable' => TRUE,
87 88 89
    ),
    // Information for accepting a uid as an argument
    'argument' => array(
90
      'handler' => 'views_handler_argument_user_uid',
91
      'name field' => 'name', // display this field in the summary
merlinofchaos's avatar
merlinofchaos committed
92
    ),
93 94
    // Information for accepting a uid as a filter
    'filter' => array(
95
      'handler' => 'views_handler_filter_numeric',
96 97 98 99 100
    ),
    // Information for sorting on a uid.
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
101
  );
102

103 104 105
  // picture
  $data['users']['picture'] = array(
    'title' => t('Picture'),
106
    'help' => t("The user's picture, if allowed."), // The help that appears on the UI,
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    // 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,
133
    'help' => t("The user's last access date."), // The help that appears on the UI,
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    '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,
149
    'help' => t("The user's last login date."), // The help that appears on the UI,
150 151 152 153 154 155 156 157 158 159 160
    'field' => array(
      'handler' => 'views_handler_field_date',
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort_date',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_date',
    ),
  );
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  // 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('Published'),
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  // log field
  $data['users']['signature'] = array(
    'title' => t('Signature'), // The item it appears as on the UI,
183
    'help' => t("The user's signature."), // The help that appears on the UI,
184 185 186 187 188 189 190 191 192
     // Information for displaying a title as a field
    'field' => array(
      'handler' => 'views_handler_field_xss',
     ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
  );

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  $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',
    ),
  );

209
  // ----------------------------------------------------------------------
210
  // users_roles table
211 212 213 214 215 216 217 218 219

  $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',
220
    ),
221 222 223 224
    'node' => array(
      'table' => 'users',
      'left_field' => 'uid',
      'field' => 'uid',
225
    ),
226 227 228 229 230 231 232 233
    'node_revisions' => array(
      'table' => 'users',
      'left_field' => 'uid',
      'field' => 'uid',
    ),
  );

  $data['users_roles']['rid'] = array(
234
    'title' => t('Roles'),
235
    'help' => t('Roles that a user belongs to.'),
236 237 238 239 240 241 242
    'field' => array(
      'handler' => 'views_handler_field_user_roles',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_user_roles',
      'numeric' => TRUE,
    ),
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
    'argument' => array(
      'handler' => 'views_handler_argument_users_roles_rid',
      'name table' => 'role',
      'name field' => 'name',
      'empty name field' => 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',
    ),
    'node' => array(
      'left_table' => 'users_roles',
      'left_field' => 'rid',
      'field' => 'rid',
    ),
    'node_revisions' => array(
      'left_table' => 'users_roles',
      'left_field' => 'rid',
      'field' => 'rid',
    ),
272 273 274
  );

  // @todo: tracker default view (in users.default_views.inc -- This really should go in comment)
275
  return $data;
276 277 278 279 280 281 282 283 284 285 286
}

/**
 * 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.
   */
287 288
  function init(&$view, &$data) {
    parent::init($view, $data);
289 290
    if (!empty($this->options['link_to_user'])) {
      $this->additional_fields['uid'] = 'uid';
291 292 293
    }
  }

294 295 296
  function options(&$options) {
    parent::options($options);
    $options['link_to_user'] = TRUE;
merlinofchaos's avatar
merlinofchaos committed
297 298
  }

299 300 301
  /**
   * Provide link to node option
   */
302
  function options_form(&$form, &$form_state) {
303
    parent::options_form($form, $form_state);
304 305 306
    $form['link_to_user'] = array(
      '#title' => t('Link this field to its user'),
      '#type' => 'checkbox',
307
      '#default_value' => $this->options['link_to_user'],
308 309 310 311
    );
  }

  function render_link($data, $values) {
312
    if (!empty($this->options['link_to_user']) && user_access('access user profiles') && $values->{$this->aliases['uid']}) {
313
      return l($data, "user/" . $values->{$this->aliases['uid']}, array('html' => TRUE));
314 315 316 317 318 319 320 321 322 323 324
    }
    else {
      return $data;
    }
  }

  function render($values) {
    return $this->render_link(check_plain($values->{$this->field_alias}), $values);
  }
}

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
/**
 * 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;
    }
  }
}

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
/**
 * 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()));
  }
}

422 423 424 425 426
/**
 * Field handler to provide acess control for the email field
 *
 * @ingroup views_field_handlers
 */
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
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'],
    );
  }
446 447 448 449 450

  // An example of field level access control.
  function access() {
    return user_access('administer users');
  }
451 452 453 454 455 456 457

  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);
  }
458 459
}

460 461 462 463 464
/**
 * Field handler to provide simple renderer that allows using a themed user link
 *
 * @ingroup views_field_handlers
 */
465 466 467
class views_handler_field_user_picture extends views_handler_field {
  function construct() {
    parent::construct();
468 469
    $this->additional_fields['uid'] = 'uid';
    $this->additional_fields['name'] = 'name';
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
  }

  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
 */
488
class views_handler_field_user_roles extends views_handler_field_prerender_list {
489 490 491 492 493 494 495
  function construct() {
    parent::construct();
    $this->additional_fields['uid'] = array('table' => 'users', 'field' => 'uid');
  }

  function query() {
    $this->add_additional_fields();
496
    $this->field_alias = $this->aliases['uid'];
497 498
  }

499
  function pre_render($values) {
500
    $uids = array();
501
    $this->items = array();
502

503
    foreach ($values as $result) {
504
      $uids[] = $result->{$this->aliases['uid']};
505
    }
506 507 508 509

    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)) {
510
        $this->items[$role->uid][$role->rid] = check_plain($role->name);
511
      }
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    }
  }
}

/**
 * Argument handler to accept a user id.
 *
 * @ingroup views_argument_handlers
 */
class views_handler_argument_user_uid extends views_handler_argument {
  // No constructor is necessary.

  /**
   * Override the behavior of title(). Get the name of the user.
   */
  function title() {
    if (!$this->argument) {
      $title = variable_get('anonymous', t('Anonymous'));
    }
    else {
532
      $title = db_result(db_query("SELECT u.name FROM {users} u WHERE u.uid = %d", $this->argument));
533 534
    }
    if (empty($title)) {
535
      return t('No user');
536 537 538 539 540 541
    }

    return check_plain($title);
  }
}

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
/**
 * 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 usernames'),
      '#default_value' => implode(', ', $values),
      '#autocomplete_path' => 'admin/build/views/autocomplete/user',
    );
  }

  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'";
      }
    }

    $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.
  }

  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.
        }
      }
    }
    return parent::admin_summary();
  }
}

/**
 * Filter handler for user roles
 *
 * @ingroup views_filter_handlers
 */
class views_handler_filter_user_roles extends views_handler_filter_many_to_one {
632
  function get_value_options() {
633 634 635 636 637
    $this->value_options = user_roles(TRUE);
    unset($this->value_options[DRUPAL_AUTHENTICATED_RID]);
  }
}

638 639 640 641 642 643 644
/**
 * 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));
}
645

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
/**
 * 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();
    $rids = implode(', ', $this->value); // only does numbers so safe

    $result = db_query("SELECT name FROM {role} WHERE rid IN (%s)", $rids);
    while ($term = db_fetch_object($result)) {
      $titles[] = check_plain($term->name);
    }
    return $titles;
  }
}

664 665 666
/**
 * @}
 */