schema__publication_widget.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. class schema__publication_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Publication';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = ['schema__publication'];
  7. /**
  8. *
  9. * @see TripalFieldWidget::form()
  10. */
  11. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  12. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  13. $field_name = $this->field['field_name'];
  14. $field_type = $this->field['type'];
  15. $field_table = $this->instance['settings']['chado_table'];
  16. $field_column = $this->instance['settings']['chado_column'];
  17. $base_table = $this->instance['settings']['base_table'];
  18. // These fields are used when the publications come through a linker table.
  19. $pkey = '';
  20. $fkeys = '';
  21. $fkey = '';
  22. $pkey_val = '';
  23. $fkey_value = '';
  24. $linker_table = '';
  25. // If the field table and the base table are not the same thing then
  26. // we are going through a linker table.
  27. $pub_item_id = '';
  28. if ($field_table != $base_table) {
  29. $schema = chado_get_schema($field_table);
  30. $pkey = $schema['primary key'][0];
  31. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  32. $fkey = $fkeys[0];
  33. $linker_table = $base_table . '_pub';
  34. $pub_item_id = 'chado-' . $field_table . '__pub_id';
  35. }
  36. else {
  37. $pub_item_id = 'chado-' . $field_table . '__' . $field_column;
  38. }
  39. // Get the field defaults.
  40. $pub_id = '';
  41. $title = '';
  42. // If the field already has a value then it will come through the $items
  43. // array. This happens when editing an existing record.
  44. if (count($items) > 0 and array_key_exists($delta, $items)) {
  45. if ($linker_table) {
  46. $pkey_val = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $pkey_val);
  47. $fkey_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $fkey, $fkey_value);
  48. }
  49. $pub_id = tripal_get_field_item_keyval($items, $delta, $pub_item_id, $pub_id);
  50. if ($pub_id) {
  51. $pub = chado_get_publication(['pub_id' => $pub_id]);
  52. $title = $pub->title . ' [id:' . $pub->pub_id . ']';
  53. }
  54. }
  55. // Check $form_state['values'] to see if an AJAX call set the values.
  56. if (array_key_exists('values', $form_state) and
  57. array_key_exists($field_name, $form_state['values'])) {
  58. $title = $form_state['values'][$field_name]['und'][$delta]['pub_title'];
  59. $pub_id = $form_state['values'][$field_name]['und'][$delta]['accession'];
  60. }
  61. $widget['#table_name'] = $field_table;
  62. $widget['#fkey_field'] = $fkey;
  63. $widget['#prefix'] = "<span id='" . $field_table . "-" . $delta . "'>";
  64. $widget['#suffix'] = "</span>";
  65. $widget['value'] = [
  66. '#type' => 'value',
  67. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  68. ];
  69. if ($linker_table) {
  70. $widget['chado-' . $field_table . '__' . $pkey] = [
  71. '#type' => 'value',
  72. '#default_value' => $pkey_val,
  73. ];
  74. $widget['chado-' . $field_table . '__' . $fkey] = [
  75. '#type' => 'value',
  76. '#default_value' => $fkey_value,
  77. ];
  78. }
  79. $widget[$pub_item_id] = [
  80. '#type' => 'value',
  81. '#default_value' => $pub_id,
  82. ];
  83. $widget['pub_title'] = [
  84. '#type' => 'textfield',
  85. '#title' => t('Publication'),
  86. '#default_value' => $title,
  87. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  88. '#maxlength' => 100000,
  89. ];
  90. }
  91. /**
  92. *
  93. * @see TripalFieldWidget::submit()
  94. */
  95. public function validate($element, $form, &$form_state, $langcode, $delta) {
  96. // Get the FK column that links to the base table.
  97. $field_name = $this->field['field_name'];
  98. $field_type = $this->field['type'];
  99. $field_table = $this->instance['settings']['chado_table'];
  100. $field_column = $this->instance['settings']['chado_column'];
  101. $base_table = $this->instance['settings']['base_table'];
  102. // If the user provided a pub_title then we want to set the foreign key
  103. // value to be the chado_record_id
  104. $title = $form_state['values'][$field_name]['und'][$delta]['pub_title'];
  105. if ($title) {
  106. $matches = [];
  107. if (preg_match('/^.*\[id:(\d+)]$/', $title, $matches)) {
  108. $pub_id = $matches[1];
  109. $pub = chado_generate_var('pub', ['pub_id' => $pub_id]);
  110. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'] = $pub->pub_id;
  111. $form_state['values'][$field_name]['und'][$delta]['value'] = $pub->pub_id;
  112. }
  113. }
  114. else {
  115. $form_state['values'][$field_name]['und'][$delta]['value'] = '';
  116. // Clear out all other values if this is a linker table except the pkey. This
  117. // lets the Chado storage API know that this is an update.
  118. if ($field_table != $base_table) {
  119. $schema = chado_get_schema($field_table);
  120. $pkey = $schema['primary key'][0];
  121. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  122. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'] = '';
  123. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey_lcolumn] = '';
  124. }
  125. else {
  126. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'] = '__NULL__';
  127. }
  128. }
  129. }
  130. /**
  131. * @see TripalFieldWidget::theme()
  132. */
  133. function theme($element) {
  134. $layout = "
  135. <div class=\"pub-widget\">
  136. <div class=\"pub-widget-item\">" .
  137. drupal_render($element['pub_title']) . "
  138. </div>
  139. </div>
  140. ";
  141. return $layout;
  142. }
  143. }
  144. /**
  145. * An Ajax callback for the pub widget.
  146. */
  147. function schema__publication_widget_form_ajax_callback($form, $form_state) {
  148. $field_name = $form_state['triggering_element']['#parents'][0];
  149. $delta = $form_state['triggering_element']['#parents'][2];
  150. return $form[$field_name]['und'][$delta];
  151. }