pub.inc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. *
  4. * @param unknown $entity_type
  5. * @param unknown $entity
  6. * @param unknown $field
  7. * @param unknown $instance
  8. * @param unknown $langcode
  9. * @param unknown $items
  10. * @param unknown $display
  11. */
  12. function tripal_chado_pub_formatter(&$element, $entity_type, $entity, $field,
  13. $instance, $langcode, $items, $display) {
  14. $list_items = array();
  15. $chado_table = $field['settings']['chado_table'];
  16. foreach ($items as $delta => $item) {
  17. if ($item[$chado_table . '__pub_id']) {
  18. $pub = chado_generate_var('pub', array('pub_id' => $item[$chado_table . '__pub_id']));
  19. $list_items[$pub->pyear] = $pub->uniquename;
  20. }
  21. }
  22. krsort($list_items, SORT_NUMERIC);
  23. $list = array(
  24. 'title' => '',
  25. 'items' => $list_items,
  26. 'type' => 'ol',
  27. 'attributes' => array(),
  28. );
  29. $element[0] = array(
  30. '#type' => 'markup',
  31. '#markup' => theme_item_list($list),
  32. );
  33. }
  34. /**
  35. *
  36. * @param unknown $field_name
  37. * @param unknown $widget
  38. * @param unknown $form
  39. * @param unknown $form_state
  40. * @param unknown $field
  41. * @param unknown $instance
  42. * @param unknown $langcode
  43. * @param unknown $items
  44. * @param unknown $delta
  45. * @param unknown $element
  46. */
  47. function tripal_chado_pub_widget(&$widget, $form, $form_state, $field,
  48. $instance, $langcode, $items, $delta, $element) {
  49. $entity = $form['#entity'];
  50. $field_name = $field['field_name'];
  51. // Get the FK column that links to the base table.
  52. $table_name = $field['settings']['chado_table'];
  53. $base_table = $field['settings']['base_table'];
  54. $schema = chado_get_schema($table_name);
  55. $pkey = $schema['primary key'][0];
  56. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  57. $fkey = $fkeys[0];
  58. // Get the field defaults.
  59. $record_id = '';
  60. $fkey_value = $element['#entity']->chado_record_id;
  61. $pub_id = '';
  62. $title = '';
  63. // If the field already has a value then it will come through the $items
  64. // array. This happens when editing an existing record.
  65. if (array_key_exists($delta, $items)) {
  66. $record_id = $items[$delta]['value'];
  67. $fkey_value = $items[$delta][$table_name . '__' . $fkey];
  68. $pub_id = $items[$delta][$table_name . '__pub_id'];
  69. $title = $items[$delta][$table_name . '--pub__uniquename'];
  70. }
  71. // Check $form_state['values'] to see if an AJAX call set the values.
  72. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  73. $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name);
  74. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  75. $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  76. $title = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__title');
  77. }
  78. $schema = chado_get_schema('pub');
  79. $widget['#table_name'] = $table_name;
  80. $widget['#fkey_field'] = $fkey;
  81. $widget['#element_validate'] = array('tripal_chado_pub_widget_validate');
  82. $widget['#theme'] = 'tripal_chado_pub_widget';
  83. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  84. $widget['#suffix'] = "</span>";
  85. $widget['value'] = array(
  86. '#type' => 'value',
  87. '#default_value' => $record_id,
  88. );
  89. $widget[$table_name . '__' . $fkey] = array(
  90. '#type' => 'value',
  91. '#default_value' => $fkey_value,
  92. );
  93. $widget[$table_name . '__pub_id'] = array(
  94. '#type' => 'value',
  95. '#default_value' => $pub_id,
  96. );
  97. $widget[$table_name . '--pub__uniquename'] = array(
  98. '#type' => 'textfield',
  99. '#title' => t('Publication ID'),
  100. '#default_value' => $title,
  101. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  102. '#ajax' => array(
  103. 'callback' => "tripal_chado_pub_widget_form_ajax_callback",
  104. 'wrapper' => "$table_name-$delta",
  105. 'effect' => 'fade',
  106. 'method' => 'replace'
  107. ),
  108. '#maxlength' => 100000,
  109. );
  110. }
  111. /**
  112. * An Ajax callback for the pub widget.
  113. */
  114. function tripal_chado_pub_widget_form_ajax_callback($form, $form_state) {
  115. $field_name = $form_state['triggering_element']['#parents'][0];
  116. $delta = $form_state['triggering_element']['#parents'][2];
  117. return $form[$field_name]['und'][$delta];
  118. }
  119. /**
  120. * Callback function for validating the tripal_chado_organism_select_widget.
  121. */
  122. function tripal_chado_pub_widget_validate($element, &$form_state) {
  123. $field_name = $element['#field_name'];
  124. $delta = $element['#delta'];
  125. $table_name = $element['#table_name'];
  126. $fkey = $element['#fkey_field'];
  127. // If the form ID is field_ui_field_edit_form, then the user is editing the
  128. // field's values in the manage fields form of Drupal. We don't want
  129. // to validate it as if it were being used in a data entry form.
  130. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  131. return;
  132. }
  133. // Get the field values.
  134. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  135. $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  136. $uname = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--pub__uniquename');
  137. // If the user provided a uniquename then we want to set the
  138. // foreign key value to be the chado_record_idd
  139. if ($uname) {
  140. // Get the pub. If one with the same name and type is already present
  141. // then use that. Otherwise, insert a new one.
  142. if (!$pub_id) {
  143. $pub = chado_generate_var('pub', array('uniquename' => $uname));
  144. // Set the pub_id and FK value
  145. tripal_chado_set_field_form_values($field_name, $form_state, $pub->pub_id, $delta, $table_name . '__pub_id');
  146. $fkey_value = $element['#entity']->chado_record_id;
  147. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  148. }
  149. }
  150. else {
  151. // If the $syn_name is not set, then remove the linker FK value to the base table.
  152. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  153. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__pub_id');
  154. }
  155. }
  156. /**
  157. * Theme function for the pub widget.
  158. *
  159. * @param $variables
  160. */
  161. function theme_tripal_chado_pub_widget($variables) {
  162. $element = $variables['element'];
  163. // These two fields were added to the widget to help identify the fields
  164. // for layout.
  165. $table_name = $element['#table_name'];
  166. $fkey = $element['#fkey_field'];
  167. $layout = "
  168. <div class=\"pub-widget\">
  169. <div class=\"pub-widget-item\">" .
  170. drupal_render($element[$table_name . '--pub__uniquename']) . "
  171. </div>
  172. </div>
  173. ";
  174. return $layout;
  175. }
  176. /**
  177. * Loads the field values with appropriate data.
  178. *
  179. * This function is called by the tripal_chado_field_storage_load() for
  180. * each property managed by the field_chado_storage storage type. This is
  181. * an optional hook function that is only needed if the field has
  182. * multiple form elements.
  183. *
  184. * @param $field
  185. * @param $entity
  186. * @param $base_table
  187. * @param $record
  188. */
  189. function tripal_chado_pub_field_load($field, $entity, $base_table, $record) {
  190. $field_name = $field['field_name'];
  191. $field_type = $field['type'];
  192. $field_table = $field['settings']['chado_table'];
  193. $field_column = $field['settings']['chado_column'];
  194. // Get the FK that links to the base record.
  195. $schema = chado_get_schema($field_table);
  196. $pkey = $schema['primary key'][0];
  197. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  198. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  199. // Set some defaults for the empty record.
  200. $entity->{$field_name}['und'][0] = array(
  201. 'value' => '',
  202. $field_table . '__' . $fkey_lcolumn => '',
  203. $field_table . '__' . 'pub_id' => '',
  204. $field_table . '--' . 'pub__uniquename' => '',
  205. );
  206. $linker_table = $base_table . '_pub';
  207. $options = array(
  208. 'return_array' => 1,
  209. );
  210. $record = chado_expand_var($record, 'table', $linker_table, $options);
  211. if (count($record->$linker_table) > 0) {
  212. $i = 0;
  213. foreach ($record->$linker_table as $index => $linker) {
  214. $pub = $linker->pub_id;
  215. $entity->{$field_name}['und'][$i] = array(
  216. 'value' => $linker->$pkey,
  217. $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
  218. $field_table . '__' . 'pub_id' => $pub->pub_id,
  219. $field_table . '--' . 'pub__uniquename' => $pub->uniquename,
  220. );
  221. $i++;
  222. }
  223. }
  224. }