sio__annotation.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. class sio__annotation 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 = 'Annotations';
  12. // The default description for this field.
  13. public static $description = 'This record is annotated with controlled vocabulary terms.';
  14. // Provide a list of instance specific settings. These can be access within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal with automatically change these settings for the instnace.
  17. // It is recommended to put settings at the instance level whenever possible.
  18. // If you override this variable in a child class be sure to replicate the
  19. // term_name, term_vocab, term_accession and term_fixed keys as these are
  20. // required for all TripalFields.
  21. public static $default_instance_settings = array(
  22. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'SIO',
  24. // The name of the term.
  25. 'term_name' => 'annotation',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '001166',
  28. // Set to TRUE if the site admin is allowed to change the term
  29. // type. This will create form elements when editing the field instance
  30. // to allow the site admin to change the term settings above.
  31. 'term_fixed' => FALSE,
  32. );
  33. // The default widget for this field.
  34. public static $default_widget = 'chado_linker__cvterm_widget';
  35. // The default formatter for this field.
  36. public static $default_formatter = 'chado_linker__cvterm_formatter';
  37. // A boolean specifying that users should not be allowed to create
  38. // fields and instances of this field type through the UI. Such
  39. // fields can only be created programmatically with field_create_field()
  40. // and field_create_instance().
  41. public static $no_ui = FALSE;
  42. /**
  43. *
  44. * @see TripalField::load()
  45. */
  46. public function load($entity) {
  47. $field_name = $this->field['field_name'];
  48. $field_type = $this->field['type'];
  49. $field_table = $this->instance['settings']['chado_table'];
  50. $field_column = $this->instance['settings']['chado_column'];
  51. $base_table = $this->instance['settings']['base_table'];
  52. $vocabulary = tripal_get_chado_semweb_term('db', 'name');
  53. $accession = tripal_get_chado_semweb_term('db', 'accession');
  54. $definition = tripal_get_chado_semweb_term('cvterm', 'definition');
  55. // Get the FK that links to the base record.
  56. $schema = chado_get_schema($field_table);
  57. $pkey = $schema['primary key'][0];
  58. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  59. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  60. // Set some defaults for the empty record.
  61. $chado_record = $entity->chado_record;
  62. $entity->{$field_name}['und'][0] = array(
  63. 'value' => '',
  64. 'chado-' . $field_table . '__' . $pkey => '',
  65. 'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  66. 'chado-' . $field_table . '__' . 'cvterm_id' => '',
  67. // The pub column is present in the cell_line_cvterm, feature_cvterm,
  68. // library_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  69. // stock_cvterm, and stock_relationship_cvterm.
  70. 'chado-' . $field_table . '__' . 'pub_id' => '',
  71. // The is_not column is present in feature_cvterm and stock_cvterm tables.
  72. 'chado-' . $field_table . '__' . 'is_not' => '',
  73. // The rank column is present in the cell_line_cvterm, expression_cvterm,
  74. // feature_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  75. // and stock_cvterm tables.
  76. 'chado-' . $field_table . '__' . 'rank' => '',
  77. );
  78. // Get the annotations associated with this base record for this fields type.
  79. $columns = array('*');
  80. $match = array(
  81. $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  82. );
  83. $options = array(
  84. 'return_array' => TRUE,
  85. 'order_by' => array('rank' => 'ASC')
  86. );
  87. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  88. for ($i = 0; $i < count($fcvterms); $i++) {
  89. $linker = $fcvterms[$i];
  90. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $linker->cvterm_id));
  91. $entity->{$field_name}['und'][$i] = array(
  92. 'value' => array(
  93. $vocabulary => $cvterm->dbxref_id->db_id->name,
  94. $accession => $cvterm->dbxref_id->accession,
  95. $definition => $cvterm->cvterm_id->name
  96. ),
  97. 'chado-' . $field_table . '__' . $pkey => $linker->$pkey,
  98. 'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  99. 'chado-' . $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  100. 'chado-' . $field_table . '__' . 'pub_id' => property_exists($linker, 'pub_id') ? $linker->pub_id : '',
  101. 'chado-' . $field_table . '__' . 'is_not' => property_exists($linker, 'is_not') ? $linker->is_not : '',
  102. 'chado-' . $field_table . '__' . 'rank' => property_exists($linker, 'rank') ? $linker->rank : '',
  103. 'chado-' . $field_table . '__' . 'cvterm_type_id' => property_exists($linker, 'cvterm_type_id') ? $linker->cvterm_type_id : '',
  104. );
  105. }
  106. }
  107. }
  108. /**
  109. * Theme function for the dbxref_id_widget.
  110. *
  111. * @param $variables
  112. */
  113. function theme_chado_linker__cvterm_widget($variables) {
  114. $element = $variables['element'];
  115. // These two fields were added to the widget to help identify the fields
  116. // for layout.
  117. $table_name = $element['#table_name'];
  118. $fkey = $element['#fkey_field'];
  119. $layout = "
  120. <div class=\"annotation-cvterm-widget\">
  121. <div class=\"annotation-cvterm-widget-item\">" .
  122. drupal_render($element['cv__cv_id']) . "
  123. </div>
  124. <div class=\"annotation-cvterm-widget-item\">" .
  125. drupal_render($element['cvterm__name']) . "
  126. </div>
  127. <div class=\"annotation-cvterm-widget-item\">" .
  128. drupal_render($element['pub']) . "
  129. </div>
  130. <div class=\"annotation-cvterm-widget-item\">" .
  131. drupal_render($element['chado-' . $table_name . '__is_not']) . "
  132. </div>
  133. </div>
  134. ";
  135. return $layout;
  136. }
  137. /**
  138. * An Ajax callback for the dbxref widget.
  139. */
  140. function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
  141. $field_name = $form_state['triggering_element']['#parents'][0];
  142. $delta = $form_state['triggering_element']['#parents'][2];
  143. return $form[$field_name]['und'][$delta];
  144. }