Skip to content
Snippets Groups Projects
Commit 8e913f8c authored by bgm's avatar bgm Committed by shaneonabike
Browse files

When a mailing is archived, delete the related queue stats

parent d807dc04
No related branches found
No related tags found
No related merge requests found
......@@ -22,14 +22,32 @@ class CRM_Archivemailing_BAO_MailingArchiveStat extends CRM_Archivemailing_DAO_M
// I guess we could feed create() with the stats, but otherwise we will fetch them
if (count($params) == 1) {
$report = CRM_Mailing_BAO_Mailing::report($mailing_id);
$params['recipients'] = $report['event_totals']['queue'];
$params['deliveries'] = $report['event_totals']['delivered'];
$params['forwards'] = $report['event_totals']['forward'];
$params['replies'] = $report['event_totals']['reply'];
$params['unsubscribes'] = $report['event_totals']['unsubscribe'];
$params['optouts'] = $report['event_totals']['optout'];
$params['bounces'] = $report['event_totals']['bounce'];
$is_archived = CRM_Core_DAO::executeQuery('SELECT is_archived FROM civicrm_mailing WHERE id = %1', [
1 => [$mailing_id, 'Positive'],
]);
if ($is_archived && empty($report['event_totals']['delivered'])) {
Civi::log()->info("archivemailing: mailing $mailing_id is already archived and stats seem empty, skipping stats archival");
}
else {
$report = CRM_Mailing_BAO_Mailing::report($mailing_id);
$params['recipients'] = $report['event_totals']['queue'];
$params['deliveries'] = $report['event_totals']['delivered'];
$params['forwards'] = $report['event_totals']['forward'];
$params['replies'] = $report['event_totals']['reply'];
$params['unsubscribes'] = $report['event_totals']['unsubscribe'];
$params['optouts'] = $report['event_totals']['optout'];
$params['bounces'] = $report['event_totals']['bounce'];
}
}
// Check if a stats entry already exists
$stat_id = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_mailing_archive_stat WHERE mailing_id = %1', [
1 => [$mailing_id, 'Positive'],
]);
if ($stat_id) {
$params['id'] = $stat_id;
}
CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
......@@ -38,6 +56,26 @@ class CRM_Archivemailing_BAO_MailingArchiveStat extends CRM_Archivemailing_DAO_M
$instance->save();
CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
// Delete the stats from CiviMail queue tables, assuming the FKs cascade deletion
$result = civicrm_api3('MailingJob', 'get', [
'mailing_id' => $mailing_id,
]);
if (!empty($result['values'])) {
$job_ids = array_keys($result['values']);
CRM_Core_DAO::executeQuery('DELETE FROM civicrm_mailing_event_queue where job_id IN (%1)', [
1 => [implode(',', $job_ids), 'CommaSeparatedIntegers'],
]);
}
// Update the mailing as being archived
// Somewhat using the DAO on purpose, in case eventually we add a hook to detect mailing archival
// from the UI, and want to automatically trigger the archival of stats.
CRM_Core_DAO::executeQuery('UPDATE civicrm_mailing SET is_archived = 1 WHERE id = %1', [
1 => [$mailing_id, 'Positive'],
]);
return $instance;
}
......
......@@ -33,11 +33,12 @@ Previous discussion on this topic:
* https://civicrm.stackexchange.com/questions/28483/best-way-to-cleanup-civimail-database-tables
* https://forum.civicrm.org/index.php%3Ftopic=6079.0.html
## Known issues
* CiviCRM 5.48 will include this patch: https://github.com/civicrm/civicrm-core/pull/22800 - until then, mailing recipients are recalculated when we view the Mailing Report.
## TODO
* Hoping this gets merged: https://github.com/civicrm/civicrm-core/pull/22800
* Api function to archive the mailings.
* delete from civicrm_mailing_event_queue where job_id IN (2270,2271,2273,2278,2279,2281); - it will cascade the deletion for clics, opens, bounces, etc.
* Override `CRM/Mailing/Page/Report::run` via hook, so that we can populate the compiled stats instead.
* When a mailing is archived in the interface, archive the stats.
* Delete CiviCRM activities related to the mailing? (or mailing activities older than X days)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment