123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- class sio__references extends ChadoField {
-
-
-
-
-
-
-
-
- public static $default_label = 'References';
-
- public static $description = 'Records references by this publication.';
-
-
-
-
-
-
-
- public static $default_instance_settings = [
-
- 'term_vocabulary' => 'SIO',
-
- 'term_name' => 'references',
-
- 'term_accession' => '000631',
-
-
-
- 'term_fixed' => FALSE,
-
- 'chado_table' => 'pub',
-
- 'chado_column' => 'pub_id',
-
- 'base_table' => 'pub',
-
- 'items_per_page' => 10,
- ];
-
- public static $default_widget = 'sio__references_widget';
-
- public static $default_formatter = 'sio__references_formatter';
-
-
-
-
- public static $no_ui = FALSE;
-
- public function elementInfo() {
- $field_term = $this->getFieldTermID();
- return [
- $field_term => [
- 'operations' => [],
- 'sortable' => FALSE,
- 'searchable' => FALSE,
- 'type' => 'xs:string',
- 'readonly' => TRUE,
- ],
- ];
- }
-
- public function load($entity) {
- $field_name = $this->field['field_name'];
- $field_type = $this->field['type'];
- $field_table = $this->instance['settings']['chado_table'];
- $field_column = $this->instance['settings']['chado_column'];
- $base_table = $this->instance['settings']['base_table'];
-
- $chado_record = $entity->chado_record;
- $entity->{$field_name}['und'][0] = [
- 'value' => '',
- ];
-
-
- $chado_tables = chado_get_table_names(TRUE);
- $delta = 0;
- foreach ($chado_tables as $chado_table) {
- $matches = [];
- if (preg_match('/^(.+?)_pub$/', $chado_table, $matches)) {
- $reference_table = $matches[1];
-
- $schema = chado_get_schema($chado_table);
-
- if (!array_key_exists('foreign keys', $schema)) {
- continue;
- }
-
- $fkeys = $schema['foreign keys'];
- $fkleft = NULL;
- $fkright = NULL;
- $islinked = FALSE;
- foreach ($fkeys as $linked_table => $fk_details) {
- if ($linked_table == 'pub') {
- $islinked = TRUE;
- }
- if ($linked_table == $reference_table) {
- $fkleft = array_keys($fk_details['columns'])[0];
- $fkright = $fk_details['columns'][$fkleft];
- }
- }
-
-
- if (!$fkleft) {
- continue;
- }
-
-
- if (!$islinked) {
- continue;
- }
-
- $ref_schema = chado_get_schema($reference_table);
- $ref_pkey = $ref_schema['primary key'][0];
- $select = "SELECT REF.* ";
- $from = "FROM {" . $chado_table . "} LINK
- INNER JOIN {" . $reference_table . "} REF on LINK.$fkleft = REF.$fkright
- ";
- if (array_key_exists('type_id', $ref_schema['fields'])) {
- $select .= ", CVT.name as type_name";
- $from .= "INNER JOIN {cvterm} CVT on REF.type_id = CVT.cvterm_id ";
- }
-
-
- $ref_mapping = db_select('chado_cvterm_mapping', 'CVM')
- ->fields('CVM')
- ->condition('chado_table', $reference_table)
- ->execute()
- ->fetchObject();
- $ref_type = NULL;
- if ($ref_mapping) {
- $ref_type = chado_get_cvterm(['cvterm_id' => $ref_mapping->cvterm_id]);
- }
-
-
- $bundles = db_select('chado_bundle', 'CB')
- ->fields('CB', ['bundle_id'])
- ->condition('CB.data_table', $reference_table)
- ->execute();
- $entity_sql = '';
- while ($bundle_id = $bundles->fetchField()) {
- $entity_sql .= "SELECT entity_id FROM [chado_bio_data_" . $bundle_id . "] CBD" . $bundle_id . " WHERE record_id = LINK.$ref_pkey UNION ";
- }
- if (!empty($entity_sql)) {
- $entity_sql = rtrim($entity_sql, " UNION ");
- $entity_sql = ", (" . $entity_sql . " LIMIT 1) as entity_id";
- }
-
-
- $sql = "$select $entity_sql $from WHERE LINK.pub_id = :pub_id";
- $args = [':pub_id' => $chado_record->pub_id];
- $records = chado_query($sql, $args);
- while($record = $records->fetchObject()) {
-
-
-
-
-
- $entity->{$field_name}['und'][$delta]['value'] = [];
-
- if (property_exists($record, 'type_name')) {
- $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $record->type_name;
- }
- else {
- if ($ref_type) {
- $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $ref_type->name;
- }
- }
-
- if (property_exists($record, 'name')) {
- $entity->{$field_name}['und'][$delta]['value']['schema:name'] = $record->name;
- }
- if (property_exists($record, 'uniquename')) {
- $entity->{$field_name}['und'][$delta]['value']['data:0842'] = $record->name;
- }
-
- if (property_exists($record, 'entity_id') and !empty($record->entity_id)) {
- $entity->{$field_name}['und'][$delta]['value']['entity'] = 'TripalEntity:' . $record->entity_id;
- }
-
-
- if (property_exists($record, 'genus')) {
- $name = '<i>' . $record->genus . ' ' . $record->species . '</i>';
- if (property_exists($record, 'infraspecific_name')) {
- if ($record->$fkleft->type_id) {
- $name .= ' ' . $record->type_name;
- }
- $name .= ' ' . $record->infraspecific_name;
- }
- $entity->{$field_name}['und'][$delta]['value']['schema:name'] = $name;
- $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $ref_type->name;
- }
- $delta++;
- }
- }
- }
- }
- }
|