• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

modules/user/views_handler_filter_user_name.inc

00001 <?php
00002 
00008 class views_handler_filter_user_name extends views_handler_filter_in_operator {
00009   var $always_multiple = TRUE;
00010 
00011   function value_form(&$form, &$form_state) {
00012     $values = array();
00013     if ($this->value) {
00014       $result = db_query("SELECT * FROM {users} u WHERE uid IN (:uids)", array(':uids' => $this->value));
00015       foreach ($result as $account) {
00016         if ($account->uid) {
00017           $values[] = $account->name;
00018         }
00019         else {
00020           $values[] = 'Anonymous'; // Intentionally NOT translated.
00021         }
00022       }
00023     }
00024 
00025     sort($values);
00026     $default_value = implode(', ', $values);
00027     $form['value'] = array(
00028       '#type' => 'textfield',
00029       '#title' => t('Usernames'),
00030       '#description' => t('Enter a comma separated list of user names.'),
00031       '#default_value' => $default_value,
00032       '#autocomplete_path' => 'admin/views/ajax/autocomplete/user',
00033     );
00034 
00035     if (!empty($form_state['exposed']) && !isset($form_state['input'][$this->options['expose']['identifier']])) {
00036       $form_state['input'][$this->options['expose']['identifier']] = $default_value;
00037     }
00038   }
00039 
00040   function value_validate($form, &$form_state) {
00041     $values = drupal_explode_tags($form_state['values']['options']['value']);
00042     $uids = $this->validate_user_strings($form['value'], $values);
00043 
00044     if ($uids) {
00045       $form_state['values']['options']['value'] = $uids;
00046     }
00047   }
00048 
00049   function accept_exposed_input($input) {
00050     $rc = parent::accept_exposed_input($input);
00051 
00052     if ($rc) {
00053       // If we have previously validated input, override.
00054       if (isset($this->validated_exposed_input)) {
00055         $this->value = $this->validated_exposed_input;
00056       }
00057     }
00058 
00059     return $rc;
00060   }
00061 
00062   function exposed_validate(&$form, &$form_state) {
00063     if (empty($this->options['exposed'])) {
00064       return;
00065     }
00066 
00067     if (empty($this->options['expose']['identifier'])) {
00068       return;
00069     }
00070 
00071     $identifier = $this->options['expose']['identifier'];
00072     $values = drupal_explode_tags($form_state['values'][$identifier]);
00073 
00074     $uids = $this->validate_user_strings($form[$identifier], $values);
00075 
00076     if ($uids) {
00077       $this->validated_exposed_input = $uids;
00078     }
00079   }
00080 
00086   function validate_user_strings(&$form, $values) {
00087     $uids = array();
00088     $placeholders = array();
00089     $args = array();
00090     $results = array();
00091     foreach ($values as $value) {
00092       if (strtolower($value) == 'anonymous') {
00093         $uids[] = 0;
00094       }
00095       else {
00096         $missing[strtolower($value)] = TRUE;
00097         $args[] = $value;
00098         $placeholders[] = "'%s'";
00099       }
00100     }
00101 
00102     if (!$args) {
00103       return $uids;
00104     }
00105 
00106     $result = db_query("SELECT * FROM {users} WHERE name IN (:names)", array(':names' => $args));
00107     foreach ($result as $account) {
00108       unset($missing[strtolower($account->name)]);
00109       $uids[] = $account->uid;
00110     }
00111 
00112     if ($missing) {
00113       form_error($form, format_plural(count($missing), 'Unable to find user: @users', 'Unable to find users: @users', array('@users' => implode(', ', array_keys($missing)))));
00114     }
00115 
00116     return $uids;
00117   }
00118 
00119   function value_submit($form, &$form_state) {
00120     // prevent array filter from removing our anonymous user.
00121   }
00122 
00123   // Override to do nothing.
00124   function get_value_options() { }
00125 
00126   function admin_summary() {
00127     // set up $this->value_options for the parent summary
00128     $this->value_options = array();
00129 
00130     if ($this->value) {
00131       $result = db_query("SELECT * FROM {users} u WHERE uid IN (:uids)", array(':uids' => $this->value));
00132 
00133       foreach ($result as $account) {
00134         if ($account->uid) {
00135           $this->value_options[$account->uid] = $account->name;
00136         }
00137         else {
00138           $this->value_options[$account->uid] = 'Anonymous'; // Intentionally NOT translated.
00139         }
00140       }
00141     }
00142 
00143     return parent::admin_summary();
00144   }
00145 }

Generated on Sun Feb 26 2012 12:52:51 for Views by  doxygen 1.7.1