sio__annotation_widget.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. class sio__annotation_widget extends ChadoFieldWidget {
  3. // The default label 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 = ['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. $field_type = $this->field['type'];
  15. $field_table = $this->instance['settings']['chado_table'];
  16. $field_column = $this->instance['settings']['chado_column'];
  17. $base_table = $this->instance['settings']['base_table'];
  18. // Get the FK that links to the base record.
  19. $schema = chado_get_schema($field_table);
  20. $pkey = $schema['primary key'][0];
  21. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  22. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  23. $vocabulary = chado_get_semweb_term('cvterm', 'cv_id');
  24. $accession = chado_get_semweb_term('dbxref', 'accession');
  25. $definition = chado_get_semweb_term('cvterm', 'definition');
  26. if (array_key_exists('is_not', $schema['fields'])) {
  27. $negation = chado_get_semweb_term($field_table, 'is_not');
  28. }
  29. // Get the field defaults.
  30. $record_id = '';
  31. $fk_value = '';
  32. $cvterm_id = '';
  33. $pub_id = '';
  34. $is_not = FALSE;
  35. $cvterm_name = '';
  36. $cv_id = '';
  37. $cvterm = NULL;
  38. $pub_name = '';
  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. // Check for element values that correspond to fields in the Chado table.
  43. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  44. $fk_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $fkey_lcolumn, $fk_value);
  45. $cvterm_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__cvterm_id', $cvterm_id);
  46. if (array_key_exists('pub_id', $schema['fields'])) {
  47. $pub_name = tripal_get_field_item_keyval($items, $delta, 'pub', $pub_name);
  48. $pub_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__pub_id', $pub_id);
  49. }
  50. if (array_key_exists('is_not', $schema['fields'])) {
  51. $is_not = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__is_not', $is_not);
  52. }
  53. if ($cvterm_id) {
  54. $cvterm = chado_generate_var('cvterm', ['cvterm_id' => $cvterm_id]);
  55. $cv_id = $cvterm->cv_id->cv_id;
  56. $cvterm_name = $cvterm->name;
  57. }
  58. }
  59. // Check $form_state['values'] to see if an AJAX call set the values.
  60. if (array_key_exists('values', $form_state) and
  61. array_key_exists($field_name, $form_state['values'])) {
  62. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  63. $fk_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey_lcolumn];
  64. $cvterm_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__cvterm_id'];
  65. if (array_key_exists('pub_id', $schema['fields'])) {
  66. $pub_name = $form_state['values'][$field_name]['und'][$delta]['pub'];
  67. $pub_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'];
  68. }
  69. if (array_key_exists('is_not', $schema['fields'])) {
  70. $is_not = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__is_not'];
  71. }
  72. $cvterm_name = $form_state['values'][$field_name]['und'][$delta]['cvterm_name'];
  73. $cv_id = $form_state['values'][$field_name]['und'][$delta]['cv_id'];
  74. $cvterm = chado_generate_var('cvterm', [
  75. 'cv_id' => $cv_id,
  76. 'name' => $cvterm_name,
  77. ]);
  78. if (!$cvterm) {
  79. $cvterm_name = '';
  80. }
  81. }
  82. $widget['#prefix'] = "<span id='$field_name-sio--annotation-$delta'>";
  83. $widget['#suffix'] = "</span>";
  84. // The value field isn't really used but it's needed because if
  85. // it doesn't have a value the element won't be considered for
  86. // insert/update.
  87. $widget['value'] = [
  88. '#type' => 'value',
  89. '#value' => $cvterm_id,
  90. ];
  91. $widget['chado-' . $field_table . '__' . $pkey] = [
  92. '#type' => 'value',
  93. '#default_value' => $record_id,
  94. ];
  95. $widget['chado-' . $field_table . '__cvterm_id'] = [
  96. '#type' => 'value',
  97. '#default_value' => $cvterm_id,
  98. ];
  99. $widget['chado-' . $field_table . '__' . $fkey_lcolumn] = [
  100. '#type' => 'value',
  101. '#default_value' => $fk_value,
  102. ];
  103. $cvs = chado_get_cv_select_options();
  104. $widget['cv_id'] = [
  105. '#type' => 'select',
  106. '#title' => t('Vocabulary'),
  107. '#options' => $cvs,
  108. '#default_value' => $cv_id,
  109. '#required' => $element['#required'],
  110. '#attributes' => ['style' => 'width: 200px;'],
  111. '#ajax' => [
  112. 'callback' => "sio__annotation_widget_form_ajax_callback",
  113. 'wrapper' => "$field_name-sio--annotation-$delta",
  114. 'effect' => 'fade',
  115. 'method' => 'replace',
  116. ],
  117. ];
  118. $cv_schema = chado_get_schema('cvterm');
  119. $widget['cvterm_name'] = [
  120. '#type' => 'textfield',
  121. '#title' => t('Term Name'),
  122. '#default_value' => $cvterm_name,
  123. '#maxlength' => array_key_exists('length', $cv_schema['fields']['name']) ? $cv_schema['fields']['name']['length'] : 255,
  124. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/cvterm/' . $cv_id,
  125. '#disabled' => $cv_id ? FALSE : TRUE,
  126. ];
  127. if (array_key_exists('pub_id', $schema['fields'])) {
  128. $widget['pub'] = [
  129. '#type' => 'textfield',
  130. '#title' => t('Publication'),
  131. '#default_value' => $pub_name,
  132. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  133. '#maxlength' => 100000,
  134. '#disabled' => $cv_id ? FALSE : TRUE,
  135. ];
  136. $widget['chado-' . $field_table . '__pub_id'] = [
  137. '#type' => 'value',
  138. '#default_value' => $pub_id ? $pub_id : '',
  139. ];
  140. }
  141. if (array_key_exists('is_not', $schema['fields'])) {
  142. $widget['chado-' . $field_table . '__is_not'] = [
  143. '#type' => 'checkbox',
  144. '#title' => t('Negate this term (NOT)'),
  145. '#default_value' => $is_not,
  146. '#required' => $element['#required'],
  147. '#disabled' => $cv_id ? FALSE : TRUE,
  148. ];
  149. }
  150. if (array_key_exists('rank', $schema['fields'])) {
  151. $widget['chado-' . $field_table . '__rank'] = [
  152. '#type' => 'value',
  153. '#default_value' => $delta,
  154. ];
  155. }
  156. }
  157. /**
  158. *
  159. * @see TripalFieldWidget::submit()
  160. */
  161. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  162. $field_name = $this->field['field_name'];
  163. $field_type = $this->field['type'];
  164. $field_table = $this->instance['settings']['chado_table'];
  165. $field_column = $this->instance['settings']['chado_column'];
  166. $base_table = $this->instance['settings']['base_table'];
  167. // Get the FK that links to the base record.
  168. $schema = chado_get_schema($field_table);
  169. $pkey = $schema['primary key'][0];
  170. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  171. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  172. $vocabulary = chado_get_semweb_term('cvterm', 'cv_id');
  173. $accession = chado_get_semweb_term('dbxref', 'accession');
  174. $definition = chado_get_semweb_term('cvterm', 'definition');
  175. if (array_key_exists('is_not', $schema['fields'])) {
  176. $negation = chado_get_semweb_term($field_table, 'is_not');
  177. }
  178. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  179. $fk_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey_lcolumn];
  180. // If a publication ID was provided then make sure the form_state
  181. // value for the pub_id is set correctly.
  182. if (array_key_exists('pub_id', $schema['fields'])) {
  183. $pub_name = $form_state['values'][$field_name]['und'][$delta]['pub'];
  184. if ($pub_name) {
  185. $pub = chado_generate_var('pub', ['uniquename' => $pub_name]);
  186. if ($pub) {
  187. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__pub_id'] = $pub->pub_id;
  188. }
  189. }
  190. // Use the NULL pub.
  191. else {
  192. $pub = chado_get_publication(['uniquename' => 'null']);
  193. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__pub_id'] = $pub->pub_id;
  194. }
  195. }
  196. // Make sure the rank is set.
  197. if (array_key_exists('rank', $schema['fields'])) {
  198. $rank = $form_state['values'][$field_name]['und'][$delta]['_weight'];
  199. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'] = $rank;
  200. }
  201. // Get the term that matches.
  202. $cvterm_name = $form_state['values'][$field_name]['und'][$delta]['cvterm_name'];
  203. $cv_id = $form_state['values'][$field_name]['und'][$delta]['cv_id'];
  204. $cvterm = chado_generate_var('cvterm', [
  205. 'cv_id' => $cv_id,
  206. 'name' => $cvterm_name,
  207. ]);
  208. if ($cvterm) {
  209. $form_state['values'][$field_name]['und'][$delta]['cvterm_name'] = '';
  210. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__cvterm_id'] = $cvterm->cvterm_id;
  211. $form_state['values'][$field_name]['und'][$delta]['value'] = $cvterm->cvterm_id;
  212. }
  213. // Remove all values so we can delete this record if there is no
  214. // cvterm.
  215. else {
  216. // There must be some value set.
  217. $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
  218. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__cvterm_id'] = '';
  219. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey_lcolumn] = '';
  220. if (array_key_exists('rank', $schema['fields'])) {
  221. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'] = '';
  222. }
  223. if (array_key_exists('pub_id', $schema['fields'])) {
  224. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'] = '';
  225. }
  226. if (array_key_exists('is_not', $schema['fields'])) {
  227. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '_is_not'] = '';
  228. }
  229. }
  230. }
  231. /**
  232. * @see TripalFieldWidget::theme()
  233. */
  234. public function theme($element) {
  235. $field_table = $this->instance['settings']['chado_table'];
  236. $layout = "
  237. <div class=\"sio--annotation-widget\">
  238. <div class=\"sio--annotation-item\">" .
  239. drupal_render($element['cv_id']) . "
  240. </div>
  241. <div class=\"sio--annotation-item\">" .
  242. drupal_render($element['cvterm_name']) . "
  243. </div>
  244. <div class=\"sio--annotation-item\">" .
  245. drupal_render($element['pub']) . "
  246. </div>
  247. <div class=\"sio--annotation-item\">" .
  248. drupal_render($element['chado-' . $field_table . '__is_not']) . "
  249. </div>
  250. </div>
  251. ";
  252. return $layout;
  253. }
  254. }
  255. /**
  256. * An Ajax callback for the tripal_chado_admin_publish_form..
  257. */
  258. function sio__annotation_widget_form_ajax_callback($form, $form_state) {
  259. $field_name = $form_state['triggering_element']['#parents'][0];
  260. $delta = $form_state['triggering_element']['#parents'][2];
  261. return $form[$field_name]['und'][$delta];
  262. }