chado_linker__cvterm.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. class chado_linker__cvterm extends ChadoField {
  3. // --------------------------------------------------------------------------
  4. // EDITABLE STATIC CONSTANTS
  5. //
  6. // The following constants SHOULD be set for each descendent class. They are
  7. // used by the static functions to provide information to Drupal about
  8. // the field and it's default widget and formatter.
  9. // --------------------------------------------------------------------------
  10. // The default lable for this field.
  11. public static $default_label = 'Chado Annotation';
  12. // The default description for this field.
  13. public static $description = 'This record can be annotated with terms from other
  14. vocabularies.';
  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' => 'local',
  25. // The name of the term.
  26. 'term_name' => 'cvterm',
  27. // The unique ID (i.e. accession) of the term.
  28. 'term_accession' => 'cvterm',
  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. );
  34. // The default widget for this field.
  35. public static $default_widget = 'chado_linker__cvterm_widget';
  36. // The default formatter for this field.
  37. public static $default_formatter = 'chado_linker__cvterm_formatter';
  38. // A boolean specifying that users should not be allowed to create
  39. // fields and instances of this field type through the UI. Such
  40. // fields can only be created programmatically with field_create_field()
  41. // and field_create_instance().
  42. public static $no_ui = FALSE;
  43. /**
  44. *
  45. * @see TripalField::load()
  46. */
  47. public function load($entity, $details = array()) {
  48. $field_name = $this->field['field_name'];
  49. $field_type = $this->field['type'];
  50. $field_table = $this->instance['settings']['chado_table'];
  51. $field_column = $this->instance['settings']['chado_column'];
  52. $base_table = $this->instance['settings']['base_table'];
  53. $vocabulary = $this->instance['settings']['term_vocabulary'];
  54. $accession = $this->instance['settings']['term_accession'];
  55. $cvterm = tripal_get_cvterm(array(
  56. 'dbxref_id' => array(
  57. 'db_id' => array(
  58. 'name' => $vocabulary,
  59. ),
  60. 'accession' => $accession,
  61. ),
  62. ));
  63. $cvterm_id = $cvterm->cvterm_id;
  64. // Get the FK that links to the base record.
  65. $schema = chado_get_schema($field_table);
  66. $pkey = $schema['primary key'][0];
  67. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  68. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  69. // Set some defaults for the empty record.
  70. $chado_record = $entity->chado_record;
  71. $entity->{$field_name}['und'][0] = array(
  72. 'value' => '',
  73. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  74. 'chado-' . $field_table . '__' . 'cvterm_id' => '',
  75. // The pub column is present in the cell_line_cvterm, feature_cvterm,
  76. // library_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  77. // stock_cvterm, and stock_relationship_cvterm.
  78. 'chado-' . $field_table . '__' . 'pub_id' => '',
  79. // The is_not column is present in feature_cvterm and stock_cvterm tables.
  80. 'chado-' . $field_table . '__' . 'is_not' => '',
  81. // The rank column is present in the cell_line_cvterm, expression_cvterm,
  82. // feature_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  83. // and stock_cvterm tables.
  84. 'chado-' . $field_table . '__' . 'rank' => '',
  85. // The cvterm_type_id is present in the expression_cvterm table.
  86. 'cvterm_type_id' => '',
  87. // The following field are to help link the cvterm.
  88. 'cv__cv_id' => '',
  89. 'cvterm__name' => '',
  90. );
  91. // Get the annotations associated with this base record for this fields type.
  92. $columns = array('*');
  93. $match = array(
  94. $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  95. 'cvterm_id' => $cvterm_id,
  96. );
  97. $options = array(
  98. 'return_array' => TRUE,
  99. 'order_by' => array('rank' => 'ASC')
  100. );
  101. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  102. for ($i = 0; $i < count($fcvterms); $i++) {
  103. $linker = $fcvterms[$i];
  104. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $linker->cvterm_id));
  105. $entity->{$field_name}['und'][$i] = array(
  106. 'value' => $linker->$pkey,
  107. 'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  108. 'chado-' . $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  109. 'chado-' . $field_table . '__' . 'pub_id' => property_exists($linker, 'pub_id') ? $linker->pub_id : '',
  110. 'chado-' . $field_table . '__' . 'is_not' => property_exists($linker, 'is_not') ? $linker->is_not : '',
  111. 'chado-' . $field_table . '__' . 'rank' => property_exists($linker, 'rank') ? $linker->rank : '',
  112. 'chado-' . $field_table . '__' . 'cvterm_type_id' => property_exists($linker, 'cvterm_type_id') ? $linker->cvterm_type_id : '',
  113. 'cv__cv_id' => $cvterm->cv_id->cv_id,
  114. 'cvterm__name' => $cvterm->name,
  115. );
  116. }
  117. }
  118. }
  119. /**
  120. * Theme function for the dbxref_id_widget.
  121. *
  122. * @param $variables
  123. */
  124. function theme_chado_linker__cvterm_widget($variables) {
  125. $element = $variables['element'];
  126. // These two fields were added to the widget to help identify the fields
  127. // for layout.
  128. $table_name = $element['#table_name'];
  129. $fkey = $element['#fkey_field'];
  130. $layout = "
  131. <div class=\"annotation-cvterm-widget\">
  132. <div class=\"annotation-cvterm-widget-item\">" .
  133. drupal_render($element['cv__cv_id']) . "
  134. </div>
  135. <div class=\"annotation-cvterm-widget-item\">" .
  136. drupal_render($element['cvterm__name']) . "
  137. </div>
  138. <div class=\"annotation-cvterm-widget-item\">" .
  139. drupal_render($element['pub']) . "
  140. </div>
  141. <div class=\"annotation-cvterm-widget-item\">" .
  142. drupal_render($element['chado-' . $table_name . '__is_not']) . "
  143. </div>
  144. </div>
  145. ";
  146. return $layout;
  147. }
  148. /**
  149. * An Ajax callback for the dbxref widget.
  150. */
  151. function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
  152. $field_name = $form_state['triggering_element']['#parents'][0];
  153. $delta = $form_state['triggering_element']['#parents'][2];
  154. return $form[$field_name]['und'][$delta];
  155. }