function chado_pub_update

2.x tripal_pub.chado_node.inc chado_pub_update($node)
3.x tripal_pub.chado_node.inc chado_pub_update($node)
1.x tripal_pub.module chado_pub_update($node)

File

tripal_pub/tripal_pub.module, line 522
The Tripal Publication module allows you to search the PubMed databse for academic articles, that relate to user specified tpoic\s. As well, it allows management of publications so that a user can enter specified details regarding a desired…

Code

function chado_pub_update($node) {
  if ($node->revision) {
    // there is no way to handle revisions in Chado but leave
    // this here just to make not we've addressed it.
  }

  // we need an array suitable for the tripal_pub_create_citation() function
  // to automatically generate a citation if a uniquename doesn't already exist
  $pub_arr = array();

  // get the publication ID for this publication
  $pub_id = chado_get_id_for_node('pub', $node);

  $properties = array(); // stores all of the properties we need to add
  $cross_refs = array(); // stores any cross references for this publication

  // get the list of properties for easy lookup (without doing lots of database queries
  $properties_list = array();
  $sql = "
    SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
    FROM {cvtermpath} CVTP
      INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
      INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
      INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
    WHERE CV.name = 'tripal_pub' and 
      (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and 
      NOT CVTS.is_obsolete = 1
    ORDER BY CVTS.name ASC 
  ";
  $prop_types = chado_query($sql);
  while ($prop = db_fetch_object($prop_types)) {
    $properties_list[$prop->cvterm_id] = $prop->name;
    // The 'Citation' term is special because it serves
    // both as a property and as the uniquename for the
    // pub and we want it stored in both the pub table and the pubprop table
    if ($prop->name == 'Citation') {
      $properties[$prop->name][0] = $node->uniquename;
    }
  }

  // get the properties that should be added. Properties are in one of three forms:
  //  1) prop_value-[type id]-[index]
  //  2) new_value-[type id]-[index]
  //  3) new_id, new_value
  //  dpm($node);
  foreach ($node as $key => $value) {
    if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
      $type_id = $matches[1];
      $index = $matches[2];
      $name = $properties_list[$type_id];
      $properties[$name][$index] = trim($value);
    }
    if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
      $type_id = $matches[1];
      $index = $matches[2];
      $name = $properties_list[$type_id];
      $properties[$name][$index] = trim($value);
    }
  }
  if ($node->new_id and $node->new_value) {
    $type_id = $node->new_id;
    $name = $properties_list[$type_id];
    $index = count($properties[$name]);
    $properties[$name][$index] = trim($node->new_value);
  }

  // iterate through all of the properties and remove those that really are
  // part of the pub table fields
  foreach ($properties as $name => $element) {
    foreach ($element as $index => $value) {
      // populate our $pub_array for building a citation
      $pub_arr[$name] = $value;

      // remove properties that are stored in the pub table
      if ($name == "Volume") {
        $volume = $value;
        unset($properties[$name]);
      }
      elseif ($name == "Volume Title") {
        $volumetitle = $value;
        unset($properties[$name]);
      }
      elseif ($name == "Issue") {
        $issue = $value;
        unset($properties[$name]);
      }
      elseif ($name == "Pages") {
        $pages = $value;
        unset($properties[$name]);
      }
      elseif ($name == "Publisher") {
        $publisher = $value;
        unset($properties[$name]);
      }
      elseif ($name == "Journal Name" or $name == "Conference Name") {
        $node->series_name = $value;
        unset($properties[$name]);
      }
      elseif ($name == "Journal Country" or $name == "Published Location") {
        $pubplace = $value;
        unset($properties[$name]);
      }
      elseif ($name == "Publication Dbxref") {
        // we will add the cross-references to the pub_dbxref table
        // but we also want to keep the property in the pubprop table so don't unset it
        $cross_refs[] = $value;
      }
    }
  }
  // generate a citation for this pub if one doesn't already exist 
  if (!$node->uniquename) {
    $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
    $pub_arr['Title'] = $node->pubtitle;
    $pub_arr['Publication Type'][0] = $pub_type->name;
    $pub_arr['Year'] = $node->pyear;
    $node->uniquename = tripal_pub_create_citation($pub_arr);
    $properties['Citation'][0] = $node->uniquename;
  }

  // update the pub record
  $match = array(
    'pub_id' => $pub_id,
  );
  $values = array(
    'title' => trim($node->pubtitle),
    'type_id' => trim($node->type_id),
    'pyear' => trim($node->pyear),
    'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
    'uniquename' => trim($node->uniquename),
    'series_name' => trim($node->series_name),
    'volumetitle' => $volumetitle,
    'volume' => $volume,
    'issue' => $issue,
    'pages' => $pages,
    'miniref' => $miniref,
    'publisher' => $publisher,
    'pubplace' => $pubplace,
  );
  $status = tripal_core_chado_update('pub', $match, $values);
  if (!$status) {
    drupal_set_message("Error updating publication", "error");
    watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
    return;
  }

  // now add in the properties by first removing any the publication
  // already has and adding the ones we have
  tripal_core_chado_delete('pubprop', array('pub_id' => $pub_id));
  foreach ($properties as $property => $elements) {
    foreach ($elements as $rank => $value) {
      $status = tripal_pub_insert_property($pub_id, $property, $value, FALSE);
      if (!$status) {
        drupal_set_message("Error cannot add property: '$property'", "error");
        watchdog('tripal_pub', "Error cannot add property: '%prop'", 
        array('%prop' => $property), WATCHDOG_ERROR);
      }
    }
  }

  // add in any database cross-references after first removing
  tripal_core_chado_delete('pub_dbxref', array('pub_id' => $pub_id));
  foreach ($cross_refs as $index => $ref) {
    $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, trim($ref));
    if (!$pub_dbxref) {
      drupal_set_message("Error cannot add publication cross reference: $ref", "error");
      watchdog('tripal_pub', "Error cannot add publication cross reference: %ref", 
      array('%ref' => $ref), WATCHDOG_ERROR);
    }
  }
}