chado_linker__contact.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 globalSettingsForm()
  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. );
  17. // Set this to the name of the storage backend that by default will support
  18. // this field.
  19. public static $default_storage = 'field_chado_storage';
  20. /**
  21. * @see TripalField::formatterView()
  22. */
  23. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  24. // Get the settings
  25. $settings = $display['settings'];
  26. $headers = array('Name', 'Description', 'Type');
  27. $rows = array();
  28. foreach ($items as $delta => $item) {
  29. $contact = $item['value'];
  30. if (!$contact) {
  31. continue;
  32. }
  33. // Get the field values
  34. $contact_name = $contact['name'];
  35. $description = $contact['description'];
  36. $type = $contact['type'];
  37. // Add a link i there is an entity.
  38. if (array_key_exists('entity', $item['value']) and $item['value']['entity']) {
  39. list($entity_type, $entity_id) = explode(':', $item['value']['entity']);
  40. $contact_name = l($contact_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  41. }
  42. $rows[] = array($contact_name, $description, $type);
  43. }
  44. $table = array(
  45. 'header' => $headers,
  46. 'rows' => $rows,
  47. 'attributes' => array(
  48. 'id' => 'tripal_linker-table-contact-object',
  49. 'class' => 'tripal-data-table'
  50. ),
  51. 'sticky' => FALSE,
  52. 'caption' => "",
  53. 'colgroups' => array(),
  54. 'empty' => 'No contacts available',
  55. );
  56. $content = theme_table($table);
  57. if (count($items) > 0) {
  58. // once we have our table array structure defined, we call Drupal's theme_table()
  59. // function to generate the table.
  60. $element[0] = array(
  61. '#type' => 'markup',
  62. '#markup' => $content,
  63. );
  64. }
  65. }
  66. /**
  67. * @see TripalField::widgetForm()
  68. */
  69. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  70. parent::widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
  71. $entity = $form['#entity'];
  72. $field_name = $this->field['field_name'];
  73. // Get the FK column that links to the base table.
  74. $table_name = $this->instance['settings']['chado_table'];
  75. $base_table = $this->instance['settings']['base_table'];
  76. $schema = chado_get_schema($table_name);
  77. $pkey = $schema['primary key'][0];
  78. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  79. $fkey = $fkeys[0];
  80. // Get the field defaults.
  81. $record_id = '';
  82. $fkey_value = $element['#entity']->chado_record_id;
  83. $contact_id = '';
  84. $name = '';
  85. // If the field already has a value then it will come through the $items
  86. // array. This happens when editing an existing record.
  87. if (count($items) > 0 and array_key_exists($delta, $items)) {
  88. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__' . $pkey, $record_id);
  89. $contact_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__contact_id', $contact_id);
  90. if ($contact_id) {
  91. $contact = chado_generate_var('contact', array('contact_id' => $contact_id));
  92. $name = $contact->name;
  93. }
  94. }
  95. $schema = chado_get_schema('contact');
  96. $widget['#table_name'] = $table_name;
  97. $widget['#fkey_field'] = $fkey;
  98. $widget['#theme'] = 'chado_linker__contact_widget';
  99. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  100. $widget['#suffix'] = "</span>";
  101. $widget['value'] = array(
  102. '#type' => 'value',
  103. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  104. );
  105. $widget['chado-' . $table_name . '__' . $pkey] = array(
  106. '#type' => 'value',
  107. '#default_value' => $record_id,
  108. );
  109. $widget['chado-' . $table_name . '__' . $fkey] = array(
  110. '#type' => 'value',
  111. '#default_value' => $fkey_value,
  112. );
  113. $widget['chado-' . $table_name . '__contact_id'] = array(
  114. '#type' => 'value',
  115. '#default_value' => $contact_id,
  116. );
  117. $widget['name'] = array(
  118. '#type' => 'textfield',
  119. '#title' => t('Contact'),
  120. '#default_value' => $name,
  121. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/contact',
  122. '#ajax' => array(
  123. 'callback' => "chado_linker__contact_widget_form_ajax_callback",
  124. 'wrapper' => "$table_name-$delta",
  125. 'effect' => 'fade',
  126. 'method' => 'replace'
  127. ),
  128. '#maxlength' => 100000,
  129. );
  130. }
  131. /**
  132. * @see TripalField::widgetFormSubmit()
  133. */
  134. public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  135. // Get the FK column that links to the base table.
  136. $table_name = $this->instance['settings']['chado_table'];
  137. $base_table = $this->instance['settings']['base_table'];
  138. $schema = chado_get_schema($table_name);
  139. $pkey = $schema['primary key'][0];
  140. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  141. $fkey = $fkeys[0];
  142. $field_name = $this->field['field_name'];
  143. // Get the field values.
  144. $fkey_value = isset($form_state['values'][$field_name][$langcode][$delta]['value']) ? $form_state['values'][$field_name][$langcode][$delta]['value'] : '';
  145. $contact_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] : '';
  146. $name = isset($form_state['values'][$field_name][$langcode][$delta]['name']) ? $form_state['values'][$field_name][$langcode][$delta]['name'] : '';
  147. // If the user provided a name then we want to set the foreign key
  148. // value to be the chado_record_id
  149. if ($name and !$contact_id) {
  150. $contact = chado_generate_var('contact', array('name' => $name));
  151. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] = $contact->contact_id;
  152. }
  153. // In the widgetForm function we automatically add the foreign key
  154. // record. But if the user did not provide a contact we want to take
  155. // it out so that the Chado field_storage infrastructure won't try to
  156. // write a record.
  157. if (!$name and !$contact_id) {
  158. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  159. }
  160. // If the user removed the contact from the contact_name field
  161. // then we want to clear out the rest of the hidden values.
  162. // Leave the primary key so the record can be deleted.
  163. if (!$name and $contact_id) {
  164. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  165. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] = '';
  166. }
  167. }
  168. /**
  169. * @see TripalField::load()
  170. */
  171. public function load($entity, $details = array()) {
  172. $record = $details['record'];
  173. $field_name = $this->field['field_name'];
  174. $field_type = $this->field['type'];
  175. $field_table = $this->instance['settings']['chado_table'];
  176. $field_column = $this->instance['settings']['chado_column'];
  177. // Get the FK that links to the base record.
  178. $schema = chado_get_schema($field_table);
  179. $base_table = $details['record']->tablename;
  180. $pkey = $schema['primary key'][0];
  181. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  182. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  183. // Set some defaults for the empty record.
  184. $entity->{$field_name}['und'][0] = array(
  185. 'value' => array(),
  186. 'chado-' . $field_table . '__' . $pkey => '',
  187. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  188. 'chado-' . $field_table . '__' . 'contact_id' => '',
  189. // Ignore the synonym_sgml column for now.
  190. );
  191. $linker_table = $base_table . '_contact';
  192. $options = array(
  193. 'return_array' => 1,
  194. 'include_fk' => array(
  195. 'contact_id' => array(
  196. 'type_id' => array(
  197. 'dbxref_id' => array(
  198. 'db_id' => TRUE,
  199. ),
  200. ),
  201. ),
  202. $fkey_lcolumn => TRUE,
  203. ),
  204. );
  205. $record = chado_expand_var($record, 'table', $linker_table, $options);
  206. $contact_linkers = $record->$linker_table;
  207. if ($contact_linkers) {
  208. foreach ($contact_linkers as $i => $contact_linker) {
  209. $contact = $contact_linker->contact_id;
  210. $entity->{$field_name}['und'][$i] = array(
  211. 'value' => array(
  212. 'type' => $contact->type_id ? $contact->type_id->name : '',
  213. 'name' => $contact->name,
  214. 'description' => $contact->description,
  215. ),
  216. // Add in the semantic web settings. This array is expected by
  217. // other Tripal modules that handle semantic web for fields.
  218. 'semantic_web' => array(
  219. 'type' => $contact->type_id ? $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession : '',
  220. 'name' => tripal_get_chado_semweb_term('contact', 'name'),
  221. 'description' => tripal_get_chado_semweb_term('contact', 'description'),
  222. ),
  223. // Add in subfield mapping to Chado tables. This is used by the
  224. // chado_field_storage for performing queries on sub element values.
  225. // It should be a comma-separated list (no spacing) of the field names
  226. // as foreign keys are followed starting from the Chado table to which
  227. // this field maps.
  228. 'chado_mapping' => array(
  229. 'type' => 'type_id,name',
  230. 'name' => 'contact_id,name',
  231. 'description' => 'contact_id,name'
  232. ),
  233. 'chado-' . $field_table . '__' . $pkey => $contact_linker->$pkey,
  234. 'chado-' . $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  235. 'chado-' . $field_table . '__' . 'contact_id' => $contact->contact_id
  236. );
  237. if (property_exists($contact, 'entity_id')) {
  238. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  239. }
  240. }
  241. }
  242. }
  243. }
  244. /**
  245. * An Ajax callback for the pub widget.
  246. */
  247. function chado_linker__contact_widget_form_ajax_callback($form, $form_state) {
  248. $field_name = $form_state['triggering_element']['#parents'][0];
  249. $delta = $form_state['triggering_element']['#parents'][2];
  250. return $form[$field_name]['und'][$delta];
  251. }
  252. /**
  253. * Theme function for the pub widget.
  254. *
  255. * @param $variables
  256. */
  257. function theme_chado_linker__contact_widget($variables) {
  258. $element = $variables['element'];
  259. // These two fields were added to the widget to help identify the fields
  260. // for layout.
  261. $table_name = $element['#table_name'];
  262. $fkey = $element['#fkey_field'];
  263. $layout = "
  264. <div class=\"pub-widget\">
  265. <div class=\"pub-widget-item\">" .
  266. drupal_render($element['name']) . "
  267. </div>
  268. </div>
  269. ";
  270. return $layout;
  271. }