chado_linker__contact.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. class chado_linker__contact extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Contacts';
  5. // The default description for this field.
  6. public static $default_description = 'Associates an indviddual or organization with
  7. this record';
  8. // Add any default settings elements. If you override the globalSettingsForm()
  9. // or the instanceSettingsForm() functions then you need to be sure that
  10. // any settings you want those functions to manage are listed in this
  11. // array.
  12. public static $default_settings = array(
  13. 'chado_table' => '',
  14. 'chado_column' => '',
  15. 'base_table' => '',
  16. 'semantic_web' => '',
  17. );
  18. // Set this to the name of the storage backend that by default will support
  19. // this field.
  20. public static $default_storage = 'field_chado_storage';
  21. /**
  22. * @see TripalField::formatterView()
  23. */
  24. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  25. // Get the settings
  26. $settings = $display['settings'];
  27. $headers = array('Name', 'Description', 'Type');
  28. $rows = array();
  29. foreach ($items as $delta => $item) {
  30. $contact = $item['value'];
  31. if (!$contact) {
  32. continue;
  33. }
  34. // Get the field values
  35. $contact_name = $contact['name'];
  36. $description = $contact['description'];
  37. $type = $contact['type'];
  38. // Add a link i there is an entity.
  39. if (array_key_exists('entity', $item['value']) and $item['value']['entity']) {
  40. list($entity_type, $entity_id) = explode(':', $item['value']['entity']);
  41. $contact_name = l($contact_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  42. }
  43. $rows[] = array($contact_name, $description, $type);
  44. }
  45. $table = array(
  46. 'header' => $headers,
  47. 'rows' => $rows,
  48. 'attributes' => array(
  49. 'id' => 'tripal_linker-table-contact-object',
  50. 'class' => 'tripal-data-table'
  51. ),
  52. 'sticky' => FALSE,
  53. 'caption' => "",
  54. 'colgroups' => array(),
  55. 'empty' => 'No contacts available',
  56. );
  57. $content = theme_table($table);
  58. if (count($items) > 0) {
  59. // once we have our table array structure defined, we call Drupal's theme_table()
  60. // function to generate the table.
  61. $element[0] = array(
  62. '#type' => 'markup',
  63. '#markup' => $content,
  64. );
  65. }
  66. }
  67. /**
  68. * @see TripalField::widgetForm()
  69. */
  70. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  71. }
  72. /**
  73. * @see TripalField::load()
  74. */
  75. public function load($entity, $details = array()) {
  76. $record = $details['record'];
  77. $field_name = $this->field['field_name'];
  78. $field_type = $this->field['type'];
  79. $field_table = $this->field['settings']['chado_table'];
  80. $field_column = $this->field['settings']['chado_column'];
  81. // Get the FK that links to the base record.
  82. $schema = chado_get_schema($field_table);
  83. $base_table = $details['record']->tablename;
  84. $pkey = $schema['primary key'][0];
  85. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  86. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  87. // Set some defaults for the empty record.
  88. $entity->{$field_name}['und'][0] = array(
  89. 'value' => array(),
  90. $field_table . '__' . $pkey => '',
  91. $field_table . '__' . $fkey_lcolumn => '',
  92. $field_table . '__' . 'contact_id' => '',
  93. // Ignore the synonym_sgml column for now.
  94. );
  95. $linker_table = $base_table . '_contact';
  96. $options = array(
  97. 'return_array' => 1,
  98. 'include_fk' => array(
  99. 'contact_id' => array(
  100. 'type_id' => array(
  101. 'dbxref_id' => array(
  102. 'db_id' => TRUE,
  103. ),
  104. ),
  105. ),
  106. $fkey_lcolumn => TRUE,
  107. ),
  108. );
  109. $record = chado_expand_var($record, 'table', $linker_table, $options);
  110. $contact_linkers = $record->$linker_table;
  111. if ($contact_linkers) {
  112. foreach ($contact_linkers as $i => $contact_linker) {
  113. $contact = $contact_linker->contact_id;
  114. $entity->{$field_name}['und'][$i] = array(
  115. 'value' => array(
  116. 'type' => $contact->type_id->name,
  117. 'name' => $contact->name,
  118. 'description' => $contact->description,
  119. ),
  120. // Add in the semantic web settings. This array is expected by
  121. // other Tripal modules that handle semantic web for fields.
  122. 'semantic_web' => array(
  123. 'type' => $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession,
  124. 'name' => tripal_get_chado_semweb_term('contact', 'name'),
  125. 'description' => tripal_get_chado_semweb_term('contact', 'description'),
  126. ),
  127. // Add in subfield mapping to Chado tables. This is used by the
  128. // chado_field_storage for performing queries on sub element values.
  129. // It should be a comma-separated list (no spacing) of the field names
  130. // as foreign keys are followed starting from the Chado table to which
  131. // this field maps.
  132. 'chado_mapping' => array(
  133. 'type' => 'type_id,name',
  134. 'name' => 'contact_id,name',
  135. 'description' => 'contact_id,name'
  136. ),
  137. $field_table . '__' . $pkey => $contact_linker->$pkey,
  138. $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  139. $field_table . '__' . 'contact_id' => $contact->contact_id
  140. );
  141. if (property_exists($contact, 'entity_id')) {
  142. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  143. }
  144. }
  145. }
  146. }
  147. }