sio__annotation.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. public function validate($entity_type, $entity, $langcode, $items, &$errors) {
  43. $field_name = $this->field['field_name'];
  44. foreach ($items as $delta => $item) {
  45. // Get the term that matches.
  46. $cvterm_name = $item['cvterm_name'];
  47. $cv_id = $item['cv_id'];
  48. if($cvterm_name and $cv_id) {
  49. $cvterm = chado_generate_var('cvterm', array(
  50. 'cv_id' => $cv_id,
  51. 'name' => $cvterm_name,
  52. ));
  53. if (!$cvterm) {
  54. $errors[$field_name][$langcode][$delta][] = array(
  55. 'message' => t("Cannot find a term that matches the term name and vocabulary."),
  56. 'error' => 'cvterm_name'
  57. );
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. *
  64. * @see TripalField::load()
  65. */
  66. public function load($entity) {
  67. $field_name = $this->field['field_name'];
  68. $field_type = $this->field['type'];
  69. $field_table = $this->instance['settings']['chado_table'];
  70. $field_column = $this->instance['settings']['chado_column'];
  71. $base_table = $this->instance['settings']['base_table'];
  72. // Get the FK that links to the base record.
  73. $schema = chado_get_schema($field_table);
  74. $pkey = $schema['primary key'][0];
  75. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  76. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  77. $vocabulary = tripal_get_chado_semweb_term('cvterm', 'cv_id');
  78. $accession = tripal_get_chado_semweb_term('dbxref', 'accession');
  79. $definition = tripal_get_chado_semweb_term('cvterm', 'definition');
  80. if (array_key_exists('is_not', $schema['fields'])) {
  81. $negation = tripal_get_chado_semweb_term($field_table, 'is_not');
  82. }
  83. // Set some defaults for the empty record.
  84. $chado_record = $entity->chado_record;
  85. $entity->{$field_name}['und'][0] = array(
  86. 'value' => '',
  87. 'chado-' . $field_table . '__' . $pkey => '',
  88. 'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  89. 'chado-' . $field_table . '__cvterm_id' => '',
  90. );
  91. if (array_key_exists('is_not', $schema['fields'])) {
  92. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__is_not'] = '';
  93. }
  94. if (array_key_exists('rank', $schema['fields'])) {
  95. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__rank'] = '';
  96. }
  97. if (array_key_exists('pub_id', $schema['fields'])) {
  98. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__pub_id'] = '';
  99. }
  100. // Get the annotations associated with this base record for this fields type.
  101. $columns = array('*');
  102. $match = array(
  103. $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  104. );
  105. $order_by = array($pkey => 'ASC');
  106. if (array_key_exists('rank', $schema['fields'])) {
  107. $order_by = array('rank' => 'ASC');
  108. }
  109. $options = array(
  110. 'return_array' => TRUE,
  111. 'order_by' => $order_by
  112. );
  113. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  114. for ($i = 0; $i < count($fcvterms); $i++) {
  115. $linker = $fcvterms[$i];
  116. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $linker->cvterm_id));
  117. $entity->{$field_name}['und'][$i] = array(
  118. 'value' => array(
  119. $vocabulary => $cvterm->dbxref_id->db_id->name,
  120. $accession => $cvterm->dbxref_id->accession,
  121. $definition => $cvterm->name
  122. ),
  123. 'chado-' . $field_table . '__' . $pkey => $linker->$pkey,
  124. 'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  125. 'chado-' . $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  126. );
  127. if (array_key_exists('is_not', $schema['fields'])) {
  128. $entity->{$field_name}['und'][$i]['value'][$negation] = $linker->is_not;
  129. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__is_not'] = $linker->is_not;
  130. }
  131. if (array_key_exists('rank', $schema['fields'])) {
  132. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $linker->rank;
  133. }
  134. if (array_key_exists('pub_id', $schema['fields'])) {
  135. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__pub_id'] = $linker->pub_id;
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. * Theme function for the dbxref_id_widget.
  142. *
  143. * @param $variables
  144. */
  145. function theme_chado_linker__cvterm_widget($variables) {
  146. $element = $variables['element'];
  147. // These two fields were added to the widget to help identify the fields
  148. // for layout.
  149. $table_name = $element['#table_name'];
  150. $fkey = $element['#fkey_field'];
  151. $layout = "
  152. <div class=\"annotation-cvterm-widget\">
  153. <div class=\"annotation-cvterm-widget-item\">" .
  154. drupal_render($element['cv__cv_id']) . "
  155. </div>
  156. <div class=\"annotation-cvterm-widget-item\">" .
  157. drupal_render($element['cvterm__name']) . "
  158. </div>
  159. <div class=\"annotation-cvterm-widget-item\">" .
  160. drupal_render($element['pub']) . "
  161. </div>
  162. <div class=\"annotation-cvterm-widget-item\">" .
  163. drupal_render($element['chado-' . $table_name . '__is_not']) . "
  164. </div>
  165. </div>
  166. ";
  167. return $layout;
  168. }
  169. /**
  170. * An Ajax callback for the dbxref widget.
  171. */
  172. function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
  173. $field_name = $form_state['triggering_element']['#parents'][0];
  174. $delta = $form_state['triggering_element']['#parents'][2];
  175. return $form[$field_name]['und'][$delta];
  176. }