chado_linker__contact.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. class chado_linker__contact extends TripalField {
  3. public function field_info() {
  4. return array(
  5. 'label' => t('Contacts'),
  6. 'description' => t('Associates an indviddual or organization with
  7. this record.'),
  8. 'default_widget' => 'chado_linker__contact_widget',
  9. 'default_formatter' => 'chado_linker__contact_formatter',
  10. 'settings' => array(),
  11. 'storage' => array(
  12. 'type' => 'field_chado_storage',
  13. 'module' => 'tripal_chado',
  14. 'active' => TRUE
  15. ),
  16. );
  17. }
  18. function widget_info() {
  19. return array(
  20. 'label' => t('Contacts'),
  21. 'field types' => array('chado_linker__contact'),
  22. );
  23. }
  24. public function formatter_info() {
  25. return array(
  26. 'label' => t('Contacts'),
  27. 'field types' => array('chado_linker__contact'),
  28. 'settings' => array(
  29. ),
  30. );
  31. }
  32. public function attach_info($entity_type, $bundle, $settings) {
  33. $field_info = array();
  34. $table_name = $settings['data_table'];
  35. $type_table = $settings['type_table'];
  36. $type_field = $settings['field'];
  37. $cv_id = $settings['cv_id'];
  38. $cvterm_id = $settings['cvterm_id'];
  39. // If the linker table does not exists then we don't want to add attach.
  40. $contact_table = $table_name . '_contact';
  41. if (!chado_table_exists($contact_table)) {
  42. return $field_info;
  43. }
  44. $schema = chado_get_schema($contact_table);
  45. $pkey = $schema['primary key'][0];
  46. // Initialize the field array.
  47. $field_info = array(
  48. 'field_name' => $table_name . '__contact',
  49. 'field_type' => 'chado_linker__contact',
  50. 'widget_type' => 'chado_linker__contact_widget',
  51. 'widget_settings' => array('display_label' => 1),
  52. 'description' => '',
  53. 'label' => 'Contacts',
  54. 'is_required' => 0,
  55. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  56. 'storage' => 'field_chado_storage',
  57. 'field_settings' => array(
  58. 'chado_table' => $contact_table,
  59. 'chado_column' => $pkey,
  60. 'base_table' => $table_name,
  61. 'semantic_web' => array(
  62. 'name' => 'contact',
  63. 'accession' => 'contact',
  64. 'ns' => 'local',
  65. 'nsurl' => '',
  66. ),
  67. ),
  68. );
  69. return $field_info;
  70. }
  71. public function formatter_settings_summary($field, $instance, $view_mode) {
  72. }
  73. public function formatter_settings_form($field, $instance,
  74. $view_mode, $form, &$form_state) {
  75. }
  76. public function formatter_view(&$element, $entity_type, $entity,
  77. $field, $instance, $langcode, $items, $display) {
  78. // Get the settings
  79. $settings = $display['settings'];
  80. $record = $entity->chado_record;
  81. $headers = array('Name', 'Description', 'Type');
  82. $rows = array();
  83. foreach ($items as $delta => $item) {
  84. $contact = $item['value'];
  85. if (!$contact) {
  86. continue;
  87. }
  88. // Get the field values
  89. $contact_name = $contact['name'];
  90. $description = $contact['description'];
  91. $type = $contact['type'];
  92. // Add a link i there is an entity.
  93. if (array_key_exists('entity_id', $item) and $item['entity_id']) {
  94. $entity_id = $item['entity_id'];
  95. $contact_name = l($contact_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  96. }
  97. $rows[] = array($contact_name, $description, $type);
  98. }
  99. $table = array(
  100. 'header' => $headers,
  101. 'rows' => $rows,
  102. 'attributes' => array(
  103. 'id' => 'tripal_linker-table-contact-object',
  104. 'class' => 'tripal-data-table'
  105. ),
  106. 'sticky' => FALSE,
  107. 'caption' => "",
  108. 'colgroups' => array(),
  109. 'empty' => 'No contacts available',
  110. );
  111. $content = theme_table($table);
  112. // once we have our table array structure defined, we call Drupal's theme_table()
  113. // function to generate the table.
  114. $element[$delta] = array(
  115. '#type' => 'markup',
  116. '#markup' => $content,
  117. );
  118. }
  119. public function widget_form(&$widget, $form, $form_state, $field, $instance,
  120. $langcode, $items, $delta, $element) {
  121. }
  122. public function load($field, $entity, $details) {
  123. $record = $details['record'];
  124. $field_name = $field['field_name'];
  125. $field_type = $field['type'];
  126. $field_table = $field['settings']['chado_table'];
  127. $field_column = $field['settings']['chado_column'];
  128. // Get the FK that links to the base record.
  129. $schema = chado_get_schema($field_table);
  130. $base_table = $details['record']->tablename;
  131. $pkey = $schema['primary key'][0];
  132. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  133. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  134. $linker_table = $base_table . '_contact';
  135. $options = array(
  136. 'return_array' => 1,
  137. 'include_fk' => array(
  138. 'contact_id' => array(
  139. 'type_id' => array(
  140. 'dbxref_id' => array(
  141. 'db_id' => TRUE,
  142. ),
  143. ),
  144. ),
  145. ),
  146. );
  147. $record = chado_expand_var($record, 'table', $linker_table, $options);
  148. $contact_linkers = $record->$linker_table;
  149. if ($contact_linkers) {
  150. foreach ($contact_linkers as $i => $contact_linker) {
  151. $contact = $contact_linker->contact_id;
  152. $entity->{$field_name}['und'][$i]['value'] = array(
  153. '@type' => $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession,
  154. 'type' => $contact->type_id->name,
  155. 'name' => $contact->name,
  156. 'description' => $contact->description,
  157. );
  158. $entity->{$field_name}['und'][$i]['contact_id'] = $contact->contact_id;
  159. if (property_exists($contact, 'entity_id')) {
  160. $entity->{$field_name}['und'][$i]['entity_id'] = $contact->entity_id;
  161. $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
  162. }
  163. }
  164. }
  165. }
  166. /**
  167. * @see TripalField::settings_form()
  168. */
  169. public function settings_form($field, $instance, $has_data) {
  170. $element = array();
  171. // TODO: add settings here.
  172. // Add in the semantic web fields.
  173. $parent_elements = parent::settings_form($field, $instance, $has_data);
  174. $element = array_merge($element, $parent_elements);
  175. return $element;
  176. }
  177. }