From 9a170f534f03d99eb941d969c2320d811aff644b Mon Sep 17 00:00:00 2001 From: Justin Freeman Date: Mon, 23 May 2022 13:37:10 +1000 Subject: [PATCH] PROJ-2288 Exclude contacts from custom greeting which are deceased, do not trade, do not email, deleted, opt out --- api/v3/Job/Partneremailgreeting.php | 15 +++++------ info.xml | 4 +-- partneremailgreeting.php | 39 +++++++++++++++++------------ 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/api/v3/Job/Partneremailgreeting.php b/api/v3/Job/Partneremailgreeting.php index 1a211d6..96102d6 100644 --- a/api/v3/Job/Partneremailgreeting.php +++ b/api/v3/Job/Partneremailgreeting.php @@ -34,13 +34,8 @@ function civicrm_api3_job_Partneremailgreeting($params) { $relationships = Relationship::get() ->addWhere('is_active', '=', TRUE) - ->addClause('OR', [ - 'relationship_type_id:name', - '=', + ->addWhere('relationship_type_id:name', 'IN', [ 'Partner of', - ], [ - 'relationship_type_id:name', - '=', 'Spouse of', ]) ->execute(); @@ -56,11 +51,17 @@ function civicrm_api3_job_Partneremailgreeting($params) { '=', $relationship['contact_id_b'], ]) + ->addWhere('is_deceased', '=', FALSE) + ->addWhere('is_deleted', '=', FALSE) + ->addWhere('do_not_email', '=', FALSE) + ->addWhere('do_not_trade', '=', FALSE) + ->addWhere('is_opt_out', '=', FALSE) ->setLimit(2) ->execute()->getArrayCopy(); + // If either contact does not meet the criteria then it will be excluded from the results and contact count < 2 // Check that the first name is set for both contacts before setting the customised greeting - if ($contacts[0]['first_name'] && $contacts[1]['first_name']) { + if (count($contacts) == 2 && $contacts[0]['first_name'] && $contacts[1]['first_name']) { Contact::update() ->addValue('email_greeting_custom', 'Dear ' . $contacts[0]['first_name'] . ' and ' . $contacts[1]['first_name']) ->addValue('email_greeting_id:name', 'Customized') diff --git a/info.xml b/info.xml index 7c1ed25..7d12725 100644 --- a/info.xml +++ b/info.xml @@ -14,8 +14,8 @@ https://github.com/agileware/au.com.agileware.partneremailgreeting http://www.gnu.org/licenses/agpl-3.0.html - 2022-05-17 - 1.0 + 2022-05-23 + 1.1 stable 5.0 diff --git a/partneremailgreeting.php b/partneremailgreeting.php index 6f91408..226f459 100644 --- a/partneremailgreeting.php +++ b/partneremailgreeting.php @@ -140,11 +140,17 @@ function partneremailgreeting_relationshipchange(PostEvent $event) { '=', $event->object->contact_id_b, ]) + ->addWhere('is_deceased', '=', FALSE) + ->addWhere('is_deleted', '=', FALSE) + ->addWhere('do_not_email', '=', FALSE) + ->addWhere('do_not_trade', '=', FALSE) + ->addWhere('is_opt_out', '=', FALSE) ->setLimit(2) ->execute()->getArrayCopy(); + // If either contact does not meet the criteria then it will be excluded from the results and contact count < 2 // Check that the first name is set for both contacts before setting the customised greeting - if ($contacts[0]['first_name'] && $contacts[1]['first_name']) { + if (count($contacts) == 2 && $contacts[0]['first_name'] && $contacts[1]['first_name']) { Contact::update() ->addValue('email_greeting_custom', 'Dear ' . $contacts[0]['first_name'] . ' and ' . $contacts[1]['first_name']) ->addValue('email_greeting_id:name', 'Customized') @@ -163,25 +169,26 @@ function partneremailgreeting_relationshipchange(PostEvent $event) { // If the first name is not set for either contact then reset to the standard greeting $custom_greeting = FALSE; } - } - if ($relationship_match && !$custom_greeting) { - Contact::update() - ->addValue('email_greeting_id:name', 'Dear {contact.first_name}') - ->addClause('OR', [ - 'id', - '=', - $event->object->contact_id_a, - ], [ - 'id', - '=', - $event->object->contact_id_b, - ]) - ->execute(); + if ($relationship_match && !$custom_greeting) { + Contact::update() + ->addValue('email_greeting_id:name', 'Dear {contact.first_name}') + ->addClause('OR', [ + 'id', + '=', + $event->object->contact_id_a, + ], [ + 'id', + '=', + $event->object->contact_id_b, + ]) + ->execute(); + } } } } - catch (API_Exception $e) { + catch + (API_Exception $e) { $errorMessage = $e->getMessage(); CRM_Core_Error::debug_var('partneremailgreeting::partneremailgreeting_relationshipchange', $errorMessage); }