chado_linker__contact.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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' => array(
  74. 'name' => 'contact',
  75. 'accession' => 'contact',
  76. 'ns' => 'local',
  77. 'nsurl' => '',
  78. ),
  79. ),
  80. );
  81. return $field_info;
  82. }
  83. /**
  84. * @see TripalField::formatter_view()
  85. */
  86. public function formatter_view(&$element, $entity_type, $entity,
  87. $field, $instance, $langcode, $items, $display) {
  88. // Get the settings
  89. $settings = $display['settings'];
  90. $record = $entity->chado_record;
  91. $headers = array('Name', 'Description', 'Type');
  92. $rows = array();
  93. foreach ($items as $delta => $item) {
  94. $contact = $item['value'];
  95. if (!$contact) {
  96. continue;
  97. }
  98. // Get the field values
  99. $contact_name = $contact['name'];
  100. $description = $contact['description'];
  101. $type = $contact['type'];
  102. // Add a link i there is an entity.
  103. if (array_key_exists('entity_id', $item) and $item['entity_id']) {
  104. $entity_id = $item['entity_id'];
  105. $contact_name = l($contact_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  106. }
  107. $rows[] = array($contact_name, $description, $type);
  108. }
  109. $table = array(
  110. 'header' => $headers,
  111. 'rows' => $rows,
  112. 'attributes' => array(
  113. 'id' => 'tripal_linker-table-contact-object',
  114. 'class' => 'tripal-data-table'
  115. ),
  116. 'sticky' => FALSE,
  117. 'caption' => "",
  118. 'colgroups' => array(),
  119. 'empty' => 'No contacts available',
  120. );
  121. $content = theme_table($table);
  122. // once we have our table array structure defined, we call Drupal's theme_table()
  123. // function to generate the table.
  124. $element[$delta] = array(
  125. '#type' => 'markup',
  126. '#markup' => $content,
  127. );
  128. }
  129. /**
  130. * @see TripalField::widget_form()
  131. */
  132. public function widget_form(&$widget, $form, $form_state, $field, $instance,
  133. $langcode, $items, $delta, $element) {
  134. }
  135. /**
  136. * @see TripalField::load()
  137. */
  138. public function load($field, $entity, $details) {
  139. $record = $details['record'];
  140. $field_name = $field['field_name'];
  141. $field_type = $field['type'];
  142. $field_table = $field['settings']['chado_table'];
  143. $field_column = $field['settings']['chado_column'];
  144. // Get the FK that links to the base record.
  145. $schema = chado_get_schema($field_table);
  146. $base_table = $details['record']->tablename;
  147. $pkey = $schema['primary key'][0];
  148. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  149. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  150. // Set some defaults for the empty record.
  151. $entity->{$field_name}['und'][0] = array(
  152. 'value' => array(),
  153. $field_table . '__' . $pkey => '',
  154. $field_table . '__' . $fkey_lcolumn => '',
  155. $field_table . '__' . 'contact_id' => '',
  156. // Ignore the synonym_sgml column for now.
  157. );
  158. $linker_table = $base_table . '_contact';
  159. $options = array(
  160. 'return_array' => 1,
  161. 'include_fk' => array(
  162. 'contact_id' => array(
  163. 'type_id' => array(
  164. 'dbxref_id' => array(
  165. 'db_id' => TRUE,
  166. ),
  167. ),
  168. ),
  169. $fkey_lcolumn => TRUE,
  170. ),
  171. );
  172. $record = chado_expand_var($record, 'table', $linker_table, $options);
  173. $contact_linkers = $record->$linker_table;
  174. if ($contact_linkers) {
  175. foreach ($contact_linkers as $i => $contact_linker) {
  176. $contact = $contact_linker->contact_id;
  177. $entity->{$field_name}['und'][$i] = array(
  178. 'value' => array(
  179. '@type' => $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession,
  180. 'type' => $contact->type_id->name,
  181. 'name' => $contact->name,
  182. 'description' => $contact->description,
  183. ),
  184. $field_table . '__' . $pkey => $contact_linker->$pkey,
  185. $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  186. $field_table . '__' . 'contact_id' => $contact->contact_id
  187. );
  188. if (property_exists($contact, 'entity_id')) {
  189. $entity->{$field_name}['und'][$i]['entity_id'] = $contact->entity_id;
  190. $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
  191. }
  192. }
  193. }
  194. }
  195. }