schema__publication_widget.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. class schema__publication_widget extends TripalFieldWidget {
  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->field['settings']['chado_table'];
  17. $base_table = $this->field['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['#theme'] = 'schema__publication_widget';
  38. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  39. $widget['#suffix'] = "</span>";
  40. $widget['value'] = array(
  41. '#type' => 'value',
  42. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  43. );
  44. $widget['chado-' . $table_name . '__' . $pkey] = array(
  45. '#type' => 'value',
  46. '#default_value' => $record_id,
  47. );
  48. $widget['chado-' . $table_name . '__' . $fkey] = array(
  49. '#type' => 'value',
  50. '#default_value' => $fkey_value,
  51. );
  52. $widget['chado-' . $table_name . '__pub_id'] = array(
  53. '#type' => 'value',
  54. '#default_value' => $pub_id,
  55. );
  56. $widget['uniquename'] = array(
  57. '#type' => 'textfield',
  58. '#title' => t('Publication'),
  59. '#default_value' => $uname,
  60. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  61. '#ajax' => array(
  62. 'callback' => "schema__publication_widget_form_ajax_callback",
  63. 'wrapper' => "$table_name-$delta",
  64. 'effect' => 'fade',
  65. 'method' => 'replace'
  66. ),
  67. '#maxlength' => 100000,
  68. );
  69. }
  70. /**
  71. * Performs validation of the widgetForm.
  72. *
  73. * Use this validate to ensure that form values are entered correctly. Note
  74. * this is different from the validate() function which ensures that the
  75. * field data meets expectations.
  76. *
  77. * @param $form
  78. * @param $form_state
  79. */
  80. public function validate($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  81. }
  82. /**
  83. *
  84. * @see TripalFieldWidget::submit()
  85. */
  86. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  87. // Get the FK column that links to the base table.
  88. $table_name = $this->field['settings']['chado_table'];
  89. $base_table = $this->field['settings']['base_table'];
  90. $schema = chado_get_schema($table_name);
  91. $pkey = $schema['primary key'][0];
  92. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  93. $fkey = $fkeys[0];
  94. $field_name = $this->field['field_name'];
  95. // Get the field values.
  96. $fkey_value = isset($form_state['values'][$field_name][$langcode][$delta]['value']) ? $form_state['values'][$field_name][$langcode][$delta]['value'] : '';
  97. $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'] : '';
  98. $uname = isset($form_state['values'][$field_name][$langcode][$delta]['uniquename']) ? $form_state['values'][$field_name][$langcode][$delta]['uniquename'] : '';
  99. // If the user provided a uniquename then we want to set the foreign key
  100. // value to be the chado_record_id
  101. if ($uname and !$pub_id) {
  102. $pub = chado_generate_var('pub', array('uniquename' => $uname));
  103. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = $pub->pub_id;
  104. }
  105. // In the widgetFrom function we automatically add the foreign key
  106. // record. But if the user did not provide a publication we want to take
  107. // it out so that the Chado field_storage infrastructure won't try to
  108. // write a record.
  109. if (!$uname and !$pub_id) {
  110. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  111. }
  112. // If the user removed the publication from the pub_uniquename field
  113. // then we want to clear out the rest of the hidden values.
  114. // Leave the primary key so the record can be deleted.
  115. if (!$uname and $pub_id) {
  116. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  117. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = '';
  118. }
  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. }
  129. /**
  130. * Theme function for the pub widget.
  131. *
  132. * @param $variables
  133. */
  134. function theme_schema__publication_widget($variables) {
  135. $element = $variables['element'];
  136. // These two fields were added to the widget to help identify the fields
  137. // for layout.
  138. $table_name = $element['#table_name'];
  139. $fkey = $element['#fkey_field'];
  140. $layout = "
  141. <div class=\"pub-widget\">
  142. <div class=\"pub-widget-item\">" .
  143. drupal_render($element['uniquename']) . "
  144. </div>
  145. </div>
  146. ";
  147. return $layout;
  148. }