From 6aac400ddb8617d4f67f33c800a67ce87f57f5b8 Mon Sep 17 00:00:00 2001 From: ManuSinergiaCRM Date: Tue, 18 Jun 2024 17:10:20 +0000 Subject: [PATCH 1/8] =?UTF-8?q?Auditar=20emails=20rehusados=20a=20trav?= =?UTF-8?q?=C3=A9s=20de=20una=20campa=C3=B1a=20de=20tipo=20email?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/Campaigns/RemoveMe.php | 35 +++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/Campaigns/RemoveMe.php b/modules/Campaigns/RemoveMe.php index 7dc1ab48357..37748b470fb 100755 --- a/modules/Campaigns/RemoveMe.php +++ b/modules/Campaigns/RemoveMe.php @@ -88,13 +88,46 @@ $id = $db->quote($id); //no opt out for users. - if (preg_match('/^[0-9A-Za-z\-]*$/', $id) && $module != 'Users') { + if (preg_match('/^[0-9A-Za-z\-]*$/', $id) && $module != 'Users') + { //record this activity in the campaing log table.. $query = "UPDATE email_addresses SET email_addresses.opt_out = 1 WHERE EXISTS(SELECT 1 FROM email_addr_bean_rel ear WHERE ear.bean_id = '$id' AND ear.deleted=0 AND email_addresses.id = ear.email_address_id)"; $status=$db->query($query); if ($status) { echo "*"; } + + // STIC-Custom 202410617 MHP - Audit in email_addresses_audit table the unsubscription action through the campaign email + // + + // SELECT the email_address_id from the emails related to the user who is unsubscribing + $query = "SELECT email_address_id FROM email_addr_bean_rel WHERE bean_id = '$id';"; + $result = $db->query($query); + + while ($row = $db->fetchByAssoc($result)) { + $emailAddressesID[] = "'" . $row['email_address_id'] . "'" ; + } + $emailsContidion = implode(", ", $emailAddressesID); + + $query = "SELECT id, opt_out FROM `email_addresses` WHERE `id` IN ($emailsContidion);"; + $result = $db->query($query); + + // Retrieve the opt-out value for each email and, if the value has changed, insert the audit log + while ($row = $db->fetchByAssoc($result)) + { + if ($row['opt_out'] != '1') { + $newId = create_guid(); + $emailAddressId = $row['id']; + $query = "INSERT INTO email_addresses_audit + (id, parent_id, date_created, created_by, field_name, data_type, before_value_string, after_value_string, before_value_text, after_value_text) VALUES + ('$newId', '$emailAddressId', UTC_TIMESTAMP(), '1', 'opt_out','bool','0','1',NULL,NULL)"; + $result2 = $db->query($query); + if ($result2) { + $GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Error inserting the corresponding record with parent_id = $id into the email_addresses_audit table"); + } + } + } + // END STIC-Custom } } //Print Confirmation Message. From bb7ffd94185b22cdf9b398af65da118c36b9112b Mon Sep 17 00:00:00 2001 From: ManuSinergiaCRM Date: Wed, 19 Jun 2024 07:22:42 +0000 Subject: [PATCH 2/8] wip - indicar identificador de PR --- modules/Campaigns/RemoveMe.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/modules/Campaigns/RemoveMe.php b/modules/Campaigns/RemoveMe.php index 37748b470fb..bc656ab9eac 100755 --- a/modules/Campaigns/RemoveMe.php +++ b/modules/Campaigns/RemoveMe.php @@ -88,8 +88,7 @@ $id = $db->quote($id); //no opt out for users. - if (preg_match('/^[0-9A-Za-z\-]*$/', $id) && $module != 'Users') - { + if (preg_match('/^[0-9A-Za-z\-]*$/', $id) && $module != 'Users') { //record this activity in the campaing log table.. $query = "UPDATE email_addresses SET email_addresses.opt_out = 1 WHERE EXISTS(SELECT 1 FROM email_addr_bean_rel ear WHERE ear.bean_id = '$id' AND ear.deleted=0 AND email_addresses.id = ear.email_address_id)"; $status=$db->query($query); @@ -98,7 +97,7 @@ } // STIC-Custom 202410617 MHP - Audit in email_addresses_audit table the unsubscription action through the campaign email - // + // https://github.com/SinergiaTIC/SinergiaCRM/pull/277 // SELECT the email_address_id from the emails related to the user who is unsubscribing $query = "SELECT email_address_id FROM email_addr_bean_rel WHERE bean_id = '$id';"; @@ -108,7 +107,6 @@ $emailAddressesID[] = "'" . $row['email_address_id'] . "'" ; } $emailsContidion = implode(", ", $emailAddressesID); - $query = "SELECT id, opt_out FROM `email_addresses` WHERE `id` IN ($emailsContidion);"; $result = $db->query($query); From abf07a5e1c0a8f5d75b464255d9ac167fd5d4c11 Mon Sep 17 00:00:00 2001 From: ManuSinergiaCRM Date: Wed, 19 Jun 2024 09:53:22 +0000 Subject: [PATCH 3/8] wip --- modules/Campaigns/RemoveMe.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/modules/Campaigns/RemoveMe.php b/modules/Campaigns/RemoveMe.php index bc656ab9eac..85e97fa0d3a 100755 --- a/modules/Campaigns/RemoveMe.php +++ b/modules/Campaigns/RemoveMe.php @@ -89,14 +89,8 @@ //no opt out for users. if (preg_match('/^[0-9A-Za-z\-]*$/', $id) && $module != 'Users') { - //record this activity in the campaing log table.. - $query = "UPDATE email_addresses SET email_addresses.opt_out = 1 WHERE EXISTS(SELECT 1 FROM email_addr_bean_rel ear WHERE ear.bean_id = '$id' AND ear.deleted=0 AND email_addresses.id = ear.email_address_id)"; - $status=$db->query($query); - if ($status) { - echo "*"; - } - // STIC-Custom 202410617 MHP - Audit in email_addresses_audit table the unsubscription action through the campaign email + // STIC-Custom 202410617 MHP - Audit in email_addresses_audit table the unsubscription action through the campaign email // https://github.com/SinergiaTIC/SinergiaCRM/pull/277 // SELECT the email_address_id from the emails related to the user who is unsubscribing @@ -126,6 +120,13 @@ } } // END STIC-Custom + + //record this activity in the campaing log table.. + $query = "UPDATE email_addresses SET email_addresses.opt_out = 1 WHERE EXISTS(SELECT 1 FROM email_addr_bean_rel ear WHERE ear.bean_id = '$id' AND ear.deleted=0 AND email_addresses.id = ear.email_address_id)"; + $status=$db->query($query); + if ($status) { + echo "*"; + } } } //Print Confirmation Message. From e7a0440bcd440493a4a1c5f1d0cf986c982a6f26 Mon Sep 17 00:00:00 2001 From: ManuSinergiaCRM Date: Thu, 11 Jul 2024 12:42:30 +0000 Subject: [PATCH 4/8] =?UTF-8?q?Filtrar=20en=20la=20consulta=20SQL=20+=20a?= =?UTF-8?q?=C3=B1adir=20deleted=20=3D=200=20a=20otra=20query?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/Campaigns/RemoveMe.php | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/modules/Campaigns/RemoveMe.php b/modules/Campaigns/RemoveMe.php index 85e97fa0d3a..eb1d30b5a16 100755 --- a/modules/Campaigns/RemoveMe.php +++ b/modules/Campaigns/RemoveMe.php @@ -94,32 +94,30 @@ // https://github.com/SinergiaTIC/SinergiaCRM/pull/277 // SELECT the email_address_id from the emails related to the user who is unsubscribing - $query = "SELECT email_address_id FROM email_addr_bean_rel WHERE bean_id = '$id';"; + $query = "SELECT email_address_id FROM email_addr_bean_rel WHERE deleted = 0 AND bean_id = '$id';"; $result = $db->query($query); while ($row = $db->fetchByAssoc($result)) { $emailAddressesID[] = "'" . $row['email_address_id'] . "'" ; } - $emailsContidion = implode(", ", $emailAddressesID); - $query = "SELECT id, opt_out FROM `email_addresses` WHERE `id` IN ($emailsContidion);"; + $emailsCondition = implode(", ", $emailAddressesID); + $query = "SELECT id, opt_out FROM `email_addresses` WHERE opt_out != '1' AND `id` IN ($emailsCondition);"; $result = $db->query($query); // Retrieve the opt-out value for each email and, if the value has changed, insert the audit log while ($row = $db->fetchByAssoc($result)) { - if ($row['opt_out'] != '1') { - $newId = create_guid(); - $emailAddressId = $row['id']; - $query = "INSERT INTO email_addresses_audit - (id, parent_id, date_created, created_by, field_name, data_type, before_value_string, after_value_string, before_value_text, after_value_text) VALUES - ('$newId', '$emailAddressId', UTC_TIMESTAMP(), '1', 'opt_out','bool','0','1',NULL,NULL)"; - $result2 = $db->query($query); - if ($result2) { - $GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Error inserting the corresponding record with parent_id = $id into the email_addresses_audit table"); - } + $newId = create_guid(); + $emailAddressId = $row['id']; + $query = "INSERT INTO email_addresses_audit + (id, parent_id, date_created, created_by, field_name, data_type, before_value_string, after_value_string, before_value_text, after_value_text) VALUES + ('$newId', '$emailAddressId', UTC_TIMESTAMP(), '1', 'opt_out','bool','0','1',NULL,NULL)"; + $result2 = $db->query($query); + if ($result2) { + $GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Error inserting the corresponding record with parent_id = $id into the email_addresses_audit table"); } - } - // END STIC-Custom + } + // END STIC-Custom //record this activity in the campaing log table.. $query = "UPDATE email_addresses SET email_addresses.opt_out = 1 WHERE EXISTS(SELECT 1 FROM email_addr_bean_rel ear WHERE ear.bean_id = '$id' AND ear.deleted=0 AND email_addresses.id = ear.email_address_id)"; From 917f29552c0731dd4f6651f176d38f22212fc7eb Mon Sep 17 00:00:00 2001 From: ManuSinergiaCRM Date: Thu, 12 Sep 2024 09:53:50 +0000 Subject: [PATCH 5/8] =?UTF-8?q?Invertir=20condici=C3=B3n=20para=20pintar?= =?UTF-8?q?=20el=20log=20de=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/Campaigns/RemoveMe.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/Campaigns/RemoveMe.php b/modules/Campaigns/RemoveMe.php index eb1d30b5a16..ac483a89e1a 100755 --- a/modules/Campaigns/RemoveMe.php +++ b/modules/Campaigns/RemoveMe.php @@ -113,7 +113,7 @@ (id, parent_id, date_created, created_by, field_name, data_type, before_value_string, after_value_string, before_value_text, after_value_text) VALUES ('$newId', '$emailAddressId', UTC_TIMESTAMP(), '1', 'opt_out','bool','0','1',NULL,NULL)"; $result2 = $db->query($query); - if ($result2) { + if (!$result2) { $GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Error inserting the corresponding record with parent_id = $id into the email_addresses_audit table"); } } From 22e14da98d7047f839591dcd57eb9bfeea8a1b79 Mon Sep 17 00:00:00 2001 From: ManuSinergiaCRM Date: Mon, 23 Sep 2024 13:52:50 +0000 Subject: [PATCH 6/8] Control de errores en peticiones SQL --- modules/Campaigns/RemoveMe.php | 59 +++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/modules/Campaigns/RemoveMe.php b/modules/Campaigns/RemoveMe.php index ac483a89e1a..56d2d43f2b9 100755 --- a/modules/Campaigns/RemoveMe.php +++ b/modules/Campaigns/RemoveMe.php @@ -92,39 +92,54 @@ // STIC-Custom 202410617 MHP - Audit in email_addresses_audit table the unsubscription action through the campaign email // https://github.com/SinergiaTIC/SinergiaCRM/pull/277 + // //record this activity in the campaing log table.. + // $query = "UPDATE email_addresses SET email_addresses.opt_out = 1 WHERE EXISTS(SELECT 1 FROM email_addr_bean_rel ear WHERE ear.bean_id = '$id' AND ear.deleted=0 AND email_addresses.id = ear.email_address_id)"; + // $status=$db->query($query); + // if ($status) { + // echo "*"; + // } // SELECT the email_address_id from the emails related to the user who is unsubscribing $query = "SELECT email_address_id FROM email_addr_bean_rel WHERE deleted = 0 AND bean_id = '$id';"; $result = $db->query($query); - while ($row = $db->fetchByAssoc($result)) { - $emailAddressesID[] = "'" . $row['email_address_id'] . "'" ; - } - $emailsCondition = implode(", ", $emailAddressesID); - $query = "SELECT id, opt_out FROM `email_addresses` WHERE opt_out != '1' AND `id` IN ($emailsCondition);"; - $result = $db->query($query); + if (!empty($result->num_rows)) + { + while ($row = $db->fetchByAssoc($result)) { + $emailAddressesID[] = "'" . $row['email_address_id'] . "'" ; + } + $emailsCondition = implode(", ", $emailAddressesID); + $query = "SELECT id, opt_out FROM `email_addresses` WHERE opt_out != '1' AND `id` IN ($emailsCondition);"; + $result = $db->query($query); - // Retrieve the opt-out value for each email and, if the value has changed, insert the audit log - while ($row = $db->fetchByAssoc($result)) - { - $newId = create_guid(); - $emailAddressId = $row['id']; - $query = "INSERT INTO email_addresses_audit + if (!empty($result->num_rows)) + { + // Retrieve the opt-out value for each email and, if the value has changed, insert the audit log + while ($row = $db->fetchByAssoc($result)) + { + $newId = create_guid(); + $emailAddressId = $row['id']; + $query = "INSERT INTO email_addresses_audit (id, parent_id, date_created, created_by, field_name, data_type, before_value_string, after_value_string, before_value_text, after_value_text) VALUES ('$newId', '$emailAddressId', UTC_TIMESTAMP(), '1', 'opt_out','bool','0','1',NULL,NULL)"; - $result2 = $db->query($query); - if (!$result2) { - $GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Error inserting the corresponding record with parent_id = $id into the email_addresses_audit table"); + + $result2 = $db->query($query); + if (!$result2) { + $GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Error inserting the corresponding record with parent_id = $id into the email_addresses_audit table"); + } + } + + //record this activity in the campaing log table.. + $query = "UPDATE email_addresses SET email_addresses.opt_out = 1 WHERE EXISTS(SELECT 1 FROM email_addr_bean_rel ear WHERE ear.bean_id = '$id' AND ear.deleted=0 AND email_addresses.id = ear.email_address_id)"; + $status=$db->query($query); + if ($status) { + echo "*"; + } } + } else { + $GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": No emails associated with the contact with id = '$id' have been found"); } // END STIC-Custom - - //record this activity in the campaing log table.. - $query = "UPDATE email_addresses SET email_addresses.opt_out = 1 WHERE EXISTS(SELECT 1 FROM email_addr_bean_rel ear WHERE ear.bean_id = '$id' AND ear.deleted=0 AND email_addresses.id = ear.email_address_id)"; - $status=$db->query($query); - if ($status) { - echo "*"; - } } } //Print Confirmation Message. From ba9b7a20dfd7572557ae152ad5d16488cb59d9cd Mon Sep 17 00:00:00 2001 From: ManuSinergiaCRM Date: Wed, 2 Oct 2024 12:54:46 +0000 Subject: [PATCH 7/8] Actualizar Flag para auditar emails una vez gestionados todos los emails del registro --- include/SugarEmailAddress/SugarEmailAddress.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/SugarEmailAddress/SugarEmailAddress.php b/include/SugarEmailAddress/SugarEmailAddress.php index b845bc58155..2ceeb664a8b 100755 --- a/include/SugarEmailAddress/SugarEmailAddress.php +++ b/include/SugarEmailAddress/SugarEmailAddress.php @@ -693,6 +693,10 @@ public function saveEmail( if (!empty($this->addresses)) { // insert new relationships and create email address record, if they don't exist foreach ($this->addresses as $address) { + // STIC-Custom 20241002 MHP - Set the createdAuditRecords flag to false to continue auditing the rest of the emails in the record + // https://github.com/SinergiaTIC/SinergiaCRM/pull/277 + $this->createdAuditRecords = false; + // END STIC-Custom if (!empty($address['email_address'])) { $guid = create_guid(); $emailId = isset($address['email_address_id']) @@ -736,6 +740,10 @@ public function saveEmail( } } } + // STIC-Custom 20241002 MHP - Set the createdAuditRecords flag to true once any changes to email properties have been audited + // https://github.com/SinergiaTIC/SinergiaCRM/pull/277 + $this->createdAuditRecords = true; + // END STIC-Custom } //delete link to dropped email address. From 5c76c04d5b72672fc9f0548e1ae495bf2cb86a6f Mon Sep 17 00:00:00 2001 From: ManuSinergiaCRM Date: Wed, 2 Oct 2024 13:03:32 +0000 Subject: [PATCH 8/8] wip --- include/SugarEmailAddress/SugarEmailAddress.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/SugarEmailAddress/SugarEmailAddress.php b/include/SugarEmailAddress/SugarEmailAddress.php index 2ceeb664a8b..cbc054690b9 100755 --- a/include/SugarEmailAddress/SugarEmailAddress.php +++ b/include/SugarEmailAddress/SugarEmailAddress.php @@ -740,7 +740,7 @@ public function saveEmail( } } } - // STIC-Custom 20241002 MHP - Set the createdAuditRecords flag to true once any changes to email properties have been audited + // STIC-Custom 20241002 MHP - Set the createdAuditRecords flag to true once changes to all emails in the record have been audited // https://github.com/SinergiaTIC/SinergiaCRM/pull/277 $this->createdAuditRecords = true; // END STIC-Custom