location_handler_filter_location_country.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Filter on country.
  4. */
  5. class location_handler_filter_location_country extends views_handler_filter_in_operator {
  6. function option_definition() {
  7. $options = parent::option_definition();
  8. $options['operator'] = array('default' => 'in');
  9. return $options;
  10. }
  11. function admin_summary() {
  12. return '';
  13. // $options = $this->operator_options('short');
  14. // return (!empty($this->options['exposed']) ? t('exposed') : $options[$this->operator]);
  15. }
  16. function get_value_options() {
  17. $this->value_options = location_get_iso3166_list();
  18. }
  19. /**
  20. * Provide widgets for filtering by country.
  21. */
  22. function value_form(&$form, &$form_state) {
  23. $this->get_value_options();
  24. $options = $this->value_options;
  25. $default_value = (array) $this->value;
  26. if (!empty($form_state['exposed'])) {
  27. $identifier = $this->options['expose']['identifier'];
  28. if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator'])) {
  29. // exposed and locked.
  30. $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
  31. }
  32. else {
  33. $source = 'edit-' . drupal_clean_css_identifier($this->options['expose']['operator']);
  34. }
  35. if (!empty($this->options['expose']['reduce'])) {
  36. $options = $this->reduce_value_options();
  37. if (empty($this->options['expose']['single']) && !empty($this->options['expose']['optional'])) {
  38. $default_value = array();
  39. }
  40. }
  41. if (!empty($this->options['expose']['single'])) {
  42. if (!empty($this->options['expose']['optional']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
  43. $default_value = 'All';
  44. }
  45. else if (empty($default_value)) {
  46. $keys = array_keys($options);
  47. $default_value = array_shift($keys);
  48. }
  49. else {
  50. $copy = $default_value;
  51. $default_value = array_shift($copy);
  52. }
  53. }
  54. }
  55. $form['value'] = array(
  56. '#type' => 'select',
  57. '#title' => t('Country'),
  58. '#default_value' => $default_value,
  59. '#options' => $options,
  60. // Used by province autocompletion js.
  61. '#attributes' => array('class' => array('location_auto_country')),
  62. '#multiple' => TRUE, // views will change this as necessary when exposed.
  63. );
  64. // Let location_autocomplete.js find the correct fields to attach.
  65. $form['value']['#attributes']['class'][] = 'location_auto_join_' . $this->options['expose']['identifier'];
  66. }
  67. function reduce_value_options($input = NULL) {
  68. if (empty($this->options)) {
  69. $this->get_value_options();
  70. }
  71. if (!empty($this->options['expose']['reduce']) && !empty($this->options['value'])) {
  72. $reduced_options = array();
  73. foreach ($this->options['value'] as $value) {
  74. $reduced_options[$value] = $this->value_options[$value];
  75. }
  76. return $reduced_options;
  77. }
  78. return $this->get_value_options();
  79. }
  80. function accept_exposed_input($input) {
  81. if (empty($this->options['exposed'])) {
  82. return TRUE;
  83. }
  84. if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
  85. $this->operator = $input[$this->options['expose']['operator_id']];
  86. }
  87. if (!empty($this->options['expose']['identifier'])) {
  88. $value = $input[$this->options['expose']['identifier']];
  89. if (empty($this->options['expose']['required'])) {
  90. if ($value == 'All' || $value === array()) {
  91. if (empty($this->options['value']) || (!empty($this->options['value']) && empty($this->options['expose']['reduce']))) {
  92. return FALSE;
  93. }
  94. else {
  95. $value = $this->options['value'];
  96. }
  97. }
  98. if (!empty($this->always_multiple) && $value === '') {
  99. return FALSE;
  100. }
  101. }
  102. if (isset($value)) {
  103. $this->value = $value;
  104. if (empty($this->always_multiple) && empty($this->options['expose']['multiple'])) {
  105. $this->value = array($value);
  106. }
  107. }
  108. else {
  109. return FALSE;
  110. }
  111. }
  112. return TRUE;
  113. }
  114. function operator_options($which = 'title') {
  115. if (empty($this->options['expose']['multiple'])) {
  116. return array(
  117. 'in' => t('Is'),
  118. 'not in' => t('Is not'),
  119. );
  120. }
  121. else {
  122. return array(
  123. 'in' => t('Is one of'),
  124. 'not in' => t('Is not one of'),
  125. );
  126. }
  127. }
  128. function query() {
  129. if (empty($this->value)) {
  130. return;
  131. }
  132. $this->ensure_my_table();
  133. $field = "$this->table_alias.$this->real_field";
  134. // Normalize values.
  135. $value = $this->value;
  136. if (is_array($value)) {
  137. if (count($value) == 1) {
  138. // If multiple is allowed but only one was chosen, use a string instead.
  139. $value = reset($value);
  140. }
  141. }
  142. if (is_array($value)) {
  143. // Multiple values
  144. $operator = ($this->operator == 'in') ? 'IN' : 'NOT IN';
  145. $this->query->add_where($this->options['group'], $field, $value, $operator);
  146. }
  147. else {
  148. // Single value
  149. $operator = ($this->operator == 'in') ? '=' : '!=';
  150. $this->query->add_where($this->options['group'], $field, $value, $operator);
  151. }
  152. }
  153. }