pub_citation.inc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * The admin form for submitting job to create citations
  4. */
  5. function tripal_pub_citation_form($form_state) {
  6. $form['create_all'] = array(
  7. '#type' => 'item',
  8. '#value' => t('Submit a job to create citation for the publications in chado. '),
  9. );
  10. $form['options'] = array(
  11. '#type' => 'radios',
  12. '#options' =>
  13. array('all' => 'Create citation for all publications. Replace the existing citation if it exists.',
  14. 'new' => 'Create citation for publication only if it does not already have one.'),
  15. '#default_value' => 'all'
  16. );
  17. $form['submit'] = array(
  18. '#type' => 'submit',
  19. '#weight' => 10,
  20. '#value' => t('Submit')
  21. );
  22. return $form;
  23. }
  24. /*
  25. * Submit form. Create Tripal job for citations
  26. */
  27. function tripal_pub_citation_form_submit(&$form_state) {
  28. $options [0] = $form_state['options']['#value'];
  29. tripal_add_job("Create citations ($options[0])", 'tripal_pub',
  30. 'tripal_pub_create_citations', $options, $user->uid);
  31. }
  32. /*
  33. * Launch the Tripal job to generate citations
  34. */
  35. function tripal_pub_create_citations ($options) {
  36. $skip_existing = TRUE;
  37. $citation_type_id = db_result(chado_query("SELECT cvterm_id FROM {cvterm} WHERE name = 'Citation' AND cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal_pub')"));
  38. // Create and replace citation for all pubs
  39. if ($options == 'all') {
  40. $sql = "SELECT pub_id FROM {pub} P WHERE pub_id <> 1";
  41. $skip_existing = FALSE;
  42. // Create citation for pubs that don't already have one
  43. } else if ($options == 'new') {
  44. $sql = "SELECT pub_id FROM {pub} P WHERE (SELECT value FROM {pubprop} PB WHERE type_id = $citation_type_id AND P.pub_id = PB.pub_id AND rank = 0) IS NULL AND pub_id <> 1";
  45. $skip_existing = TRUE;
  46. }
  47. $result = chado_query($sql);
  48. $counter_updated = 0;
  49. $counter_generated = 0;
  50. while ($pub = db_fetch_object($result)) {
  51. $pub_arr = tripal_pub_get_publication_array($pub->pub_id, $skip_existing);
  52. if ($pub_arr) {
  53. $citation = tripal_pub_create_citation ($pub_arr);
  54. print $citation . "\n\n";
  55. // Replace if citation exists. This condition is never TRUE if $skip_existing is TRUE
  56. if ($pub_arr['Citation']) {
  57. $sql =
  58. "UPDATE {pubprop}
  59. SET value = '%s'
  60. WHERE pub_id = %d
  61. AND type_id = %d
  62. AND rank = %d";
  63. chado_query($sql, $citation, $pub->pub_id, $citation_type_id, 0);
  64. $counter_updated ++;
  65. // Generate a new citation
  66. } else {
  67. $sql = "INSERT INTO {pubprop} (pub_id, type_id, value, rank) VALUES (%d, %d, '%s', %d)";
  68. chado_query($sql, $pub->pub_id, $citation_type_id, $citation, 0);
  69. $counter_generated ++;
  70. }
  71. }
  72. }
  73. print "$counter_generated citations generated. $counter_updated citations updated.\n";
  74. }