ChadoField.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }
  79. /**
  80. * A convient way to join a table to a query without duplicates.
  81. *
  82. * @param $query
  83. * The SelectQuery object.
  84. * @param $table
  85. * The table to join.
  86. * @param $alias
  87. * The table alias to use.
  88. * @param $condition
  89. * The join condition.
  90. * @param $type
  91. * The type of join: INNER, LEFT OUTER, or RIGHT OUTER.
  92. */
  93. protected function queryJoinOnce($query, $table, $alias, $condition, $type = 'INNER') {
  94. $joins = $query->getTables();
  95. // If this join is already present then don't add it again.
  96. if (in_array($alias, array_keys($joins))) {
  97. return;
  98. }
  99. switch($type) {
  100. case 'LEFT OUTER':
  101. $query->leftjoin($table, $alias, $condition);
  102. break;
  103. case 'RIGHT OUTER':
  104. $query->rightjoin($table, $alias, $condition);
  105. break;
  106. default:
  107. $query->innerjoin($table, $alias, $condition);
  108. }
  109. }
  110. /**
  111. * @see TripalField::instanceSettingsForm()
  112. */
  113. public function instanceSettingsForm() {
  114. // Make sure we don't lose our Chado table mappings when the settings
  115. // are updated. Setting them as values in the form ensures they don't
  116. // get accidentally overwritten.
  117. $element['base_table'] = array(
  118. '#type' => 'value',
  119. '#value' => $this->instance['settings']['base_table'],
  120. );
  121. $element['chado_table'] = array(
  122. '#type' => 'value',
  123. '#value' => $this->instance['settings']['chado_table'],
  124. );
  125. $element['chado_column'] = array(
  126. '#type' => 'value',
  127. '#value' => $this->instance['settings']['chado_column'],
  128. );
  129. return $element;
  130. }
  131. }