function chado_pub_insert

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

Implementation of tripal_pub_insert().

This function inserts user entered information pertaining to the Publication instance into the 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.

@parm $node Then node which contains the information stored within the node-ID

File

tripal_pub/tripal_pub.module, line 333
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_insert($node) {

  // 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();

  // if a pub_id already exists for this node then it already exists in Chado and
  // we get here because we are syncing the node.  Therefore, we can skip the insert
  // but we always want to set the URL path alias to be the Chado pub ID
  if ($node->pub_id) {
    $pub['pub_id'] = $node->pub_id;
  }
  else {
    $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 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' 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 two forms:
    //  1) prop_value-[type id]-[index]
    //  2) new_value-[type id]-[index]
    //  3) new_id, new_value
    foreach ($node as $name => $value) {
      if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $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;
      $index = count($properties[$name]);
      $name = $properties_list[$type_id];
      $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) {
      $value = trim($element[0]);

      // 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 and array_key_exists('Citation', $properties)) {
      $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;
    }

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

    // now add in the properties
    foreach ($properties as $property => $elements) {
      foreach ($elements as $rank => $value) {

        $status = tripal_pub_insert_property($pub['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('%property' => $property), WATCHDOG_ERROR);
        }
      }
    }

    // add in any database cross-references
    foreach ($cross_refs as $index => $ref) {
      $pub_dbxref = tripal_pub_add_pub_dbxref($pub['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);
      }
    }
  } // if ($node->pub_id) {} else

  if ($pub) {
    // make sure the entry for this feature doesn't already exist in the chado_pub table
    // if it doesn't exist then we want to add it.
    $pub_id = chado_get_id_for_node('pub', $node);
    if (!$pub_id) {
      // next add the item to the drupal table
      $sql = "INSERT INTO {chado_pub} (nid, vid, pub_id) " .
        "VALUES (%d, %d, %d)";
      db_query($sql, $node->nid, $node->vid, $pub['pub_id']);
    }
  }
  else {
    drupal_set_message(t('Unable to add publication.', 'warning'));
    watchdog('tripal_pub', 'Insert publication: Unable to create pub where values: %values', 
    array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  }
}