tripal_views_handler_filter_select_id.inc

  1. 2.x tripal_views/views/handlers/tripal_views_handler_filter_select_id.inc
  2. 3.x tripal_chado_views/views/handlers/tripal_views_handler_filter_select_id.inc

Contains tripal_views_handler_filter_select_string

File

tripal_views/views/handlers/tripal_views_handler_filter_select_id.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains tripal_views_handler_filter_select_string
  5. */
  6. /**
  7. * This Handler provides a select list for the type field
  8. *
  9. * NOTE: This handler only works when applied to the type_id field in the base_table of
  10. * this view.
  11. *
  12. * @ingroup tripal_views
  13. */
  14. class tripal_views_handler_filter_select_id extends tripal_views_handler_filter_select_string {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. function init(&$view, &$options) {
  19. parent::init($view, $options);
  20. if (preg_match('/(\w+)_id/',$this->field,$matches)) {
  21. $this->parent_table = $matches[1];
  22. }
  23. }
  24. /**
  25. * Provide the options used in the select list.
  26. * Override this function in extended handlers to easily change option list.
  27. *
  28. * @return
  29. * An array of options where the key is the value of this field in the database
  30. */
  31. function get_select_options() {
  32. // @TODO: Make name field configurable.
  33. $name_field = 'common_name';
  34. // First check that this table has a name field.
  35. $table_desc = chado_get_schema($this->parent_table);
  36. if (!isset($table_desc['fields'][$name_field])) {
  37. return array();
  38. }
  39. // If the "Show All" options is set then show all the "names" from
  40. // the table referenced by the foreign key constraint.
  41. if (isset($this->options['show_all']) AND $this->options['show_all'] == TRUE) {
  42. // We still want to use any hidden fitlers on the parent table
  43. // but the arguments will need to be field names rather than
  44. // generic placeholders so we need to tell get_select_option_where() that.
  45. $return = $this->get_select_option_where($this->parent_table, FALSE);
  46. $args = $return['arguments'];
  47. // Simply grab all the values from the table referenced by
  48. // the foreign key constraint. Since we use the id as the key of
  49. // the options there is no need to use DISTRINCT in the query.
  50. $resource = chado_select_record($this->parent_table, array($this->field, $name_field), $args);
  51. $options = array();
  52. foreach ($resource as $r) {
  53. $options[$r->{$this->field}] = $r->{$name_field};
  54. }
  55. }
  56. // Otherwise, only show those that are actually used in the base table.
  57. else {
  58. $return = $this->get_select_option_where($this->parent_table);
  59. $where_clauses = $return['where_clauses'];
  60. $arguments = $return['arguments'];
  61. $where = '';
  62. if (!empty($where_clauses)) {
  63. $where = implode(' AND ', $where_clauses);
  64. }
  65. // Using a "Loose Index Scan" to get a list of all the unique values for
  66. // the name in the table referenced by the foreign key constraint.
  67. // See https://wiki.postgresql.org/wiki/Loose_indexscan
  68. $sql = "WITH RECURSIVE t AS (
  69. SELECT MIN(filter_table.!id_field) AS col
  70. FROM {!filter_table} filter_table
  71. LEFT JOIN {!foreign_table} foreign_table ON filter_table.!id_field=foreign_table.!id_field
  72. " . ($where == '' ? '' : "WHERE " . $where) . "
  73. UNION ALL
  74. SELECT (
  75. SELECT MIN(filter_table.!id_field)
  76. FROM {!filter_table} filter_table
  77. LEFT JOIN {!foreign_table} foreign_table ON filter_table.!id_field=foreign_table.!id_field
  78. WHERE filter_table.!id_field > col " . ($where == '' ? '' : " AND " . $where) . "
  79. )
  80. FROM t WHERE col IS NOT NULL
  81. )
  82. SELECT !id_field as id, !name_field as name
  83. FROM {!foreign_table}
  84. WHERE !id_field IN (SELECT col FROM t where col IS NOT NULL)
  85. ORDER BY !name_field ASC";
  86. $sql = format_string($sql, array(
  87. '!filter_table' => $this->table,
  88. '!foreign_table' => $this->parent_table,
  89. '!id_field' => $this->field,
  90. '!name_field' => $name_field
  91. ));
  92. $resource = chado_query($sql, $arguments);
  93. $options = array();
  94. if ($this->options['select_optional']) {
  95. $options['All'] = '- Any -';
  96. }
  97. foreach ($resource as $r) {
  98. $options[$r->id] = $r->name;
  99. }
  100. }
  101. return $options;
  102. }
  103. /**
  104. * For the SQL generating the options, determine the WHERE clauses
  105. *
  106. * @return
  107. * An array of full qualified where clauses (ie: table.myfield = 'fred')
  108. */
  109. function get_select_option_where($table = NULL, $generic_placeholder = TRUE) {
  110. return parent::get_select_option_where($table, $generic_placeholder);
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. function option_definition() {
  116. return parent::option_definition();
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. function expose_form(&$form, &$form_state) {
  122. parent::expose_form($form, $form_state);
  123. return $form;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. function expose_submit($form, &$form_state) {
  129. parent::expose_submit($form, $form_state);
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. function expose_options() {
  135. parent::expose_options();
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. function value_form(&$form, &$form_state) {
  141. parent::value_form($form, $form_state);
  142. }
  143. }