local__contact_widget.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. class local__contact_widget extends ChadoFieldWidget {
  3. // The default label for this field.
  4. public static $default_label = 'Contact';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = ['local_contact'];
  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. $base_table = $this->instance['settings']['base_table'];
  16. $field_table = $this->instance['settings']['chado_table'];
  17. $chado_column = $this->instance['settings']['chado_column'];
  18. $instance = $this->instance;
  19. $schema = chado_get_schema($field_table);
  20. $pkey = $schema['primary key'][0];
  21. // Get the field defaults.
  22. $record_id = '';
  23. $contact_id = '';
  24. $name = '';
  25. $value = '';
  26. $name_term = chado_get_semweb_term('contact', 'name');
  27. // Set the linker field appropriately.
  28. $linker_field = 'chado-' . $field_table . '__' . $chado_column;
  29. // If the field already has a value then it will come through the $items
  30. // array. This happens when editing an existing record.
  31. if (count($items) > 0 and array_key_exists($delta, $items)) {
  32. $name = array_key_exists($name_term, $items[$delta]['value']) ? $items[$delta]['value'][$name_term] : $name;
  33. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  34. if ($field_table == 'biomaterial') {
  35. $contact_id = tripal_get_field_item_keyval($items, $delta, $linker_field, $contact_id);
  36. }
  37. }
  38. // Check $form_state['values'] to see if an AJAX call set the values.
  39. if (array_key_exists('values', $form_state) and
  40. array_key_exists($field_name, $form_state['values'])) {
  41. $name = $form_state['values'][$field_name]['und'][$delta]['name'];
  42. }
  43. $schema = chado_get_schema('contact');
  44. $widget['value'] = [
  45. '#type' => 'value',
  46. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  47. ];
  48. $widget[$linker_field] = [
  49. '#type' => 'value',
  50. '#default_value' => $contact_id,
  51. ];
  52. $widget['name'] = [
  53. '#type' => 'textfield',
  54. '#title' => $element['#title'],
  55. '#default_value' => $name,
  56. '#required' => $element['#required'],
  57. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/contact',
  58. '#maxlength' => 100000,
  59. ];
  60. }
  61. /**
  62. *
  63. * @see TripalFieldWidget::submit()
  64. */
  65. public function validate($element, $form, &$form_state, $langcode, $delta) {
  66. $field_name = $this->field['field_name'];
  67. $field_type = $this->field['type'];
  68. $base_table = $this->instance['settings']['base_table'];
  69. $field_table = $this->instance['settings']['chado_table'];
  70. $chado_column = $this->instance['settings']['chado_column'];
  71. $instance = $this->instance;
  72. // Get information about this contact linke rtable.
  73. $schema = chado_get_schema($field_table);
  74. $pkey = $schema['primary key'][0];
  75. // Get the name from the form state.
  76. $name = $form_state['values'][$field_name]['und'][$delta]['name'];
  77. // Set the linker field appropriately.
  78. $linker_field = 'chado-' . $field_table . '__' . $chado_column;
  79. // If the user provided a name then we want to set the foreign key
  80. // value to be the chado_record_id
  81. if ($name) {
  82. $contact = chado_generate_var('contact', ['name' => $name]);
  83. if ($contact) {
  84. $form_state['values'][$field_name]['und'][$delta][$linker_field] = $contact->contact_id;
  85. $form_state['values'][$field_name]['und'][$delta]['value'] = $name;
  86. }
  87. }
  88. // If no name is provided then we want to set the field for deletion.
  89. else {
  90. $form_state['values'][$field_name]['und'][$delta][$linker_field] = '__NULL__';
  91. }
  92. }
  93. }