sio__annotation_widget.inc 11 KB

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