chado_linker__prop_widget.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. class chado_linker__prop_widget extends TripalFieldWidget {
  3. // The default lable for this field.
  4. public static $label = 'Property';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('chado_linker__prop');
  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. $field_table = $this->field['settings']['chado_table'];
  16. $field_column = $this->field['settings']['chado_column'];
  17. $instance = $this->instance;
  18. // Get the table name and cvterm that this field maps to.
  19. $matches = array();
  20. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  21. // If the field name is not properly formatted then we can't tell what
  22. // table and type this is. So just return.
  23. if (count($matches) != 3) {
  24. return $widget;
  25. }
  26. $table_name = $matches[1];
  27. $cvterm_id = $matches[2];
  28. // Get the name of the pkey field for this property table and the name
  29. // of the FK field that links to the base table.
  30. $schema = chado_get_schema($table_name);
  31. $pkey = $schema['primary key'][0];
  32. $base_table = $this->field['settings']['base_table'];
  33. $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
  34. $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
  35. // Get the field defaults.
  36. $fk_value =key_exists(0, $items) ? $items[0]['chado-' . $field_table . '__' . $lfkey_field] : '';
  37. $propval = '';
  38. if (array_key_exists($delta, $items)) {
  39. $propval = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__value', $propval);
  40. }
  41. $widget['value'] = array(
  42. '#type' => 'value',
  43. '#value' => key_exists($delta, $items) ? $items[$delta]['value'] : '',
  44. );
  45. $widget['chado-' . $table_name . '__' . $pkey] = array(
  46. '#type' => 'hidden',
  47. '#default_value' => !empty($items[$delta]['chado-' . $field_table . '__' . $pkey]) ? $items[$delta]['chado-' . $field_table . '__' . $pkey] : '',
  48. );
  49. $widget['chado-' . $table_name . '__' . $lfkey_field] = array(
  50. '#type' => 'hidden',
  51. '#value' => $fk_value,
  52. );
  53. $widget['chado-' . $table_name . '__value'] = array(
  54. '#type' => 'textfield',
  55. '#default_value' => $propval,
  56. '#title' => $instance['label'] . ' value',
  57. '#description' => $instance['description'],
  58. );
  59. $widget['chado-' . $table_name . '__type_id'] = array(
  60. '#type' => 'hidden',
  61. '#value' => $cvterm_id,
  62. );
  63. $widget['chado-' . $table_name . '__rank'] = array(
  64. '#type' => 'hidden',
  65. '#value' => $delta,
  66. );
  67. }
  68. /**
  69. * Performs validation of the widgetForm.
  70. *
  71. * Use this validate to ensure that form values are entered correctly. Note
  72. * this is different from the validate() function which ensures that the
  73. * field data meets expectations.
  74. *
  75. * @param $form
  76. * @param $form_state
  77. */
  78. public function validate($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  79. }
  80. /**
  81. *
  82. * @see TripalFieldWidget::submit()
  83. */
  84. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  85. $field_name = $this->field['field_name'];
  86. $table_name = $this->field['settings']['chado_table'];
  87. $schema = chado_get_schema($table_name);
  88. $pkey = $schema['primary key'][0];
  89. $base_table = $this->field['settings']['base_table'];
  90. $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
  91. $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
  92. $prop_value = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__value']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__value'] : '';
  93. // If the user removed the contact from the contact_name field
  94. // then we want to clear out the rest of the hidden values.
  95. // Leave the primary key so the record can be deleted.
  96. if (!$prop_value) {
  97. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $pkey] = '';
  98. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__rank'] = '';
  99. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__type_id'] = '';
  100. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $lfkey_field] = '';
  101. }
  102. }
  103. }