sio__annotation_widget.inc 12 KB

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