schema__publication_widget.inc 5.6 KB

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