chado_linker__contact.inc 8.6 KB

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