chado_linker__contact.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. $contact_id = $contact['contact_id'];
  89. // Get the field values
  90. $contact_name = $contact['name'];
  91. $description = $contact['description'];
  92. $type = $contact['type_id'];
  93. // Add a link i there is an entity.
  94. if (array_key_exists('entity_id', $contact) and $contact['$entity_id']) {
  95. $entity_id = $contact['entity_id'];
  96. $contact_name = l($contact_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  97. }
  98. $rows[] = array($contact_name, $description, $type->name);
  99. }
  100. $table = array(
  101. 'header' => $headers,
  102. 'rows' => $rows,
  103. 'attributes' => array(
  104. 'id' => 'tripal_linker-table-contact-object',
  105. 'class' => 'tripal-data-table'
  106. ),
  107. 'sticky' => FALSE,
  108. 'caption' => "",
  109. 'colgroups' => array(),
  110. 'empty' => 'No contacts available',
  111. );
  112. $content = theme_table($table);
  113. // once we have our table array structure defined, we call Drupal's theme_table()
  114. // function to generate the table.
  115. $element[$delta] = array(
  116. '#type' => 'markup',
  117. '#markup' => $content,
  118. );
  119. }
  120. public function widget_form(&$widget, $form, $form_state, $field, $instance,
  121. $langcode, $items, $delta, $element) {
  122. }
  123. public function load($field, $entity, $details) {
  124. $record = $details['record'];
  125. $field_name = $field['field_name'];
  126. $field_type = $field['type'];
  127. $field_table = $field['settings']['chado_table'];
  128. $field_column = $field['settings']['chado_column'];
  129. // Get the FK that links to the base record.
  130. $schema = chado_get_schema($field_table);
  131. $base_table = $details['record']->tablename;
  132. $pkey = $schema['primary key'][0];
  133. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  134. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  135. $linker_table = $base_table . '_contact';
  136. $options = array(
  137. 'return_array' => 1,
  138. 'include_fk' => array(
  139. 'contact_id' => array(
  140. 'type_id' => array(
  141. 'dbxref_id' => array(
  142. 'db_id' => TRUE,
  143. ),
  144. ),
  145. ),
  146. ),
  147. );
  148. $record = chado_expand_var($record, 'table', $linker_table, $options);
  149. $contact_linkers = $record->$linker_table;
  150. if ($contact_linkers) {
  151. foreach ($contact_linkers as $i => $contact_linker) {
  152. $contact = $contact_linker->contact_id;
  153. dpm($contact);
  154. $entity->{$field_name}['und'][$i]['value'] = array(
  155. '@type' => $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession,
  156. 'type' => $contact->type_id->name,
  157. 'name' => $contact->name,
  158. 'description' => $contact->description,
  159. );
  160. $entity->$field_name['und'][$i]['contact_id'] = $contact->contact_id;
  161. if (property_exists($contact, 'entity_id')) {
  162. $entity->{$field_name}['und'][$i]['entity_id'] = $contact->entity_id;
  163. $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * @see TripalField::settings_form()
  170. */
  171. public function settings_form($field, $instance, $has_data) {
  172. $element = array();
  173. // TODO: add settings here.
  174. // Add in the semantic web fields.
  175. $parent_elements = parent::settings_form($field, $instance, $has_data);
  176. $element = array_merge($element, $parent_elements);
  177. return $element;
  178. }
  179. }