ChadoField.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 witihn 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. // The module that manages this field.
  41. public static $module = 'tripal_chado';
  42. /**
  43. * @see TripalField::query()
  44. *
  45. * In addition to the rules to follow for the TripalField::query function
  46. * these should also be followed for the ChadoField::query implementation.
  47. *
  48. * - When giving alias to joined tables be sure to use aliases that are
  49. * unique to avoid conflicts with other fields.
  50. * - When joining with the base table its alias is 'base'.
  51. * - You may join to materialized views if need be to help speed queries.
  52. */
  53. public function query($query, $condition) {
  54. // If we are here it is because the child class did not implement the
  55. // query function. So, we will do our best to make the query work.
  56. $chado_table = $this->instance['settings']['chado_table'];
  57. $base_table = $this->instance['settings']['base_table'];
  58. $bschema = chado_get_schema($base_table);
  59. $bpkey = $bschema['primary key'][0];
  60. $alias = 'dbx_linker';
  61. $operator = $condition['operator'];
  62. // If the chado_table and the base_table are the same then this is easy.
  63. if ($chado_table == $base_table) {
  64. // Get the base table column that is associated with the term
  65. // passed as $condition['column'].
  66. $base_field = tripal_get_chado_semweb_column($chado_table, $condition['column']);
  67. $query->condition('base.' . $base_field , $condition['value'], $operator);
  68. }
  69. else {
  70. // If the two are not the same then we expect that the child class
  71. // will implement a query() function.
  72. }
  73. }
  74. /**
  75. * @see TripalField::queryOrder()
  76. */
  77. public function queryOrder($query, $order) {
  78. // If we are here it is because the child class did not implement the
  79. // queryOrder function. So, we will do our best to make the query work.
  80. $chado_table = $this->instance['settings']['chado_table'];
  81. $base_table = $this->instance['settings']['base_table'];
  82. $bschema = chado_get_schema($base_table);
  83. $bpkey = $bschema['primary key'][0];
  84. $alias = 'dbx_linker';
  85. $operator = $condition['operator'];
  86. // If the chado_table and the base_table are the same then this is easy.
  87. if ($chado_table == $base_table) {
  88. // Get the base table column that is associated with the term
  89. // passed as $condition['column'].
  90. $base_field = tripal_get_chado_semweb_column($chado_table, $order['column']);
  91. $query->orderBy('base.' . $base_field, $order['direction']);
  92. }
  93. else {
  94. // If the two are not the same then we expect that the child class
  95. // will implement a query() function.
  96. }
  97. }
  98. /**
  99. * A convient way to join a table to a query without duplicates.
  100. *
  101. * @param $query
  102. * The SelectQuery object.
  103. * @param $table
  104. * The table to join.
  105. * @param $alias
  106. * The table alias to use.
  107. * @param $condition
  108. * The join condition.
  109. * @param $type
  110. * The type of join: INNER, LEFT OUTER, or RIGHT OUTER.
  111. */
  112. protected function queryJoinOnce($query, $table, $alias, $condition, $type = 'INNER') {
  113. $joins = $query->getTables();
  114. // If this join is already present then don't add it again.
  115. if (in_array($alias, array_keys($joins))) {
  116. return;
  117. }
  118. switch($type) {
  119. case 'LEFT OUTER':
  120. $query->leftjoin($table, $alias, $condition);
  121. break;
  122. case 'RIGHT OUTER':
  123. $query->rightjoin($table, $alias, $condition);
  124. break;
  125. default:
  126. $query->innerjoin($table, $alias, $condition);
  127. }
  128. }
  129. /**
  130. * @see TripalField::instanceSettingsForm()
  131. */
  132. public function instanceSettingsForm() {
  133. // Make sure we don't lose our Chado table mappings when the settings
  134. // are updated. Setting them as values in the form ensures they don't
  135. // get accidentally overwritten.
  136. $element['base_table'] = array(
  137. '#type' => 'value',
  138. '#value' => $this->instance['settings']['base_table'],
  139. );
  140. $element['chado_table'] = array(
  141. '#type' => 'value',
  142. '#value' => $this->instance['settings']['chado_table'],
  143. );
  144. $element['chado_column'] = array(
  145. '#type' => 'value',
  146. '#value' => $this->instance['settings']['chado_column'],
  147. );
  148. return $element;
  149. }
  150. }