sio__annotation_widget.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. class sio__annotation_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Chado Annotation';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('chado_linker__cvterm');
  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. $matches = array();
  15. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  16. // If the field name is not properly formatted then we can't tell what
  17. // table and type this is. So just return.
  18. if (count($matches) != 3) {
  19. return $widget;
  20. }
  21. $table_name = $matches[1];
  22. $cv_id = $matches[2];
  23. // Get the FK column that links to the base table.
  24. $chado_table = $this->instance['settings']['chado_table'];
  25. $base_table = $this->instance['settings']['base_table'];
  26. $schema = chado_get_schema($chado_table);
  27. $pkey = $schema['primary key'][0];
  28. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  29. $fkey = $fkeys[0];
  30. // Get the field defaults.
  31. $record_id = '';
  32. $fkey_value = $element['#entity']->chado_record_id;
  33. $cvterm_name = '';
  34. $cvterm_id = '';
  35. $pub_id = '';
  36. $uname = '';
  37. $is_not = '';
  38. $cvterm = NULL;
  39. // If the field already has a value then it will come through the $items
  40. // array. This happens when editing an existing record.
  41. if (array_key_exists($delta, $items)) {
  42. $record_id = $items[$delta]['value'];
  43. $cvterm_name = $items[$delta]['cvterm__name'];
  44. $pub_id =$items[$delta]['chado-' . $table_name . '__pub_id'];
  45. if ($pub_id && $pub_id != 1) {
  46. $pub = chado_generate_var('pub', array('pub_id' => $pub_id));
  47. $uname = $pub->uniquename;
  48. }
  49. $is_not = $items[$delta]['chado-' . $table_name . '__is_not'];
  50. $cvterm_id = $items[$delta]['chado-' . $table_name . '__cvterm_id'];
  51. }
  52. // Check $form_state['values'] to see if an AJAX call set the values.
  53. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  54. // See example in chado_linker_contact.inc
  55. // $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name);
  56. // $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  57. // $is_not = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_not');
  58. // $cvterm_name = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--cvterm__name');
  59. }
  60. if ($cvterm_name) {
  61. $cvterm = chado_generate_var('cvterm', array('cv_id' => $cv_id, 'name' => $cvterm_name));
  62. }
  63. $schema = chado_get_schema('cvterm');
  64. $options = tripal_get_cv_select_options();
  65. $widget['#table_name'] = $chado_table;
  66. $widget['#fkey_field'] = $fkey;
  67. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  68. $widget['#suffix'] = "</span>";
  69. $widget['value'] = array(
  70. '#type' => 'value',
  71. '#value' => key_exists($delta, $items) ? $items[$delta]['value'] : '',
  72. );
  73. $widget['chado-' . $table_name . '__' . $pkey] = array(
  74. '#type' => 'value',
  75. '#default_value' => $record_id,
  76. );
  77. $widget['cv__cv_id'] = array(
  78. '#type' => 'value',
  79. '#default_value' => $cv_id,
  80. );
  81. $widget['chado-' . $table_name . '__cvterm_id'] = array(
  82. '#type' => 'value',
  83. '#default_value' => $cvterm ? $cvterm->cvterm_id : '',
  84. );
  85. $widget['chado-' . $table_name . '__' . $fkey] = array(
  86. '#type' => 'value',
  87. '#default_value' => $fkey_value,
  88. );
  89. $widget['cvterm__name'] = array(
  90. '#type' => 'textfield',
  91. '#title' => t('Term Name'),
  92. '#default_value' => $cvterm_name,
  93. '#required' => $element['#required'],
  94. '#maxlength' => array_key_exists('length', $schema['fields']['name']) ? $schema['fields']['name']['length'] : 255,
  95. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/cvterm/' . $cv_id,
  96. '#size' => 30
  97. );
  98. $widget['pub'] = array(
  99. '#type' => 'textfield',
  100. '#title' => t('Publication'),
  101. '#default_value' => $uname,
  102. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  103. '#ajax' => array(
  104. 'callback' => "schema__publication_widget_form_ajax_callback",
  105. 'wrapper' => "$table_name-$delta",
  106. 'effect' => 'fade',
  107. 'method' => 'replace'
  108. ),
  109. '#maxlength' => 100000,
  110. );
  111. $widget['chado-' . $table_name . '__pub_id'] = array(
  112. '#type' => 'value',
  113. '#default_value' => $pub_id ? $pub_id : 1,
  114. );
  115. $widget['chado-' . $table_name . '__is_not'] = array(
  116. '#type' => 'checkbox',
  117. '#title' => t('Is Not'),
  118. '#default_value' => $is_not,
  119. '#required' => $element['#required'],
  120. );
  121. $widget['cvterm__definition'] = array(
  122. '#type' => 'item',
  123. '#markup' => '',
  124. );
  125. }
  126. /**
  127. *
  128. * @see TripalFieldWidget::submit()
  129. */
  130. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  131. $field_name = $this->field['field_name'];
  132. $table_name = $this->instance['settings']['chado_table'];
  133. $schema = chado_get_schema($table_name);
  134. $pkey = $schema['primary key'][0];
  135. $base_table = $this->instance['settings']['base_table'];
  136. $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
  137. $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
  138. // If the form ID is field_ui_field_edit_form, then the user is editing the
  139. // field's values in the manage fields form of Drupal. We don't want
  140. // to validate it as if it were being used in a data entry form.
  141. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  142. return;
  143. }
  144. // If the user provided a cv_id and a name then we want to set the
  145. // foreign key value to be the chado_record_idd
  146. $cvterm_name = isset($form_state['values'][$field_name][$langcode][$delta]['cvterm__name']) ? $form_state['values'][$field_name][$langcode][$delta]['cvterm__name'] : '';
  147. if (!$cvterm_name) {
  148. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__cvterm_id'] = '';
  149. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $lfkey_field] = '';
  150. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = '';
  151. }
  152. }
  153. }