sio__annotation_widget.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 = 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', 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. }
  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'] = array(
  88. '#type' => 'value',
  89. '#value' => $cvterm_id,
  90. );
  91. $widget['chado-' . $field_table . '__' . $pkey] = array(
  92. '#type' => 'value',
  93. '#default_value' => $record_id,
  94. );
  95. $widget['chado-' . $field_table . '__cvterm_id'] = array(
  96. '#type' => 'value',
  97. '#default_value' => $cvterm_id,
  98. );
  99. $widget['chado-' . $field_table . '__' . $fkey_lcolumn] = array(
  100. '#type' => 'value',
  101. '#default_value' => $fk_value,
  102. );
  103. tripal_get_term_lookup_form($widget, $form_state, $cvterm_name,
  104. $element['#title'], $element['#description'], $element['#required'],
  105. $field_name, $delta);
  106. if (array_key_exists('pub_id', $schema['fields'])) {
  107. $widget['pub'] = array(
  108. '#type' => 'textfield',
  109. '#title' => t('Publication'),
  110. '#default_value' => $pub_name,
  111. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  112. '#maxlength' => 100000,
  113. '#disabled' => $cv_id ? FALSE : TRUE,
  114. );
  115. $widget['chado-' . $field_table . '__pub_id'] = array(
  116. '#type' => 'value',
  117. '#default_value' => $pub_id ? $pub_id : '',
  118. );
  119. }
  120. if (array_key_exists('is_not', $schema['fields'])) {
  121. $widget['chado-' . $field_table . '__is_not'] = array(
  122. '#type' => 'checkbox',
  123. '#title' => t('Negate this term (NOT)'),
  124. '#default_value' => $is_not,
  125. '#required' => $element['#required'],
  126. '#disabled' => $cv_id ? FALSE : TRUE,
  127. );
  128. }
  129. if (array_key_exists('rank', $schema['fields'])) {
  130. $widget['chado-' . $field_table . '__rank'] = array(
  131. '#type' => 'value',
  132. '#default_value' => $delta,
  133. );
  134. }
  135. }
  136. /**
  137. *
  138. * @see TripalFieldWidget::submit()
  139. */
  140. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  141. $field_name = $this->field['field_name'];
  142. $field_type = $this->field['type'];
  143. $field_table = $this->instance['settings']['chado_table'];
  144. $field_column = $this->instance['settings']['chado_column'];
  145. $base_table = $this->instance['settings']['base_table'];
  146. // Get the FK that links to the base record.
  147. $schema = chado_get_schema($field_table);
  148. $pkey = $schema['primary key'][0];
  149. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  150. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  151. $vocabulary = chado_get_semweb_term('cvterm', 'cv_id');
  152. $accession = chado_get_semweb_term('dbxref', 'accession');
  153. $definition = chado_get_semweb_term('cvterm', 'definition');
  154. if (array_key_exists('is_not', $schema['fields'])) {
  155. $negation = chado_get_semweb_term($field_table, 'is_not');
  156. }
  157. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  158. $fk_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey_lcolumn];
  159. // If a publication ID was provided then make sure the form_state
  160. // value for the pub_id is set correctly.
  161. if (array_key_exists('pub_id', $schema['fields'])) {
  162. $pub_name = $form_state['values'][$field_name]['und'][$delta]['pub'];
  163. if ($pub_name) {
  164. $pub = chado_generate_var('pub', array('uniquename' => $pub_name));
  165. if ($pub) {
  166. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__pub_id'] = $pub->pub_id;
  167. }
  168. }
  169. // Use the NULL pub.
  170. else {
  171. $pub = chado_get_publication(array('uniquename' => 'null'));
  172. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__pub_id'] = $pub->pub_id;
  173. }
  174. }
  175. // Make sure the rank is set.
  176. if (array_key_exists('rank', $schema['fields'])) {
  177. $rank = $form_state['values'][$field_name]['und'][$delta]['_weight'];
  178. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'] = $rank;
  179. }
  180. // Get the term that matches.
  181. $cvterm_name = $form_state['values'][$field_name]['und'][$delta]['cvterm_name'];
  182. $cv_id = $form_state['values'][$field_name]['und'][$delta]['cv_id'];
  183. $cvterm = chado_generate_var('cvterm', array(
  184. 'cv_id' => $cv_id,
  185. 'name' => $cvterm_name,
  186. ));
  187. if ($cvterm) {
  188. $form_state['values'][$field_name]['und'][$delta]['cvterm_name'] = '';
  189. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__cvterm_id'] = $cvterm->cvterm_id;
  190. $form_state['values'][$field_name]['und'][$delta]['value'] = $cvterm->cvterm_id;
  191. }
  192. // Remove all values so we can delete this record if there is no
  193. // cvterm.
  194. else {
  195. // There must be some value set.
  196. $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
  197. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__cvterm_id'] = '';
  198. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey_lcolumn] = '';
  199. if (array_key_exists('rank', $schema['fields'])) {
  200. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'] = '';
  201. }
  202. if (array_key_exists('pub_id', $schema['fields'])) {
  203. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'] = '';
  204. }
  205. if (array_key_exists('is_not', $schema['fields'])) {
  206. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '_is_not'] = '';
  207. }
  208. }
  209. }
  210. /**
  211. * @see TripalFieldWidget::theme()
  212. */
  213. public function theme($element) {
  214. $field_table = $this->instance['settings']['chado_table'];
  215. $layout = "
  216. <div class=\"sio--annotation-widget\">
  217. <div class=\"sio--annotation-item\">" .
  218. drupal_render($element['cv_id']) . "
  219. </div>
  220. <div class=\"sio--annotation-item\">" .
  221. drupal_render($element['cvterm_name']) . "
  222. </div>
  223. <div class=\"sio--annotation-item\">" .
  224. drupal_render($element['pub']) . "
  225. </div>
  226. <div class=\"sio--annotation-item\">" .
  227. drupal_render($element['chado-' . $field_table . '__is_not']) . "
  228. </div>
  229. </div>
  230. ";
  231. return $layout;
  232. }
  233. }
  234. /**
  235. * An Ajax callback for the tripal_chado_admin_publish_form..
  236. */
  237. function sio__annotation_widget_form_ajax_callback($form, $form_state) {
  238. $field_name = $form_state['triggering_element']['#parents'][0];
  239. $delta = $form_state['triggering_element']['#parents'][2];
  240. return $form[$field_name]['und'][$delta];
  241. }