tripal_pub.pub_citation.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * The admin form for submitting job to create citations
  4. */
  5. function tripal_pub_citation_form($form_state) {
  6. $form['instructions'] = array(
  7. '#markup' => '<p>Use this form to unify publication citations. Citations are created automtically when
  8. importing publications but citations are set by the user when publications are added manually.
  9. Or publications added to the Chado database by tools other than the Tripal Publication Importer may
  10. not have citations set. If you are certain that all necessary information for all publications is present (e.g.
  11. authors, volume, issue, page numbers, etc.) but citations are not consistent, then you can
  12. choose to update all citations for all publications using the form below. Alternatively, you
  13. can update citations only for publication that do not already have one.</p>'
  14. );
  15. $form['options'] = array(
  16. '#type' => 'radios',
  17. '#options' => array(
  18. 'all' => 'Create citation for all publications. Replace the existing citation if it exists.',
  19. 'new' => 'Create citation for publication only if it does not already have one.'),
  20. '#default_value' => 'all'
  21. );
  22. $form['submit'] = array(
  23. '#type' => 'submit',
  24. '#value' => t('Submit')
  25. );
  26. return $form;
  27. }
  28. /*
  29. * Submit form. Create Tripal job for citations
  30. */
  31. function tripal_pub_citation_form_submit(&$form_state) {
  32. $options [0] = $form_state['options']['#value'];
  33. tripal_add_job("Create citations ($options[0])", 'tripal_pub', 'tripal_pub_create_citations', $options, $user->uid);
  34. }
  35. /*
  36. * Launch the Tripal job to generate citations
  37. */
  38. function tripal_pub_create_citations ($options) {
  39. $skip_existing = TRUE;
  40. $sql = "
  41. SELECT cvterm_id
  42. FROM {cvterm}
  43. WHERE
  44. name = 'Citation' AND
  45. cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal_pub')
  46. ";
  47. $citation_type_id = chado_query($sql)->fetchField();
  48. // Create and replace citation for all pubs
  49. if ($options == 'all') {
  50. $sql = "SELECT pub_id FROM {pub} P WHERE pub_id <> 1";
  51. $skip_existing = FALSE;
  52. }
  53. // Create citation for pubs that don't already have one
  54. else if ($options == 'new') {
  55. $sql = "
  56. SELECT pub_id
  57. FROM {pub} P
  58. WHERE
  59. (SELECT value
  60. FROM {pubprop} PB
  61. WHERE type_id = :type_id AND P.pub_id = PB.pub_id AND rank = 0) IS NULL
  62. AND pub_id <> 1
  63. ";
  64. $skip_existing = TRUE;
  65. }
  66. $result = chado_query($sql, array(':type_id' => $citation_type_id));
  67. $counter_updated = 0;
  68. $counter_generated = 0;
  69. while ($pub = $result->fetchObject()) {
  70. $pub_arr = tripal_pub_get_publication_array($pub->pub_id, $skip_existing);
  71. if ($pub_arr) {
  72. $citation = tripal_pub_create_citation ($pub_arr);
  73. print $citation . "\n\n";
  74. // Replace if citation exists. This condition is never TRUE if $skip_existing is TRUE
  75. if ($pub_arr['Citation']) {
  76. $sql = "
  77. UPDATE {pubprop} SET value = :value
  78. WHERE pub_id = :pub_id AND type_id = :type_id AND rank = :rank
  79. ";
  80. chado_query($sql, array(':value' => $citation, ':pub_id' => $pub->pub_id,
  81. ':type_id' => $citation_type_id, ':rank' => 0));
  82. $counter_updated ++;
  83. // Generate a new citation
  84. } else {
  85. $sql = "
  86. INSERT INTO {pubprop} (pub_id, type_id, value, rank)
  87. VALUES (:pub_id, :type_id, :value, :rank)
  88. ";
  89. chado_query($sql, array(':pub_id' => $pub->pub_id, ':type_id' => $citation_type_id,
  90. ':value' => $citation, ':rank' => 0));
  91. $counter_generated ++;
  92. }
  93. }
  94. }
  95. print "$counter_generated citations generated. $counter_updated citations updated.\n";
  96. }