chado_linker__cvterm_widget.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. class chado_linker__cvterm_widget extends TripalFieldWidget {
  3. // The default lable for this field.
  4. public static $label = 'Annotations';
  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->field['settings']['chado_table'];
  25. $base_table = $this->field['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['#theme'] = 'chado_linker__cvterm_widget';
  68. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  69. $widget['#suffix'] = "</span>";
  70. $widget['value'] = array(
  71. '#type' => 'value',
  72. '#value' => key_exists($delta, $items) ? $items[$delta]['value'] : '',
  73. );
  74. $widget['chado-' . $table_name . '__' . $pkey] = array(
  75. '#type' => 'value',
  76. '#default_value' => $record_id,
  77. );
  78. $widget['cv__cv_id'] = array(
  79. '#type' => 'value',
  80. '#default_value' => $cv_id,
  81. );
  82. $widget['chado-' . $table_name . '__cvterm_id'] = array(
  83. '#type' => 'value',
  84. '#default_value' => $cvterm ? $cvterm->cvterm_id : '',
  85. );
  86. $widget['chado-' . $table_name . '__' . $fkey] = array(
  87. '#type' => 'value',
  88. '#default_value' => $fkey_value,
  89. );
  90. $widget['cvterm__name'] = array(
  91. '#type' => 'textfield',
  92. '#title' => t('Term Name'),
  93. '#default_value' => $cvterm_name,
  94. '#required' => $element['#required'],
  95. '#maxlength' => array_key_exists('length', $schema['fields']['name']) ? $schema['fields']['name']['length'] : 255,
  96. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/cvterm/' . $cv_id,
  97. '#size' => 30
  98. );
  99. $widget['pub'] = array(
  100. '#type' => 'textfield',
  101. '#title' => t('Publication'),
  102. '#default_value' => $uname,
  103. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  104. '#ajax' => array(
  105. 'callback' => "chado_linker__pub_widget_form_ajax_callback",
  106. 'wrapper' => "$table_name-$delta",
  107. 'effect' => 'fade',
  108. 'method' => 'replace'
  109. ),
  110. '#maxlength' => 100000,
  111. );
  112. $widget['chado-' . $table_name . '__pub_id'] = array(
  113. '#type' => 'value',
  114. '#default_value' => $pub_id ? $pub_id : 1,
  115. );
  116. $widget['chado-' . $table_name . '__is_not'] = array(
  117. '#type' => 'checkbox',
  118. '#title' => t('Is Not'),
  119. '#default_value' => $is_not,
  120. '#required' => $element['#required'],
  121. );
  122. $widget['cvterm__definition'] = array(
  123. '#type' => 'item',
  124. '#markup' => '',
  125. );
  126. }
  127. /**
  128. * Performs validation of the widgetForm.
  129. *
  130. * Use this validate to ensure that form values are entered correctly. Note
  131. * this is different from the validate() function which ensures that the
  132. * field data meets expectations.
  133. *
  134. * @param $form
  135. * @param $form_state
  136. */
  137. public function validate($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  138. }
  139. /**
  140. *
  141. * @see TripalFieldWidget::submit()
  142. */
  143. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  144. $field_name = $this->field['field_name'];
  145. $table_name = $this->field['settings']['chado_table'];
  146. $schema = chado_get_schema($table_name);
  147. $pkey = $schema['primary key'][0];
  148. $base_table = $this->field['settings']['base_table'];
  149. $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
  150. $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
  151. // If the form ID is field_ui_field_edit_form, then the user is editing the
  152. // field's values in the manage fields form of Drupal. We don't want
  153. // to validate it as if it were being used in a data entry form.
  154. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  155. return;
  156. }
  157. // If the user provided a cv_id and a name then we want to set the
  158. // foreign key value to be the chado_record_idd
  159. $cvterm_name = isset($form_state['values'][$field_name][$langcode][$delta]['cvterm__name']) ? $form_state['values'][$field_name][$langcode][$delta]['cvterm__name'] : '';
  160. if (!$cvterm_name) {
  161. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__cvterm_id'] = '';
  162. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $lfkey_field] = '';
  163. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = '';
  164. }
  165. }
  166. }