chado_linker__contact.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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::can_attach()
  23. */
  24. protected function can_attach($entity_type, $bundle, $details) {
  25. $table_name = $details['chado_table'];
  26. $type_table = $details['chado_type_table'];
  27. $type_field = $details['chado_type_column'];
  28. $cv_id = $details['chado_cv_id'];
  29. $cvterm_id = $details['chado_cvterm_id'];;
  30. // If the linker table does not exists then we don't want to add attach.
  31. $contact_table = $table_name . '_contact';
  32. if (chado_table_exists($contact_table)) {
  33. return TRUE;
  34. }
  35. return FALSE;
  36. }
  37. /**
  38. * @see TripalField::create_info()
  39. */
  40. function create_info($entity_type, $bundle, $details) {
  41. if (!$this->can_attach($entity_type, $bundle, $details)) {
  42. return;
  43. }
  44. $table_name = $details['chado_table'];
  45. $type_table = $details['chado_type_table'];
  46. $type_field = $details['chado_type_column'];
  47. $cv_id = $details['chado_cv_id'];
  48. $cvterm_id = $details['chado_cvterm_id'];
  49. $contact_table = $table_name . '_contact';
  50. $schema = chado_get_schema($contact_table);
  51. $pkey = $schema['primary key'][0];
  52. return array(
  53. 'field_name' => $contact_table,
  54. 'type' => 'chado_linker__contact',
  55. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  56. 'locked' => FALSE,
  57. 'storage' => array(
  58. 'type' => 'field_chado_storage',
  59. ),
  60. 'settings' => array(
  61. 'chado_table' => $contact_table,
  62. 'chado_column' => $pkey,
  63. 'base_table' => $table_name,
  64. 'semantic_web' => 'local:contact'
  65. ),
  66. );
  67. }
  68. /**
  69. * @see TripalField::create_instance_info()
  70. */
  71. function create_instance_info($entity_type, $bundle, $details) {
  72. if (!$this->can_attach($entity_type, $bundle, $details)) {
  73. return;
  74. }
  75. $table_name = $details['chado_table'];
  76. $type_table = $details['chado_type_table'];
  77. $type_field = $details['chado_type_column'];
  78. $cv_id = $details['chado_cv_id'];
  79. $cvterm_id = $details['chado_cvterm_id'];;
  80. $contact_table = $table_name . '_contact';
  81. return array(
  82. 'field_name' => $contact_table,
  83. 'entity_type' => $entity_type,
  84. 'bundle' => $bundle->name,
  85. 'label' => 'Contacts',
  86. 'description' => 'An individual, organization or entity that has had
  87. some responsibility for the creation, delivery or maintenance of
  88. the associated data.',
  89. 'required' => FALSE,
  90. 'settings' => array(
  91. 'auto_attach' => FALSE,
  92. ),
  93. 'widget' => array(
  94. 'type' => 'chado_linker__contact_widget',
  95. 'settings' => array(
  96. 'display_label' => 1,
  97. ),
  98. ),
  99. 'display' => array(
  100. 'deafult' => array(
  101. 'label' => 'above',
  102. 'type' => 'chado_linker__contact_formatter',
  103. 'settings' => array(),
  104. ),
  105. ),
  106. );
  107. }
  108. /**
  109. * @see TripalField::widget_info()
  110. */
  111. function widget_info() {
  112. return array(
  113. 'label' => t('Contacts'),
  114. 'field types' => array('chado_linker__contact'),
  115. );
  116. }
  117. /**
  118. * @see TripalField::formatter_info()
  119. */
  120. public function formatter_info() {
  121. return array(
  122. 'label' => t('Contacts'),
  123. 'field types' => array('chado_linker__contact'),
  124. 'settings' => array(
  125. ),
  126. );
  127. }
  128. /**
  129. * @see TripalField::formatter_view()
  130. */
  131. public function formatter_view(&$element, $entity_type, $entity,
  132. $field, $instance, $langcode, $items, $display) {
  133. // Get the settings
  134. $settings = $display['settings'];
  135. $record = $entity->chado_record;
  136. $headers = array('Name', 'Description', 'Type');
  137. $rows = array();
  138. foreach ($items as $delta => $item) {
  139. $contact = $item['value'];
  140. if (!$contact) {
  141. continue;
  142. }
  143. // Get the field values
  144. $contact_name = $contact['name'];
  145. $description = $contact['description'];
  146. $type = $contact['type'];
  147. // Add a link i there is an entity.
  148. if (array_key_exists('entity_id', $item) and $item['entity_id']) {
  149. $entity_id = $item['entity_id'];
  150. $contact_name = l($contact_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  151. }
  152. $rows[] = array($contact_name, $description, $type);
  153. }
  154. $table = array(
  155. 'header' => $headers,
  156. 'rows' => $rows,
  157. 'attributes' => array(
  158. 'id' => 'tripal_linker-table-contact-object',
  159. 'class' => 'tripal-data-table'
  160. ),
  161. 'sticky' => FALSE,
  162. 'caption' => "",
  163. 'colgroups' => array(),
  164. 'empty' => 'No contacts available',
  165. );
  166. $content = theme_table($table);
  167. if (count($items) > 0) {
  168. // once we have our table array structure defined, we call Drupal's theme_table()
  169. // function to generate the table.
  170. $element[0] = array(
  171. '#type' => 'markup',
  172. '#markup' => $content,
  173. );
  174. }
  175. }
  176. /**
  177. * @see TripalField::widget_form()
  178. */
  179. public function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  180. $langcode, $items, $delta, $element) {
  181. }
  182. /**
  183. * @see TripalField::load()
  184. */
  185. public function load($field, $entity, $details) {
  186. $record = $details['record'];
  187. $field_name = $field['field_name'];
  188. $field_type = $field['type'];
  189. $field_table = $field['settings']['chado_table'];
  190. $field_column = $field['settings']['chado_column'];
  191. // Get the FK that links to the base record.
  192. $schema = chado_get_schema($field_table);
  193. $base_table = $details['record']->tablename;
  194. $pkey = $schema['primary key'][0];
  195. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  196. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  197. // Set some defaults for the empty record.
  198. $entity->{$field_name}['und'][0] = array(
  199. 'value' => array(),
  200. $field_table . '__' . $pkey => '',
  201. $field_table . '__' . $fkey_lcolumn => '',
  202. $field_table . '__' . 'contact_id' => '',
  203. // Ignore the synonym_sgml column for now.
  204. );
  205. $linker_table = $base_table . '_contact';
  206. $options = array(
  207. 'return_array' => 1,
  208. 'include_fk' => array(
  209. 'contact_id' => array(
  210. 'type_id' => array(
  211. 'dbxref_id' => array(
  212. 'db_id' => TRUE,
  213. ),
  214. ),
  215. ),
  216. $fkey_lcolumn => TRUE,
  217. ),
  218. );
  219. $record = chado_expand_var($record, 'table', $linker_table, $options);
  220. $contact_linkers = $record->$linker_table;
  221. if ($contact_linkers) {
  222. foreach ($contact_linkers as $i => $contact_linker) {
  223. $contact = $contact_linker->contact_id;
  224. $entity->{$field_name}['und'][$i] = array(
  225. 'value' => array(
  226. '@type' => $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession,
  227. 'type' => $contact->type_id->name,
  228. 'name' => $contact->name,
  229. 'description' => $contact->description,
  230. ),
  231. $field_table . '__' . $pkey => $contact_linker->$pkey,
  232. $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  233. $field_table . '__' . 'contact_id' => $contact->contact_id
  234. );
  235. if (property_exists($contact, 'entity_id')) {
  236. $entity->{$field_name}['und'][$i]['entity_id'] = $contact->entity_id;
  237. $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
  238. }
  239. }
  240. }
  241. }
  242. }