sep__protocol_widget.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @class
  4. * Purpose:
  5. *
  6. * Data:
  7. * Assumptions:
  8. */
  9. class sep__protocol_widget extends ChadoFieldWidget {
  10. // The default label for this field.
  11. public static $default_label = 'Protocol';
  12. // The list of field types for which this formatter is appropriate.
  13. public static $field_types = ['sep__protocol'];
  14. /**
  15. * @see TripalFieldWidget::form()
  16. */
  17. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  18. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  19. $settings = $this->field['settings'];
  20. $field_name = $this->field['field_name'];
  21. $field_type = $this->field['type'];
  22. $field_table = $this->instance['settings']['chado_table'];
  23. $field_column = $this->instance['settings']['chado_column'];
  24. $base_table = $this->instance['settings']['base_table'];
  25. $linker_field = 'chado-' . $field_table . '__' . $field_column;
  26. $protocol_term = chado_get_semweb_term($field_column, $field_table);
  27. $protocol_name_term = chado_get_semweb_term('protocol', 'name');
  28. $protocol_type_term = chado_get_semweb_term('protocol', 'type_id');
  29. // Set defaults for the form elements.
  30. $protocol_name = '';
  31. $protocol_id = '';
  32. // If the field already has a value then it will come through the $items
  33. // array. This happens when editing an existing record.
  34. if (count($items) > 0 and array_key_exists($delta, $items)) {
  35. $protocol_name = array_key_exists($protocol_name_term, $items[$delta]['value']) ? $items[$delta]['value'][$protocol_name_term] : $protocol_name;
  36. }
  37. // Get the list of protocols
  38. $protocols = [];
  39. $sql = "SELECT * FROM {protocol}";
  40. $results = chado_query($sql);
  41. foreach ($results as $protocol) {
  42. $protocols[$protocol->protocol_id] = $protocol->name;
  43. if ($protocol->name == $protocol_name) {
  44. $protocol_id = $protocol->protocol_id;
  45. }
  46. }
  47. $widget['value'] = array(
  48. '#type' => 'value',
  49. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  50. );
  51. $widget[$linker_field] = [
  52. '#type' => 'select',
  53. '#title' => $element['#title'],
  54. '#description' => $element['#description'],
  55. '#options' => $protocols,
  56. '#empty_option' => '- Select a Protocol -',
  57. '#required' => $element['#required'],
  58. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  59. '#delta' => $delta,
  60. '#default_value' => $protocol_id,
  61. ];
  62. }
  63. /**
  64. * @see TripalFieldWidget::validate()
  65. */
  66. public function validate($element, $form, &$form_state, $langcode, $delta) {
  67. $field_name = $this->field['field_name'];
  68. $field_table = $this->instance['settings']['chado_table'];
  69. $field_column = $this->instance['settings']['chado_column'];
  70. $linker_field = 'chado-' . $field_table . '__' . $field_column;
  71. // Make sure the value is set to the organism_id
  72. $protocol_id = $form_state['values'][$field_name]['und'][0][$linker_field];
  73. if ($protocol_id) {
  74. $form_state['values'][$field_name]['und'][0]['value'] = $protocol_id;
  75. }
  76. else {
  77. $form_state['values'][$field_name]['und'][0]['value'] = '';
  78. $form_state['values'][$field_name]['und'][0][$linker_field] = '__NULL__';
  79. }
  80. }
  81. /**
  82. * @see TripalFieldWidget::submit()
  83. */
  84. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  85. }
  86. }