View source
<?php
class location_handler_filter_location_province extends views_handler_filter {
var $location_country = FALSE;
var $location_country_identifier = FALSE;
function option_definition() {
$options = parent::option_definition();
$options['operator'] = array(
'default' => 'is',
);
return $options;
}
function admin_summary() {
return '';
}
function value_form(&$form, &$form_state) {
$country = $this
->grovel_country();
drupal_add_js(drupal_get_path('module', 'location') . '/location_autocomplete.js');
$ac = $country;
if (is_array($ac)) {
$ac = implode(',', $ac);
}
$form['value'] = array(
'#type' => 'textfield',
'#title' => t('State/Province'),
'#autocomplete_path' => 'location/autocomplete/' . $ac,
'#default_value' => $this->value,
'#size' => 64,
'#maxlength' => 64,
'#attributes' => array(
'class' => 'location_auto_province',
),
'#multiple' => TRUE,
);
if ($this->location_country_identifier) {
$form['value']['#attributes']['class'] .= ' location_auto_join_' . $this->location_country_identifier;
}
}
function operator_options() {
if ($this->options['multiple']) {
return array(
'is' => t('Is one of'),
'is not' => t('Is not one of'),
);
}
else {
return array(
'is' => t('Is'),
'is not' => t('Is not'),
);
}
}
function grovel_country() {
$country = variable_get('location_default_country', 'us');
if (!empty($this->view->filter)) {
foreach ($this->view->filter as $k => $v) {
if ($v->table == 'location' && $v->field == 'country' && $v->options['relationship'] == $this->options['relationship']) {
$country = $v->value;
if (!empty($v->options['expose']['identifier'])) {
if (isset($this->view->exposed_input[$v->options['expose']['identifier']])) {
$country = $this->view->exposed_input[$v->options['expose']['identifier']];
}
$this->location_country_identifier = $v->options['expose']['identifier'];
}
}
}
}
if ($country == '' || $country == 'All' || $country == ' ' || $country == 'xx') {
$country = variable_get('location_default_country', 'us');
}
$this->location_country = $country;
return $country;
}
function query() {
$value = $this->value;
if (is_array($value)) {
if (count($value) == 1) {
$value = reset($value);
}
}
if (empty($value)) {
return;
}
$country = $this
->grovel_country();
$this
->ensure_my_table();
$field = "{$this->table_alias}.{$this->real_field}";
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = location_province_code($country, $v);
}
$placeholders = db_placeholders($value, 'varchar');
$operator = $this->operator == 'is' ? 'IN' : 'NOT IN';
$this->query
->add_where($this->options['group'], "{$field} {$operator}({$placeholders})", $value);
}
else {
$value = location_province_code($country, $value);
$operator = $this->operator == 'is' ? '=' : '!=';
$this->query
->add_where($this->options['group'], "{$field} {$operator} '%s'", $value);
}
}
}