sio__references.inc

File

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