Skip to content

Commit

Permalink
PROJ-2288 Exclude contacts from custom greeting which are deceased, d…
Browse files Browse the repository at this point in the history
…o not trade, do not email, deleted, opt out
  • Loading branch information
agileware-justin committed May 23, 2022
1 parent fabd87f commit 9a170f5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
15 changes: 8 additions & 7 deletions api/v3/Job/Partneremailgreeting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<url desc="Support">https://github.com/agileware/au.com.agileware.partneremailgreeting</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2022-05-17</releaseDate>
<version>1.0</version>
<releaseDate>2022-05-23</releaseDate>
<version>1.1</version>
<develStage>stable</develStage>
<compatibility>
<ver>5.0</ver>
Expand Down
39 changes: 23 additions & 16 deletions partneremailgreeting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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);
}
Expand Down

0 comments on commit 9a170f5

Please sign in to comment.