From: JKingsnorth Date: Mon, 4 Apr 2016 10:53:12 +0000 (+0100) Subject: CRM-18345: Don't delete mailing data on email/phone deletion X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d2c742e91f9e3d4f05817955d445ab2a63b00798;p=civicrm-core.git CRM-18345: Don't delete mailing data on email/phone deletion --- diff --git a/CRM/Report/Form/Mailing/Bounce.php b/CRM/Report/Form/Mailing/Bounce.php index f7f6fb1fda..82e1b0a6ac 100644 --- a/CRM/Report/Form/Mailing/Bounce.php +++ b/CRM/Report/Form/Mailing/Bounce.php @@ -189,7 +189,6 @@ class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form { 'email' => array( 'title' => ts('Email'), 'no_repeat' => TRUE, - 'required' => TRUE, ), 'on_hold' => array( 'title' => ts('On hold'), @@ -294,7 +293,7 @@ class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form { $this->_from .= " INNER JOIN civicrm_mailing_event_queue ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id - INNER JOIN civicrm_email {$this->_aliases['civicrm_email']} + LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id INNER JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']} ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = civicrm_mailing_event_queue.id @@ -439,6 +438,15 @@ class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form { $entryFound = FALSE; foreach ($rows as $rowNum => $row) { + + // If the email address has been deleted + if (array_key_exists('civicrm_email_email', $row)) { + if (empty($rows[$rowNum]['civicrm_email_email'])) { + $rows[$rowNum]['civicrm_email_email'] = 'Email address deleted'; + } + $entryFound = TRUE; + } + // make count columns point to detail report // convert display name to links if (array_key_exists('civicrm_contact_sort_name', $row) && diff --git a/CRM/Report/Form/Mailing/Clicks.php b/CRM/Report/Form/Mailing/Clicks.php index 3c67fe6d4a..d8bdf6cf2e 100644 --- a/CRM/Report/Form/Mailing/Clicks.php +++ b/CRM/Report/Form/Mailing/Clicks.php @@ -134,7 +134,6 @@ class CRM_Report_Form_Mailing_Clicks extends CRM_Report_Form { 'email' => array( 'title' => ts('Email'), 'no_repeat' => TRUE, - 'required' => TRUE, ), ), 'grouping' => 'contact-fields', @@ -231,7 +230,7 @@ class CRM_Report_Form_Mailing_Clicks extends CRM_Report_Form { $this->_from .= " INNER JOIN civicrm_mailing_event_queue ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id - INNER JOIN civicrm_email {$this->_aliases['civicrm_email']} + LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id INNER JOIN civicrm_mailing_event_trackable_url_open ON civicrm_mailing_event_trackable_url_open.event_queue_id = civicrm_mailing_event_queue.id @@ -320,6 +319,15 @@ class CRM_Report_Form_Mailing_Clicks extends CRM_Report_Form { public function alterDisplay(&$rows) { $entryFound = FALSE; foreach ($rows as $rowNum => $row) { + + // If the email address has been deleted + if (array_key_exists('civicrm_email_email', $row)) { + if (empty($rows[$rowNum]['civicrm_email_email'])) { + $rows[$rowNum]['civicrm_email_email'] = 'Email address deleted'; + } + $entryFound = TRUE; + } + // make count columns point to detail report // convert display name to links if (array_key_exists('civicrm_contact_sort_name', $row) && diff --git a/CRM/Report/Form/Mailing/Detail.php b/CRM/Report/Form/Mailing/Detail.php index 00837cbeef..ccbddd44e0 100644 --- a/CRM/Report/Form/Mailing/Detail.php +++ b/CRM/Report/Form/Mailing/Detail.php @@ -236,7 +236,6 @@ class CRM_Report_Form_Mailing_Detail extends CRM_Report_Form { 'fields' => array( 'email' => array( 'title' => ts('Email'), - 'required' => TRUE, ), ), 'grouping' => 'contact-fields', @@ -311,7 +310,7 @@ class CRM_Report_Form_Mailing_Detail extends CRM_Report_Form { $this->_from .= " INNER JOIN civicrm_mailing_event_queue ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id - INNER JOIN civicrm_email {$this->_aliases['civicrm_email']} + LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id"; if (array_key_exists('delivery_id', $this->_params['fields'])) { @@ -459,6 +458,15 @@ class CRM_Report_Form_Mailing_Detail extends CRM_Report_Form { public function alterDisplay(&$rows) { $entryFound = FALSE; foreach ($rows as $rowNum => $row) { + + // If the email address has been deleted + if (array_key_exists('civicrm_email_email', $row)) { + if (empty($rows[$rowNum]['civicrm_email_email'])) { + $rows[$rowNum]['civicrm_email_email'] = 'Email address deleted'; + } + $entryFound = TRUE; + } + // make count columns point to detail report // convert display name to links if (array_key_exists('civicrm_contact_sort_name', $row) && diff --git a/CRM/Report/Form/Mailing/Opened.php b/CRM/Report/Form/Mailing/Opened.php index c01a3314e8..5f210fdc90 100644 --- a/CRM/Report/Form/Mailing/Opened.php +++ b/CRM/Report/Form/Mailing/Opened.php @@ -134,7 +134,6 @@ class CRM_Report_Form_Mailing_Opened extends CRM_Report_Form { 'email' => array( 'title' => ts('Email'), 'no_repeat' => TRUE, - 'required' => TRUE, ), ), 'order_bys' => array( @@ -211,7 +210,7 @@ class CRM_Report_Form_Mailing_Opened extends CRM_Report_Form { $this->_from .= " INNER JOIN civicrm_mailing_event_queue ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id - INNER JOIN civicrm_email {$this->_aliases['civicrm_email']} + LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id INNER JOIN civicrm_mailing_event_opened ON civicrm_mailing_event_opened.event_queue_id = civicrm_mailing_event_queue.id @@ -297,6 +296,15 @@ class CRM_Report_Form_Mailing_Opened extends CRM_Report_Form { public function alterDisplay(&$rows) { $entryFound = FALSE; foreach ($rows as $rowNum => $row) { + + // If the email address has been deleted + if (array_key_exists('civicrm_email_email', $row)) { + if (empty($rows[$rowNum]['civicrm_email_email'])) { + $rows[$rowNum]['civicrm_email_email'] = 'Email address deleted'; + } + $entryFound = TRUE; + } + // make count columns point to detail report // convert display name to links if (array_key_exists('civicrm_contact_sort_name', $row) && diff --git a/CRM/Upgrade/Incremental/sql/4.7.5.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.7.5.mysql.tpl index 5138ace125..acb2898c55 100644 --- a/CRM/Upgrade/Incremental/sql/4.7.5.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.7.5.mysql.tpl @@ -31,3 +31,44 @@ WHERE ov.name = 'Contact Deleted by Merge' AND og.name = 'activity_type'; {else} ALTER TABLE civicrm_option_value CHANGE label label varchar( 512 ) DEFAULT NULL ; {/if} + +-- CRM-18345: Don't delete mailing records when email address / phone is deleted +ALTER TABLE `civicrm_mailing_event_queue` + DROP FOREIGN KEY `FK_civicrm_mailing_event_queue_email_id`; + +ALTER TABLE `civicrm_mailing_event_queue` + ADD CONSTRAINT `FK_civicrm_mailing_event_queue_email_id` + FOREIGN KEY (`email_id`) + REFERENCES `civicrm_email`(`id`) + ON DELETE SET NULL + ON UPDATE RESTRICT; + +ALTER TABLE `civicrm_mailing_event_queue` + DROP FOREIGN KEY `FK_civicrm_mailing_event_queue_phone_id`; + +ALTER TABLE `civicrm_mailing_event_queue` + ADD CONSTRAINT `FK_civicrm_mailing_event_queue_phone_id` + FOREIGN KEY (`phone_id`) + REFERENCES `civicrm_phone`(`id`) + ON DELETE SET NULL + ON UPDATE RESTRICT; + +ALTER TABLE `civicrm_mailing_recipients` + DROP FOREIGN KEY `FK_civicrm_mailing_recipients_email_id`; + +ALTER TABLE `civicrm_mailing_recipients` + ADD CONSTRAINT `FK_civicrm_mailing_recipients_email_id` + FOREIGN KEY (`email_id`) + REFERENCES `civicrm_email`(`id`) + ON DELETE SET NULL + ON UPDATE RESTRICT; + +ALTER TABLE `civicrm_mailing_recipients` + DROP FOREIGN KEY `FK_civicrm_mailing_recipients_phone_id`; + +ALTER TABLE `civicrm_mailing_recipients` + ADD CONSTRAINT `FK_civicrm_mailing_recipients_phone_id` + FOREIGN KEY (`phone_id`) + REFERENCES `civicrm_phone`(`id`) + ON DELETE SET NULL + ON UPDATE RESTRICT; diff --git a/xml/schema/Mailing/Event/Queue.xml b/xml/schema/Mailing/Event/Queue.xml index c8d2028a80..f6b251663c 100644 --- a/xml/schema/Mailing/Event/Queue.xml +++ b/xml/schema/Mailing/Event/Queue.xml @@ -40,7 +40,7 @@ email_id civicrm_email
id - CASCADE + SET NULL contact_id @@ -74,6 +74,6 @@ phone_id civicrm_phone
id - CASCADE + SET NULL diff --git a/xml/schema/Mailing/Recipients.xml b/xml/schema/Mailing/Recipients.xml index 188f64dbb1..729354c184 100644 --- a/xml/schema/Mailing/Recipients.xml +++ b/xml/schema/Mailing/Recipients.xml @@ -53,7 +53,7 @@ email_id civicrm_email
id - CASCADE + SET NULL phone_id @@ -66,6 +66,6 @@ phone_id civicrm_phone
id - CASCADE + SET NULL