-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
partneremailgreeting.php
195 lines (172 loc) · 6.42 KB
/
partneremailgreeting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
require_once 'partneremailgreeting.civix.php';
// phpcs:disable
use Civi\Api4\Contact;
use Civi\Api4\RelationshipType;
use Civi\Core\Event\PostEvent;
use CRM_Partneremailgreeting_ExtensionUtil as E;
// phpcs:enable
/**
* Implements hook_civicrm_config().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
*/
function partneremailgreeting_civicrm_config(&$config) {
_partneremailgreeting_civix_civicrm_config($config);
if (isset(Civi::$statics[__FUNCTION__])) {
return;
}
Civi::$statics[__FUNCTION__] = 1;
\Civi::dispatcher()
->addListener('hook_civicrm_post', 'partneremailgreeting_relationshipchange');
}
/**
* Implements hook_civicrm_install().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
function partneremailgreeting_civicrm_install() {
_partneremailgreeting_civix_civicrm_install();
}
/**
* Implements hook_civicrm_postInstall().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
*/
function partneremailgreeting_civicrm_postInstall() {
_partneremailgreeting_civix_civicrm_postInstall();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
*/
function partneremailgreeting_civicrm_uninstall() {
_partneremailgreeting_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
function partneremailgreeting_civicrm_enable() {
_partneremailgreeting_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_disable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
*/
function partneremailgreeting_civicrm_disable() {
_partneremailgreeting_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade
*/
function partneremailgreeting_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _partneremailgreeting_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_entityTypes().
*
* Declare entity types provided by this module.
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
*/
function partneremailgreeting_civicrm_entityTypes(&$entityTypes) {
_partneremailgreeting_civix_civicrm_entityTypes($entityTypes);
}
function partneremailgreeting_relationshipchange(PostEvent $event) {
try {
if ($event->object instanceof CRM_Contact_DAO_Relationship) {
$relationshipTypes = RelationshipType::get()
->addSelect('id')
->addWhere('contact_type_a', '=', 'Individual')
->addClause('OR', [
'name_a_b',
'=',
'Spouse of',
], [
'name_a_b',
'=',
'Partner of',
])
->setLimit(2)
->execute()
->getArrayCopy();
$relationship_match = FALSE;
if ($event->object->relationship_type_id == $relationshipTypes[0]['id'] || $event->object->relationship_type_id == $relationshipTypes[1]['id']) {
$relationship_match = TRUE;
}
$custom_greeting = FALSE;
if ($event->action == 'create' && $relationship_match) {
$custom_greeting = TRUE;
}
elseif ($event->action == 'edit' && $relationship_match && $event->object->is_active == '1') {
$custom_greeting = TRUE;
}
if ($relationship_match && $custom_greeting) {
$contacts = Contact::get()
->addSelect('first_name')
->addClause('OR', [
'id',
'=',
$event->object->contact_id_a,
], [
'id',
'=',
$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 (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')
->addClause('OR', [
'id',
'=',
$event->object->contact_id_a,
], [
'id',
'=',
$event->object->contact_id_b,
])
->execute();
}
else {
// 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();
}
}
}
}
catch
(API_Exception $e) {
$errorMessage = $e->getMessage();
CRM_Core_Error::debug_var('partneremailgreeting::partneremailgreeting_relationshipchange', $errorMessage);
}
}