local__source_data_widget.inc

File

tripal_chado/includes/TripalFields/local__source_data/local__source_data_widget.inc
View source
  1. <?php
  2. class local__source_data_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Data Source';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('local__source_data');
  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. $settings = $this->field['settings'];
  14. $field_name = $this->field['field_name'];
  15. $field_type = $this->field['type'];
  16. $field_table = $this->instance['settings']['chado_table'];
  17. $field_column = $this->instance['settings']['chado_column'];
  18. // Get the field defaults.
  19. $sourcename = '';
  20. $sourceversion = '';
  21. $sourceuri = '';
  22. // Set defaults based on incoming item values.
  23. if (count($items) > 0 and array_key_exists('value', $items[0])) {
  24. $sourcename = $items[0]['chado-analysis__sourcename'];
  25. $sourceversion = $items[0]['chado-analysis__sourceversion'];
  26. $sourceuri = $items[0]['chado-analysis__sourceuri'];
  27. }
  28. // Set defaults based on incoming form state (happens on a failed form
  29. // submit).
  30. if (array_key_exists('values', $form_state) and
  31. array_key_exists($field_name, $form_state['values'])) {
  32. $sourcename = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourcename'];
  33. $sourceversion = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourceversion'];
  34. $sourceuri = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourceuri'];
  35. }
  36. $widget['value'] = array(
  37. '#type' => 'value',
  38. '#value' => $sourcename,
  39. );
  40. $widget['chado-analysis__sourcename'] = array(
  41. '#type' => 'value',
  42. '#value' => $sourcename,
  43. );
  44. $widget['chado-analysis__sourceversion'] = array(
  45. '#type' => 'value',
  46. '#value' => $sourceversion,
  47. );
  48. $widget['chado-analysis__sourceuri'] = array(
  49. '#type' => 'value',
  50. '#value' => $sourceuri,
  51. );
  52. $widget['source_data'] = array(
  53. '#type' => 'fieldset',
  54. '#title' => $this->instance['label'],
  55. '#description' => $this->instance['description'],
  56. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  57. '#delta' => $delta,
  58. );
  59. $widget['source_data']['sourcename'] = array(
  60. '#type' => 'textfield',
  61. '#title' => 'Data Source Name',
  62. '#description' => 'The name of the source where data was obtained for this analysis.',
  63. '#default_value' => $sourcename,
  64. '#required' => $element['#required'],
  65. );
  66. $widget['source_data']['sourceversion'] = array(
  67. '#type' => 'textfield',
  68. '#title' => 'Data Source Version',
  69. '#description' => 'The version number of the data source (if applicable).',
  70. '#default_value' => $sourceversion,
  71. );
  72. $widget['source_data']['sourceuri'] = array(
  73. '#type' => 'textfield',
  74. '#title' => 'Data Source URI',
  75. '#description' => 'The URI (e.g. web URL) where the source data can be retreived.',
  76. '#default_value' => $sourceuri,
  77. );
  78. }
  79. public function validate($element, $form, &$form_state, $langcode, $delta) {
  80. $field_name = $this->field['field_name'];
  81. $field_type = $this->field['type'];
  82. $table_name = $this->instance['settings']['chado_table'];
  83. $field_table = $this->instance['settings']['chado_table'];
  84. $field_column = $this->instance['settings']['chado_column'];
  85. $base_table = $this->instance['settings']['base_table'];
  86. $source_name = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourcename'];
  87. $sourceversion = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourceversion'];
  88. $sourceuri = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourceuri'];
  89. // Set the value so the form element won't be ignored.
  90. $form_state['values'][$field_name]['und'][$delta]['value'] = $source_name;
  91. if (!$sourceversion) {
  92. $sourceversion = '__NULL__';
  93. }
  94. if (!$sourceuri) {
  95. $sourceuri = '__NULL__';
  96. }
  97. // And set the values for our Chado insert/update
  98. $form_state['values'][$field_name]['und'][$delta]['chado-analysis__sourcename'] = $source_name;
  99. $form_state['values'][$field_name]['und'][$delta]['chado-analysis__sourceversion'] = $sourceversion;
  100. $form_state['values'][$field_name]['und'][$delta]['chado-analysis__sourceuri'] = $sourceuri;
  101. }
  102. }