views_handler_filter_chado_select_cvterm_name.inc

Purpose: This Handler provides a select list for the type field

NOTE: This handler only works when applied to the type_id field in the base_table of this view.

File

tripal_views/views/handlers/deprecated/views_handler_filter_chado_select_cvterm_name.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Purpose: This Handler provides a select list for the type field
  5. *
  6. * NOTE: This handler only works when applied to the type_id field in the base_table of
  7. * this view.
  8. *
  9. * @ingroup views_filter_handlers
  10. * @ingroup tripal_core
  11. */
  12. class views_handler_filter_chado_select_cvterm_name extends views_handler_filter_string {
  13. /**
  14. * Executed when the field is added
  15. * Determine which cv to limit the cvterms to
  16. */
  17. function init(&$view, $options) {
  18. parent::init($view, $options);
  19. if ($this->options['show_all']) {
  20. $cv_id = variable_get('chado_' . $this->view->base_table . '_cv', NULL);
  21. if ($cv_id) {
  22. $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array('cv_id' => $cv_id));
  23. if (empty($results)) {
  24. $results = array();
  25. }
  26. foreach ($results as $c) {
  27. $cvterms[$c->cvterm_id] = $c->name;
  28. }
  29. }
  30. else {
  31. //get a list of cvs currently used
  32. if ($this->view->base_table == 'cvterm') {
  33. $sql = 'SELECT distinct(cv.cv_id) FROM {' . $this->view->base_table . '}'
  34. .' LEFT JOIN {cv} cv ON cv.cv_id=cvterm.cv_id';
  35. }
  36. else {
  37. $sql = 'SELECT distinct(cv.cv_id) FROM {' . $this->view->base_table . '}'
  38. .' LEFT JOIN {cvterm} cvterm ON cvterm.cvterm_id=' . $this->view->base_table . '.type_id '
  39. .'LEFT JOIN {cv} cv ON cv.cv_id=cvterm.cv_id';
  40. }
  41. $resource = chado_query($sql);
  42. $cvterms = array();
  43. while ( $r = db_fetch_object($resource) ) {
  44. $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array('cv_id' => $r->cv_id));
  45. if (empty($results)) {
  46. $results = array();
  47. }
  48. foreach ($results as $c) {
  49. $cvterms[$c->cvterm_id] = $c->name;
  50. }
  51. }
  52. }// end of if variable not defined
  53. }
  54. else {
  55. // @coder-ignore: non-drupal schema therefore table prefixing does not apply
  56. $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cvterm_id IN (SELECT distinct(type_id) FROM {%s})";
  57. $resource = chado_query($sql, $this->table);
  58. $cvterms = array();
  59. while ( $r = db_fetch_object($resource) ) {
  60. $cvterms[$r->cvterm_id] = $r->name;
  61. }
  62. }
  63. //sort cvterms by name (case insensitive)
  64. natcasesort($cvterms);
  65. //add to this handler
  66. $this->cvterm_options = $cvterms;
  67. }
  68. /**
  69. * Defines options for the option forms
  70. */
  71. function options_form(&$form, &$form_state) {
  72. parent::options_form($form, $form_state);
  73. $form['values_form_type'] = array(
  74. '#type' => 'radios',
  75. '#title' => t('Filter Type'),
  76. '#options' => array(
  77. 'textfield' => 'Text Field',
  78. 'select' => 'Drop-Down Box',
  79. ),
  80. '#default_value' => ($this->options['values_form_type']) ? $this->options['values_form_type'] : 'select',
  81. );
  82. $form['multiple'] = array(
  83. '#type' => 'checkbox',
  84. '#title' => t('Select Multiple'),
  85. '#description' => t('Allows more then one option to be selected.'),
  86. '#default_value' => (isset($this->options['multiple'])) ? $this->options['multiple'] : FALSE,
  87. );
  88. $form['optional'] = array(
  89. '#type' => 'checkbox',
  90. '#title' => t('Optional'),
  91. '#description' => t('Adds --Any-- to the available options.'),
  92. '#default_value' => (isset($this->options['optional'])) ? $this->options['optional'] : TRUE,
  93. );
  94. $form['show_all'] = array(
  95. '#type' => 'checkbox',
  96. '#title' => t('Show All Terms'),
  97. '#description' => 'Otherwise only cvterms used in the base table will be used'
  98. );
  99. }
  100. /**
  101. * Alters the query so that WHERE cvterm.cvterm_id=212 is used
  102. */
  103. function query() {
  104. $this->ensure_table;
  105. if ($this->options['multiple']) {
  106. // Remove any if it's there
  107. unset($this->value['All']);
  108. if (sizeof($this->value)) {
  109. $holders = array();
  110. foreach ($this->value as $v) {
  111. if (preg_match('/^[\d\.]+$/', $v)) {
  112. $holders[] = '%d';
  113. }
  114. else {
  115. $holders[] = "'%s'";
  116. }
  117. }
  118. $where = "cvterm.cvterm_id IN (" . implode(", ", $holders) . ")";
  119. }
  120. }
  121. elseif ($this->value != 'All') {
  122. if (preg_match('/^\d+$/', $this->value)) {
  123. $where = 'cvterm.cvterm_id=%d';
  124. }
  125. else {
  126. $where = "cvterm.name" . $this->operator . "'%s'";
  127. }
  128. }
  129. if ($where) {
  130. $this->query->add_where($this->options['group'], $where, $this->value);
  131. }
  132. }
  133. /**
  134. * Defines the value field in both the views filter options form
  135. * and the exposed form
  136. */
  137. function value_form(&$form, &$form_state) {
  138. parent::value_form($form, $form_state);
  139. if (preg_match('/select/', $this->options['values_form_type'])) {
  140. // Get Options
  141. if ($this->options['optional']) {
  142. $options['<select ' . $this->table . '>'] = '--None--';
  143. $options['All'] = '--Any--';
  144. }
  145. $max_length = 40;
  146. foreach ($this->cvterm_options as $cvterm_id => $cvterm_name) {
  147. if (drupal_strlen($cvterm_name) > $max_length) {
  148. $options[$cvterm_id] = drupal_substr($cvterm_name, 0, $max_length) . '...';
  149. }
  150. else {
  151. $options[$cvterm_id] = $cvterm_name;
  152. }
  153. }
  154. if (empty($options)) {
  155. $options[0] = '';
  156. }
  157. //Select List
  158. $form['value'] = array(
  159. '#type' => 'select',
  160. '#title' => t('%label', array('%label' => $this->options['label'])),
  161. '#options' => $options,
  162. '#default_value' => $this->value,
  163. );
  164. if ($this->options['multiple']) {
  165. $form['value']['#multiple'] = TRUE;
  166. }
  167. }
  168. else {
  169. $form['value'] = array(
  170. '#type' => 'textfield',
  171. '#title' => t('%label', array('%label' => $this->options['label'])),
  172. '#default_value' => $this->value,
  173. );
  174. }
  175. }
  176. /**
  177. * Ensures the select list gets rendered when the filter is exposed
  178. */
  179. function exposed_form(&$form, &$form_state) {
  180. if (empty($this->options['exposed'])) {
  181. return;
  182. }
  183. $value = $this->options['expose']['identifier'];
  184. $this->value_form($form, $form_state);
  185. $form[$value] = $form['value'];
  186. if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
  187. unset($form[$value]['#title']);
  188. }
  189. $this->exposed_translate($form[$value], 'value');
  190. if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
  191. unset($form[$value]['#default_value']);
  192. }
  193. if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
  194. $form[$value]['#default_value'] = 'All';
  195. }
  196. if ($value != 'value') {
  197. unset($form['value']);
  198. }
  199. }
  200. /**
  201. * This kind of construct makes it relatively easy for a child class
  202. * to add or remove functionality by overriding this function and
  203. * adding/removing items from this array.
  204. */
  205. function operators() {
  206. $operators = array(
  207. '=' => array(
  208. 'title' => t('Is equal to'),
  209. 'short' => t('='),
  210. 'method' => 'op_equal',
  211. 'values' => 1,
  212. ),
  213. '!=' => array(
  214. 'title' => t('Is not equal to'),
  215. 'short' => t('!='),
  216. 'method' => 'op_equal',
  217. 'values' => 1,
  218. ),
  219. '~' => array(
  220. 'title' => t('Contains'),
  221. 'short' => t('contains'),
  222. 'method' => 'op_contains',
  223. 'values' => 1,
  224. ),
  225. );
  226. return $operators;
  227. }
  228. }

Related topics