pub_citation.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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', 'tripal_pub_create_citations', $options, $user->uid);
  30. }
  31. /*
  32. * Launch the Tripal job to generate citations
  33. */
  34. function tripal_pub_create_citations ($options) {
  35. $skip_existing = TRUE;
  36. $sql = "
  37. SELECT cvterm_id
  38. FROM {cvterm}
  39. WHERE
  40. name = 'Citation' AND
  41. cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal_pub')
  42. ";
  43. $citation_type_id = chado_query($sql)->fetchField();
  44. // Create and replace citation for all pubs
  45. if ($options == 'all') {
  46. $sql = "SELECT pub_id FROM {pub} P WHERE pub_id <> 1";
  47. $skip_existing = FALSE;
  48. }
  49. // Create citation for pubs that don't already have one
  50. else if ($options == 'new') {
  51. $sql = "
  52. SELECT pub_id
  53. FROM {pub} P
  54. WHERE
  55. (SELECT value
  56. FROM {pubprop} PB
  57. WHERE type_id = :type_id AND P.pub_id = PB.pub_id AND rank = 0) IS NULL
  58. AND pub_id <> 1
  59. ";
  60. $skip_existing = TRUE;
  61. }
  62. $result = chado_query($sql, array(':type_id' => $citation_type_id));
  63. $counter_updated = 0;
  64. $counter_generated = 0;
  65. while ($pub = $result->fetchObject()) {
  66. $pub_arr = tripal_pub_get_publication_array($pub->pub_id, $skip_existing);
  67. if ($pub_arr) {
  68. $citation = tripal_pub_create_citation ($pub_arr);
  69. print $citation . "\n\n";
  70. // Replace if citation exists. This condition is never TRUE if $skip_existing is TRUE
  71. if ($pub_arr['Citation']) {
  72. $sql = "
  73. UPDATE {pubprop} SET value = :value
  74. WHERE pub_id = :pub_id AND type_id = :type_id AND rank = :rank
  75. ";
  76. chado_query($sql, array(':value' => $citation, ':pub_id' => $pub->pub_id,
  77. ':type_id' => $citation_type_id, ':rank' => 0));
  78. $counter_updated ++;
  79. // Generate a new citation
  80. } else {
  81. $sql = "
  82. INSERT INTO {pubprop} (pub_id, type_id, value, rank)
  83. VALUES (:pub_id, :type_id, :value, :rank)
  84. ";
  85. chado_query($sql, array(':pub_id' => $pub->pub_id, ':type_id' => $citation_type_id,
  86. ':value' => $citation, ':rank' => 0));
  87. $counter_generated ++;
  88. }
  89. }
  90. }
  91. print "$counter_generated citations generated. $counter_updated citations updated.\n";
  92. }