Stephen Ficklin 6 лет назад
Родитель
Сommit
5ad30399e1

+ 0 - 25
legacy/tripal_pub/tripal_pub.module

@@ -265,31 +265,6 @@ function tripal_pub_permission() {
   );
 }
 
-/**
- * Implements hook_mail().
- *
- * @ingroup tripal_legacy_pub
- */
-function tripal_pub_mail($key, &$message, $params) {
-  $site_name = variable_get('site_name');
-  $language = $message['language'];
-  switch($key) {
-    case 'import_report':
-      $headers = array(
-        'MIME-Version' => '1.0',
-        'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
-        'Content-Transfer-Encoding' => '8Bit',
-        'X-Mailer' => 'Drupal'
-      );
-      foreach ($headers as $key => $value) {
-        $message['headers'][$key] = $value;
-      }
-      $message['subject'] = t('Publication import from !site', array('!site' => $site_name));
-      $message['body'][] = $params['message'];
-      break;
-  }
-}
-
 /**
  * Implementation of hook_form_alter().
  *

+ 34 - 35
tripal_chado/api/modules/tripal_chado.pub.api.inc

@@ -429,41 +429,29 @@ function chado_import_pub_by_dbxref($pub_dbxref, $do_contact = FALSE,
 function chado_execute_active_pub_importers($report_email = FALSE, 
   $publish = TRUE, $do_update = FALSE) {
 
+  $report = [];
+  $report['error'] = [];
+  $report['inserted'] = [];
+  $report['skipped'] = [];
+  $report['updated'] = [];
+    
   // Get all of the loaders.
   $args = array();
   $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0 ";
   $importers = db_query($sql, $args);
   $do_contact = FALSE;
-  $reports = array();
   while ($import = $importers->fetchObject()) {
-    chado_execute_pub_importer($import->pub_import_id, $publish, $do_update);
-  }
-
-  // Iterate through each of the reports and generate a final report with HTML
-  // links.
-  $HTML_report = '';
-  if ($report_email) {
-    $HTML_report .= "<html>";
-    global $base_url;
-    foreach ($reports as $importer => $report) {
-      $total = count($report['inserted']);
-      $HTML_report .= "<b>$total new publications from importer: $importer</b><br><ol>\n";
-      foreach ($report['inserted'] as $pub) {
-        $item = $pub['Title'];
-        if (array_key_exists('pub_id', $pub)) {
-          $item = l($pub['Title'], "$base_url/pub/" . $pub['pub_id']);
-        }
-        $HTML_report .= "<li>$item</li>\n";
-      }
-      $HTML_report .= "</ol>\n";
+    $importer_report = chado_execute_pub_importer($import->pub_import_id, $publish, $do_update);
+    foreach ($importer_report as $action => $pubs) {
+      $report[$action] = array_merge($report[$action], $pubs);
     }
-    $HTML_report .= "</html>";
-    $site_email = variable_get('site_mail', '');
-    $params = array(
-      'message' => $HTML_report
-    );
-    drupal_mail('tripal_pub', 'import_report', $report_email, language_default(), $params, $site_email, TRUE);
   }
+
+  $site_email = variable_get('site_mail', '');
+  $params = array(
+    'report' => $report
+  );
+  drupal_mail('tripal_chado', 'import_report', $report_email, language_default(), $params, $site_email, TRUE);
   print "Done.\n";
 }
 
@@ -488,12 +476,20 @@ function chado_execute_active_pub_importers($report_email = FALSE,
  *   this function is run directly.
  *   
  * @return
- *   TRUE if importing occured with on errors. FALSE otherwise. If failed, 
- *   all database changes are rolled back.
+ *   Returns an array containing the number of publications that were
+ *   inserted, updated, skipped and which had an error during import.
  *
  * @ingroup tripal_pub
  */
-function chado_execute_pub_importer($import_id, $publish = TRUE, $do_update = FALSE, $job = NULL) {
+function chado_execute_pub_importer($import_id, $publish = TRUE, 
+  $do_update = FALSE, $job = NULL) {
+  
+  // Holds the list of imported pubs which includes their ID and Citation.
+  $report = [];
+  $report['error'] = [];
+  $report['inserted'] = [];
+  $report['skipped'] = [];
+  $report['updated'] = [];
   
   // These are options for the tripal_report_error function. We do not
   // want to log messages to the watchdog but we do for the job and to
@@ -538,15 +534,18 @@ function chado_execute_pub_importer($import_id, $publish = TRUE, $do_update = FA
       tripal_report_error($message_type, TRIPAL_INFO,
         "Querying !remote_db for up to !num pubs that match the criteria.", 
         ['!num' => $num_to_retrieve, '!remote_db' => $remote_db], $message_opts);
-      $results  = tripal_get_remote_pubs($remote_db, $criteria, $num_to_retrieve, $page);
-      $pubs     = $results['pubs'];
+      $results = tripal_get_remote_pubs($remote_db, $criteria, $num_to_retrieve, $page);
+      $pubs = $results['pubs'];
       $num_pubs = $results['total_records'];
       $total_pubs += $num_pubs;
       tripal_report_error($message_type, TRIPAL_INFO,
-        "Adding %num new publications.",
+        "Found %num publications.",
         ['%num' => $num_pubs], $message_opts);
       
-      tripal_pub_add_publications($pubs, $import->do_contact, $do_update, $job);
+      $subset_report = tripal_pub_add_publications($pubs, $import->do_contact, $do_update, $job);
+      foreach ($subset_report as $action => $pubs) {
+        $report[$action] = array_merge($report[$action], $pubs);
+      }
       $page++;
     }
     while (count($pubs) == $num_to_retrieve);
@@ -570,7 +569,7 @@ function chado_execute_pub_importer($import_id, $publish = TRUE, $do_update = FA
   tripal_report_error($message_type, TRIPAL_INFO,
     "Done.", [], $message_opts);
   
-  return TRUE;
+  return $report;
 }
 
 

+ 24 - 22
tripal_chado/includes/loaders/tripal_chado.pub_importers.inc

@@ -367,7 +367,7 @@ function tripal_pub_importer_setup_form($form, &$form_state = NULL, $pub_import_
   );
   $dbs = chado_select_record('db', array('*'), $values);
   foreach ($dbs as $index => $db) {
-    $remote_dbs[$db->name] = $db->description;
+    $remote_dbs[$db->name] = $db->description ? $db->description : $db->name;
   };
   // use PubMed as the default
   if (!$remote_db) {
@@ -876,7 +876,7 @@ function tripal_pub_importer_delete($import_id) {
  *
  * @param $pubs
  *   An array containing a list of publications to add to Chado.  The
- *   array contains a set of details for the publication.
+ *   array contains a set of details for the publication. 
  * @param $do_contact
  *   Set to TRUE if authors should automatically have a contact record added
  *   to Chado.
@@ -906,10 +906,11 @@ function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE, $job =
     'print' => TRUE,
   ];
   
-  $report = array();
-  $report['error'] = 0;
-  $report['inserted'] = array();
-  $report['skipped'] = array();
+  $report = [];
+  $report['error'] = [];
+  $report['inserted'] = [];
+  $report['skipped'] = [];
+  $report['updated'] = [];
   $total_pubs = count($pubs);
 
   // iterate through the publications and add each one
@@ -942,21 +943,20 @@ function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE, $job =
 
     switch ($action) {
       case 'error':
-        $report['error']++;
+        $report['error'][] = $pub['Citation'];
         break;
       case 'inserted':
-        $report['inserted'][] = $pub;
+        $report['inserted'][] = $pub['Citation'];
         break;
       case 'updated':
-        $report['updated'][] = $pub;
+        $report['updated'][] = $pub['Citation'];
         break;
       case 'skipped':
-        $report['skipped'][] = $pub;
+        $report['skipped'][] = $pub['Citation'];
         break;
     }
     $i++;
   }
-  print "\n";
   return $report;
 }
 
@@ -972,24 +972,26 @@ function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE, $job =
  *   This variable will get set to a text value indicating the action that was
  *   performed. The values include 'skipped', 'inserted', 'updated' or 'error'.
  * @param $do_contact
- *   Optional. Set to TRUE if a contact entry should be added to the Chado contact table
- *   for authors of the publication.
+ *   Optional. Set to TRUE if a contact entry should be added to the Chado 
+ *   contact table for authors of the publication.
  * @param $update_if_exists
  *   Optional.  If the publication already exists then this function will return
  *   without adding a new publication.  However, set this value to TRUE to force
- *   the function to pudate the publication using the $pub_details that are provided.
+ *   the function to pudate the publication using the $pub_details that are 
+ *   provided.
  * @param $job
  *   The jobs management object for the job if this function is run as a job. 
  *   This argument is added by Tripal during a job run and is not needed if
  *   this function is run directly.
  * @return
- *   If the publication already exists, is inserted or updated then the publication
- *   ID is returned, otherwise FALSE is returned. If the publication already exists
- *   and $update_if_exists is not TRUE then the $action variable is set to 'skipped'.
- *   If the publication already exists and $update_if_exists is TRUE and if the update
- *   was successful then $action is set to 'updated'.  Otherwise on successful insert
- *   the $action variable is set to 'inserted'.  If the function failes then the
- *   $action variable is set to 'error'
+ *   If the publication already exists, is inserted or updated then the 
+ *   publication ID is returned, otherwise FALSE is returned. If the publication
+ *   already exists and $update_if_exists is not TRUE then the $action variable 
+ *   is set to 'skipped'. If the publication already exists and 
+ *   $update_if_exists is TRUE and if the update was successful then $action 
+ *   is set to 'updated'.  Otherwise on successful insert the $action variable 
+ *   is set to 'inserted'.  If the function failes then the $action variable 
+ *   is set to 'error'
  *
  * @ingroup tripal_pub
  */
@@ -1118,7 +1120,7 @@ function tripal_pub_add_publication($pub_details, &$action, $do_contact = FALSE,
   }
 
   // If there is a pub_id and we've been told to update, then do the update.
-  if ($pub_id and $update_if_exists) {
+  else if ($pub_id and $update_if_exists) {
     $match = array('pub_id' => $pub_id);
     $options = array('statement_name' => 'up_pub_tivoseispypaunty');
     $success = chado_update_record('pub', $match, $values, $options);

+ 61 - 1
tripal_chado/tripal_chado.module

@@ -1190,6 +1190,66 @@ function tripal_feature_match_features_page($id) {
     ));
     return $output;
   }
+}
 
-
+/**
+ * Implements hook_mail().
+ *
+ * @ingroup tripal_legacy_pub
+ */
+function tripal_chado_mail($key, &$message, $params) {
+  $site_name = variable_get('site_name');
+  $language = $message['language'];
+  switch($key) {
+    case 'import_report':
+      
+      $content = [];
+      $content[] = [
+        '#type' => 'markup',
+        '#markup' => '<h3>Tripal Bulk Publication Report</h3>'
+      ];
+      $report = $params['report'];
+      print_r($report);
+      foreach ($report as $action => $pubs) {
+        if (count($pubs) > 0) {
+          $title = count($pubs) . ' publication(s) were ' . $action . ":";
+          if ($action == 'error') {
+            $title = count($pubs) . ' publications were not imported due to errors:';
+          }
+          $content[] = [
+            '#type' => 'item',
+            '#title' => $title, 
+            '#markup' => theme_item_list([
+              'title' => '',
+              'type' => 'ol', 
+              'items' => $pubs,
+              'attributes' => [],
+            ]),
+          ];
+        }
+      }
+      $content = '<html>' . drupal_render($content) . '</html';
+      
+      $message['subject'] = t('Publication import from !site', array('!site' => $site_name));
+      
+      
+      if (module_exists('htmlmail') or module_exists('mimemail')) {
+        $headers = array(
+          'MIME-Version' => '1.0',
+          'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
+          'Content-Transfer-Encoding' => '8Bit',
+          'X-Mailer' => 'Drupal'
+        );
+        foreach ($headers as $key => $value) {
+          $message['headers'][$key] = $value;
+        }
+        $message['body'][] = $content;
+      }
+      else {
+        $message['body'][] =  drupal_html_to_text($content);
+      }
+            
+      break;
+  }
 }
+