chado_linker__contact.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. 'widget' => array(
  92. 'type' => 'chado_linker__contact_widget',
  93. 'settings' => array(
  94. 'display_label' => 1,
  95. ),
  96. ),
  97. 'display' => array(
  98. 'deafult' => array(
  99. 'label' => 'above',
  100. 'type' => 'chado_linker__contact_formatter',
  101. 'settings' => array(),
  102. ),
  103. ),
  104. );
  105. }
  106. /**
  107. * @see TripalField::widget_info()
  108. */
  109. function widget_info() {
  110. return array(
  111. 'label' => t('Contacts'),
  112. 'field types' => array('chado_linker__contact'),
  113. );
  114. }
  115. /**
  116. * @see TripalField::formatter_info()
  117. */
  118. public function formatter_info() {
  119. return array(
  120. 'label' => t('Contacts'),
  121. 'field types' => array('chado_linker__contact'),
  122. 'settings' => array(
  123. ),
  124. );
  125. }
  126. /**
  127. * @see TripalField::formatter_view()
  128. */
  129. public function formatter_view(&$element, $entity_type, $entity,
  130. $field, $instance, $langcode, $items, $display) {
  131. // Get the settings
  132. $settings = $display['settings'];
  133. $record = $entity->chado_record;
  134. $headers = array('Name', 'Description', 'Type');
  135. $rows = array();
  136. foreach ($items as $delta => $item) {
  137. $contact = $item['value'];
  138. if (!$contact) {
  139. continue;
  140. }
  141. // Get the field values
  142. $contact_name = $contact['name'];
  143. $description = $contact['description'];
  144. $type = $contact['type'];
  145. // Add a link i there is an entity.
  146. if (array_key_exists('entity_id', $item) and $item['entity_id']) {
  147. $entity_id = $item['entity_id'];
  148. $contact_name = l($contact_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  149. }
  150. $rows[] = array($contact_name, $description, $type);
  151. }
  152. $table = array(
  153. 'header' => $headers,
  154. 'rows' => $rows,
  155. 'attributes' => array(
  156. 'id' => 'tripal_linker-table-contact-object',
  157. 'class' => 'tripal-data-table'
  158. ),
  159. 'sticky' => FALSE,
  160. 'caption' => "",
  161. 'colgroups' => array(),
  162. 'empty' => 'No contacts available',
  163. );
  164. $content = theme_table($table);
  165. // once we have our table array structure defined, we call Drupal's theme_table()
  166. // function to generate the table.
  167. $element[$delta] = array(
  168. '#type' => 'markup',
  169. '#markup' => $content,
  170. );
  171. }
  172. /**
  173. * @see TripalField::widget_form()
  174. */
  175. public function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  176. $langcode, $items, $delta, $element) {
  177. }
  178. /**
  179. * @see TripalField::load()
  180. */
  181. public function load($field, $entity, $details) {
  182. $record = $details['record'];
  183. $field_name = $field['field_name'];
  184. $field_type = $field['type'];
  185. $field_table = $field['settings']['chado_table'];
  186. $field_column = $field['settings']['chado_column'];
  187. // Get the FK that links to the base record.
  188. $schema = chado_get_schema($field_table);
  189. $base_table = $details['record']->tablename;
  190. $pkey = $schema['primary key'][0];
  191. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  192. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  193. // Set some defaults for the empty record.
  194. $entity->{$field_name}['und'][0] = array(
  195. 'value' => array(),
  196. $field_table . '__' . $pkey => '',
  197. $field_table . '__' . $fkey_lcolumn => '',
  198. $field_table . '__' . 'contact_id' => '',
  199. // Ignore the synonym_sgml column for now.
  200. );
  201. $linker_table = $base_table . '_contact';
  202. $options = array(
  203. 'return_array' => 1,
  204. 'include_fk' => array(
  205. 'contact_id' => array(
  206. 'type_id' => array(
  207. 'dbxref_id' => array(
  208. 'db_id' => TRUE,
  209. ),
  210. ),
  211. ),
  212. $fkey_lcolumn => TRUE,
  213. ),
  214. );
  215. $record = chado_expand_var($record, 'table', $linker_table, $options);
  216. $contact_linkers = $record->$linker_table;
  217. if ($contact_linkers) {
  218. foreach ($contact_linkers as $i => $contact_linker) {
  219. $contact = $contact_linker->contact_id;
  220. $entity->{$field_name}['und'][$i] = array(
  221. 'value' => array(
  222. '@type' => $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession,
  223. 'type' => $contact->type_id->name,
  224. 'name' => $contact->name,
  225. 'description' => $contact->description,
  226. ),
  227. $field_table . '__' . $pkey => $contact_linker->$pkey,
  228. $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  229. $field_table . '__' . 'contact_id' => $contact->contact_id
  230. );
  231. if (property_exists($contact, 'entity_id')) {
  232. $entity->{$field_name}['und'][$i]['entity_id'] = $contact->entity_id;
  233. $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
  234. }
  235. }
  236. }
  237. }
  238. }