schema__publication_widget.inc 5.5 KB

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