Browse Source

Fixed some bugs and added ability to turn of creation of contacts for each author in the importer and upater

spficklin 12 years ago
parent
commit
56aa9501c3

+ 61 - 43
tripal_pub/api/tripal_pub.api.inc

@@ -76,7 +76,8 @@ function tripal_pub_get_remote_search_results($remote_db, $search_array,
 /*
  * @ingroup tripal_pub_api
  */
-function tripal_pub_update_publications() {
+function tripal_pub_update_publications($do_contact = FALSE) {
+  
   // get a persistent connection
   $connection = tripal_db_persistent_chado();
   if (!$connection) {
@@ -120,7 +121,7 @@ function tripal_pub_update_publications() {
       if ($curr_db) {
         $search['num_criteria'] = $i - 1;
         $pubs = tripal_pub_get_remote_search_results($remote_db, $search, $i, 0);
-        tripal_pub_add_publications($pubs); 
+        tripal_pub_add_publications($pubs, $do_contact); 
       }
       $curr_db = $remote_db;      
       $search = array(
@@ -135,7 +136,7 @@ function tripal_pub_update_publications() {
     if($i == $num_to_retrieve) {      
       $search['num_criteria'] = $i - 1;
       $pubs = tripal_pub_get_remote_search_results($remote_db, $search, $i, 0);
-      tripal_pub_add_publications($pubs);
+      tripal_pub_add_publications($pubs, $do_contact);
       $search['criteria'] = array();
       $i = 0;
     }
@@ -151,13 +152,17 @@ function tripal_pub_update_publications() {
   // now update any remaining in the search criteria array
   $search['num_criteria'] = $i - 1;
   $pubs = tripal_pub_get_remote_search_results($remote_db, $search, $i, 0);
-  tripal_pub_add_publications($pubs);
+  tripal_pub_add_publications($pubs, $do_contact);
   
   // sync the newly added publications with Drupal
   print "Syncing publications with Drupal...\n";
   tripal_pub_sync_pubs();
-  print "Syncing contacts with Drupal...\n";
-  tripal_contact_sync_contacts();
+  
+  // if the caller wants to create contacts then we should sync them
+  if ($do_contact) {
+    print "Syncing contacts with Drupal...\n";
+    tripal_contact_sync_contacts();
+  }
   
   // transaction is complete
   tripal_db_commit_transaction();
@@ -191,24 +196,34 @@ function tripal_pub_import_publications() {
   // get all of the loaders
   $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0";
   $results = db_query($sql);
+  $do_contact = FALSE;
   while ($import = db_fetch_object($results)) {
-     $criteria = unserialize($import->criteria); 
-     $remote_db = $criteria['remote_db'];
-     do {       
-       // retrieve the pubs for this page. We'll retreive 10 at a time
-       $pubs = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $pager_id, $page);             
-       $page++;
-     } 
-     // continue looping until we have a $pubs array that does not have
-     // our requested numer of records.  This means we've hit the end
-     while (count($pubs) == $num_to_retrieve);    
+    // keep track if any of the importers want to create contacts from authors
+    if ($import->do_contact == 1) {
+      $do_contact = TRUE;      
+    }
+    $criteria = unserialize($import->criteria); 
+    $remote_db = $criteria['remote_db'];
+    do {       
+      // retrieve the pubs for this page. We'll retreive 10 at a time
+      $pubs = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $pager_id, $page);
+      tripal_pub_add_publications($pubs, $import->do_contact);             
+      $page++;
+    } 
+    // continue looping until we have a $pubs array that does not have
+    // our requested numer of records.  This means we've hit the end
+    while (count($pubs) == $num_to_retrieve);    
   }
   
   // sync the newly added publications with Drupal
   print "Syncing publications with Drupal...\n";
   tripal_pub_sync_pubs();
-  print "Syncing contacts with Drupal...\n";
-  tripal_contact_sync_contacts();
+  
+  // if any of the importers wanted to create contacts from the authors then sync them
+  if($do_contact) {
+    print "Syncing contacts with Drupal...\n";
+    tripal_contact_sync_contacts();
+  }
   
   // transaction is complete
   tripal_db_commit_transaction();
@@ -218,13 +233,13 @@ function tripal_pub_import_publications() {
 /*
  * 
  */
-function tripal_pub_add_publications($pubs) {
-  
+function tripal_pub_add_publications($pubs, $do_contact) {
+ 
   // iterate through the publications and add each one
   foreach ($pubs as $pub) {
          
     // add the publication to Chado and sync it with Chado
-    $pub_id = tripal_pub_add_publication($pub);
+    $pub_id = tripal_pub_add_publication($pub, $do_contact);
    
     // add the publication cross reference (e.g. to PubMed)
     if ($pub_id) {         
@@ -289,7 +304,7 @@ function tripal_pub_add_pub_dbxref($pub_id, $pub) {
 /*
  * 
  */
-function tripal_pub_add_publication($pub_details) {
+function tripal_pub_add_publication($pub_details, $do_contact) {
   
    // check to see if the publication already exists
    $pub_id = 0;
@@ -355,7 +370,7 @@ function tripal_pub_add_publication($pub_details) {
 
      // skip details that won't be stored as properties
      if ($key == 'Authors') {
-       tripal_pub_add_authors($pub_id, $value);
+       tripal_pub_add_authors($pub_id, $value, $do_contact);
        continue;
      }
      if ($key == 'Title' or $key == 'Volume' or $key == 'Journal Name' or $key == 'Issue' or
@@ -392,8 +407,8 @@ function tripal_pub_add_publication($pub_details) {
 /*
  * 
  */
-function tripal_pub_add_authors($pub_id, $authors) {
-  $rank = 0;  
+function tripal_pub_add_authors($pub_id, $authors, $do_contact) {
+  $rank = 0;   
   
   // first remove any of the existing pubauthor entires
   $sql = "DELETE FROM {pubauthor} WHERE pub_id = %d";
@@ -426,10 +441,7 @@ function tripal_pub_add_authors($pub_id, $authors) {
       $type = 'Collective';
     }
     $name = trim($name);    
-        
-    // Add the contact 
-    $contact = tripal_contact_add_contact($name, '', $type, $author);
-    
+           
     // add an entry to the pubauthors table
     $values = array(
       'pub_id' => $pub_id,
@@ -440,20 +452,26 @@ function tripal_pub_add_authors($pub_id, $authors) {
     );
     $options = array('statement_name' => 'ins_pubauthor_idrasugisu');
     $pubauthor = tripal_core_chado_insert('pubauthor', $values, $options);
-    
-    // if we have succesfully added the contact and the pubauthor entries then we want to
-    // link them together 
-    if ($contact and $pubauthor) {
-    
-      // link the pubauthor entry to the contact 
-      $values = array(
-        'pubauthor_id' => $pubauthor['pubauthor_id'],
-        'contact_id' => $contact['contact_id'],
-      );
-      $options = array('statement_name' => 'ins_pubauthorcontact_puco');
-      $pubauthor_contact = tripal_core_chado_insert('pubauthor_contact', $values, $options);
-      if (!$pubauthor_contact) {
-        watchdog('tripal_pub', "Cannot link pub authro and contact.", array(), WATCHDOG_ERROR);
+
+    // if the user wants us to create a contact for each author then do it.
+    if ($do_contact) {
+      // Add the contact 
+      $contact = tripal_contact_add_contact($name, '', $type, $author);
+            
+      // if we have succesfully added the contact and the pubauthor entries then we want to
+      // link them together 
+      if ($contact and $pubauthor) {
+      
+        // link the pubauthor entry to the contact 
+        $values = array(
+          'pubauthor_id' => $pubauthor['pubauthor_id'],
+          'contact_id' => $contact['contact_id'],
+        );
+        $options = array('statement_name' => 'ins_pubauthorcontact_puco');
+        $pubauthor_contact = tripal_core_chado_insert('pubauthor_contact', $values, $options);
+        if (!$pubauthor_contact) {
+          watchdog('tripal_pub', "Cannot link pub authro and contact.", array(), WATCHDOG_ERROR);
+        }
       }
     }
     $rank++;   

+ 31 - 16
tripal_pub/includes/tripal_pub.admin.inc

@@ -6,7 +6,7 @@
  * @ingroup tripal_pub
  */
 function tripal_pub_importers_list() {
-  $header = array('', 'Importer Name', 'Database', 'Search String', 'Disabled', '');
+  $header = array('', 'Importer Name', 'Database', 'Search String', 'Disabled', 'Create Contact', '');
   $rows = array();
   $importers = db_query("SELECT * FROM {tripal_pub_import} ORDER BY name");    
 
@@ -27,6 +27,7 @@ function tripal_pub_importers_list() {
       $criteria['remote_db'],
       $criteria_str,
       $importer->disabled ? 'Yes' : 'No',
+      $importer->do_contact ? 'Yes' : 'No',
       l(t('Delete'), "admin/tripal/tripal_pub/import/delete/$importer->pub_import_id"),
     );
   }
@@ -34,7 +35,7 @@ function tripal_pub_importers_list() {
   $rows[] = array(
     'data' => array(
       array('data' => l(t('Create a new publication importer.'), "admin/tripal/tripal_pub/import/new"),
-        'colspan' => 6),
+        'colspan' => 7),
     )
   );
 
@@ -123,8 +124,9 @@ function theme_tripal_pub_importer_setup_form($form) {
   $markup .= '  <div id="pub-search-form-col1">' . drupal_render($form['loader_name']) . '</div>';
   $markup .= '  <div id="pub-search-form-col2">' . drupal_render($form['remote_db']) . '</div>';
   $markup .= '  <div id="pub-search-form-col3">' . drupal_render($form['days']) . '</div>';
-  $markup .= '  <div id="pub-search-form-col4">' . drupal_render($form['disabled']) . '</div>';
   $markup .= '</div>';
+  $markup .= '<div id="pub-search-form-row2">' . drupal_render($form['disabled']) . '</div>'; 
+  $markup .= '<div id="pub-search-form-row3">' . drupal_render($form['do_contact']) . '</div>';
   $markup .= theme('table', $headers, $rows);
   
   $form['criteria'] = array(
@@ -139,7 +141,7 @@ function theme_tripal_pub_importer_setup_form($form) {
  *
   * @ingroup tripal_pub
  */
-function tripal_pub_importer_setup_form(&$form_state = NULL, $import_id = NULL, $action = 'new') {
+function tripal_pub_importer_setup_form(&$form_state = NULL, $pub_import_id = NULL, $action = 'new') {
   tripal_core_ahah_init_form();
 
   
@@ -147,16 +149,16 @@ function tripal_pub_importer_setup_form(&$form_state = NULL, $import_id = NULL,
   // and one is provided then look it up in the database
   $criteria = NULL;
   if ($action == "edit" and !$form_state['values']) {
-    $pub_import_id = $form_state['values']['$pub_import_id'];    
     $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = %d";
-    $importer = db_fetch_object(db_query($sql, $import_id));
+    $importer = db_fetch_object(db_query($sql, $pub_import_id));
     
-    $criteria     = unserialize($importer->criteria);
-    $remote_db    = $criteria['remote_db'];
-    $days         = $criteria['days'];
-    $disabled     = $criteria['disabled'];
-    $num_criteria = $criteria['num_criteria'];    
-    $loader_name  = $criteria['loader_name'];  
+    $criteria       = unserialize($importer->criteria);
+    $remote_db      = $criteria['remote_db'];
+    $days           = $criteria['days'];
+    $disabled       = $criteria['disabled'];
+    $do_contact     = $criteria['do_contact'];
+    $num_criteria   = $criteria['num_criteria'];    
+    $loader_name    = $criteria['loader_name'];  
   }
 
   // if the session has variables then use those.  This should only happen when
@@ -165,6 +167,7 @@ function tripal_pub_importer_setup_form(&$form_state = NULL, $import_id = NULL,
   $loader_name  = $_SESSION['tripal_pub_search']['loader_name']  ? $_SESSION['tripal_pub_search']['loader_name']  : $loader_name;
   $remote_db    = $_SESSION['tripal_pub_search']['remote_db']    ? $_SESSION['tripal_pub_search']['remote_db']    : $remote_db;
   $disabled     = $_SESSION['tripal_pub_search']['disabled']     ? $_SESSION['tripal_pub_search']['disabled']     : $disabled;    
+  $do_contact   = $_SESSION['tripal_pub_search']['do_contact']   ? $_SESSION['tripal_pub_search']['do_contact']   : $do_contact;
   $days         = $_SESSION['tripal_pub_search']['days']         ? $_SESSION['tripal_pub_search']['days']         : $days;    
   
   
@@ -173,7 +176,8 @@ function tripal_pub_importer_setup_form(&$form_state = NULL, $import_id = NULL,
   $num_criteria = $form_state['values']['num_criteria'] ? $form_state['values']['num_criteria'] : $num_criteria;    
   $loader_name  = $form_state['values']['loader_name']  ? $form_state['values']['loader_name']  : $loader_name;
   $remote_db    = $form_state['values']['remote_db']    ? $form_state['values']['remote_db']    : $remote_db;
-  $disabled     = $form_state['values']['disabled']     ? $form_state['values']['disabled']     : $disabled;    
+  $disabled     = $form_state['values']['disabled']     ? $form_state['values']['disabled']     : $disabled;
+  $do_contact   = $form_state['values']['do_contact']   ? $form_state['values']['do_contact']   : $do_contact;
   $days         = $form_state['values']['days']         ? $form_state['values']['days']         : $days;    
   
    
@@ -227,15 +231,22 @@ function tripal_pub_importer_setup_form(&$form_state = NULL, $import_id = NULL,
   $form['days'] = array(
     '#type'          => 'textfield',
     '#title'         => t('Days'),
-    '#description'   => t('The number of days <br>from today to search.'),
+    '#description'   => t('The number of days from today to search.'),
     '#default_value' => $days,
     '#size'          => 5,
   );
   $form['disabled'] = array(
     '#type'          => 'checkbox',
     '#title'         => t('Disabled'),
+    '#description'   => t('Check to disable this importer.'),
     '#default_value' => $disabled,
-    '#size'          => 5,
+  );
+  $form['do_contact'] = array(
+    '#type'          => 'checkbox',
+    '#title'         => t('Create Contact'),
+    '#description'   => t('Check to create an entry in the contact table for each author of a matching publication during import. This allows storage of 
+       additional information such as affilation, etc. Otherwise, only authors names are retrieved.'),
+    '#default_value' => $do_contact,
   );
   
   for($i = 0; $i <= $num_criteria; $i++) {
@@ -262,7 +273,7 @@ function tripal_pub_importer_setup_form(&$form_state = NULL, $import_id = NULL,
   
     $form['criteria'][$i]["search_terms-$i"] = array(
       '#type'          => 'textfield',
-      '#description'   => t('Please provide a list of words, separated by spaces for searching.'),
+      '#description'   => t('Please provide a list of words for searching. You may use conjunctions such as "AND" or "OR" to separate words if they are expected in the same scope"'),
       '#default_value' => $search_terms,
       '#required'      => TRUE,
     );
@@ -341,6 +352,7 @@ function tripal_pub_importer_setup_form_validate($form, &$form_state) {
   $remote_db =  $form_state['values']["remote_db"];
   $days =  trim($form_state['values']["days"]);
   $disabled =  $form_state['values']["disabled"];
+  $do_contact =  $form_state['values']["do_contact"];
   $loader_name =  trim($form_state['values']["loader_name"]);
 
   for ($i = 0; $i <= $num_criteria; $i++) {            
@@ -365,6 +377,7 @@ function tripal_pub_importer_setup_form_submit($form, &$form_state) {
   $days =  trim($form_state['values']["days"]);
   $loader_name =  trim($form_state['values']["loader_name"]);
   $disabled =  $form_state['values']["disabled"];
+  $do_contact =  $form_state['values']["do_contact"];
 
   // set the session variables
   $_SESSION['tripal_pub_search']['remote_db'] = $remote_db;
@@ -372,6 +385,7 @@ function tripal_pub_importer_setup_form_submit($form, &$form_state) {
   $_SESSION['tripal_pub_search']['num_criteria'] = $num_criteria;
   $_SESSION['tripal_pub_search']['loader_name'] = $loader_name;
   $_SESSION['tripal_pub_search']['disabled'] = $disabled;
+  $_SESSION['tripal_pub_search']['do_contact'] = $do_contact;
   unset($_SESSION['tripal_pub_search']['criteria']);
   for ($i = 0; $i <= $num_criteria; $i++) {
     $search_terms =  trim($form_state['values']["search_terms-$i"]);
@@ -394,6 +408,7 @@ function tripal_pub_importer_setup_form_submit($form, &$form_state) {
       'name' => $loader_name,
       'criteria' => serialize($_SESSION['tripal_pub_search']),
       'disabled' => $disabled,
+      'do_contact' => $do_contact
     );
     // first check to see if this pub_import_id is already present. If so,
     // do an update rather than an insert    

+ 7 - 1
tripal_pub/tripal_pub.drush.inc

@@ -35,8 +35,12 @@ function tripal_pub_drush_command() {
   );
   $items['tripal-pubs-update'] = array(
     'description' => dt('Updates publication information for publications with a supported database cross-reference.'),
+    'options' => array(
+      'create_contacts' => dt('provide this option to create or update contacts for authors. By default contacts are not created or updated.'),
+    ),
     'examples' => array(
       'Standard example' => 'drush tripal-pubs-update',
+      'Create contacts during update' => 'drush tripal-pubs-update --create_contacts=1',
     ),
     'aliases' => array('tpubs-update'),
   );
@@ -56,5 +60,7 @@ function drush_tripal_pub_tripal_pubs_import() {
  *
  */
 function drush_tripal_pub_tripal_pubs_update() {
-  tripal_pub_update_publications();
+  $create_contacts = drush_get_option('create_contacts');
+  
+  tripal_pub_update_publications($create_contacts);
 }

+ 3 - 15
tripal_pub/tripal_pub.install

@@ -21,10 +21,7 @@ function tripal_pub_install() {
 
   // install the tripal_oub
   drupal_install_schema('tripal_pub');
-  
-  // add any cvterms we need for this module
-  tripal_pub_add_cvterms();  
-  
+    
   // add loading of the the tripal pub ontology to the job queue
   $obo_path =  realpath('./') . '/' . drupal_get_path('module', 'tripal_pub') . '/tpub.obo';
   $obo_id = tripal_cv_add_obo_ref('Tripal Publication', $obo_path);
@@ -35,16 +32,6 @@ function tripal_pub_install() {
 }
 
 
-/*
- * 
- * 
- */
-function tripal_pub_add_cvterms(){
-  
-  tripal_cv_add_cvterm(array('name' => 'abstract', 'def' => 'Publication abstract'), 
-    'tripal_pub', 0, 1, 'tripal');    
-}
-
 /**
  * Implementation of hook_uninstall().
  */
@@ -78,10 +65,11 @@ function tripal_pub_schema() {
   );
   $schema['tripal_pub_import'] = array(
     'fields' => array(
-      'pub_import_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'pub_import_id' => array('type' => 'serial', 'not null' => TRUE),
       'name' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
       'criteria' => array('type' => 'text', 'size' => 'normal', 'not null' => TRUE, 'description' => 'Contains a serialized PHP array containing the search criteria'),
       'disabled'  => array('type' => 'int', 'unsigned' => TRUE, 'not NULL' => TRUE, 'default' => 0),      
+      'do_contact'  => array('type' => 'int', 'unsigned' => TRUE, 'not NULL' => TRUE, 'default' => 0),
     ),
     'primary key' => array('pub_import_id'),
     'indexes' => array(

+ 3 - 20
tripal_pub/tripal_pub.module

@@ -116,24 +116,7 @@ function tripal_pub_menu() {
     'access arguments' => array('administer tripal pubs'),    
     'type ' => MENU_CALLBACK,
   );
-  
-  // Automatically generate checkboxes.
-  $items['node/add/tripal_pub/ahah_authors'] = array(
-    'title' => 'Add Additional Authors',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('author_addition_fields_ahah_callback'),
-    'access callback' => TRUE,
-    'type' => MENU_CALLBACK,
-    'weight' => 2,
-  );
-
-  $items['tripal_pub/js/%'] = array(
-    'page callback' => 'tripal_pub_js',
-    'page arguments' => array(2),
-    'access arguments' => array('access content'),
-    'type ' => MENU_CALLBACK,
-  );
-
+ 
   return $items;
 }
 
@@ -541,8 +524,8 @@ function tripal_pub_form_alter(&$form, &$form_state, $form_id) {
     if ($form_state['values']['action'] == 'edit') {
       $form['#action'] = url("admin/tripal/tripal_pub/import/edit/" . $form['pub_import_id']);
     } 
-    if ($form_state['values']['action'] == 'add') {
-      $form['#action'] = url("admin/tripal/tripal_pub/import/add");
+    if ($form_state['values']['action'] == 'new') {
+      $form['#action'] = url("admin/tripal/tripal_pub/import/new");
     }
   }
 }