chado_linker__contact_widget.inc

File

tripal_chado/includes/TripalFields/chado_linker__contact/chado_linker__contact_widget.inc
View source
  1. <?php
  2. class chado_linker__contact_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Contacts';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('chado_linker__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. // Get the FK column that links to the base table.
  20. $schema = chado_get_schema($field_table);
  21. $pkey = $schema['primary key'][0];
  22. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  23. // $fkey_lcolumn = key($schema['foreign keys']['protocol']['columns']);
  24. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  25. // Get the field defaults.
  26. $record_id = '';
  27. $fk_value = (array_key_exists('#entity', $element) and $element['#entity']) ? $element['#entity']->chado_record_id : NULL;
  28. $contact_id = '';
  29. $name = '';
  30. $value = '';
  31. $name_term = chado_get_semweb_term('contact', 'name');
  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. $name = array_key_exists($name_term, $items[$delta]['value']) ? $items[$delta]['value'][$name_term] : $name;
  36. $fk_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $fkey_lcolumn, $fk_value);
  37. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  38. $contact_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__contact_id', $contact_id);
  39. }
  40. // Check $form_state['values'] to see if an AJAX call set the values.
  41. if (array_key_exists('values', $form_state) and
  42. array_key_exists($field_name, $form_state['values'])) {
  43. $name = $form_state['values'][$field_name]['und'][$delta]['name'];
  44. }
  45. $schema = chado_get_schema('contact');
  46. $widget['value'] = array(
  47. '#type' => 'value',
  48. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  49. );
  50. $widget['chado-' . $field_table . '__' . $pkey] = array(
  51. '#type' => 'value',
  52. '#default_value' => $record_id,
  53. );
  54. $widget['chado-' . $field_table . '__' . $fkey_lcolumn] = array(
  55. '#type' => 'value',
  56. '#default_value' => $fk_value,
  57. );
  58. $widget['chado-' . $field_table . '__contact_id'] = array(
  59. '#type' => 'value',
  60. '#default_value' => $contact_id,
  61. );
  62. $widget['name'] = array(
  63. '#type' => 'textfield',
  64. '#title' => t('Contact'),
  65. '#default_value' => $name,
  66. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/contact',
  67. '#maxlength' => 100000,
  68. );
  69. }
  70. /**
  71. *
  72. * @see TripalFieldWidget::submit()
  73. */
  74. public function validate($element, $form, &$form_state, $langcode, $delta) {
  75. $field_name = $this->field['field_name'];
  76. $field_type = $this->field['type'];
  77. $base_table = $this->instance['settings']['base_table'];
  78. $field_table = $this->instance['settings']['chado_table'];
  79. $chado_column = $this->instance['settings']['chado_column'];
  80. $instance = $this->instance;
  81. // Get information about this contact linke rtable.
  82. $schema = chado_get_schema($field_table);
  83. $pkey = $schema['primary key'][0];
  84. $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
  85. // Get the name from the form state.
  86. $name = $form_state['values'][$field_name]['und'][$delta]['name'];
  87. // If the user provided a name then we want to set the foreign key
  88. // value to be the chado_record_id
  89. if ($name) {
  90. $contact = chado_generate_var('contact', array('name' => $name));
  91. if ($contact) {
  92. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__contact_id'] = $contact->contact_id;
  93. $form_state['values'][$field_name]['und'][$delta]['value'] = $name;
  94. }
  95. }
  96. // If no name is provided then we want to set the field for deletion.
  97. else {
  98. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $lfkey_field] = '';
  99. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__contact_id'] = '';
  100. }
  101. }
  102. }