cvterm.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. *
  4. * @param unknown $entity_type
  5. * @param unknown $entity
  6. * @param unknown $field
  7. * @param unknown $instance
  8. * @param unknown $langcode
  9. * @param unknown $items
  10. * @param unknown $display
  11. */
  12. function tripal_chado_cvterm_formatter(&$element, $entity_type, $entity, $field,
  13. $instance, $langcode, $items, $display) {
  14. /* $chado_table = $field['settings']['chado_table'];
  15. foreach ($items as $delta => $item) {
  16. $accession = '';
  17. if ($item[$chado_table . '__cvterm_id']) {
  18. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $item[$chado_table . '__cvterm_id']));
  19. $dbxref = $cvterm->dbxref_id;
  20. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  21. if ($dbxref->db_id->urlprefix) {
  22. $accession = l($accession, $dbxref->db_id->urlprefix . '/' . $dbxref->accession, array('attributes' => array('target' => '_blank')));
  23. }
  24. }
  25. $element[$delta] = array(
  26. '#type' => 'markup',
  27. '#markup' => $accession,
  28. );
  29. } */
  30. }
  31. /**
  32. *
  33. * @param unknown $field_name
  34. * @param unknown $widget
  35. * @param unknown $form
  36. * @param unknown $form_state
  37. * @param unknown $field
  38. * @param unknown $instance
  39. * @param unknown $langcode
  40. * @param unknown $items
  41. * @param unknown $delta
  42. * @param unknown $element
  43. */
  44. function tripal_chado_cvterm_widget(&$widget, $form, $form_state, $field,
  45. $instance, $langcode, $items, $delta, $element) {
  46. $field_name = $field['field_name'];
  47. // Get the FK column that links to the base table.
  48. $chado_table = $field['settings']['chado_table'];
  49. $base_table = $field['settings']['base_table'];
  50. $schema = chado_get_schema($chado_table);
  51. $pkey = $schema['primary key'][0];
  52. $fkey = array_values($schema['foreign keys'][$base_table]['columns'])[0];
  53. // Get the field defaults.
  54. $record_id = '';
  55. $fkey_value = $element['#entity']->chado_record_id;
  56. $dbxref_id = '';
  57. $db_id = '';
  58. $accession = '';
  59. $version = '';
  60. $description = '';
  61. // If the field already has a value then it will come through the $items
  62. // array. This happens when editing an existing record.
  63. if (array_key_exists($delta, $items)) {
  64. $record_id = $items[$delta]['value'];
  65. $fkey_value = $items[$delta][$field_name . '__' . $fkey];
  66. $dbxref_id = $items[$delta][$field_name . '__dbxref_id'];
  67. $db_id = $items[$delta][$field_name . '--dbxref__db_id'];
  68. $accession = $items[$delta][$field_name . '--dbxref__accession'];
  69. $version = $items[$delta][$field_name . '--dbxref__version'];
  70. $description = $items[$delta][$field_name . '--dbxref__description'];
  71. }
  72. // Check $form_state['values'] to see if an AJAX call set the values.
  73. if (array_key_exists('values', $form_state)) {
  74. $record_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name);
  75. $fkey_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '__' . $fkey);
  76. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '__dbxref_id');
  77. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__db_id');
  78. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__accession');
  79. $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__version');
  80. $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__description');
  81. }
  82. // If we are here because our parent was triggered in a form submit
  83. // then that means an ajax call was made and we don't want the fieldset to
  84. // be closed when it returns from the ajax call.
  85. $collapsed = TRUE;
  86. if (array_key_exists('triggering_element', $form_state) and
  87. $form_state['triggering_element']['#parents'][0] == $field_name) {
  88. $collapsed = FALSE;
  89. }
  90. if ($dbxref_id) {
  91. $collapsed = FALSE;
  92. }
  93. $schema = chado_get_schema('dbxref');
  94. $options = tripal_get_db_select_options();
  95. $widget['#table_name'] = $chado_table;
  96. $widget['#fkey_field'] = $fkey;
  97. $widget['#element_validate'] = array('tripal_chado_cvterm_widget_validate');
  98. $widget['#theme'] = 'tripal_chado_cvterm_widget';
  99. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id-$delta'>";
  100. $widget['#suffix'] = "</span>";
  101. // A temporary element used for theming the fieldset.
  102. $widget['#theme_settings'] = array(
  103. '#title' => $element['#title'],
  104. '#description' => $element['#description'],
  105. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  106. '#theme' => 'tripal_chado_cvterm_widget',
  107. '#collapsible' => TRUE,
  108. '#collapsed' => $collapsed,
  109. );
  110. $widget['value'] = array(
  111. '#type' => 'value',
  112. '#default_value' => $record_id,
  113. );
  114. $widget[$field_name . '__dbxref_id'] = array(
  115. '#type' => 'value',
  116. '#default_value' => $dbxref_id,
  117. );
  118. $widget[$field_name . '__' . $fkey] = array(
  119. '#type' => 'value',
  120. '#default_value' => $fkey_value,
  121. );
  122. $widget[$field_name . '--dbxref__dbxref_id'] = array(
  123. '#type' => 'value',
  124. '#default_value' => $dbxref_id,
  125. );
  126. $widget[$field_name . '--dbxref__db_id'] = array(
  127. '#type' => 'select',
  128. '#title' => t('Database'),
  129. '#options' => $options,
  130. '#required' => $element['#required'],
  131. '#default_value' => $db_id,
  132. '#ajax' => array(
  133. 'callback' => "tripal_chado_cvterm_widget_form_ajax_callback",
  134. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  135. 'effect' => 'fade',
  136. 'method' => 'replace'
  137. ),
  138. );
  139. $widget[$field_name . '--dbxref__accession'] = array(
  140. '#type' => 'textfield',
  141. '#title' => t('Accession'),
  142. '#default_value' => $accession,
  143. '#required' => $element['#required'],
  144. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  145. '#size' => 15,
  146. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  147. '#ajax' => array(
  148. 'callback' => "tripal_chado_cvterm_widget_form_ajax_callback",
  149. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  150. 'effect' => 'fade',
  151. 'method' => 'replace'
  152. )
  153. );
  154. $widget[$field_name . '--dbxref__version'] = array(
  155. '#type' => 'textfield',
  156. '#title' => t('Version'),
  157. '#default_value' => $version,
  158. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  159. '#size' => 5,
  160. );
  161. $widget[$field_name . '--dbxref__description'] = array(
  162. '#type' => 'textfield',
  163. '#title' => t('Description'),
  164. '#default_value' => $description,
  165. '#size' => 20,
  166. );
  167. $widget['links'] = array(
  168. '#type' => 'item',
  169. '#markup' => l('Add a new database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  170. );
  171. }
  172. /**
  173. * An Ajax callback for the dbxref widget.
  174. */
  175. function tripal_chado_cvterm_widget_form_ajax_callback($form, $form_state) {
  176. $field_name = $form_state['triggering_element']['#parents'][0];
  177. $delta = $form_state['triggering_element']['#parents'][2];
  178. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__db_id');
  179. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__accession');
  180. if ($db_id and $accession) {
  181. $values = array(
  182. 'db_id' => $db_id,
  183. 'accession' => $accession,
  184. );
  185. $options = array('is_duplicate' => TRUE);
  186. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  187. if (!$has_duplicate) {
  188. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  189. }
  190. }
  191. return $form[$field_name]['und'][$delta];
  192. }
  193. /**
  194. * Callback function for validating the tripal_chado_organism_select_widget.
  195. */
  196. function tripal_chado_cvterm_widget_validate($element, &$form_state) {
  197. $field_name = $element['#field_name'];
  198. $delta = $element['#delta'];
  199. $table_name = $element['#table_name'];
  200. $fkey = $element['#fkey_field'];
  201. // If the form ID is field_ui_field_edit_form, then the user is editing the
  202. // field's values in the manage fields form of Drupal. We don't want
  203. // to validate it as if it were being used in a data entry form.
  204. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  205. return;
  206. }
  207. // Get the field values.
  208. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '__dbxref_id');
  209. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__db_id');
  210. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__accession');
  211. $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__version');
  212. $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__description');
  213. // Make sure that if a database ID is provided that an accession is also
  214. // provided. Here we use the form_set_error function rather than the
  215. // form_error function because the form_error will add a red_highlight
  216. // around all of the fields in the fieldset which is confusing as it's not
  217. // clear to the user what field is required and which isn't. Therefore,
  218. // we borrow the code from the 'form_error' function and append the field
  219. // so that the proper field is highlighted on error.
  220. if (!$db_id and $accession) {
  221. form_set_error(implode('][', $element ['#parents']) . '][' . $table_name . '--dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  222. }
  223. if ($db_id and !$accession) {
  224. form_set_error(implode('][', $element ['#parents']) . '][' . $table_name . '--dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  225. }
  226. if (!$db_id and !$accession and ($version or $description)) {
  227. form_set_error(implode('][', $element ['#parents']) . '][' . $table_name . '--dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  228. }
  229. // If user did not select a database, we want to remove dbxref_id from the
  230. // field.
  231. if (!$db_id) {
  232. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
  233. }
  234. // If the dbxref_id does not match the db_id + accession then the user
  235. // has selected a new dbxref record and we need to update the hidden
  236. // value accordingly.
  237. if ($db_id and $accession) {
  238. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  239. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  240. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $table_name . '__dbxref_id');
  241. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $table_name . '--dbxref__dbxref_id');
  242. }
  243. }
  244. }
  245. /**
  246. * Theme function for the dbxref_id_widget.
  247. *
  248. * @param $variables
  249. */
  250. function theme_tripal_chado_cvterm_widget($variables) {
  251. $element = $variables['element'];
  252. // These two fields were added to the widget to help identify the fields
  253. // for layout.
  254. $table_name = $element['#table_name'];
  255. $fkey = $element['#fkey_field'];
  256. $layout = "
  257. <div class=\"secondary-dbxref-widget\">
  258. <div class=\"secondary-dbxref-widget-item\">" .
  259. drupal_render($element[$table_name . '--dbxref__db_id']) . "
  260. </div>
  261. <div class=\"secondary-dbxref-widget-item\">" .
  262. drupal_render($element[$table_name . '--dbxref__accession']) . "
  263. </div>
  264. <div class=\"secondary-dbxref-widget-item\">" .
  265. drupal_render($element[$table_name . '--dbxref__version']) . "
  266. </div>
  267. <div class=\"secondary-dbxref-widget-item\">" .
  268. drupal_render($element[$table_name . '--dbxref__description']) . "
  269. </div>
  270. <div class=\"secondary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  271. </div>
  272. ";
  273. return $layout;
  274. }
  275. /**
  276. * Loads the field values with appropriate data.
  277. *
  278. * This function is called by the tripal_chado_field_storage_load() for
  279. * each property managed by the field_chado_storage storage type. This is
  280. * an optional hook function that is only needed if the field has
  281. * multiple form elements.
  282. *
  283. * @param $field
  284. * @param $entity
  285. * @param $base_table
  286. * @param $record
  287. */
  288. function tripal_chado_cvterm_field_load($field, $entity, $base_table, $record) {
  289. $field_name = $field['field_name'];
  290. $field_type = $field['type'];
  291. $chado_table = $field['settings']['chado_table'];
  292. $schema = chado_get_schema($chado_table);
  293. $pkey = $schema['primary key'][0];
  294. $fkey = array_values($schema['foreign keys'][$base_table]['columns'])[0];
  295. // Set some defaults for the empty record.
  296. $entity->{$field_name}['und'][0] = array(
  297. 'value' => '',
  298. $chado_table . '__' . $fkey => '',
  299. $chado_table . '__' . 'cvterm_id' => '',
  300. $chado_table . '__' . 'pub_id' => '',
  301. $chado_table . '__' . 'is_not' => '',
  302. $chado_table . '__' . 'rank' => '',
  303. $chado_table . '--' . 'cvterm__name' => '',
  304. $chado_table . '--' . 'cvterm__cvterm_id' => '',
  305. );
  306. $linker_table = $base_table . '_cvterm';
  307. /* $options = array('return_array' => 1);
  308. $record = chado_expand_var($record, 'table', $linker_table, $options);
  309. $i = 0;
  310. foreach ($record->$linker_table as $index => $linker) {
  311. $cvterm = $linker->cvterm_id;
  312. $entity->{$field_name}['und'][$i] = array(
  313. 'value' => $linker->$pkey,
  314. $chado_table . '__' . $fkey => $linker->$fkey->$fkey,
  315. $chado_table . '__' . 'cvterm_id' => $linker->cvterm_id->cvterm_id,
  316. $chado_table . '__' . 'pub_id' => property_exists($linker, 'pub_id') ? $linker->pub_id : '',
  317. $chado_table . '__' . 'is_not' => property_exists($linker, 'is_not') ? $linker->is_not : '',
  318. $chado_table . '__' . 'rank' => property_exists($linker, 'rank') ? $linker->rank : '',
  319. $chado_table . '__' . 'cvterm_type_id' => property_exists($linker, 'cvterm_type_id') ? $linker->cvterm_type_id : '',
  320. $chado_table . '--' . 'cvterm__name' => $cvterm->name,
  321. $chado_table . '--' . 'cvterm__cvterm_id' => $cvterm->cvterm_id,
  322. );
  323. $i++;
  324. } */
  325. }