schema__publication_widget.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. $field_type = $this->field['type'];
  15. $field_table = $this->instance['settings']['chado_table'];
  16. $field_column = $this->instance['settings']['chado_column'];
  17. $base_table = $this->instance['settings']['base_table'];
  18. // These fields are used when the publications come through a linker table.
  19. $pkey = '';
  20. $fkeys = '';
  21. $fkey = '';
  22. $pkey_val = '';
  23. $fkey_value = '';
  24. $linker_table = '';
  25. // If the field table and the base table are not the same thing then
  26. // we are going through a linker table.
  27. $pub_item_id = '';
  28. if ($field_table != $base_table) {
  29. $schema = chado_get_schema($field_table);
  30. $pkey = $schema['primary key'][0];
  31. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  32. $fkey = $fkeys[0];
  33. $linker_table = $base_table . '_pub';
  34. $pub_item_id = 'chado-' . $field_table . '__pub_id';
  35. }
  36. else {
  37. $pub_item_id = 'chado-' . $field_table . '__' . $field_column;
  38. }
  39. // Get the field defaults.
  40. $pub_id = '';
  41. $title = '';
  42. // If the field already has a value then it will come through the $items
  43. // array. This happens when editing an existing record.
  44. if (count($items) > 0 and array_key_exists($delta, $items)) {
  45. if ($linker_table) {
  46. $pkey_val = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $pkey_val);
  47. $fkey_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $fkey, $fkey_value);
  48. }
  49. $pub_id = tripal_get_field_item_keyval($items, $delta, $pub_item_id, $pub_id);
  50. if ($pub_id) {
  51. $pub = chado_get_publication(array('pub_id' => $pub_id));
  52. $title = $pub->title . ' [id:' . $pub->pub_id . ']';
  53. }
  54. }
  55. // Check $form_state['values'] to see if an AJAX call set the values.
  56. if (array_key_exists('values', $form_state) and
  57. array_key_exists($field_name, $form_state['values'])) {
  58. $title = $form_state['values'][$field_name]['und'][$delta]['pub_title'];
  59. $pub_id = $form_state['values'][$field_name]['und'][$delta]['accession'];
  60. }
  61. $widget['#table_name'] = $field_table;
  62. $widget['#fkey_field'] = $fkey;
  63. $widget['#prefix'] = "<span id='" . $field_table . "-" . $delta . "'>";
  64. $widget['#suffix'] = "</span>";
  65. $widget['value'] = array(
  66. '#type' => 'value',
  67. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  68. );
  69. if ($linker_table) {
  70. $widget['chado-' . $field_table . '__' . $pkey] = array(
  71. '#type' => 'value',
  72. '#default_value' => $pkey_val,
  73. );
  74. $widget['chado-' . $field_table . '__' . $fkey] = array(
  75. '#type' => 'value',
  76. '#default_value' => $fkey_value,
  77. );
  78. }
  79. $widget[$pub_item_id] = array(
  80. '#type' => 'value',
  81. '#default_value' => $pub_id,
  82. );
  83. $widget['pub_title'] = array(
  84. '#type' => 'textfield',
  85. '#title' => t('Publication'),
  86. '#default_value' => $title,
  87. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  88. '#maxlength' => 100000,
  89. );
  90. }
  91. /**
  92. *
  93. * @see TripalFieldWidget::submit()
  94. */
  95. public function validate($element, $form, &$form_state, $langcode, $delta) {
  96. // Get the FK column that links to the base table.
  97. $field_name = $this->field['field_name'];
  98. $field_type = $this->field['type'];
  99. $field_table = $this->instance['settings']['chado_table'];
  100. $field_column = $this->instance['settings']['chado_column'];
  101. $base_table = $this->instance['settings']['base_table'];
  102. if ($field_table != $base_table) {
  103. $schema = chado_get_schema($field_table);
  104. $pkey = $schema['primary key'][0];
  105. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  106. $fkey = $fkeys[0];
  107. $pub_item_id = 'chado-' . $field_table . '__pub_id';
  108. // Get the field values.
  109. $fkey_value = $form_state['values'][$field_name]['und'][$delta]['value'];
  110. }
  111. else {
  112. $pub_item_id = 'chado-' . $field_table . '__' . $field_column;
  113. }
  114. $pub_id = $form_state['values'][$field_name]['und'][$delta][$pub_item_id];
  115. $title = $form_state['values'][$field_name]['und'][$delta]['pub_title'];
  116. // If the user provided a pub_title then we want to set the foreign key
  117. // value to be the chado_record_id
  118. if ($title) {
  119. $matches = array();
  120. if (preg_match('/^.*\[id:(\d+)]$/', $title, $matches)) {
  121. $pub_id = $matches[1];
  122. $pub = chado_generate_var('pub', array('pub_id' => $pub_id));
  123. $form_state['values'][$field_name]['und'][$delta][$pub_item_id] = $pub->pub_id;
  124. $form_state['values'][$field_name]['und'][$delta]['value'] = $pub->pub_id;
  125. }
  126. }
  127. // In the widgetFrom function we automatically add the foreign key
  128. // record. But if the user did not provide a publication we want to take
  129. // it out so that the Chado field_storage infrastructure won't try to
  130. // write a record.
  131. if (!$title) {
  132. $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
  133. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey] = '';
  134. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'] = '';
  135. }
  136. }
  137. /**
  138. * @see TripalFieldWidget::theme()
  139. */
  140. function theme($element) {
  141. $layout = "
  142. <div class=\"pub-widget\">
  143. <div class=\"pub-widget-item\">" .
  144. drupal_render($element['pub_title']) . "
  145. </div>
  146. </div>
  147. ";
  148. return $layout;
  149. }
  150. }
  151. /**
  152. * An Ajax callback for the pub widget.
  153. */
  154. function schema__publication_widget_form_ajax_callback($form, $form_state) {
  155. $field_name = $form_state['triggering_element']['#parents'][0];
  156. $delta = $form_state['triggering_element']['#parents'][2];
  157. return $form[$field_name]['und'][$delta];
  158. }