tripal_views_handler_filter_select_cvterm.inc

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

Contains tripal_views_handler_filter_select_cvterm

File

tripal_chado_views/views/handlers/tripal_views_handler_filter_select_cvterm.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains tripal_views_handler_filter_select_cvterm
  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
  10. * base_table of this view.
  11. *
  12. * @ingroup tripal_views
  13. */
  14. class tripal_views_handler_filter_select_cvterm extends tripal_views_handler_filter_select_string {
  15. /**
  16. * Provide the options used in the select list.
  17. * Override this function in extended handlers to easily change option list.
  18. *
  19. * @return
  20. * An array of options where the key is the value of this field in the
  21. * database
  22. */
  23. function get_select_options() {
  24. // If the admin has set the "Show All" option then we want to show all the
  25. // cvterms regardless of whether they are used in the base table or not.
  26. if (isset($this->options['show_all']) AND $this->options['show_all'] == TRUE) {
  27. // Get a list of cvs currently used.
  28. // If the filter is for a field in the cvterm table (weird, I know but
  29. // we can't assume that tripal admin won't do this) then we only need
  30. // to make one-hop to the cv table.
  31. if ($this->table == 'cvterm') {
  32. $return = $this->get_select_option_where($this->table);
  33. $where_clauses = $return['where_clauses'];
  34. $arguments = $return['arguments'];
  35. $base_where = '';
  36. if (!empty($where_clauses)) {
  37. $base_where = implode(' AND ', $where_clauses);
  38. }
  39. // Using a "Loose Index Scan" to get a list of all the cvs used
  40. // in the cvterm table (ie: all the cv's with at least one term).
  41. // See https://wiki.postgresql.org/wiki/Loose_indexscan
  42. $sql = "
  43. WITH RECURSIVE t AS (
  44. SELECT MIN(cv_id) AS col FROM {!table}
  45. " . ($base_where == '' ? '' : "WHERE " . $base_where) . "
  46. UNION ALL
  47. SELECT (SELECT MIN(cv_id) FROM {!table} WHERE cv_id > col " . ($base_where == '' ? '' : " AND " . $base_where) . ")
  48. FROM t WHERE col IS NOT NULL
  49. )
  50. SELECT cv_id, name
  51. FROM {cv}
  52. WHERE cv_id IN (SELECT col FROM t where col IS NOT NULL)
  53. ORDER BY cv.name ASC";
  54. $sql = format_string($sql, array('!table' => $this->table));
  55. }
  56. // Otherwise, (most often the case) we need to make two-hops
  57. // to the cv table through the cvterm table.
  58. else {
  59. // There are actually two sets of conditions we care about and of course
  60. // they are placed in different places in the query :p.
  61. // 1. Restrictions on the cvterm table. This lets users specify: only
  62. // show these exact types.
  63. $return = $this->get_select_option_where('cvterm');
  64. $where_clauses = $return['where_clauses'];
  65. $cvterm_args = $return['arguments'];
  66. $cvterm_where = '';
  67. if (!empty($where_clauses)) {
  68. $cvterm_where = implode(' AND ', $where_clauses);
  69. }
  70. // 2. Restrictions on the filter table Since those affect which types
  71. // have been used.
  72. $return = $this->get_select_option_where($this->table);
  73. $where_clauses = $return['where_clauses'];
  74. $base_args = $return['arguments'];
  75. $base_where = '';
  76. if (!empty($where_clauses)) {
  77. $base_where = implode(' AND ', $where_clauses);
  78. }
  79. // We only supply one set or arguments those so merge the two.
  80. $arguments = array_merge($cvterm_args, $base_args);
  81. // Using a "Loose Index Scan" to get a list of all the cvs used
  82. // in the table the drop-down filter is from.
  83. // See https://wiki.postgresql.org/wiki/Loose_indexscan
  84. $sql = "
  85. WITH RECURSIVE t AS (
  86. SELECT MIN(cvterm.cv_id) AS col
  87. FROM {!table} filter_table
  88. LEFT JOIN {cvterm} ON filter_table.!field=cvterm.cvterm_id
  89. " . ($base_where == '' ? '' : "WHERE " . $base_where) . "
  90. UNION ALL
  91. SELECT (
  92. SELECT MIN(cv_id)
  93. FROM {!table} filter_table
  94. LEFT JOIN {cvterm} ON filter_table.!field=cvterm.cvterm_id
  95. WHERE cv_id > col " . ($base_where == '' ? '' : " AND " . $base_where) . "
  96. )
  97. FROM t WHERE col IS NOT NULL
  98. )
  99. SELECT cvterm_id, name
  100. FROM {cvterm}
  101. WHERE cv_id IN (SELECT col FROM t where col IS NOT NULL) " . ($cvterm_where == '' ? '' : " AND " . $cvterm_where) . "
  102. ORDER BY cvterm.name ASC";
  103. $sql = format_string($sql, array('!table' => $this->table, '!field' => $this->field));
  104. }
  105. $resource = chado_query($sql, $arguments);
  106. // Now actually gerenate the select list
  107. // based on the results from the above query.
  108. $cvterms = array();
  109. foreach ($resource as $r) {
  110. $cvterms[$r->cvterm_id] = $r->name;
  111. }
  112. }
  113. // Otherwise, show the user the much smaller list of all cvterms used in
  114. // the base table.
  115. else {
  116. // There are actually two sets of conditions we care about and of course
  117. // they are placed in different places in the query :p.
  118. // 1. Restrictions on the cvterm table. This lets users specify: only
  119. // show these exact types.
  120. $return = $this->get_select_option_where('cvterm');
  121. $where_clauses = $return['where_clauses'];
  122. $cvterm_args = $return['arguments'];
  123. $cvterm_where = '';
  124. if (!empty($where_clauses)) {
  125. $cvterm_where = implode(' AND ', $where_clauses);
  126. }
  127. // 2. Restrictions on the filter table Since those affect which types
  128. // have been used.
  129. $return = $this->get_select_option_where($this->table);
  130. $where_clauses = $return['where_clauses'];
  131. $base_args = $return['arguments'];
  132. $base_where = '';
  133. if (!empty($where_clauses)) {
  134. $base_where = implode(' AND ', $where_clauses);
  135. }
  136. // We only supply one set or arguments those so merge the two.
  137. $arguments = array_merge($cvterm_args, $base_args);
  138. // Using a "Loose Index Scan" to get a list of all the cvterms used
  139. // in the base table. See https://wiki.postgresql.org/wiki/Loose_indexscan
  140. $sql = "
  141. WITH RECURSIVE t AS (
  142. SELECT MIN(!field) AS col FROM {!table}
  143. " . ($base_where == '' ? '' : "WHERE " . $base_where) . "
  144. UNION ALL
  145. SELECT (
  146. SELECT MIN(!field)
  147. FROM {!table}
  148. WHERE !field > col " . ($base_where == '' ? '' : " AND " . $base_where) . "
  149. )
  150. FROM t WHERE col IS NOT NULL
  151. )
  152. SELECT cvterm_id, name
  153. FROM {cvterm}
  154. WHERE cvterm_id IN (SELECT col FROM t where col IS NOT NULL) " . ($cvterm_where == '' ? '' : " AND " . $cvterm_where) . "
  155. ORDER BY cvterm.name ASC";
  156. $sql = format_string($sql, array('!table' => $this->table, '!field' => $this->field));
  157. $resource = chado_query($sql, $arguments);
  158. $cvterms = array();
  159. // Add an "- Any - " option to allow a type to not be set by default.
  160. if ($this->options['select_optional']) {
  161. $cvterms['All'] = '- Any -';
  162. }
  163. // Now actually gerenate the select list
  164. // based on the results from the above query.
  165. foreach ($resource as $r) {
  166. $cvterms[$r->cvterm_id] = $r->name;
  167. }
  168. }
  169. return $cvterms;
  170. }
  171. /**
  172. * For the SQL generating the options, determine the WHERE clauses
  173. *
  174. * @return
  175. * An array of full qualified where clauses (ie: table.myfield = 'fred')
  176. */
  177. function get_select_option_where($table = NULL, $generic_placeholder = TRUE) {
  178. return parent::get_select_option_where($table, $generic_placeholder);
  179. }
  180. /*
  181. // build a where clause that will filter the list in the drop box
  182. // using fields that are not exposed and that are for the table
  183. // from whcih the values in the drop box will be slected and
  184. // we only want to use non-exposed fields because these are not
  185. // available to the user to edit--they're fixed.
  186. $filters = (is_array($this->view->filter)) ? $this->view->filter : array();
  187. foreach ($filters as $filter_name => $details) {
  188. // we only want to inclue non-exposed filters
  189. if ($details->options['exposed'] == FALSE) {
  190. $value = $details->value;
  191. if (is_array($details->value) AND isset($details->value['value'])) {
  192. $value = $details->value['value'];
  193. }
  194. $field = $details->field;
  195. if (($this->table == $this->view->base_table) AND ($details->field == 'type_id')) {
  196. $field = 'cvterm_id';
  197. }
  198. if (is_array($value)) {
  199. // we only want to filter on the table we're getting the list from
  200. if (strcmp($details->table, $this->table)==0 AND !empty($value)) {
  201. $where[] = "$field IN (" . implode(', ', $value) . ')';
  202. }
  203. }
  204. else {
  205. // we only want to filter on the table we're getting the list from
  206. if (strcmp($details->table, 'cvterm')==0 AND !empty($value)) {
  207. $where[] = "$field $details->operator " . $value;
  208. }
  209. }
  210. }
  211. }
  212. return $where;
  213. }
  214. */
  215. /**
  216. * {@inheritdoc}
  217. */
  218. function option_definition() {
  219. return parent::option_definition();
  220. }
  221. /**
  222. * {@inheritdoc}
  223. */
  224. function expose_form(&$form, &$form_state) {
  225. parent::expose_form($form, $form_state);
  226. return $form;
  227. }
  228. /**
  229. * {@inheritdoc}
  230. */
  231. function expose_submit($form, &$form_state) {
  232. parent::expose_submit($form, $form_state);
  233. }
  234. /**
  235. * {@inheritdoc}
  236. */
  237. function expose_options() {
  238. parent::expose_options();
  239. }
  240. /**
  241. * {@inheritdoc}
  242. */
  243. function value_form(&$form, &$form_state) {
  244. parent::value_form($form, $form_state);
  245. }
  246. }