schema__publication_widget.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 = array('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. // Get the FK column that links to the base table.
  15. $table_name = $this->instance['settings']['chado_table'];
  16. $base_table = $this->instance['settings']['base_table'];
  17. $schema = chado_get_schema($table_name);
  18. $pkey = $schema['primary key'][0];
  19. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  20. $fkey = $fkeys[0];
  21. // Get the field defaults.
  22. $fkey_value = (array_key_exists('#entity', $element) and is_object($element['#entity'])) ? $element['#entity']->chado_record_id : NULL;
  23. $pkey_value = '';
  24. $pub_id = '';
  25. $uname = '';
  26. // If the field already has a value then it will come through the $items
  27. // array. This happens when editing an existing record.
  28. if (count($items) > 0 and array_key_exists($delta, $items)) {
  29. $pkey_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__' . $pkey, $pkey_value);
  30. $pub_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__pub_id', $pub_id);
  31. $uname = tripal_get_field_item_keyval($items, $delta, 'uniquename', $uname);
  32. }
  33. // We also need to take into account the case where a field is changed by a user.
  34. // In this case the info is in the form state.
  35. if (isset($form_state['values']) AND !empty($form_state['values'])) {
  36. // If the user entered a new publication then we only have the uniquename...
  37. // so we need to query the db for the rest of the information.
  38. if (empty($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'])) {
  39. $uname = $form_state['values'][$field_name][$langcode][$delta]['uniquename'];
  40. $pub_id = chado_query('SELECT pub_id FROM {pub} WHERE uniquename=:uniquename',
  41. array(':uniquename' => $uname))->fetchField();
  42. }
  43. // If the user changed an existing publication...
  44. // then we have to update the pub_id and reset the pkey.
  45. // We can tell if this element was changed by comparing it to the triggering element.
  46. $current_value = $form_state['values'][$field_name][$langcode][$delta]['value'];
  47. $triggering_value = $form_state['triggering_element']['#value'];
  48. if ($triggering_value == $current_value) {
  49. $uname = $form_state['values'][$field_name][$langcode][$delta]['uniquename'];
  50. $pub_id = chado_query('SELECT pub_id FROM {pub} WHERE uniquename=:uniquename',
  51. array(':uniquename' => $uname))->fetchField();
  52. }
  53. }
  54. $schema = chado_get_schema('pub');
  55. $widget['#table_name'] = $table_name;
  56. $widget['#fkey_field'] = $fkey;
  57. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  58. $widget['#suffix'] = "</span>";
  59. $widget['value'] = array(
  60. '#type' => 'value',
  61. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  62. );
  63. $widget['chado-' . $table_name . '__' . $pkey] = array(
  64. '#type' => 'hidden',
  65. '#value' => $pkey_value,
  66. );
  67. $widget['chado-' . $table_name . '__' . $fkey] = array(
  68. '#type' => 'hidden',
  69. '#value' => $fkey_value,
  70. );
  71. $widget['chado-' . $table_name . '__pub_id'] = array(
  72. '#type' => 'hidden',
  73. '#value' => $pub_id,
  74. );
  75. $widget['uniquename'] = array(
  76. '#type' => 'textfield',
  77. '#title' => t('Publication'),
  78. '#default_value' => $uname,
  79. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  80. '#ajax' => array(
  81. 'callback' => "schema__publication_widget_form_ajax_callback",
  82. 'wrapper' => "$table_name-$delta",
  83. 'effect' => 'fade',
  84. 'method' => 'replace'
  85. ),
  86. '#maxlength' => 100000,
  87. );
  88. }
  89. /**
  90. *
  91. * @see TripalFieldWidget::validate()
  92. */
  93. public function validate($element, $form, &$form_state, $langcode, $delta) {
  94. $field_name = $this->field['field_name'];
  95. $field_type = $this->field['type'];
  96. $base_table = $this->instance['settings']['base_table'];
  97. $field_table = $this->instance['settings']['chado_table'];
  98. $chado_column = $this->instance['settings']['chado_column'];
  99. $instance = $this->instance;
  100. $schema = chado_get_schema($field_table);
  101. $pkey = $schema['primary key'][0];
  102. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  103. $fkey = $fkeys[0];
  104. $value = $form_state['values'][$field_name]['und'][$delta]['uniquename'];
  105. $form_state['values'][$field_name]['und'][$delta]['value'] = $value;
  106. // If the user removed the publication then we want to clear out the other
  107. // fields except the pkey value. If the pkey field is present and the value
  108. // is present then the chado storage backend will delete the record.
  109. if (!$value) {
  110. $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
  111. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey] = '';
  112. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'] = '';
  113. }
  114. }
  115. /**
  116. * @see TripalFieldWidget::theme()
  117. */
  118. function theme($element) {
  119. $layout = "
  120. <div class=\"pub-widget\">
  121. <div class=\"pub-widget-item\">" .
  122. drupal_render($element['uniquename']) . "
  123. </div>
  124. </div>
  125. ";
  126. return $layout;
  127. }
  128. }
  129. /**
  130. * An Ajax callback for the pub widget.
  131. */
  132. function schema__publication_widget_form_ajax_callback($form, $form_state) {
  133. $field_name = $form_state['triggering_element']['#parents'][0];
  134. $delta = $form_state['triggering_element']['#parents'][2];
  135. return $form[$field_name]['und'][$delta];
  136. }