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)

Implements hook_insert().

Related topics

File

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

Code

function chado_pub_insert($node) {

  $pub_id = '';

  // If there is an pub_id in the $node object then this must be a sync so
  // we can skip adding the pub as it is already there, although
  // we do need to proceed with insertion into the chado/drupal linking table.
  if (!property_exists($node, 'pub_id')) {

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

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

    // 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 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 = $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 == "Series Name" or $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 and !array_key_exists($citation_id, $properties)) {
      $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;
    }

    // insert the pub record
    $values = array(
      'title' => $node->pubtitle,
      'series_name' => substr($series_name, 0, 255),
      'type_id' => $node->type_id,
      'pyear' => $node->pyear,
      'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
      'uniquename' => $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 = chado_insert_record('pub', $values);
    if (!$pub) {
      drupal_set_message("Error inserting publication", "error");
      watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
      return;
    }
    $pub_id = $pub['pub_id'];

    // now add in the properties
    // Only adds in those not used in the pub record
    $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
    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['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);
  }
  else {
    $pub_id = $node->pub_id;
  }

  // Make sure the entry for this pub doesn't already exist in the
  // chado_pub table if it doesn't exist then we want to add it.
  $check_org_id = chado_get_id_from_nid('pub', $node->nid);
  if (!$check_org_id) {
    $record = new stdClass();
    $record->nid = $node->nid;
    $record->vid = $node->vid;
    $record->pub_id = $pub_id;
    drupal_write_record('chado_pub', $record);
  }

}