sio__annotation_widget.inc 12 KB

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