sio__references.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. class sio__references 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 = 'References';
  12. // The default description for this field.
  13. public static $description = 'Records references by this publication.';
  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' => 'references',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '000631',
  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. // The table in Chado that the instance maps to.
  33. 'chado_table' => 'pub',
  34. // The primary key column of hte table in Dhado.
  35. 'chado_column' => 'pub_id',
  36. // The base table.
  37. 'base_table' => 'pub',
  38. );
  39. // The default widget for this field.
  40. public static $default_widget = 'sio__references_widget';
  41. // The default formatter for this field.
  42. public static $default_formatter = 'sio__references_formatter';
  43. // A boolean specifying that users should not be allowed to create
  44. // fields and instances of this field type through the UI. Such
  45. // fields can only be created programmatically with field_create_field()
  46. // and field_create_instance().
  47. public static $no_ui = FALSE;
  48. /**
  49. * @see TripalField::elementInfo()
  50. */
  51. public function elementInfo() {
  52. $field_term = $this->getFieldTermID();
  53. return array(
  54. $field_term => array(
  55. 'operations' => array(),
  56. 'sortable' => FALSE,
  57. 'searchable' => FALSE,
  58. 'elements' => array(
  59. 'rdfs:type' => array(
  60. 'searchable' => TRUE,
  61. 'name' => 'type',
  62. 'label' => 'Reference Type',
  63. 'help' => 'The type of item referred to by the publication.',
  64. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  65. 'sortable' => TRUE,
  66. ),
  67. 'schema:name' => array(
  68. 'searchable' => TRUE,
  69. 'name' => 'name',
  70. 'label' => 'Reference Name',
  71. 'help' => 'The name of the item referred to by the publication.',
  72. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  73. 'sortable' => TRUE,
  74. ),
  75. 'data:0842'=> array(
  76. 'searchable' => TRUE,
  77. 'name' => 'identifier',
  78. 'label' => 'Reference Identifier',
  79. 'help' => 'The unique identifier of the item referred to by the publication.',
  80. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  81. 'sortable' => TRUE,
  82. ),
  83. 'entity'=> array(
  84. 'searchable' => FALSE,
  85. 'sortable' => FALSE,
  86. ),
  87. ),
  88. ),
  89. );
  90. }
  91. /**
  92. *
  93. * @see TripalField::load()
  94. */
  95. public function load($entity) {
  96. $field_name = $this->field['field_name'];
  97. $field_type = $this->field['type'];
  98. $field_table = $this->instance['settings']['chado_table'];
  99. $field_column = $this->instance['settings']['chado_column'];
  100. $base_table = $this->instance['settings']['base_table'];
  101. // Set some defaults for the empty record.
  102. $chado_record = $entity->chado_record;
  103. $entity->{$field_name}['und'][0] = array(
  104. 'value' => '',
  105. );
  106. // Iterate through all of the _pub tables and look for any that have
  107. // linked to this record. If so then add them.
  108. $chado_tables = chado_get_table_names(TRUE);
  109. $delta = 0;
  110. foreach ($chado_tables as $chado_table) {
  111. $matches = array();
  112. if (preg_match('/^(.+?)_pub$/', $chado_table, $matches)) {
  113. $reference_table = $matches[1];
  114. // Find the base table this links to and get the fk columns that map it.
  115. $schema = chado_get_schema($chado_table);
  116. $fkeys = $schema['foreign keys'];
  117. foreach ($fkeys as $linked_table => $fk_details) {
  118. if ($linked_table == $reference_table) {
  119. $fkleft = array_keys($fk_details['columns'])[0];
  120. $fkright = $fk_details['columns'][$fkleft];
  121. }
  122. }
  123. // Iterate through all of the records in the linker table that
  124. // match the given pub ID.
  125. $records = chado_generate_var($chado_table, array('pub_id' => $chado_record->pub_id), array('return_array' => TRUE));
  126. foreach ($records as $record) {
  127. // We want to add a 'type' and 'name' element to the values (at a
  128. // minimum) for each of the records. Unfortunately, every base table
  129. // is different and their may not be an easy to identify name,
  130. // so... we'll do the best we can.
  131. $entity->{$field_name}['und'][$delta]['value'] = array();
  132. // First get the type of record.
  133. if (property_exists($record->$fkleft, 'type_id') and $record->$fkleft->type_id) {
  134. $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $record->$fkleft->type_id->name;
  135. }
  136. else {
  137. // If there's not a type_id column then see if the table is mapped
  138. // to a type.
  139. $mapping = db_select('chado_cvterm_mapping', 'CVM')
  140. ->fields('CVM')
  141. ->condition('chado_table', $reference_table)
  142. ->execute()
  143. ->fetchObject();
  144. if ($mapping) {
  145. $cvterm = tripal_get_cvterm(array('cvterm_id' => $mapping->cvterm_id));
  146. $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $cvterm->name;
  147. }
  148. }
  149. // Add in the name and uniquename (identifier) if those fields exist.
  150. if (property_exists($record->$fkleft, 'name')) {
  151. $entity->{$field_name}['und'][$delta]['value']['schema:name'] = $record->$fkleft->name;
  152. }
  153. if (property_exists($record->$fkleft, 'uniquename')) {
  154. $entity->{$field_name}['und'][$delta]['value']['data:0842'] = $record->$fkleft->name;
  155. }
  156. // If this records is also a published entity then include that.
  157. if (property_exists($record->$fkleft, 'entity_id')) {
  158. $entity->{$field_name}['und'][$delta]['value']['entity'] = 'TripalEntity:' . $record->$fkleft->entity_id;
  159. }
  160. // If this is the organism table then we will create the name
  161. // specially.
  162. if (property_exists($record->$fkleft, 'genus')) {
  163. $name = '<i>' . $record->$fkleft->genus . ' ' . $record->$fkleft->species . '</i>';
  164. if (property_exists($record->$fkleft, 'infraspecific_name')) {
  165. if ($record->$fkleft->type_id) {
  166. $name .= ' ' . $record->$fkleft->type_id->name;
  167. }
  168. $name .= ' ' . $record->$fkleft->infraspecific_name;
  169. }
  170. $entity->{$field_name}['und'][$delta]['value']['schema:name'] = $name;
  171. }
  172. $delta++;
  173. }
  174. }
  175. }
  176. }
  177. }