local__contact_widget.inc

File

tripal_chado/includes/TripalFields/local__contact/local__contact_widget.inc
View source
  1. <?php
  2. class local__contact_widget extends ChadoFieldWidget {
  3. // The default lable 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 = array('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. if ($field_table == 'biomaterial') {
  29. $linker_field = 'chado-biomaterial__biosourceprovider_id';
  30. }
  31. else {
  32. $linker_field = 'chado-' . $field_table . '__contact_id';
  33. }
  34. // If the field already has a value then it will come through the $items
  35. // array. This happens when editing an existing record.
  36. if (count($items) > 0 and array_key_exists($delta, $items)) {
  37. $name = array_key_exists($name_term, $items[$delta]['value']) ? $items[$delta]['value'][$name_term] : $name;
  38. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  39. if ($field_table == 'biomaterial') {
  40. $contact_id = tripal_get_field_item_keyval($items, $delta, $linker_field, $contact_id);
  41. }
  42. }
  43. // Check $form_state['values'] to see if an AJAX call set the values.
  44. if (array_key_exists('values', $form_state) and
  45. array_key_exists($field_name, $form_state['values'])) {
  46. $name = $form_state['values'][$field_name]['und'][$delta]['name'];
  47. }
  48. $schema = chado_get_schema('contact');
  49. $widget['value'] = array(
  50. '#type' => 'value',
  51. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  52. );
  53. $widget['chado-' . $field_table . '__' . $pkey] = array(
  54. '#type' => 'value',
  55. '#default_value' => $record_id,
  56. );
  57. $widget[$linker_field] = array(
  58. '#type' => 'value',
  59. '#default_value' => $contact_id,
  60. );
  61. $widget['name'] = array(
  62. '#type' => 'textfield',
  63. '#title' => t('Contact'),
  64. '#default_value' => $name,
  65. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/contact',
  66. '#maxlength' => 100000,
  67. );
  68. }
  69. /**
  70. *
  71. * @see TripalFieldWidget::submit()
  72. */
  73. public function validate($element, $form, &$form_state, $langcode, $delta) {
  74. $field_name = $this->field['field_name'];
  75. $field_type = $this->field['type'];
  76. $base_table = $this->instance['settings']['base_table'];
  77. $field_table = $this->instance['settings']['chado_table'];
  78. $chado_column = $this->instance['settings']['chado_column'];
  79. $instance = $this->instance;
  80. // Get information about this contact linke rtable.
  81. $schema = chado_get_schema($field_table);
  82. $pkey = $schema['primary key'][0];
  83. // Get the name from the form state.
  84. $name = $form_state['values'][$field_name]['und'][$delta]['name'];
  85. // Set the linker field appropriately.
  86. if ($field_table == 'biomaterial') {
  87. $linker_field = 'chado-biomaterial__biosourceprovider_id';
  88. }
  89. else {
  90. $linker_field = 'chado-' . $field_table . '__contact_id';
  91. }
  92. // If the user provided a name then we want to set the foreign key
  93. // value to be the chado_record_id
  94. if ($name) {
  95. $contact = chado_generate_var('contact', array('name' => $name));
  96. if ($contact) {
  97. $form_state['values'][$field_name]['und'][$delta][$linker_field] = $contact->contact_id;
  98. $form_state['values'][$field_name]['und'][$delta]['value'] = $name;
  99. }
  100. }
  101. // If no name is provided then we want to set the field for deletion.
  102. else {
  103. $form_state['values'][$field_name]['und'][$delta][$linker_field] = '';
  104. }
  105. }
  106. }