location_handler_filter_location_province.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Filter on province.
  4. */
  5. class location_handler_filter_location_province extends views_handler_filter {
  6. var $location_country = FALSE;
  7. var $location_country_identifier = FALSE;
  8. function option_definition() {
  9. $options = parent::option_definition();
  10. $options['operator'] = array('default' => 'is');
  11. return $options;
  12. }
  13. function admin_summary() {
  14. return '';
  15. // $options = $this->operator_options('short');
  16. // return (!empty($this->options['exposed']) ? t('exposed') : $options[$this->operator]);
  17. }
  18. /**
  19. * Provide a simple textfield for equality
  20. */
  21. function value_form(&$form, &$form_state) {
  22. $country = $this->grovel_country();
  23. drupal_add_js(drupal_get_path('module', 'location') .'/location_autocomplete.js');
  24. $ac = $country;
  25. if (is_array($ac)) {
  26. $ac = implode(',', $ac);
  27. }
  28. $form['value'] = array(
  29. '#type' => 'textfield',
  30. '#title' => t('State/Province'),
  31. '#autocomplete_path' => 'location/autocomplete/'. $ac,
  32. '#default_value' => $this->value,
  33. '#size' => 64,
  34. '#maxlength' => 64,
  35. // Used by province autocompletion js.
  36. '#attributes' => array('class' => array('location_auto_province')),
  37. '#multiple' => TRUE, //$this->options['multiple'],
  38. );
  39. // Let location_autocomplete.js find the correct fields to attach.
  40. if ($this->location_country_identifier) {
  41. $form['value']['#attributes']['class'][] = 'location_auto_join_' . $this->location_country_identifier;
  42. }
  43. }
  44. function operator_options() {
  45. if ($this->options['expose']['single']) {
  46. return array(
  47. 'is' => t('Is'),
  48. 'is not' => t('Is not'),
  49. );
  50. }
  51. else {
  52. return array(
  53. 'is' => t('Is one of'),
  54. 'is not' => t('Is not one of'),
  55. );
  56. }
  57. }
  58. function grovel_country() {
  59. $country = variable_get('location_default_country', 'us');
  60. if (!empty($this->view->filter))
  61. foreach ($this->view->filter as $k => $v) {
  62. if ($v->table == 'location' && $v->field == 'country' && $v->options['relationship'] == $this->options['relationship']) {
  63. $country = $v->value;
  64. if (!empty($v->options['expose']['identifier'])) {
  65. if (isset($this->view->exposed_input[$v->options['expose']['identifier']])) {
  66. $country = $this->view->exposed_input[$v->options['expose']['identifier']];
  67. }
  68. $this->location_country_identifier = $v->options['expose']['identifier'];
  69. }
  70. }
  71. }
  72. if ($country == '' || $country == 'All' || $country == ' ' || $country == 'xx') {
  73. // It's set to something nonsensical, reset to the default to prevent malfunctions.
  74. $country = variable_get('location_default_country', 'us');
  75. }
  76. $this->location_country = $country;
  77. return $country;
  78. }
  79. function query() {
  80. // Normalize values.
  81. $value = $this->value;
  82. if (is_array($value)) {
  83. // At one point during development, provinces was a select box.
  84. // Right now it's an autocomplete textfield.
  85. // @@@ Investigate correct fix sometime.
  86. //$value = array_keys($value);
  87. if (count($value) == 1) {
  88. // If multiple is allowed but only one was chosen, use a string instead.
  89. $value = reset($value);
  90. }
  91. }
  92. if (empty($value)) {
  93. return;
  94. }
  95. $country = $this->grovel_country();
  96. $this->ensure_my_table();
  97. $field = "$this->table_alias.$this->real_field";
  98. if (is_array($value)) {
  99. // Multiple values
  100. foreach ($value as $k => $v) {
  101. // Convert to province codes.
  102. $value[$k] = location_province_code($country, $v);
  103. }
  104. $operator = ($this->operator == 'is') ? 'IN' : 'NOT IN';
  105. $this->query->add_where($this->options['group'], $field, $value, $operator);
  106. }
  107. else {
  108. // Single value
  109. // Convert to province code.
  110. $value = location_province_code($country, $value);
  111. $operator = ($this->operator == 'is') ? '=' : '!=';
  112. $this->query->add_where($this->options['group'], $field, $value, $operator);
  113. }
  114. }
  115. }