sio__references.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. protected 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. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  63. 'sortable' => TRUE,
  64. ),
  65. 'schema:name' => array(
  66. 'searchable' => TRUE,
  67. 'name' => 'name',
  68. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  69. 'sortable' => TRUE,
  70. ),
  71. 'data:0842'=> array(
  72. 'searchable' => TRUE,
  73. 'name' => 'identifier',
  74. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  75. 'sortable' => TRUE,
  76. ),
  77. 'entity'=> array(
  78. 'searchable' => FALSE,
  79. 'sortable' => FALSE,
  80. ),
  81. ),
  82. ),
  83. );
  84. }
  85. /**
  86. *
  87. * @see TripalField::load()
  88. */
  89. public function load($entity) {
  90. $field_name = $this->field['field_name'];
  91. $field_type = $this->field['type'];
  92. $field_table = $this->instance['settings']['chado_table'];
  93. $field_column = $this->instance['settings']['chado_column'];
  94. $base_table = $this->instance['settings']['base_table'];
  95. // Set some defaults for the empty record.
  96. $chado_record = $entity->chado_record;
  97. $entity->{$field_name}['und'][0] = array(
  98. 'value' => '',
  99. );
  100. // Iterate through all of the _pub tables and look for any that have
  101. // linked to this record. If so then add them.
  102. $chado_tables = chado_get_table_names(TRUE);
  103. $delta = 0;
  104. foreach ($chado_tables as $chado_table) {
  105. $matches = array();
  106. if (preg_match('/^(.+?)_pub$/', $chado_table, $matches)) {
  107. $reference_table = $matches[1];
  108. // Find the base table this links to and get the fk columns that map it.
  109. $schema = chado_get_schema($chado_table);
  110. $fkeys = $schema['foreign keys'];
  111. foreach ($fkeys as $linked_table => $fk_details) {
  112. if ($linked_table == $reference_table) {
  113. $fkleft = array_keys($fk_details['columns'])[0];
  114. $fkright = $fk_details['columns'][$fkleft];
  115. }
  116. }
  117. // Iterate through all of the records in the linker table that
  118. // match the given pub ID.
  119. $records = chado_generate_var($chado_table, array('pub_id' => $chado_record->pub_id), array('return_array' => TRUE));
  120. foreach ($records as $record) {
  121. // We want to add a 'type' and 'name' element to the values (at a
  122. // minimum) for each of the records. Unfortunately, every base table
  123. // is different and their may not be an easy to identify name,
  124. // so... we'll do the best we can.
  125. $entity->{$field_name}['und'][$delta]['value'] = array();
  126. // First get the type of record.
  127. if (property_exists($record->$fkleft, 'type_id') and $record->$fkleft->type_id) {
  128. $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $record->$fkleft->type_id->name;
  129. }
  130. else {
  131. // If there's not a type_id column then see if the table is mapped
  132. // to a type.
  133. $mapping = db_select('chado_cvterm_mapping', 'CVM')
  134. ->fields('CVM')
  135. ->condition('chado_table', $reference_table)
  136. ->execute()
  137. ->fetchObject();
  138. if ($mapping) {
  139. $cvterm = tripal_get_cvterm(array('cvterm_id' => $mapping->cvterm_id));
  140. $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $cvterm->name;
  141. }
  142. }
  143. // Add in the name and uniquename (identifier) if those fields exist.
  144. if (property_exists($record->$fkleft, 'name')) {
  145. $entity->{$field_name}['und'][$delta]['value']['schema:name'] = $record->$fkleft->name;
  146. }
  147. if (property_exists($record->$fkleft, 'uniquename')) {
  148. $entity->{$field_name}['und'][$delta]['value']['data:0842'] = $record->$fkleft->name;
  149. }
  150. // If this records is also a published entity then include that.
  151. if (property_exists($record->$fkleft, 'entity_id')) {
  152. $entity->{$field_name}['und'][$delta]['value']['entity'] = 'TripalEntity:' . $record->$fkleft->entity_id;
  153. }
  154. // If this is the organism table then we will create the name
  155. // specially.
  156. if (property_exists($record->$fkleft, 'genus')) {
  157. $name = '<i>' . $record->$fkleft->genus . ' ' . $record->$fkleft->species . '</i>';
  158. if (property_exists($record->$fkleft, 'infraspecific_name')) {
  159. if ($record->$fkleft->type_id) {
  160. $name .= ' ' . $record->$fkleft->type_id->name;
  161. }
  162. $name .= ' ' . $record->$fkleft->infraspecific_name;
  163. }
  164. $entity->{$field_name}['und'][$delta]['value']['schema:name'] = $name;
  165. }
  166. $delta++;
  167. }
  168. }
  169. }
  170. }
  171. }