tripal_pub.pub_citation.inc 4.0 KB

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