Skip to content

Commit

Permalink
Merge pull request #585 from tripal/465-tv3-pub_reporter
Browse files Browse the repository at this point in the history
Fix Tripal Bulk Pub Importer Email Report
  • Loading branch information
spficklin committed Aug 31, 2018
2 parents dbdfe0f + 9f247c3 commit ac6aa41
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 73 deletions.
2 changes: 1 addition & 1 deletion legacy/tripal_pub/tripal_pub.install
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function tripal_pub_disable() {
require_once("tripal_pub.views_default.inc");
$views = tripal_pub_views_default_views();
foreach (array_keys($views) as $view_name) {
tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
tripal_disable_view($view_name, FALSE);
}

}
Expand Down
25 changes: 0 additions & 25 deletions legacy/tripal_pub/tripal_pub.module
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*
Expand Down
69 changes: 34 additions & 35 deletions tripal_chado/api/modules/tripal_chado.pub.api.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}


Expand Down
22 changes: 11 additions & 11 deletions tripal_chado/includes/loaders/tripal_chado.pub_importers.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -1122,7 +1122,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);
Expand Down
62 changes: 61 additions & 1 deletion tripal_chado/tripal_chado.module
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

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;
}
}

0 comments on commit ac6aa41

Please sign in to comment.