chado_linker__prop_widget.inc

File

tripal_chado/includes/TripalFields/chado_linker__prop/chado_linker__prop_widget.inc
View source
  1. <?php
  2. class chado_linker__prop_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Chado 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. $base_table = $this->instance['settings']['base_table'];
  16. $field_table = $this->instance['settings']['chado_table'];
  17. $chado_column = $this->instance['settings']['chado_column'];
  18. $rows = array_key_exists('rows', $this->instance['settings']) ? $this->instance['settings']['rows'] : 1;
  19. $instance = $this->instance;
  20. // Get the name of the pkey field for this property table and the name
  21. // of the FK field that links to the base table.
  22. $schema = chado_get_schema($field_table);
  23. $pkey = $schema['primary key'][0];
  24. $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
  25. $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
  26. // Get the field defaults.
  27. $record_id = '';
  28. $fk_value = '';
  29. $value = '';
  30. $rank = $delta;
  31. $type_id = '';
  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) {
  35. // Check for element values that correspond to fields in the Chado table.
  36. $fk_value = tripal_get_field_item_keyval($items, 0, 'chado-' . $field_table . '__' . $lfkey_field, $fk_value);
  37. $type_id = tripal_get_field_item_keyval($items, 0, 'chado-' . $field_table . '__type_id', $type_id);
  38. if (array_key_exists($delta, $items)) {
  39. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  40. $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__value', $value);
  41. $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
  42. }
  43. }
  44. // Check $form_state['values'] to see if an AJAX call set the values.
  45. if (array_key_exists('values', $form_state) and
  46. array_key_exists($field_name, $form_state['values'])) {
  47. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  48. $fk_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $lfkey_field];
  49. $type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
  50. $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
  51. $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
  52. }
  53. // Use default value for the field if it's not already set
  54. if (!$value && isset($instance['default_value'][$delta])) {
  55. $value = $instance['default_value'][$delta]['value'];
  56. }
  57. if (!$type_id) {
  58. $vocabulary = $this->instance['settings']['term_vocabulary'];
  59. $accession = $this->instance['settings']['term_accession'];
  60. $cvterm = chado_get_cvterm(array(
  61. 'dbxref_id' => array(
  62. 'db_id' => array(
  63. 'name' => $vocabulary,
  64. ),
  65. 'accession' => $accession,
  66. ),
  67. ));
  68. if ($cvterm) {
  69. $type_id = $cvterm->cvterm_id;
  70. }
  71. }
  72. $widget['value'] = array(
  73. '#type' => 'value',
  74. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  75. );
  76. $widget['chado-' . $field_table . '__' . $pkey] = array(
  77. '#type' => 'hidden',
  78. '#default_value' => $record_id,
  79. );
  80. $widget['chado-' . $field_table . '__' . $lfkey_field] = array(
  81. '#type' => 'hidden',
  82. '#value' => $fk_value,
  83. );
  84. $widget['chado-' . $field_table . '__value'] = array(
  85. '#type' => 'textarea',
  86. '#default_value' => $value,
  87. '#title' => $instance['label'],
  88. '#description' => $instance['description'],
  89. '#rows' => $rows,
  90. '#required' => $instance['required'],
  91. );
  92. $widget['chado-' . $field_table . '__type_id'] = array(
  93. '#type' => 'hidden',
  94. '#value' => $type_id,
  95. );
  96. $widget['chado-' . $field_table . '__rank'] = array(
  97. '#type' => 'hidden',
  98. '#value' => $rank,
  99. );
  100. }
  101. /**
  102. *
  103. * @see TripalFieldWidget::submit()
  104. */
  105. public function validate($element, $form, &$form_state, $langcode, $delta) {
  106. $field_name = $this->field['field_name'];
  107. $field_type = $this->field['type'];
  108. $base_table = $this->instance['settings']['base_table'];
  109. $field_table = $this->instance['settings']['chado_table'];
  110. $chado_column = $this->instance['settings']['chado_column'];
  111. $instance = $this->instance;
  112. $schema = chado_get_schema($field_table);
  113. $pkey = $schema['primary key'][0];
  114. $base_table = $this->instance['settings']['base_table'];
  115. $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
  116. $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
  117. $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
  118. $form_state['values'][$field_name]['und'][$delta]['value'] = $value;
  119. // If the user removed the property then we want to clear out the other
  120. // fields except the pkey value. If the pkey field is present and the value
  121. // is present then the chado storage backend will delete the record.
  122. if (!$value) {
  123. $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
  124. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $lfkey_field] = '';
  125. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'] = '';
  126. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'] = '';
  127. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'] = '';
  128. }
  129. }
  130. }