chado_linker__cvterm.inc 7.9 KB

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