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)

Implements hook_update().

The purpose of the function is to allow the module to take action when an edited node is being updated. It updates any name changes to the database tables that werec reated upon registering a Publication. As well, the database will be changed, so the user changed information will be saved to the database.

Parameters

$node: The node being updated

Related topics

File

legacy/tripal_pub/includes/tripal_pub.chado_node.inc, line 802
Implements Drupal Node hooks to create the chado_analysis node content type.

Code

function chado_pub_update($node) {
  $node->pubtitle = trim($node->pubtitle);
  $node->pyear = trim($node->pyear);
  $node->uniquename = trim($node->uniquename);
  $is_obsolete = $node->is_obsolete;
  $type_id = $node->type_id;

  // 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_from_nid('pub', $node->nid);

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

  // get the properties from the form
  $properties = chado_retrieve_node_form_properties($node);

  // 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 = $prop_types->fetchObject()) {
    $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') {
      $citation_id = $prop->cvterm_id;
      if (!empty($node->uniquename)) {
        $properties[$citation_id][0] = $node->uniquename;
      }
    }
  }

  // iterate through all of the properties and remove those that really are
  // part of the pub table fields
  $volume = '';
  $volumetitle = '';
  $issue = '';
  $pages = '';
  $publisher = '';
  $series_name = '';
  $pubplace = '';
  $miniref = '';
  $cross_refs = array();
  foreach ($properties as $type_id => $element) {
    foreach ($element as $index => $value) {
      $name = $properties_list[$type_id];
      // 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[$type_id]);
      }
      elseif ($name == "Volume Title") {
        $volumetitle = $value;
        unset($properties[$type_id]);
      }
      elseif ($name == "Issue") {
        $issue = $value;
        unset($properties[$type_id]);
      }
      elseif ($name == "Pages") {
        $pages = $value;
        unset($properties[$type_id]);
      }
      elseif ($name == "Publisher") {
        $publisher = $value;
        unset($properties[$type_id]);
      }
      elseif ($name == "Journal Name" or $name == "Conference Name") {
        $series_name = $value;
        unset($properties[$type_id]);
      }
      elseif ($name == "Journal Country" or $name == "Published Location") {
        $pubplace = $value;
        // allow this property to go into the pubprop table so we don't loose info
        // so don't unset it. But it will also go into the pub.pubplace field
      }
      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_get_cvterm(array('cvterm_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_id][0] = $node->uniquename;
  }

  // update the pub record
  $match = array(
    'pub_id' => $pub_id,
  );
  $values = array(
    'title' => $node->pubtitle,
    'type_id' => $node->type_id,
    'pyear' => $node->pyear,
    'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
    'uniquename' => $node->uniquename,
    'series_name' => substr($series_name, 0, 255),
    'volumetitle' => $volumetitle,
    'volume' => $volume,
    'issue' => $issue,
    'pages' => $pages,
    'miniref' => substr($miniref, 0, 255),
    'publisher' => substr($publisher, 0, 255),
    'pubplace' => substr($pubplace, 0, 255),
  );
  $status = chado_update_record('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
  $details = array(
    'property_table' => 'pubprop',
    'base_table' => 'pub',
    'foreignkey_name' => 'pub_id',
    'foreignkey_value' => $pub_id
  );
  chado_update_node_form_properties($node, $details, $properties);

  // * Relationships Form *
  $details = array(
    'relationship_table' => 'pub_relationship', // name of the _relationship table
    'foreignkey_value' => $pub_id // value of the pub_id key
  );
  chado_update_node_form_relationships($node, $details);

  // add in any database cross-references after first removing
  chado_delete_record('pub_dbxref', array('pub_id' => $pub_id));
  foreach ($cross_refs as $index => $ref) {
    $dbxref = array();
    if (preg_match('/^(.*?):(.*?)$/', trim($ref), $matches)) {
      $dbxref['db_name'] = $matches[1];
      $dbxref['accession'] = $matches[2];
    }
    $success = chado_associate_dbxref('pub', $pub_id, $dbxref);
    if (!$success) {
      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);
    }
  }

  // * Additional DBxrefs Form *
  $details = array(
    'linking_table' => 'pub_dbxref', // the name of your _dbxref table
    'foreignkey_name' => 'pub_id', // the name of the key in your base table
    'foreignkey_value' => $pub_id // the value of the pub_id key
  );
  chado_update_node_form_dbxrefs($node, $details);
}