diff --git a/contrib/search_api_views/includes/handler_filter.inc b/contrib/search_api_views/includes/handler_filter.inc
index cc965da..ef41189 100644
--- a/contrib/search_api_views/includes/handler_filter.inc
+++ b/contrib/search_api_views/includes/handler_filter.inc
@@ -37,6 +37,8 @@ class SearchApiViewsHandlerFilter extends views_handler_filter {
'<>' => t('Is not equal to'),
'>=' => t('Is greater than or equal to'),
'>' => t('Is greater than'),
+ 'empty' => t('Is empty'),
+ 'not empty' => t('Is not empty'),
);
}
@@ -53,17 +55,51 @@ class SearchApiViewsHandlerFilter extends views_handler_filter {
'#size' => 30,
'#default_value' => isset($this->value) ? $this->value : '',
);
+
+ // 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.
+ // #states as replacement of #dependency (http://drupal.org/node/1595022).
+ $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'),
+ );
+ }
+
+ /**
+ * Display the filter on the administrative summary
+ */
+ function admin_summary() {
+ if (!empty($this->options['exposed'])) {
+ return t('exposed');
+ }
+
+ if ($this->operator === 'empty') {
+ return t('is empty');
+ }
+ if ($this->operator === 'not empty') {
+ return t('is not empty');
+ }
+
+ return check_plain((string) $this->operator) . ' ' . check_plain((string) $this->value);
}
/**
* Add this filter to the query.
*/
public function query() {
- while (is_array($this->value)) {
- $this->value = $this->value ? reset($this->value) : NULL;
+ 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']);
}
- if ($this->value) {
- $this->query->condition($this->real_field, $this->value, $this->operator, $this->options['group']);
+ else {
+ while (is_array($this->value)) {
+ $this->value = $this->value ? reset($this->value) : NULL;
+ }
+ if (strlen($this->value) > 0) {
+ $this->query->condition($this->real_field, $this->value, $this->operator, $this->options['group']);
+ }
}
}
diff --git a/contrib/search_api_views/includes/handler_filter_date.inc b/contrib/search_api_views/includes/handler_filter_date.inc
index 9263bf9..5dee524 100644
--- a/contrib/search_api_views/includes/handler_filter_date.inc
+++ b/contrib/search_api_views/includes/handler_filter_date.inc
@@ -9,29 +9,32 @@ class SearchApiViewsHandlerFilterDate extends SearchApiViewsHandlerFilter {
* Provide a form for setting the filter value.
*/
public function value_form(&$form, &$form_state) {
- while (is_array($this->value)) {
- $this->value = $this->value ? array_shift($this->value) : NULL;
- }
- $form['value'] = array(
- '#type' => 'textfield',
- '#title' => empty($form_state['exposed']) ? t('Value') : '',
- '#description' => t('A date in any format understood by PHP. For example, "@date1" or "@date2".',
- array('@doc-link' => 'http://php.net/manual/en/function.strtotime.php', '@date1' => format_date(REQUEST_TIME, 'custom', 'Y-m-d H:i:s'), '@date2' => 'now + 1 day')),
- '#size' => 30,
- '#default_value' => isset($this->value) ? $this->value : '',
- );
+ parent::value_form($form, $form_state);
+ $form['value']['#description'] = t('A date in any format understood by PHP. For example, "@date1" or "@date2".', array(
+ '@doc-link' => 'http://php.net/manual/en/function.strtotime.php',
+ '@date1' => format_date(REQUEST_TIME, 'custom', 'Y-m-d H:i:s'),
+ '@date2' => 'now + 1 day',
+ ));
}
/**
* Add this filter to the query.
*/
public function query() {
- while (is_array($this->value)) {
- $this->value = $this->value ? reset($this->value) : NULL;
+ 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']);
}
- $v = is_numeric($this->value) ? $this->value : strtotime($this->value, REQUEST_TIME);
- if ($v !== FALSE) {
- $this->query->condition($this->real_field, $v, $this->operator, $this->options['group']);
+ else {
+ while (is_array($this->value)) {
+ $this->value = $this->value ? reset($this->value) : NULL;
+ }
+ $v = is_numeric($this->value) ? $this->value : strtotime($this->value, REQUEST_TIME);
+ if ($v !== FALSE) {
+ $this->query->condition($this->real_field, $v, $this->operator, $this->options['group']);
+ }
}
}
diff --git a/contrib/search_api_views/includes/handler_filter_options.inc b/contrib/search_api_views/includes/handler_filter_options.inc
index e8606f4..eda4344 100644
--- a/contrib/search_api_views/includes/handler_filter_options.inc
+++ b/contrib/search_api_views/includes/handler_filter_options.inc
@@ -18,6 +18,8 @@ class SearchApiViewsHandlerFilterOptions extends SearchApiViewsHandlerFilter {
return array(
'=' => t('Is one of'),
'<>' => t('Is not one of'),
+ 'empty' => t('Is empty'),
+ 'not empty' => t('Is not empty'),
);
}
@@ -99,6 +101,14 @@ class SearchApiViewsHandlerFilterOptions extends SearchApiViewsHandlerFilter {
'#size' => min(4, count($this->definition['options'])),
'#default_value' => isset($this->value) ? $this->value : array(),
);
+
+ // Hide the value box if operator is 'empty' or 'not empty'.
+ // Radios share the same selector so we have to add some dummy selector.
+ // #states replace #dependency (http://drupal.org/node/1595022).
+ $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'),
+ );
}
/**
@@ -109,6 +119,13 @@ class SearchApiViewsHandlerFilterOptions extends SearchApiViewsHandlerFilter {
return t('exposed');
}
+ if ($this->operator === 'empty') {
+ return t('is empty');
+ }
+ if ($this->operator === 'not empty') {
+ return t('is not empty');
+ }
+
if (!is_array($this->value)) {
return;
}
@@ -152,26 +169,34 @@ class SearchApiViewsHandlerFilterOptions extends SearchApiViewsHandlerFilter {
* Add this filter to the query.
*/
public function query() {
- while (is_array($this->value) && count($this->value) == 1) {
- $this->value = reset($this->value);
+ if ($this->operator === 'empty') {
+ $this->query->condition($this->real_field, NULL, '=', $this->options['group']);
}
- if (is_scalar($this->value) && $this->value !== '') {
- $this->query->condition($this->real_field, $this->value, $this->operator, $this->options['group']);
+ elseif ($this->operator === 'not empty') {
+ $this->query->condition($this->real_field, NULL, '<>', $this->options['group']);
}
- elseif ($this->value) {
- if ($this->operator == '=') {
- $filter = $this->query->createFilter('OR');
- // $filter will be NULL if there were errors in the query.
- if ($filter) {
- foreach ($this->value as $v) {
- $filter->condition($this->real_field, $v, '=');
+ else {
+ while (is_array($this->value) && count($this->value) == 1) {
+ $this->value = reset($this->value);
+ }
+ if (is_scalar($this->value) && $this->value !== '') {
+ $this->query->condition($this->real_field, $this->value, $this->operator, $this->options['group']);
+ }
+ elseif ($this->value) {
+ if ($this->operator == '=') {
+ $filter = $this->query->createFilter('OR');
+ // $filter will be NULL if there were errors in the query.
+ if ($filter) {
+ foreach ($this->value as $v) {
+ $filter->condition($this->real_field, $v, '=');
+ }
+ $this->query->filter($filter, $this->options['group']);
}
- $this->query->filter($filter, $this->options['group']);
}
- }
- else {
- foreach ($this->value as $v) {
- $this->query->condition($this->real_field, $v, $this->operator, $this->options['group']);
+ else {
+ foreach ($this->value as $v) {
+ $this->query->condition($this->real_field, $v, $this->operator, $this->options['group']);
+ }
}
}
}