function chado_pub_create_citations

3.x tripal_chado.pub.api.inc chado_pub_create_citations($options)

Launch the Tripal job to generate citations.

This function will recreate citations for all publications currently loaded into Tripal. This is useful to create a consistent format for all citations.

Parameters

$options: Options pertaining to what publications to generate citations for. One of the following must be present:

  • all: Create and replace citation for all pubs.
  • new: Create citation for pubs that don't already have one.

Related topics

1 call to chado_pub_create_citations()
tripal_pub_create_citations in tripal_chado/api/modules/tripal_chado.module.DEPRECATED.api.inc
Launch the Tripal job to generate citations.
1 string reference to 'chado_pub_create_citations'
tripal_pub_citation_form_submit in tripal_chado/includes/loaders/tripal_chado.pub_importers.inc
Submit form. Create Tripal job for citations

File

tripal_chado/api/modules/tripal_chado.pub.api.inc, line 639
Provides API functions specificially for managing publication records in Chado.

Code

function chado_pub_create_citations($options) {
  $skip_existing = TRUE;
  $sql = "
    SELECT cvterm_id
    FROM {cvterm}
    WHERE
      name = 'Citation' AND
      cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal_pub')
  ";
  $citation_type_id = chado_query($sql)->fetchField();

  // Create and replace citation for all pubs.
  if ($options == 'all') {
    $sql = "SELECT pub_id FROM {pub} P WHERE pub_id <> 1";
    $skip_existing = FALSE;
  }
  // Create citation for pubs that don't already have one.
  else if ($options == 'new') {
    $sql = "
      SELECT pub_id
      FROM {pub} P
      WHERE
        (SELECT value
         FROM {pubprop} PB
         WHERE type_id = :type_id AND P.pub_id = PB.pub_id AND rank = 0) IS NULL
        AND  pub_id <> 1
    ";
    $skip_existing = TRUE;
  }

  $result = chado_query($sql, array(':type_id' => $citation_type_id));
  $counter_updated = 0;
  $counter_generated = 0;
  while ($pub = $result->fetchObject()) {
    $pub_arr = tripal_pub_get_publication_array($pub->pub_id, $skip_existing);
    if ($pub_arr) {
      $citation = chado_pub_create_citation($pub_arr);
      print $citation . "\n\n";
      // Replace if citation exists. This condition is never TRUE if 
      // $skip_existing is TRUE.
      if ($pub_arr['Citation']) {
        $sql = "
          UPDATE {pubprop} SET value = :value
          WHERE pub_id = :pub_id  AND type_id = :type_id AND rank = :rank
        ";
        chado_query($sql, array(':value' => $citation, ':pub_id' => $pub->pub_id,
          ':type_id' => $citation_type_id, ':rank' => 0));
        $counter_updated++;
        // Generate a new citation.
      }
      else {
        $sql = "
          INSERT INTO {pubprop} (pub_id, type_id, value, rank)
          VALUES (:pub_id, :type_id, :value, :rank)
        ";
        chado_query($sql, array(':pub_id' => $pub->pub_id, ':type_id' => $citation_type_id,
          ':value' => $citation, ':rank' => 0));
        $counter_generated++;
      }
    }
  }
  print "$counter_generated citations generated. $counter_updated citations updated.\n";
}