diff --git a/contrib/search_api_views/includes/handler_filter_user.inc b/contrib/search_api_views/includes/handler_filter_user.inc new file mode 100644 index 0000000..ec90987 --- /dev/null +++ b/contrib/search_api_views/includes/handler_filter_user.inc @@ -0,0 +1,146 @@ + t('Is one of'), + 'not in' => t('Is not one of'), + 'empty' => t('Is empty'), + 'not empty' => t('Is not empty'), + ); + } + + /** + * Provide a form for setting the filter value. + */ + public function value_form(&$form, &$form_state) { + $form['value'] = array( + '#type' => 'textfield', + '#title' => empty($form_state['exposed']) ? t('Value') : '', + '#size' => 30, + '#default_value' => isset($this->value) ? $this->value : '', + '#autocomplete_path' => 'admin/views/ajax/autocomplete/user', + ); + + // Hide the value box if the operator is 'empty' or 'not empty'. + // Radios share the same selector so we have to add some dummy selector. + $form['value']['#states']['visible'] = array( + ':input[name="options[operator]"],dummy-empty' => array('!value' => 'empty'), + ':input[name="options[operator]"],dummy-not-empty' => array('!value' => 'not empty'), + ); + } + + function value_validate($form, &$form_state) { + $values = drupal_explode_tags($form_state['values']['options']['value']); + $uids = $this->validate_user_strings($form['value'], $values); + + if ($uids) { + $form_state['values']['options']['value'] = $uids; + } + } + + function accept_exposed_input($input) { + $rc = parent::accept_exposed_input($input); + + if ($rc) { + // If we have previously validated input, override. + if (isset($this->validated_exposed_input)) { + $this->value = $this->validated_exposed_input; + } + } + + return $rc; + } + + function exposed_validate(&$form, &$form_state) { + if (empty($this->options['exposed'])) { + return; + } + + if (empty($this->options['expose']['identifier'])) { + return; + } + + $identifier = $this->options['expose']['identifier']; + $input = $form_state['values'][$identifier]; + + if ($this->options['is_grouped'] && isset($this->options['group_info']['group_items'][$input])) { + $this->operator = $this->options['group_info']['group_items'][$input]['operator']; + $input = $this->options['group_info']['group_items'][$input]['value']; + } + + $values = drupal_explode_tags($input); + + if (!$this->options['is_grouped'] || ($this->options['is_grouped'] && ($input != 'All'))) { + $uids = $this->validate_user_strings($form[$identifier], $values); + } + else { + $uids = FALSE; + } + + if ($uids) { + $this->validated_exposed_input = $uids; + } + } + + /** + * Validate the user string. Since this can come from either the form + * or the exposed filter, this is abstracted out a bit so it can + * handle the multiple input sources. + */ + function validate_user_strings(&$form, $values) { + $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'"; + } + } + + if (!$args) { + return $uids; + } + + $result = db_query("SELECT * FROM {users} WHERE name IN (:names)", array(':names' => $args)); + foreach ($result as $account) { + unset($missing[strtolower($account->name)]); + $uids[] = $account->uid; + } + + if ($missing) { + form_error($form, format_plural(count($missing), 'Unable to find user: @users', 'Unable to find users: @users', array('@users' => implode(', ', array_keys($missing))))); + } + + return $uids; + } + + /** + * Add this filter to the query. + */ + public function query() { + if ($this->operator === 'empty') { + $this->query->condition($this->real_field, NULL, '=', $this->options['group']); + } + elseif ($this->operator === 'not empty') { + $this->query->condition($this->real_field, NULL, '<>', $this->options['group']); + } + elseif (is_array($this->value)) { + $this->query->condition($this->real_field, $this->value, $this->operator, $this->options['group']); + } + } +} diff --git a/contrib/search_api_views/search_api_views.info b/contrib/search_api_views/search_api_views.info index 735ccfa..d7af5eb 100644 --- a/contrib/search_api_views/search_api_views.info +++ b/contrib/search_api_views/search_api_views.info @@ -20,6 +20,7 @@ files[] = includes/handler_filter_fulltext.inc files[] = includes/handler_filter_language.inc files[] = includes/handler_filter_options.inc files[] = includes/handler_filter_text.inc +files[] = includes/handler_filter_user.inc files[] = includes/handler_sort.inc files[] = includes/plugin_cache.inc files[] = includes/query.inc diff --git a/contrib/search_api_views/search_api_views.views.inc b/contrib/search_api_views/search_api_views.views.inc index 1ee9927..651df9e 100644 --- a/contrib/search_api_views/search_api_views.views.inc +++ b/contrib/search_api_views/search_api_views.views.inc @@ -181,6 +181,9 @@ function _search_api_views_add_handlers($id, array $field, EntityMetadataWrapper elseif ($inner_type == 'date') { $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterDate'; } + elseif ($table[$id]['field']['type'] === 'user') { + $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterUser'; + } else { $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilter'; }