tripal_views_handler_filter_select_string.inc

  1. 2.x tripal_views/views/handlers/tripal_views_handler_filter_select_string.inc
  2. 3.x tripal/views_handlers/tripal_views_handler_filter_select_string.inc
  3. 3.x tripal_chado_views/views/handlers/tripal_views_handler_filter_select_string.inc
  4. 1.x tripal_views/views/handlers/tripal_views_handler_filter_select_string.inc

Contains tripal_views_handler_filter_select_string Filter Handler

File

tripal_views/views/handlers/tripal_views_handler_filter_select_string.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains tripal_views_handler_filter_select_string Filter Handler
  5. */
  6. /**
  7. * This Handler provides a generic select list for any chado field that is a string
  8. * The select list includes all distinct values for that field.
  9. *
  10. * @ingroup tripal_views
  11. */
  12. class tripal_views_handler_filter_select_string extends views_handler_filter_string {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. function init(&$view, &$options) {
  17. parent::init($view, $options);
  18. // Backwards compatibility
  19. if (isset($this->options['expose']['values_form_type'])) {
  20. $this->options['values_form_type'] = $this->options['expose']['values_form_type'];
  21. unset($this->options['expose']['values_form_type']);
  22. }
  23. if (isset($this->options['expose']['select_multiple'])) {
  24. $this->options['select_multiple'] = $this->options['expose']['select_multiple'];
  25. unset($this->options['expose']['select_multiple']);
  26. }
  27. if (isset($this->options['expose']['select_optional'])) {
  28. $this->options['select_optional'] = $this->options['expose']['select_optional'];
  29. unset($this->options['expose']['select_optional']);
  30. }
  31. if (isset($this->options['expose']['max_length'])) {
  32. $this->options['max_length'] = $this->options['expose']['max_length'];
  33. unset($this->options['expose']['max_length']);
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. function has_extra_options() {
  40. return TRUE;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. function option_definition() {
  46. $options = parent::option_definition();
  47. $options['values_form_type'] = array(
  48. 'default' => 'textfield',
  49. 'export' => TRUE
  50. );
  51. $options['select_multiple'] = array(
  52. 'default' => FALSE,
  53. 'bool' => TRUE,
  54. 'export' => TRUE
  55. );
  56. $options['select_optional'] = array(
  57. 'default' => FALSE,
  58. 'bool' => TRUE,
  59. 'export' => TRUE
  60. );
  61. $options['show_all'] = array(
  62. 'default' => FALSE,
  63. 'bool' => TRUE,
  64. 'export' => TRUE
  65. );
  66. $options['max_length'] = array(
  67. 'default' => 40,
  68. 'export' => TRUE
  69. );
  70. return $options;
  71. }
  72. /**
  73. * Provide the options used in the select list.
  74. * Override this function in extended handlers to easily change option list.
  75. *
  76. * @return
  77. * An array of options where the key is the value of this field in the database
  78. */
  79. function get_select_options() {
  80. $return = $this->get_select_option_where();
  81. $where_clauses = $return['where_clauses'];
  82. $arguments = $return['arguments'];
  83. $where = '';
  84. if (!empty($where_clauses)) {
  85. $where = ' WHERE ' . implode(' AND ', $where_clauses);
  86. }
  87. // get the values from the table
  88. $sql = 'SELECT ' . $this->real_field . ' FROM {' . $this->table . '} ' . $where . ' ORDER BY ' . $this->field . ' ASC';
  89. $results = chado_query($sql, $arguments);
  90. // Build the select box options
  91. $max_length = (isset($this->options['max_length'])) ? $this->options['max_length'] : 40;
  92. if (!$max_length) {
  93. $max_length = 40;
  94. }
  95. $options = array();
  96. if ($this->options['select_optional']) {
  97. $options['All'] = '--Any--';
  98. }
  99. foreach ($results as $r) {
  100. if (drupal_strlen($r->{$this->field}) > $max_length) {
  101. $options[$r->{$this->field}] = drupal_substr($r->{$this->field}, 0, $max_length) . '...';
  102. }
  103. else {
  104. $options[$r->{$this->field}] = $r->{$this->field};
  105. }
  106. }
  107. return $options;
  108. }
  109. /**
  110. * For the SQL generating the options, determine the WHERE clauses
  111. *
  112. * @return
  113. * An array of full qualified where clauses (ie: table.myfield = 'fred')
  114. */
  115. function get_select_option_where($table = NULL, $generic_placeholder = TRUE) {
  116. $where = array();
  117. $values = array();
  118. $table = (is_null($table)) ? $this->table : $table;
  119. // Build a where clause that will filter the list in the drop box
  120. // using fields that are not exposed and that are for the table
  121. // from whcih the values in the drop box will be slected and
  122. // we only want to use non-exposed fields because these are not
  123. // available to the user to edit--they're fixed.
  124. $where = array();
  125. $filters = (is_array($this->view->filter)) ? $this->view->filter : array();
  126. $placeholder_prefix = 'arg';
  127. $i = 0;
  128. foreach ($filters as $filter_name => $details) {
  129. // we only want to inclue non-exposed filters
  130. if ($details->options['exposed'] == FALSE) {
  131. $i++;
  132. $value = $details->value;
  133. if (is_array($details->value) AND isset($details->value['value'])) {
  134. $value = $details->value['value'];
  135. }
  136. // Generate the current placeholder.
  137. if ($generic_placeholder) {
  138. $placeholder = ':'.$placeholder_prefix.$i;
  139. }
  140. else {
  141. $placeholder = $details->real_field;
  142. }
  143. // we only want to filter on the table we're getting the list from
  144. if (strcmp($details->table, $table)==0 AND !empty($value)) {
  145. // If the value is an array then use IN instead of the choosen operator.
  146. if (is_array($value)) {
  147. $where[] = "$details->field IN ($placeholder)";
  148. $values[$placeholder] = $value;
  149. }
  150. // Otherwise, just use the operator choosen by the admin.
  151. else {
  152. $where[] = "$details->field $details->operator $placeholder";
  153. $values[$placeholder] = $value;
  154. }
  155. }
  156. }
  157. }
  158. return array(
  159. 'where_clauses' => $where,
  160. 'arguments' => $values
  161. );
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. function extra_options_form(&$form, &$form_state) {
  167. parent::extra_options_form($form, $form_state);
  168. $form['values_form_type'] = array(
  169. '#type' => 'radios',
  170. '#title' => t('Filter Type'),
  171. '#options' => array(
  172. 'textfield' => 'Text Field',
  173. 'select' => 'Drop-Down Box',
  174. ),
  175. '#default_value' => $this->options['values_form_type'],
  176. );
  177. $form['show_all'] = array(
  178. '#type' => 'checkbox',
  179. '#title' => t('Show All'),
  180. '#description' => t('When selected all terms from the controlled vocaulbary used by the table will be shown where the default is to only show those that are used.'),
  181. '#default_value' => $this->options['show_all'],
  182. );
  183. $form['select_multiple'] = array(
  184. '#type' => 'checkbox',
  185. '#title' => t('Select Multiple'),
  186. '#description' => t('Allows more then one option to be selected.'),
  187. '#default_value' => $this->options['select_multiple'],
  188. );
  189. $form['select_optional'] = array(
  190. '#type' => 'checkbox',
  191. '#title' => t('Optional'),
  192. '#description' => t('Adds --Any-- to the available options.'),
  193. '#default_value' => $this->options['select_optional'],
  194. );
  195. $form['max_length'] = array(
  196. '#type' => 'textfield',
  197. '#title' => t('Max Width'),
  198. '#description' => t('Specify the maximum width of the select box'),
  199. '#default_value' => $this->options['max_length'],
  200. );
  201. $form['note'] = array(
  202. '#type' => 'markup',
  203. '#value' => t('<strong><font color="red">Note:</font></strong> If another filter exists for the same table then '.
  204. 'the values shown in the drop box will only include those from rows that are not filtered.'),
  205. );
  206. return $form;
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. function extra_options_submit($form, &$form_state) {
  212. $this->options['values_form_type'] = $form_state['values']['options']['values_form_type'];
  213. $this->options['select_multiple'] = $form_state['values']['options']['select_multiple'];
  214. $this->options['select_optional'] = $form_state['values']['options']['select_optional'];
  215. $this->options['max_length'] = $form_state['values']['options']['max_length'];
  216. $this->options['show_all'] = $form_state['values']['options']['show_all'];
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. function extra_options_options() {
  222. $this->options['values_form_type'] = 'textfield';
  223. $this->options['select_multiple'] = FALSE;
  224. $this->options['select_optional'] = FALSE;
  225. $this->options['max_length'] = 40;
  226. $this->options['show_all'] = FALSE;
  227. }
  228. /**
  229. * {@inheritdoc}
  230. */
  231. function value_form(&$form, &$form_state) {
  232. parent::value_form($form, $form_state);
  233. $this->options['values_form_type'] = (isset($this->options['values_form_type'])) ? $this->options['values_form_type'] : 'textfield';
  234. if (preg_match('/select/', $this->options['values_form_type'])) {
  235. //Select List
  236. $form['value'] = array(
  237. '#type' => 'select',
  238. '#title' => t('%label', array('%label' => $this->options['expose']['label'])),
  239. '#options' => $this->get_select_options(),
  240. '#default_value' => $this->value,
  241. );
  242. if ($this->options['select_multiple']) {
  243. $form['value']['#multiple'] = TRUE;
  244. }
  245. }
  246. else {
  247. $form['value'] = array(
  248. '#type' => 'textfield',
  249. '#title' => t('%label', array('%label' => $this->options['expose']['label'])),
  250. '#default_value' => $this->value,
  251. );
  252. }
  253. }
  254. /**
  255. * {@inheritdoc}
  256. */
  257. function exposed_form(&$form, &$form_state) {
  258. parent::exposed_form($form, $form_state);
  259. if (isset($this->options['select_multiple'])) {
  260. if ($this->options['select_multiple']) {
  261. if (isset($this->options['expose']['identifier'])) {
  262. $id = $this->options['expose']['identifier'];
  263. }
  264. else {
  265. $id = $this->options['id'];
  266. }
  267. $form[$id]['#multiple'] = TRUE;
  268. }
  269. }
  270. }
  271. /**
  272. * {@inheritdoc}
  273. */
  274. function query() {
  275. // make optional
  276. // if it is not set or empty then don't restrict the query
  277. if (!$this->value) {
  278. return;
  279. }
  280. $this->ensure_my_table();
  281. $field = $this->table_alias . "." . $this->real_field;
  282. $table = $this->query->get_table_info($this->table);
  283. $this->options['values_form_type'] = (isset($this->options['values_form_type'])) ? $this->options['values_form_type'] : 'textfield';
  284. if (preg_match('/select/', $this->options['values_form_type'])) {
  285. if (is_array($this->value)) {
  286. if (isset($this->value['All'])) {
  287. unset($this->value['All']);
  288. }
  289. if ($this->operator == '!=') {
  290. $this->operator = 'NOT IN';
  291. }
  292. else {
  293. $this->operator = 'IN';
  294. }
  295. }
  296. else {
  297. // don't allow operators other than = and !=
  298. if ($this->operator != '!=') {
  299. $this->operator = '=';
  300. }
  301. }
  302. if ($this->value) {
  303. $this->query->add_where($this->options['group'], $field, $this->value, $this->operator);
  304. }
  305. }
  306. else {
  307. $info = $this->operators();
  308. if (!empty($info[$this->operator]['method'])) {
  309. $this->{$info[$this->operator]['method']}($field);
  310. }
  311. }
  312. }
  313. }