sio__references.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 label 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 accessed within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal will automatically change these settings for the instance.
  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 = [
  22. // The short name for the vocabulary (e.g. schema, 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. // The number of items to show on a page.
  39. 'items_per_page' => 10,
  40. ];
  41. // The default widget for this field.
  42. public static $default_widget = 'sio__references_widget';
  43. // The default formatter for this field.
  44. public static $default_formatter = 'sio__references_formatter';
  45. // A boolean specifying that users should not be allowed to create
  46. // fields and instances of this field type through the UI. Such
  47. // fields can only be created programmatically with field_create_field()
  48. // and field_create_instance().
  49. public static $no_ui = FALSE;
  50. /**
  51. * @see TripalField::elementInfo()
  52. */
  53. public function elementInfo() {
  54. $field_term = $this->getFieldTermID();
  55. return [
  56. $field_term => [
  57. 'operations' => [],
  58. 'sortable' => FALSE,
  59. 'searchable' => FALSE,
  60. 'type' => 'xs:string',
  61. 'readonly' => TRUE,
  62. ],
  63. ];
  64. }
  65. /**
  66. *
  67. * @see TripalField::load()
  68. */
  69. public function load($entity) {
  70. $field_name = $this->field['field_name'];
  71. $field_type = $this->field['type'];
  72. $field_table = $this->instance['settings']['chado_table'];
  73. $field_column = $this->instance['settings']['chado_column'];
  74. $base_table = $this->instance['settings']['base_table'];
  75. // Set some defaults for the empty record.
  76. $chado_record = $entity->chado_record;
  77. $entity->{$field_name}['und'][0] = [
  78. 'value' => '',
  79. ];
  80. // Iterate through all of the _pub tables and look for any that have
  81. // linked to this record. If so then add them.
  82. $chado_tables = chado_get_table_names(TRUE);
  83. $delta = 0;
  84. foreach ($chado_tables as $chado_table) {
  85. $matches = [];
  86. if (preg_match('/^(.+?)_pub$/', $chado_table, $matches)) {
  87. $reference_table = $matches[1];
  88. // Get the schema for the pub linker table table.
  89. $schema = chado_get_schema($chado_table);
  90. // Skip tables that don't have a foreign key definition.
  91. if (!array_key_exists('foreign keys', $schema)) {
  92. continue;
  93. }
  94. // Get information about the linking table.
  95. $fkeys = $schema['foreign keys'];
  96. $fkleft = NULL;
  97. $fkright = NULL;
  98. $islinked = FALSE;
  99. foreach ($fkeys as $linked_table => $fk_details) {
  100. if ($linked_table == 'pub') {
  101. $islinked = TRUE;
  102. }
  103. if ($linked_table == $reference_table) {
  104. $fkleft = array_keys($fk_details['columns'])[0];
  105. $fkright = $fk_details['columns'][$fkleft];
  106. }
  107. }
  108. // If this table doesn't have an FK to a reference table
  109. // then it's just a table with a _pub in the name.
  110. if (!$fkleft) {
  111. continue;
  112. }
  113. // If this table does not have a FK to the pub table then
  114. // we don't want to search it.
  115. if (!$islinked) {
  116. continue;
  117. }
  118. // Build the SQL to find records assocaited with this publication.
  119. $ref_schema = chado_get_schema($reference_table);
  120. $ref_pkey = $ref_schema['primary key'][0];
  121. $select = "SELECT REF.* ";
  122. $from = "FROM {" . $chado_table . "} LINK
  123. INNER JOIN {" . $reference_table . "} REF on LINK.$fkleft = REF.$fkright
  124. ";
  125. if (array_key_exists('type_id', $ref_schema['fields'])) {
  126. $select .= ", CVT.name as type_name";
  127. $from .= "INNER JOIN {cvterm} CVT on REF.type_id = CVT.cvterm_id ";
  128. }
  129. // Get the mapping of the refrence table to a CV term in case the
  130. // records in the table don't have a type_id.
  131. $ref_mapping = db_select('chado_cvterm_mapping', 'CVM')
  132. ->fields('CVM')
  133. ->condition('chado_table', $reference_table)
  134. ->execute()
  135. ->fetchObject();
  136. $ref_type = NULL;
  137. if ($ref_mapping) {
  138. $ref_type = chado_get_cvterm(['cvterm_id' => $ref_mapping->cvterm_id]);
  139. }
  140. // Are the records in this table associated with a content type?
  141. // if so, we want to get those types so we can find the entity ID.
  142. $bundles = db_select('chado_bundle', 'CB')
  143. ->fields('CB', ['bundle_id'])
  144. ->condition('CB.data_table', $reference_table)
  145. ->execute();
  146. $entity_sql = '';
  147. while ($bundle_id = $bundles->fetchField()) {
  148. $entity_sql .= "SELECT entity_id FROM [chado_bio_data_" . $bundle_id . "] CBD" . $bundle_id . " WHERE record_id = LINK.$ref_pkey UNION ";
  149. }
  150. if (!empty($entity_sql)) {
  151. $entity_sql = rtrim($entity_sql, " UNION ");
  152. $entity_sql = ", (" . $entity_sql . " LIMIT 1) as entity_id";
  153. }
  154. // Iterate through all of the records in the linker table that
  155. // match the given pub ID.
  156. $sql = "$select $entity_sql $from WHERE LINK.pub_id = :pub_id";
  157. $args = [':pub_id' => $chado_record->pub_id];
  158. $records = chado_query($sql, $args);
  159. while($record = $records->fetchObject()) {
  160. //foreach ($records as $record) {
  161. // We want to add a 'type' and 'name' element to the values (at a
  162. // minimum) for each of the records. Unfortunately, every base table
  163. // is different and there may not be an easy to identify name,
  164. // so... we'll do the best we can.
  165. $entity->{$field_name}['und'][$delta]['value'] = [];
  166. // First get the type of record.
  167. if (property_exists($record, 'type_name')) {
  168. $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $record->type_name;
  169. }
  170. else {
  171. if ($ref_type) {
  172. $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $ref_type->name;
  173. }
  174. }
  175. // Add in the name and uniquename (identifier) if those fields exist.
  176. if (property_exists($record, 'name')) {
  177. $entity->{$field_name}['und'][$delta]['value']['schema:name'] = $record->name;
  178. }
  179. if (property_exists($record, 'uniquename')) {
  180. $entity->{$field_name}['und'][$delta]['value']['data:0842'] = $record->name;
  181. }
  182. // If this records is also a published entity then include that.
  183. if (property_exists($record, 'entity_id') and !empty($record->entity_id)) {
  184. $entity->{$field_name}['und'][$delta]['value']['entity'] = 'TripalEntity:' . $record->entity_id;
  185. }
  186. // If this is the organism table then we will create the name
  187. // specially.
  188. if (property_exists($record, 'genus')) {
  189. $name = '<i>' . $record->genus . ' ' . $record->species . '</i>';
  190. if (property_exists($record, 'infraspecific_name')) {
  191. if ($record->$fkleft->type_id) {
  192. $name .= ' ' . $record->type_name;
  193. }
  194. $name .= ' ' . $record->infraspecific_name;
  195. }
  196. $entity->{$field_name}['und'][$delta]['value']['schema:name'] = $name;
  197. $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $ref_type->name;
  198. }
  199. $delta++;
  200. }
  201. }
  202. }
  203. }
  204. }