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