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 = 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. $rank = $delta;
  36. $cvterm_name = '';
  37. $cv_id = '';
  38. $cvterm = NULL;
  39. $pub_name = '';
  40. // If the field already has a value then it will come through the $items
  41. // array. This happens when editing an existing record.
  42. if (array_key_exists($delta, $items)) {
  43. // Check for element values that correspond to fields in the Chado table.
  44. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  45. $fk_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $fkey_lcolumn, $fk_value);
  46. $cvterm_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__cvterm_id', $cvterm_id);
  47. if (array_key_exists('pub_id', $schema['fields'])) {
  48. $pub_name = tripal_get_field_item_keyval($items, $delta, 'pub', $pub_name);
  49. $pub_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__pub_id', $pub_id);
  50. }
  51. if (array_key_exists('rank', $schema['fields'])) {
  52. $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
  53. }
  54. if (array_key_exists('is_not', $schema['fields'])) {
  55. $is_not = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__is_not', $is_not);
  56. }
  57. if ($cvterm_id) {
  58. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  59. $cv_id = $cvterm->cv_id->cv_id;
  60. $cvterm_name = $cvterm->name;
  61. }
  62. }
  63. // Check $form_state['values'] to see if an AJAX call set the values.
  64. if ((array_key_exists('values', $form_state) and array_key_exists($field_name, $form_state['values'])) or
  65. (array_key_exists('input', $form_state) and array_key_exists($field_name, $form_state['input']))) {
  66. $vtype = 'input';
  67. if (array_key_exists('values', $form_state)) {
  68. $vtype = 'values';
  69. }
  70. $record_id = $form_state[$vtype][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  71. $fk_value = $form_state[$vtype][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey_lcolumn];
  72. $cvterm_id = $form_state[$vtype][$field_name]['und'][$delta]['chado-' . $field_table . '__cvterm_id'];
  73. if (array_key_exists('pub_id', $schema['fields'])) {
  74. $pub_name = $form_state[$vtype][$field_name]['und'][$delta]['pub'];
  75. $pub_id = $form_state[$vtype][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'];
  76. }
  77. if (array_key_exists('rank', $schema['fields'])) {
  78. $rank = $form_state[$vtype][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
  79. }
  80. if (array_key_exists('is_not', $schema['fields'])) {
  81. $is_not = $form_state[$vtype][$field_name]['und'][$delta]['chado-' . $field_table . '__is_not'];
  82. }
  83. $cvterm_name = $form_state[$vtype][$field_name]['und'][$delta]['cvterm_name'];
  84. $cv_id = $form_state[$vtype][$field_name]['und'][$delta]['cv_id'];
  85. $cvterm = chado_generate_var('cvterm', array(
  86. 'cv_id' => $cv_id,
  87. 'name' => $cvterm_name,
  88. ));
  89. if (!$cvterm) {
  90. $cvterm_name = '';
  91. }
  92. }
  93. $widget['#prefix'] = "<span id='$field_name-sio--annotation-$delta'>";
  94. $widget['#suffix'] = "</span>";
  95. $widget['value'] = array(
  96. '#type' => 'value',
  97. '#value' => key_exists($delta, $items) ? $items[$delta]['value'] : '',
  98. );
  99. $widget['chado-' . $field_table . '__' . $pkey] = array(
  100. '#type' => 'value',
  101. '#default_value' => $record_id,
  102. );
  103. $widget['chado-' . $field_table . '__cvterm_id'] = array(
  104. '#type' => 'value',
  105. '#default_value' => $cvterm_id,
  106. );
  107. $widget['chado-' . $field_table . '__' . $fkey_lcolumn] = array(
  108. '#type' => 'value',
  109. '#default_value' => $fk_value,
  110. );
  111. $cvs = tripal_get_cv_select_options();
  112. $widget['cv_id'] = array(
  113. '#type' => 'select',
  114. '#title' => t('Vocabulary'),
  115. '#options' => $cvs,
  116. '#default_value' => $cv_id,
  117. '#required' => $element['#required'],
  118. '#attributes' => array('style' => 'width: 200px;'),
  119. '#ajax' => array(
  120. 'callback' => "sio__annotation_widget_form_ajax_callback",
  121. 'wrapper' => "$field_name-sio--annotation-$delta",
  122. 'effect' => 'fade',
  123. 'method' => 'replace'
  124. ),
  125. );
  126. $cv_schema = chado_get_schema('cvterm');
  127. $widget['cvterm_name'] = array(
  128. '#type' => 'textfield',
  129. '#title' => t('Term Name'),
  130. '#default_value' => $cvterm_name,
  131. '#maxlength' => array_key_exists('length', $cv_schema['fields']['name']) ? $cv_schema['fields']['name']['length'] : 255,
  132. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/cvterm/' . $cv_id,
  133. '#disabled' => $cv_id ? FALSE : TRUE,
  134. );
  135. if (array_key_exists('pub_id', $schema['fields'])) {
  136. $widget['pub'] = array(
  137. '#type' => 'textfield',
  138. '#title' => t('Publication'),
  139. '#default_value' => $pub_name,
  140. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  141. '#maxlength' => 100000,
  142. '#disabled' => $cv_id ? FALSE : TRUE,
  143. );
  144. $widget['chado-' . $field_table . '__pub_id'] = array(
  145. '#type' => 'value',
  146. '#default_value' => $pub_id ? $pub_id : '',
  147. );
  148. }
  149. if (array_key_exists('is_not', $schema['fields'])) {
  150. $widget['chado-' . $field_table . '__is_not'] = array(
  151. '#type' => 'checkbox',
  152. '#title' => t('Negate this term (NOT)'),
  153. '#default_value' => $is_not,
  154. '#required' => $element['#required'],
  155. '#disabled' => $cv_id ? FALSE : TRUE,
  156. );
  157. }
  158. }
  159. /**
  160. *
  161. * @see TripalFieldWidget::submit()
  162. */
  163. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  164. $field_name = $this->field['field_name'];
  165. $field_type = $this->field['type'];
  166. $field_table = $this->instance['settings']['chado_table'];
  167. $field_column = $this->instance['settings']['chado_column'];
  168. $base_table = $this->instance['settings']['base_table'];
  169. // Get the FK that links to the base record.
  170. $schema = chado_get_schema($field_table);
  171. $pkey = $schema['primary key'][0];
  172. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  173. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  174. $vocabulary = tripal_get_chado_semweb_term('cvterm', 'cv_id');
  175. $accession = tripal_get_chado_semweb_term('dbxref', 'accession');
  176. $definition = tripal_get_chado_semweb_term('cvterm', 'definition');
  177. if (array_key_exists('is_not', $schema['fields'])) {
  178. $negation = tripal_get_chado_semweb_term($field_table, 'is_not');
  179. }
  180. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  181. $fk_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey_lcolumn];
  182. // If a publication ID was provided then make sure the form_state
  183. // value for the pub_id is set correctly.
  184. if (array_key_exists('pub_id', $schema['fields'])) {
  185. $pub_name = $form_state['values'][$field_name]['und'][$delta]['pub'];
  186. if ($pub_name) {
  187. $pub = chado_generate_var('pub', array('uniquename' => $pub_name));
  188. if ($pub) {
  189. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = $pub->pub_id;
  190. }
  191. }
  192. }
  193. // Make sure the rank is set.
  194. if (array_key_exists('rank', $schema['fields'])) {
  195. $rank = $form_state['values'][$field_name]['und'][$delta]['_weight'];
  196. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'] = $rank;
  197. }
  198. // Get the term that matches.
  199. $cvterm_name = $form_state['values'][$field_name]['und'][$delta]['cvterm_name'];
  200. $cv_id = $form_state['values'][$field_name]['und'][$delta]['cv_id'];
  201. $cvterm = chado_generate_var('cvterm', array(
  202. 'cv_id' => $cv_id,
  203. 'name' => $cvterm_name,
  204. ));
  205. if ($cvterm) {
  206. $form_state['values'][$field_name]['und'][$delta]['cvterm_name'] = '';
  207. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__cvterm_id'] = $cvterm->cvterm_id;
  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. }