chado_linker__contact.inc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. class chado_linker__contact extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Contacts';
  5. // The default description for this field.
  6. public static $default_description = 'Associates an indviddual or organization with
  7. this record';
  8. // Add any default settings elements. If you override the fieldSettingsForm()
  9. // or the instanceSettingsForm() functions then you need to be sure that
  10. // any settings you want those functions to manage are listed in this
  11. // array.
  12. public static $default_settings = array(
  13. 'chado_table' => '',
  14. 'chado_column' => '',
  15. 'base_table' => '',
  16. 'semantic_web' => '',
  17. );
  18. // Set this to the name of the storage backend that by default will support
  19. // this field.
  20. public static $default_storage = 'field_chado_storage';
  21. /**
  22. * @see TripalField::create_info()
  23. */
  24. function createInfo() {
  25. if (!$this->can_attach) {
  26. return;
  27. }
  28. $table_name = $this->details['chado_table'];
  29. $type_table = $this->details['chado_type_table'];
  30. $type_field = $this->details['chado_type_column'];
  31. $cv_id = $this->details['chado_cv_id'];
  32. $cvterm_id = $this->details['chado_cvterm_id'];
  33. $contact_table = $table_name . '_contact';
  34. $schema = chado_get_schema($contact_table);
  35. $pkey = $schema['primary key'][0];
  36. return array(
  37. 'field_name' => $this->field_name,
  38. 'type' => 'chado_linker__contact',
  39. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  40. 'locked' => FALSE,
  41. 'storage' => array(
  42. 'type' => 'field_chado_storage',
  43. ),
  44. 'settings' => array(
  45. 'chado_table' => $contact_table,
  46. 'chado_column' => 'contact_id',
  47. 'base_table' => $table_name,
  48. 'semantic_web' => 'local:contact'
  49. ),
  50. );
  51. }
  52. /**
  53. * @see TripalField::createInstanceInfo()
  54. */
  55. function createInstanceInfo() {
  56. if (!$this->can_attach) {
  57. return;
  58. }
  59. $table_name = $this->details['chado_table'];
  60. $type_table = $this->details['chado_type_table'];
  61. $type_field = $this->details['chado_type_column'];
  62. $cv_id = $this->details['chado_cv_id'];
  63. $cvterm_id = $this->details['chado_cvterm_id'];;
  64. $contact_table = $table_name . '_contact';
  65. return array(
  66. 'field_name' => $this->field_name,
  67. 'entity_type' => $this->entity_type,
  68. 'bundle' => $this->bundle->name,
  69. 'label' => 'Contacts',
  70. 'description' => 'An individual, organization or entity that has had
  71. some responsibility for the creation, delivery or maintenance of
  72. the associated data.',
  73. 'required' => FALSE,
  74. 'settings' => array(
  75. 'auto_attach' => FALSE,
  76. ),
  77. 'widget' => array(
  78. 'type' => 'chado_linker__contact_widget',
  79. 'settings' => array(
  80. 'display_label' => 1,
  81. ),
  82. ),
  83. 'display' => array(
  84. 'default' => array(
  85. 'label' => 'above',
  86. 'type' => 'chado_linker__contact_formatter',
  87. 'settings' => array(),
  88. ),
  89. ),
  90. );
  91. }
  92. /**
  93. * @see TripalField::widgetInfo()
  94. */
  95. public static function widgetInfo() {
  96. return array(
  97. 'chado_linker__contact_widget' => array(
  98. 'label' => t('Contacts'),
  99. 'field types' => array('chado_linker__contact'),
  100. ),
  101. );
  102. }
  103. /**
  104. * @see TripalField::formatterInfo()
  105. */
  106. public static function formatterInfo() {
  107. return array(
  108. 'chado_linker__contact_formatter' => array(
  109. 'label' => t('Contacts'),
  110. 'field types' => array('chado_linker__contact'),
  111. 'settings' => array(
  112. ),
  113. ),
  114. );
  115. }
  116. /**
  117. * @see TripalField::formatterView()
  118. */
  119. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  120. // Get the settings
  121. $settings = $display['settings'];
  122. $headers = array('Name', 'Description', 'Type');
  123. $rows = array();
  124. foreach ($items as $delta => $item) {
  125. $contact = $item['value'];
  126. if (!$contact) {
  127. continue;
  128. }
  129. // Get the field values
  130. $contact_name = $contact['name'];
  131. $description = $contact['description'];
  132. $type = $contact['type'];
  133. // Add a link i there is an entity.
  134. if (array_key_exists('entity', $item['value']) and $item['value']['entity']) {
  135. list($entity_type, $entity_id) = explode(':', $item['value']['entity']);
  136. $contact_name = l($contact_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  137. }
  138. $rows[] = array($contact_name, $description, $type);
  139. }
  140. $table = array(
  141. 'header' => $headers,
  142. 'rows' => $rows,
  143. 'attributes' => array(
  144. 'id' => 'tripal_linker-table-contact-object',
  145. 'class' => 'tripal-data-table'
  146. ),
  147. 'sticky' => FALSE,
  148. 'caption' => "",
  149. 'colgroups' => array(),
  150. 'empty' => 'No contacts available',
  151. );
  152. $content = theme_table($table);
  153. if (count($items) > 0) {
  154. // once we have our table array structure defined, we call Drupal's theme_table()
  155. // function to generate the table.
  156. $element[0] = array(
  157. '#type' => 'markup',
  158. '#markup' => $content,
  159. );
  160. }
  161. }
  162. /**
  163. * @see TripalField::widgetForm()
  164. */
  165. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  166. }
  167. /**
  168. * @see TripalField::load()
  169. */
  170. public function load($entity, $details = array()) {
  171. $record = $details['record'];
  172. $field_name = $this->field['field_name'];
  173. $field_type = $this->field['type'];
  174. $field_table = $this->field['settings']['chado_table'];
  175. $field_column = $this->field['settings']['chado_column'];
  176. // Get the FK that links to the base record.
  177. $schema = chado_get_schema($field_table);
  178. $base_table = $details['record']->tablename;
  179. $pkey = $schema['primary key'][0];
  180. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  181. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  182. // Set some defaults for the empty record.
  183. $entity->{$field_name}['und'][0] = array(
  184. 'value' => array(),
  185. $field_table . '__' . $pkey => '',
  186. $field_table . '__' . $fkey_lcolumn => '',
  187. $field_table . '__' . 'contact_id' => '',
  188. // Ignore the synonym_sgml column for now.
  189. );
  190. $linker_table = $base_table . '_contact';
  191. $options = array(
  192. 'return_array' => 1,
  193. 'include_fk' => array(
  194. 'contact_id' => array(
  195. 'type_id' => array(
  196. 'dbxref_id' => array(
  197. 'db_id' => TRUE,
  198. ),
  199. ),
  200. ),
  201. $fkey_lcolumn => TRUE,
  202. ),
  203. );
  204. $record = chado_expand_var($record, 'table', $linker_table, $options);
  205. $contact_linkers = $record->$linker_table;
  206. if ($contact_linkers) {
  207. foreach ($contact_linkers as $i => $contact_linker) {
  208. $contact = $contact_linker->contact_id;
  209. $entity->{$field_name}['und'][$i] = array(
  210. 'value' => array(
  211. 'type' => $contact->type_id->name,
  212. 'name' => $contact->name,
  213. 'description' => $contact->description,
  214. ),
  215. // Add in the semantic web settings. This array is expected by
  216. // other Tripal modules that handle semantic web for fields.
  217. 'semantic_web' => array(
  218. 'type' => $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession,
  219. 'name' => tripal_get_chado_semweb_term('contact', 'name'),
  220. 'description' => tripal_get_chado_semweb_term('contact', 'description'),
  221. ),
  222. // Add in subfield mapping to Chado tables. This is used by the
  223. // chado_field_storage for performing queries on sub element values.
  224. // It should be a comma-separated list (no spacing) of the field names
  225. // as foreign keys are followed starting from the Chado table to which
  226. // this field maps.
  227. 'chado_mapping' => array(
  228. 'type' => 'type_id,name',
  229. 'name' => 'contact_id,name',
  230. 'description' => 'contact_id,name'
  231. ),
  232. $field_table . '__' . $pkey => $contact_linker->$pkey,
  233. $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  234. $field_table . '__' . 'contact_id' => $contact->contact_id
  235. );
  236. if (property_exists($contact, 'entity_id')) {
  237. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  238. }
  239. }
  240. }
  241. }
  242. }