pub.inc 8.9 KB

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