sbo__database_cross_reference.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. class sbo__database_cross_reference 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 = 'SBO:0000554';
  16. // The default lable for this field.
  17. public static $label = 'Database cross reference';
  18. // The default description for this field.
  19. public static $description = 'This record can be cross referenced with a record in
  20. another online database. This field is intended for one or more
  21. references. At a minimum, the database and accession must be provided.';
  22. // Provide a list of global settings. These can be accessed witihn the
  23. // globalSettingsForm. When the globalSettingsForm is submitted then
  24. // Drupal will automatically change these settings for all fields.
  25. public static $settings = array(
  26. 'chado_table' => '',
  27. 'chado_column' => '',
  28. 'base_table' => '',
  29. );
  30. // Provide a list of instance specific settings. These can be access within
  31. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  32. // then Drupal with automatically change these settings for the instnace.
  33. // It is recommended to put settings at the instance level whenever possible.
  34. public static $instance_settings = array();
  35. // Set this to the name of the storage backend that by default will support
  36. // this field.
  37. public static $storage = 'tripal_no_storage';
  38. // The default widget for this field.
  39. public static $default_widget = 'sbo__database_cross_reference_widget';
  40. // The default formatter for this field.
  41. public static $default_formatter = 'sbo__database_cross_reference_formatter';
  42. // --------------------------------------------------------------------------
  43. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  44. // --------------------------------------------------------------------------
  45. // An array containing details about the field. The format of this array
  46. // is the same as that returned by field_info_fields()
  47. protected $field;
  48. // An array containing details about an instance of the field. A field does
  49. // not have to have an instance. But if dealing with an instance (such as
  50. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  51. protected $instance;
  52. /**
  53. *
  54. * @see TripalField::validate()
  55. */
  56. public function validate($entity_type, $entity, $field, $items, &$errors) {
  57. }
  58. /**
  59. *
  60. * @see TripalField::load()
  61. */
  62. public function load($entity, $details = array()) {
  63. $record = $details['record'];
  64. $field_name = $this->field['field_name'];
  65. $field_type = $this->field['type'];
  66. $field_table = $this->field['settings']['chado_table'];
  67. $field_column = $this->field['settings']['chado_column'];
  68. $base_table = $record->tablename;
  69. $schema = chado_get_schema($field_table);
  70. $pkey = $schema['primary key'][0];
  71. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  72. $fkey = $fkeys[0];
  73. // Set some defaults for the empty record.
  74. $entity->{$field_name}['und'][0] = array(
  75. 'value' => array(),
  76. 'chado-' . $field_table . '__' . $pkey => '',
  77. 'chado-' . $field_table . '__' . $fkey => '',
  78. 'chado-' . $field_table . '__dbxref_id' => '',
  79. 'dbxref_id' => '',
  80. 'db_id' => '',
  81. 'accession' => '',
  82. 'version' => '',
  83. 'description' => '',
  84. );
  85. $linker_table = $base_table . '_dbxref';
  86. $options = array('return_array' => 1);
  87. $record = chado_expand_var($record, 'table', $linker_table, $options);
  88. if (count($record->$linker_table) > 0) {
  89. $i = 0;
  90. foreach ($record->$linker_table as $index => $linker) {
  91. $dbxref = $linker->dbxref_id;
  92. $URL = tripal_get_dbxref_url($dbxref);
  93. $entity->{$field_name}['und'][$i] = array(
  94. 'value' => array(
  95. 'vocabulary' => $dbxref->db_id->name,
  96. 'accession' => $dbxref->accession,
  97. 'URL' => $URL,
  98. ),
  99. 'chado-' . $field_table . '__' . $pkey => $linker->$pkey,
  100. 'chado-' . $field_table . '__' . $fkey => $linker->$fkey->$fkey,
  101. 'chado-' . $field_table . '__dbxref_id' => $dbxref->dbxref_id,
  102. 'dbxref_id' => $dbxref->dbxref_id,
  103. 'db_id' => $dbxref->db_id->db_id,
  104. 'accession' => $dbxref->accession,
  105. 'version' => $dbxref->version,
  106. 'description' => $dbxref->description,
  107. );
  108. $i++;
  109. }
  110. }
  111. }
  112. /**
  113. *
  114. * @see TripalField::settingsForm()
  115. */
  116. public function settingsForm($has_data) {
  117. }
  118. /**
  119. *
  120. * @param unknown $form
  121. * @param unknown $form_state
  122. */
  123. public function settingsFormValidate($form, &$form_state) {
  124. }
  125. /**
  126. *
  127. * @see TripalField::instanceSettingsForm()
  128. */
  129. public function instanceSettingsForm() {
  130. }
  131. /**
  132. *
  133. * @see TripalField::instanceSettingsFormValidate()
  134. */
  135. public function instanceSettingsFormValidate($form, &$form_state) {
  136. }
  137. }
  138. /**
  139. * Theme function for the dbxref_id_widget.
  140. *
  141. * @param $variables
  142. */
  143. function theme_sbo__database_cross_reference_widget($variables) {
  144. $element = $variables['element'];
  145. // These two fields were added to the widget to help identify the fields
  146. // for layout.
  147. $table_name = $element['#table_name'];
  148. $fkey = $element['#fkey_field'];
  149. $layout = "
  150. <div class=\"secondary-dbxref-widget\">
  151. <div class=\"secondary-dbxref-widget-item\">" .
  152. drupal_render($element['db_id']) . "
  153. </div>
  154. <div class=\"secondary-dbxref-widget-item\">" .
  155. drupal_render($element['accession']) . "
  156. </div>
  157. <div class=\"secondary-dbxref-widget-item\">" .
  158. drupal_render($element['version']) . "
  159. </div>
  160. <div class=\"secondary-dbxref-widget-item\">" .
  161. drupal_render($element['description']) . "
  162. </div>
  163. <div class=\"secondary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  164. </div>
  165. ";
  166. return $layout;
  167. }
  168. /**
  169. * An Ajax callback for the dbxref widget.
  170. */
  171. function sbo__database_cross_reference_widget_form_ajax_callback($form, $form_state) {
  172. $field_name = $form_state['triggering_element']['#parents'][0];
  173. $delta = $form_state['triggering_element']['#parents'][2];
  174. $field = field_info_field($field_name);
  175. $field_type = $field['type'];
  176. $field_table = $field['settings']['chado_table'];
  177. $field_column = $field['settings']['chado_column'];
  178. $field_prefix = 'chado-' . $field_table . '__dbxref_id';
  179. // Check to see if this dbxref already exists. If not then
  180. // give a notice to the user that the dbxref will be added.
  181. $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
  182. $db_id = $form_state['values'][$field_name]['und'][$delta]['accession'];
  183. if ($db_id and $accession) {
  184. $values = array(
  185. 'db_id' => $db_id,
  186. 'accession' => $accession,
  187. );
  188. $options = array('is_duplicate' => TRUE);
  189. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  190. if (!$has_duplicate) {
  191. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  192. }
  193. }
  194. return $form[$field_name]['und'][$delta];
  195. }