CRM-18345: Don't delete mailing data on email/phone deletion
authorJKingsnorth <john@johnkingsnorth.co.uk>
Mon, 4 Apr 2016 10:53:12 +0000 (11:53 +0100)
committerJKingsnorth <john@johnkingsnorth.co.uk>
Tue, 5 Apr 2016 13:41:46 +0000 (14:41 +0100)
CRM/Report/Form/Mailing/Bounce.php
CRM/Report/Form/Mailing/Clicks.php
CRM/Report/Form/Mailing/Detail.php
CRM/Report/Form/Mailing/Opened.php
CRM/Upgrade/Incremental/sql/4.7.5.mysql.tpl
xml/schema/Mailing/Event/Queue.xml
xml/schema/Mailing/Recipients.xml

index f7f6fb1fda87df56b41fe1b2f678a5239fc07868..82e1b0a6ac58cb2de7dbdd8d319b9e4a089acbd3 100644 (file)
@@ -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'] = '<del>Email address deleted</del>';
+        }
+        $entryFound = TRUE;
+      }
+
       // make count columns point to detail report
       // convert display name to links
       if (array_key_exists('civicrm_contact_sort_name', $row) &&
index 3c67fe6d4a6e2bc956f2aff423b68b72f011de33..d8bdf6cf2e0c0287bc10019b3f5a151652ccd21d 100644 (file)
@@ -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'] = '<del>Email address deleted</del>';
+        }
+        $entryFound = TRUE;
+      }
+
       // make count columns point to detail report
       // convert display name to links
       if (array_key_exists('civicrm_contact_sort_name', $row) &&
index 00837cbeef1a4939dd770707497a5882e565471f..ccbddd44e0331f543d80de4fd3ddeee6371c9963 100644 (file)
@@ -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'] = '<del>Email address deleted</del>';
+        }
+        $entryFound = TRUE;
+      }
+
       // make count columns point to detail report
       // convert display name to links
       if (array_key_exists('civicrm_contact_sort_name', $row) &&
index c01a3314e80b289c71ad19e4da2c846bca4fcc4b..5f210fdc903c6a58990c3048df8485c2e4cfab61 100644 (file)
@@ -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'] = '<del>Email address deleted</del>';
+        }
+        $entryFound = TRUE;
+      }
+      
       // make count columns point to detail report
       // convert display name to links
       if (array_key_exists('civicrm_contact_sort_name', $row) &&
index 5138ace125682f98a9a08acfac4fd5a5ab80e8af..acb2898c55fa887daf7f3154e9680635a04a80df 100644 (file)
@@ -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;
index c8d2028a80001bf5fad332ee0ff9aced060f3026..f6b251663cc1ee8c72d58ade6cb848258c3268b7 100644 (file)
@@ -40,7 +40,7 @@
     <name>email_id</name>
     <table>civicrm_email</table>
     <key>id</key>
-    <onDelete>CASCADE</onDelete>
+    <onDelete>SET NULL</onDelete>
   </foreignKey>
   <field>
     <name>contact_id</name>
@@ -74,6 +74,6 @@
     <name>phone_id</name>
     <table>civicrm_phone</table>
     <key>id</key>
-    <onDelete>CASCADE</onDelete>
+    <onDelete>SET NULL</onDelete>
   </foreignKey>
 </table>
index 188f64dbb1fa9942446e328ce1c57c0559debb19..729354c184b0a3ce1bbb7047e43bba2a6eac7f7d 100644 (file)
@@ -53,7 +53,7 @@
     <name>email_id</name>
     <table>civicrm_email</table>
     <key>id</key>
-    <onDelete>CASCADE</onDelete>
+    <onDelete>SET NULL</onDelete>
   </foreignKey>
   <field>
     <name>phone_id</name>
@@ -66,6 +66,6 @@
     <name>phone_id</name>
     <table>civicrm_phone</table>
     <key>id</key>
-    <onDelete>CASCADE</onDelete>
+    <onDelete>SET NULL</onDelete>
   </foreignKey>
 </table>