schema__publication_widget.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. $pub_id = '';
  24. $uname = '';
  25. // If the field already has a value then it will come through the $items
  26. // array. This happens when editing an existing record.
  27. if (count($items) > 0 and array_key_exists($delta, $items)) {
  28. $pub_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__pub_id', $pub_id);
  29. $uname = tripal_get_field_item_keyval($items, $delta, 'uniquename', $uname);
  30. }
  31. $schema = chado_get_schema('pub');
  32. $widget['#table_name'] = $table_name;
  33. $widget['#fkey_field'] = $fkey;
  34. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  35. $widget['#suffix'] = "</span>";
  36. $widget['value'] = array(
  37. '#type' => 'value',
  38. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  39. );
  40. $widget['chado-' . $table_name . '__' . $pkey] = array(
  41. '#type' => 'value',
  42. '#default_value' => '',
  43. );
  44. $widget['chado-' . $table_name . '__' . $fkey] = array(
  45. '#type' => 'value',
  46. '#default_value' => $fkey_value,
  47. );
  48. $widget['chado-' . $table_name . '__pub_id'] = array(
  49. '#type' => 'value',
  50. '#default_value' => $pub_id,
  51. );
  52. $widget['uniquename'] = array(
  53. '#type' => 'textfield',
  54. '#title' => t('Publication'),
  55. '#default_value' => $uname,
  56. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  57. '#ajax' => array(
  58. 'callback' => "schema__publication_widget_form_ajax_callback",
  59. 'wrapper' => "$table_name-$delta",
  60. 'effect' => 'fade',
  61. 'method' => 'replace'
  62. ),
  63. '#maxlength' => 100000,
  64. );
  65. }
  66. /**
  67. *
  68. * @see TripalFieldWidget::submit()
  69. */
  70. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  71. // Get the FK column that links to the base table.
  72. $table_name = $this->instance['settings']['chado_table'];
  73. $base_table = $this->instance['settings']['base_table'];
  74. $schema = chado_get_schema($table_name);
  75. $pkey = $schema['primary key'][0];
  76. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  77. $fkey = $fkeys[0];
  78. $field_name = $this->field['field_name'];
  79. // Get the field values.
  80. $fkey_value = isset($form_state['values'][$field_name][$langcode][$delta]['value']) ? $form_state['values'][$field_name][$langcode][$delta]['value'] : '';
  81. $pub_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] : '';
  82. $uname = isset($form_state['values'][$field_name][$langcode][$delta]['uniquename']) ? $form_state['values'][$field_name][$langcode][$delta]['uniquename'] : '';
  83. // If the user provided a uniquename then we want to set the foreign key
  84. // value to be the chado_record_id
  85. if ($uname and !$pub_id) {
  86. $pub = chado_generate_var('pub', array('uniquename' => $uname));
  87. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = $pub->pub_id;
  88. }
  89. // In the widgetFrom function we automatically add the foreign key
  90. // record. But if the user did not provide a publication we want to take
  91. // it out so that the Chado field_storage infrastructure won't try to
  92. // write a record.
  93. if (!$uname and !$pub_id) {
  94. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  95. }
  96. // If the user removed the publication from the pub_uniquename field
  97. // then we want to clear out the rest of the hidden values.
  98. // Leave the primary key so the record can be deleted.
  99. if (!$uname and $pub_id) {
  100. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  101. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = '';
  102. }
  103. }
  104. /**
  105. * @see TripalFieldWidget::theme()
  106. */
  107. function theme($element) {
  108. $layout = "
  109. <div class=\"pub-widget\">
  110. <div class=\"pub-widget-item\">" .
  111. drupal_render($element['uniquename']) . "
  112. </div>
  113. </div>
  114. ";
  115. return $layout;
  116. }
  117. }
  118. /**
  119. * An Ajax callback for the pub widget.
  120. */
  121. function schema__publication_widget_form_ajax_callback($form, $form_state) {
  122. $field_name = $form_state['triggering_element']['#parents'][0];
  123. $delta = $form_state['triggering_element']['#parents'][2];
  124. return $form[$field_name]['und'][$delta];
  125. }