chado_linker__cvterm.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. class chado_linker__cvterm extends TripalField {
  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 can be annotated with terms from other
  14. vocabularies.';
  15. // Provide a list of global settings. These can be accessed witihn the
  16. // globalSettingsForm. When the globalSettingsForm is submitted then
  17. // Drupal will automatically change these settings for all fields.
  18. public static $default_settings = array(
  19. 'chado_table' => '',
  20. 'chado_column' => '',
  21. 'base_table' => '',
  22. );
  23. // Provide a list of instance specific settings. These can be access within
  24. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  25. // then Drupal with automatically change these settings for the instnace.
  26. // It is recommended to put settings at the instance level whenever possible.
  27. // If you override this variable in a child class be sure to replicate the
  28. // term_name, term_vocab, term_accession and term_fixed keys as these are
  29. // required for all TripalFields.
  30. public static $default_instance_settings = array(
  31. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  32. 'term_vocabulary' => 'local',
  33. // The name of the term.
  34. 'term_name' => 'cvterm',
  35. // The unique ID (i.e. accession) of the term.
  36. 'term_accession' => 'cvterm',
  37. // Set to TRUE if the site admin is allowed to change the term
  38. // type. This will create form elements when editing the field instance
  39. // to allow the site admin to change the term settings above.
  40. 'term_fixed' => FALSE,
  41. );
  42. // Set this to the name of the storage backend that by default will support
  43. // this field.
  44. public static $storage = 'field_chado_storage';
  45. // The default widget for this field.
  46. public static $default_widget = 'chado_linker__cvterm_widget';
  47. // The default formatter for this field.
  48. public static $default_formatter = 'chado_linker__cvterm_formatter';
  49. // --------------------------------------------------------------------------
  50. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  51. // --------------------------------------------------------------------------
  52. // An array containing details about the field. The format of this array
  53. // is the same as that returned by field_info_fields()
  54. protected $field;
  55. // An array containing details about an instance of the field. A field does
  56. // not have to have an instance. But if dealing with an instance (such as
  57. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  58. protected $instance;
  59. /**
  60. *
  61. * @see TripalField::validate()
  62. */
  63. public function validate($entity_type, $entity, $field, $items, &$errors) {
  64. }
  65. /**
  66. *
  67. * @see TripalField::load()
  68. */
  69. public function load($entity, $details = array()) {
  70. $field_name = $this->field['field_name'];
  71. $field_type = $this->field['type'];
  72. $field_table = $this->field['settings']['chado_table'];
  73. $field_column = $this->field['settings']['chado_column'];
  74. $base_table = $this->field['settings']['base_table'];
  75. $matches = array();
  76. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  77. $table_name = $matches[1];
  78. $cv_id = $matches[2];
  79. // Get the FK that links to the base record.
  80. $schema = chado_get_schema($field_table);
  81. $pkey = $schema['primary key'][0];
  82. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  83. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  84. // Set some defaults for the empty record.
  85. $chado_record = $entity->chado_record;
  86. $entity->{$field_name}['und'][0] = array(
  87. 'value' => '',
  88. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  89. 'chado-' . $field_table . '__' . 'cvterm_id' => '',
  90. // The pub column is present in the cell_line_cvterm, feature_cvterm,
  91. // library_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  92. // stock_cvterm, and stock_relationship_cvterm.
  93. 'chado-' . $field_table . '__' . 'pub_id' => '',
  94. // The is_not column is present in feature_cvterm and stock_cvterm tables.
  95. 'chado-' . $field_table . '__' . 'is_not' => '',
  96. // The rank column is present in the cell_line_cvterm, expression_cvterm,
  97. // feature_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  98. // and stock_cvterm tables.
  99. 'chado-' . $field_table . '__' . 'rank' => '',
  100. // The cvterm_type_id is present in the expression_cvterm table.
  101. 'cvterm_type_id' => '',
  102. // The following field are to help link the cvterm.
  103. 'cv__cv_id' => '',
  104. 'cvterm__name' => '',
  105. );
  106. // Get the annotations associated with this base record for this fields type.
  107. $columns = array('*');
  108. $match = array(
  109. $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  110. 'cvterm_id' => array(
  111. 'cv_id' => $cv_id,
  112. ),
  113. );
  114. $options = array(
  115. 'return_array' => TRUE,
  116. 'order_by' => array('rank' => 'ASC')
  117. );
  118. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  119. for ($i = 0; $i < count($fcvterms); $i++) {
  120. $linker = $fcvterms[$i];
  121. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $linker->cvterm_id));
  122. $entity->{$field_name}['und'][$i] = array(
  123. 'value' => $linker->$pkey,
  124. 'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  125. 'chado-' . $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  126. 'chado-' . $field_table . '__' . 'pub_id' => property_exists($linker, 'pub_id') ? $linker->pub_id : '',
  127. 'chado-' . $field_table . '__' . 'is_not' => property_exists($linker, 'is_not') ? $linker->is_not : '',
  128. 'chado-' . $field_table . '__' . 'rank' => property_exists($linker, 'rank') ? $linker->rank : '',
  129. 'chado-' . $field_table . '__' . 'cvterm_type_id' => property_exists($linker, 'cvterm_type_id') ? $linker->cvterm_type_id : '',
  130. 'cv__cv_id' => $cvterm->cv_id->cv_id,
  131. 'cvterm__name' => $cvterm->name,
  132. );
  133. }
  134. }
  135. }
  136. /**
  137. * Theme function for the dbxref_id_widget.
  138. *
  139. * @param $variables
  140. */
  141. function theme_chado_linker__cvterm_widget($variables) {
  142. $element = $variables['element'];
  143. // These two fields were added to the widget to help identify the fields
  144. // for layout.
  145. $table_name = $element['#table_name'];
  146. $fkey = $element['#fkey_field'];
  147. $layout = "
  148. <div class=\"annotation-cvterm-widget\">
  149. <div class=\"annotation-cvterm-widget-item\">" .
  150. drupal_render($element['cv__cv_id']) . "
  151. </div>
  152. <div class=\"annotation-cvterm-widget-item\">" .
  153. drupal_render($element['cvterm__name']) . "
  154. </div>
  155. <div class=\"annotation-cvterm-widget-item\">" .
  156. drupal_render($element['pub']) . "
  157. </div>
  158. <div class=\"annotation-cvterm-widget-item\">" .
  159. drupal_render($element['chado-' . $table_name . '__is_not']) . "
  160. </div>
  161. </div>
  162. ";
  163. return $layout;
  164. }
  165. /**
  166. * An Ajax callback for the dbxref widget.
  167. */
  168. function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
  169. $field_name = $form_state['triggering_element']['#parents'][0];
  170. $delta = $form_state['triggering_element']['#parents'][2];
  171. return $form[$field_name]['und'][$delta];
  172. }