ChadoField.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. class ChadoField extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Chado Field';
  5. // The default description for this field.
  6. public static $default_description = 'The generic base class for all Chado fields. Replace this text as appropriate for the child implementation.';
  7. // A list of global settings. These can be accessed within the
  8. // globalSettingsForm. When the globalSettingsForm is submitted then
  9. // Drupal will automatically change these settings for all fields.
  10. // Once instances exist for a field type then these settings cannot be
  11. // changed.
  12. public static $default_settings = array(
  13. 'storage' => 'field_chado_storage',
  14. );
  15. // Provide a list of instance specific settings. These can be access within
  16. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  17. // then Drupal with automatically change these settings for the instnace.
  18. // It is recommended to put settings at the instance level whenever possible.
  19. // If you override this variable in a child class be sure to replicate the
  20. // term_name, term_vocab, term_accession and term_fixed keys as these are
  21. // required for all TripalFields.
  22. public static $default_instance_settings = array(
  23. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  24. 'term_vocabulary' => 'schema',
  25. // The name of the term.
  26. 'term_name' => 'Thing',
  27. // The unique ID (i.e. accession) of the term.
  28. 'term_accession' => 'Thing',
  29. // Set to TRUE if the site admin is allowed to change the term
  30. // type. This will create form elements when editing the field instance
  31. // to allow the site admin to change the term settings above.
  32. 'term_fixed' => FALSE,
  33. // The table in Chado that the instance maps to.
  34. 'chado_table' => '',
  35. // The column of the table in Chado where the value of the field comes from.
  36. 'chado_column' => '',
  37. // The base table.
  38. 'base_table' => '',
  39. );
  40. // Indicates the download formats for this field. The list must be the
  41. // name of a child class of the TripalFieldDownloader.
  42. public static $download_formatters = array(
  43. 'TripalTabDownloader',
  44. 'TripalCSVDownloader',
  45. );
  46. // The module that manages this field.
  47. public static $module = 'tripal_chado';
  48. /**
  49. * @see TripalField::query()
  50. *
  51. * In addition to the rules to follow for the TripalField::query function
  52. * these should also be followed for the ChadoField::query implementation.
  53. *
  54. * - When giving alias to joined tables be sure to use aliases that are
  55. * unique to avoid conflicts with other fields.
  56. * - When joining with the base table its alias is 'base'.
  57. * - You may join to materialized views if need be to help speed queries.
  58. */
  59. public function query($query, $condition) {
  60. // If we are here it is because the child class did not implement the
  61. // query function. So, we will do our best to make the query work.
  62. $chado_table = $this->instance['settings']['chado_table'];
  63. $base_table = $this->instance['settings']['base_table'];
  64. $bschema = chado_get_schema($base_table);
  65. $bpkey = $bschema['primary key'][0];
  66. $alias = 'dbx_linker';
  67. $operator = $condition['operator'];
  68. // If the chado_table and the base_table are the same then this is easy.
  69. if ($chado_table == $base_table) {
  70. // Get the base table column that is associated with the term
  71. // passed as $condition['column'].
  72. $base_field = chado_get_semweb_column($chado_table, $condition['column']);
  73. $query->condition('base.' . $base_field , $condition['value'], $operator);
  74. }
  75. else {
  76. // If the two are not the same then we expect that the child class
  77. // will implement a query() function.
  78. }
  79. }
  80. /**
  81. * @see TripalField::queryOrder()
  82. */
  83. public function queryOrder($query, $order) {
  84. // If we are here it is because the child class did not implement the
  85. // queryOrder function. So, we will do our best to make the query work.
  86. $chado_table = $this->instance['settings']['chado_table'];
  87. $base_table = $this->instance['settings']['base_table'];
  88. $bschema = chado_get_schema($base_table);
  89. $bpkey = $bschema['primary key'][0];
  90. $alias = 'dbx_linker';
  91. $operator = $condition['operator'];
  92. // If the chado_table and the base_table are the same then this is easy.
  93. if ($chado_table == $base_table) {
  94. // Get the base table column that is associated with the term
  95. // passed as $condition['column'].
  96. $base_field = chado_get_semweb_column($chado_table, $order['column']);
  97. $query->orderBy('base.' . $base_field, $order['direction']);
  98. }
  99. else {
  100. // If the two are not the same then we expect that the child class
  101. // will implement a query() function.
  102. }
  103. }
  104. /**
  105. * A convient way to join a table to a query without duplicates.
  106. *
  107. * @param $query
  108. * The SelectQuery object.
  109. * @param $table
  110. * The table to join.
  111. * @param $alias
  112. * The table alias to use.
  113. * @param $condition
  114. * The join condition.
  115. * @param $type
  116. * The type of join: INNER, LEFT OUTER, or RIGHT OUTER.
  117. */
  118. protected function queryJoinOnce($query, $table, $alias, $condition, $type = 'INNER') {
  119. $joins = $query->getTables();
  120. // If this join is already present then don't add it again.
  121. if (in_array($alias, array_keys($joins))) {
  122. return;
  123. }
  124. switch($type) {
  125. case 'LEFT OUTER':
  126. $query->leftjoin($table, $alias, $condition);
  127. break;
  128. case 'RIGHT OUTER':
  129. $query->rightjoin($table, $alias, $condition);
  130. break;
  131. default:
  132. $query->innerjoin($table, $alias, $condition);
  133. }
  134. }
  135. /**
  136. * @see TripalField::instanceSettingsForm()
  137. */
  138. public function instanceSettingsForm() {
  139. // Make sure we don't lose our Chado table mappings when the settings
  140. // are updated. Setting them as values in the form ensures they don't
  141. // get accidentally overwritten.
  142. $element['base_table'] = array(
  143. '#type' => 'value',
  144. '#value' => $this->instance['settings']['base_table'],
  145. );
  146. $element['chado_table'] = array(
  147. '#type' => 'value',
  148. '#value' => $this->instance['settings']['chado_table'],
  149. );
  150. $element['chado_column'] = array(
  151. '#type' => 'value',
  152. '#value' => $this->instance['settings']['chado_column'],
  153. );
  154. return $element;
  155. }
  156. }