chado_linker__contact.inc 6.3 KB

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