chado_linker__contact_widget.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. class chado_linker__contact_widget extends TripalFieldWidget {
  3. // The default lable for this field.
  4. public static $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. $entity = $form['#entity'];
  14. $field_name = $this->field['field_name'];
  15. // Get the FK column that links to the base table.
  16. $table_name = $this->field['settings']['chado_table'];
  17. $base_table = $this->field['settings']['base_table'];
  18. $schema = chado_get_schema($table_name);
  19. $pkey = $schema['primary key'][0];
  20. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  21. $fkey = $fkeys[0];
  22. // Get the field defaults.
  23. $record_id = '';
  24. $fkey_value = $element['#entity']->chado_record_id;
  25. $contact_id = '';
  26. $name = '';
  27. // If the field already has a value then it will come through the $items
  28. // array. This happens when editing an existing record.
  29. if (count($items) > 0 and array_key_exists($delta, $items)) {
  30. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__' . $pkey, $record_id);
  31. $contact_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__contact_id', $contact_id);
  32. if ($contact_id) {
  33. $contact = chado_generate_var('contact', array('contact_id' => $contact_id));
  34. $name = $contact->name;
  35. }
  36. }
  37. $schema = chado_get_schema('contact');
  38. $widget['#table_name'] = $table_name;
  39. $widget['#fkey_field'] = $fkey;
  40. $widget['#theme'] = 'chado_linker__contact_widget';
  41. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  42. $widget['#suffix'] = "</span>";
  43. $widget['value'] = array(
  44. '#type' => 'value',
  45. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  46. );
  47. $widget['chado-' . $table_name . '__' . $pkey] = array(
  48. '#type' => 'value',
  49. '#default_value' => $record_id,
  50. );
  51. $widget['chado-' . $table_name . '__' . $fkey] = array(
  52. '#type' => 'value',
  53. '#default_value' => $fkey_value,
  54. );
  55. $widget['chado-' . $table_name . '__contact_id'] = array(
  56. '#type' => 'value',
  57. '#default_value' => $contact_id,
  58. );
  59. $widget['name'] = array(
  60. '#type' => 'textfield',
  61. '#title' => t('Contact'),
  62. '#default_value' => $name,
  63. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/contact',
  64. '#ajax' => array(
  65. 'callback' => "chado_linker__contact_widget_form_ajax_callback",
  66. 'wrapper' => "$table_name-$delta",
  67. 'effect' => 'fade',
  68. 'method' => 'replace'
  69. ),
  70. '#maxlength' => 100000,
  71. );
  72. }
  73. /**
  74. * Performs validation of the widgetForm.
  75. *
  76. * Use this validate to ensure that form values are entered correctly. Note
  77. * this is different from the validate() function which ensures that the
  78. * field data meets expectations.
  79. *
  80. * @param $form
  81. * @param $form_state
  82. */
  83. public function validate($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  84. }
  85. /**
  86. *
  87. * @see TripalFieldWidget::submit()
  88. */
  89. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  90. // Get the FK column that links to the base table.
  91. $table_name = $this->field['settings']['chado_table'];
  92. $base_table = $this->field['settings']['base_table'];
  93. $schema = chado_get_schema($table_name);
  94. $pkey = $schema['primary key'][0];
  95. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  96. $fkey = $fkeys[0];
  97. $field_name = $this->field['field_name'];
  98. // Get the field values.
  99. $fkey_value = isset($form_state['values'][$field_name][$langcode][$delta]['value']) ? $form_state['values'][$field_name][$langcode][$delta]['value'] : '';
  100. $contact_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] : '';
  101. $name = isset($form_state['values'][$field_name][$langcode][$delta]['name']) ? $form_state['values'][$field_name][$langcode][$delta]['name'] : '';
  102. // If the user provided a name then we want to set the foreign key
  103. // value to be the chado_record_id
  104. if ($name and !$contact_id) {
  105. $contact = chado_generate_var('contact', array('name' => $name));
  106. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] = $contact->contact_id;
  107. }
  108. // In the widgetForm function we automatically add the foreign key
  109. // record. But if the user did not provide a contact we want to take
  110. // it out so that the Chado field_storage infrastructure won't try to
  111. // write a record.
  112. if (!$name and !$contact_id) {
  113. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  114. }
  115. // If the user removed the contact from the contact_name field
  116. // then we want to clear out the rest of the hidden values.
  117. // Leave the primary key so the record can be deleted.
  118. if (!$name and $contact_id) {
  119. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  120. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] = '';
  121. }
  122. }
  123. }