00001 <?php
00002
00010 class views_plugin_argument_validate_user extends views_plugin_argument_validate {
00011 function option_definition() {
00012 $options = parent::option_definition();
00013 $options['type'] = array('default' => 'uid');
00014 $options['restrict_roles'] = array('default' => FALSE);
00015 $options['roles'] = array('default' => array());
00016
00017 return $options;
00018 }
00019
00020 function options_form(&$form, &$form_state) {
00021 $form['type'] = array(
00022 '#type' => 'radios',
00023 '#title' => t('Type of user filter value to allow'),
00024 '#options' => array(
00025 'uid' => t('Only allow numeric UIDs'),
00026 'name' => t('Only allow string usernames'),
00027 'either' => t('Allow both numeric UIDs and string usernames'),
00028 ),
00029 '#default_value' => $this->options['type'],
00030 );
00031
00032 $form['restrict_roles'] = array(
00033 '#type' => 'checkbox',
00034 '#title' => t('Restrict user based on role'),
00035 '#default_value' => $this->options['restrict_roles'],
00036 );
00037
00038 $form['roles'] = array(
00039 '#type' => 'checkboxes',
00040 '#prefix' => '<div id="edit-options-validate-options-user-roles-wrapper">',
00041 '#suffix' => '</div>',
00042 '#title' => t('Restrict to the selected roles'),
00043 '#options' => array_map('check_plain', user_roles(TRUE)),
00044 '#default_value' => $this->options['roles'],
00045 '#description' => t('If no roles are selected, users from any role will be allowed.'),
00046 '#dependency' => array(
00047 'edit-options-validate-options-user-restrict-roles' => array(1),
00048 ),
00049 );
00050 }
00051
00052 function options_submit(&$form, &$form_state, &$options = array()) {
00053
00054 $options['roles'] = array_filter($options['roles']);
00055 }
00056
00057 function convert_options(&$options) {
00058 if (!isset($options['type']) && isset($this->argument->options['validate_user_argument_type'])) {
00059 $options['type'] = $this->argument->options['validate_user_argument_type'];
00060 $options['restrict_roles'] = $this->argument->options['validate_user_restrict_roles'];
00061 $options['roles'] = $this->argument->options['validate_user_roles'];
00062 }
00063 }
00064
00065 function validate_argument($argument) {
00066 $type = $this->options['type'];
00067
00068
00069 if (is_numeric($argument) && $argument == (int)$argument) {
00070 if ($type == 'uid' || $type == 'either') {
00071 if ($argument == $GLOBALS['user']->uid) {
00072
00073
00074
00075
00076 $account = clone $GLOBALS['user'];
00077 }
00078 $where = 'uid = :argument';
00079 }
00080 }
00081 else {
00082 if ($type == 'name' || $type == 'either') {
00083 $name = !empty($GLOBALS['user']->name) ? $GLOBALS['user']->name : variable_get('anonymous', t('Anonymous'));
00084 if ($argument == $name) {
00085 $account = clone $GLOBALS['user'];
00086 }
00087 $where = "name = :argument";
00088 }
00089 }
00090
00091
00092 if (empty($where)) {
00093 return FALSE;
00094 }
00095
00096 if (!isset($account)) {
00097 $query = "SELECT uid, name FROM {users} WHERE $where";
00098 $account = db_query($query, array(':argument' => $argument))->fetchObject();
00099 }
00100 if (empty($account)) {
00101
00102 return FALSE;
00103 }
00104
00105
00106 if (!empty($this->options['restrict_roles']) && !empty($this->options['roles'])) {
00107 $roles = $this->options['roles'];
00108 $account->roles = array();
00109 $account->roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
00110 $result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
00111 foreach ($result as $role) {
00112 $account->roles[] = $role->rid;
00113 }
00114 if (!(bool) array_intersect($account->roles, $roles)) {
00115 return FALSE;
00116 }
00117 }
00118
00119 $this->argument->argument = $account->uid;
00120 $this->argument->validated_title = isset($account->name) ? check_plain($account->name) : check_plain(variable_get('anonymous', t('Anonymous')));
00121 return TRUE;
00122 }
00123
00124 function process_summary_arguments(&$args) {
00125
00126
00127 $uids_arg_keys = array_flip($args);
00128 if ($this->options['type'] == 'name') {
00129 $users = user_load_multiple($args);
00130 foreach ($users as $uid => $account) {
00131 $args[$uids_arg_keys[$uid]] = $account->name;
00132 }
00133 }
00134 }
00135 }