sio__references.inc 6.7 KB

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