chado_linker__contact.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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' => 'contact_id',
  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. $headers = array('Name', 'Description', 'Type');
  152. $rows = array();
  153. foreach ($items as $delta => $item) {
  154. $contact = $item['value'];
  155. if (!$contact) {
  156. continue;
  157. }
  158. // Get the field values
  159. $contact_name = $contact['name'];
  160. $description = $contact['description'];
  161. $type = $contact['type'];
  162. // Add a link i there is an entity.
  163. if (array_key_exists('entity', $item['value']) and $item['value']['entity']) {
  164. list($entity_type, $entity_id) = explode(':', $item['value']['entity']);
  165. $contact_name = l($contact_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  166. }
  167. $rows[] = array($contact_name, $description, $type);
  168. }
  169. $table = array(
  170. 'header' => $headers,
  171. 'rows' => $rows,
  172. 'attributes' => array(
  173. 'id' => 'tripal_linker-table-contact-object',
  174. 'class' => 'tripal-data-table'
  175. ),
  176. 'sticky' => FALSE,
  177. 'caption' => "",
  178. 'colgroups' => array(),
  179. 'empty' => 'No contacts available',
  180. );
  181. $content = theme_table($table);
  182. if (count($items) > 0) {
  183. // once we have our table array structure defined, we call Drupal's theme_table()
  184. // function to generate the table.
  185. $element[0] = array(
  186. '#type' => 'markup',
  187. '#markup' => $content,
  188. );
  189. }
  190. }
  191. /**
  192. * @see TripalField::widgetForm()
  193. */
  194. public static function widgetForm(&$widget, &$form, &$form_state, $field, $instance,
  195. $langcode, $items, $delta, $element) {
  196. }
  197. /**
  198. * @see TripalField::load()
  199. */
  200. static function load($field, $entity, $details = array()) {
  201. $record = $details['record'];
  202. $field_name = $field['field_name'];
  203. $field_type = $field['type'];
  204. $field_table = $field['settings']['chado_table'];
  205. $field_column = $field['settings']['chado_column'];
  206. // Get the FK that links to the base record.
  207. $schema = chado_get_schema($field_table);
  208. $base_table = $details['record']->tablename;
  209. $pkey = $schema['primary key'][0];
  210. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  211. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  212. // Set some defaults for the empty record.
  213. $entity->{$field_name}['und'][0] = array(
  214. 'value' => array(),
  215. $field_table . '__' . $pkey => '',
  216. $field_table . '__' . $fkey_lcolumn => '',
  217. $field_table . '__' . 'contact_id' => '',
  218. // Ignore the synonym_sgml column for now.
  219. );
  220. $linker_table = $base_table . '_contact';
  221. $options = array(
  222. 'return_array' => 1,
  223. 'include_fk' => array(
  224. 'contact_id' => array(
  225. 'type_id' => array(
  226. 'dbxref_id' => array(
  227. 'db_id' => TRUE,
  228. ),
  229. ),
  230. ),
  231. $fkey_lcolumn => TRUE,
  232. ),
  233. );
  234. $record = chado_expand_var($record, 'table', $linker_table, $options);
  235. $contact_linkers = $record->$linker_table;
  236. if ($contact_linkers) {
  237. foreach ($contact_linkers as $i => $contact_linker) {
  238. $contact = $contact_linker->contact_id;
  239. $entity->{$field_name}['und'][$i] = array(
  240. 'value' => array(
  241. 'type' => $contact->type_id->name,
  242. 'name' => $contact->name,
  243. 'description' => $contact->description,
  244. ),
  245. // Add in the semantic web settings. This array is expected by
  246. // other Tripal modules that handle semantic web for fields.
  247. 'semantic_web' => array(
  248. 'type' => $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession,
  249. 'name' => tripal_get_chado_semweb_term('contact', 'name'),
  250. 'description' => tripal_get_chado_semweb_term('contact', 'description'),
  251. ),
  252. // Add in subfield mapping to Chado tables. This is used by the
  253. // chado_field_storage for performing queries on sub element values.
  254. // It should be a comma-separated list (no spacing) of the field names
  255. // as foreign keys are followed starting from the Chado table to which
  256. // this field maps.
  257. 'chado_mapping' => array(
  258. 'type' => 'type_id,name',
  259. 'name' => 'contact_id,name',
  260. 'description' => 'contact_id,name'
  261. ),
  262. $field_table . '__' . $pkey => $contact_linker->$pkey,
  263. $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  264. $field_table . '__' . 'contact_id' => $contact->contact_id
  265. );
  266. if (property_exists($contact, 'entity_id')) {
  267. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  268. }
  269. }
  270. }
  271. }
  272. }