Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
d7b498bc EM |
18 | use Civi\Api4\Email; |
19 | ||
6a488035 | 20 | /** |
192d36c5 | 21 | * This class contains functions for email handling. |
6a488035 | 22 | */ |
7a592535 | 23 | class CRM_Core_BAO_Email extends CRM_Core_DAO_Email implements Civi\Test\HookInterface { |
1d3cbc3c | 24 | use CRM_Contact_AccessTrait; |
6a488035 | 25 | |
cac01cdc | 26 | /** |
192d36c5 | 27 | * Create email address. |
28 | * | |
29 | * Note that the create function calls 'add' but has more business logic. | |
6a488035 | 30 | * |
6a0b768e TO |
31 | * @param array $params |
32 | * Input parameters. | |
77b97be7 EM |
33 | * |
34 | * @return object | |
6a488035 | 35 | */ |
00be9182 | 36 | public static function create($params) { |
3bec4854 | 37 | CRM_Core_BAO_Block::handlePrimary($params, get_class()); |
4b6bf55f | 38 | |
6a488035 TO |
39 | $hook = empty($params['id']) ? 'create' : 'edit'; |
40 | CRM_Utils_Hook::pre($hook, 'Email', CRM_Utils_Array::value('id', $params), $params); | |
41 | ||
42 | $email = new CRM_Core_DAO_Email(); | |
43 | $email->copyValues($params); | |
7629d5a6 | 44 | if (!empty($email->email)) { |
45 | // lower case email field to optimize queries | |
46 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
47 | $email->email = $strtolower($email->email); | |
48 | } | |
6a488035 | 49 | |
93ff2b21 RLAR |
50 | // |
51 | // Since we're setting bulkmail for 1 of this contact's emails, first reset | |
52 | // all their other emails to is_bulkmail false. We shouldn't set the current | |
53 | // email to false even though we are about to reset it to avoid | |
54 | // contaminating the changelog if logging is enabled. (only 1 email | |
55 | // address can have is_bulkmail = true) | |
56 | // | |
57 | // Note setting a the is_bulkmail to '' in $params results in $email->is_bulkmail === 'null'. | |
58 | // @see https://lab.civicrm.org/dev/core/-/issues/2254 | |
59 | // | |
60 | if ($email->is_bulkmail == 1 && !empty($params['contact_id']) && !self::isMultipleBulkMail()) { | |
6a488035 TO |
61 | $sql = " |
62 | UPDATE civicrm_email | |
63 | SET is_bulkmail = 0 | |
64 | WHERE contact_id = {$params['contact_id']} | |
65 | "; | |
2ffdaed1 | 66 | if ($hook === 'edit') { |
2aa397bc TO |
67 | $sql .= " AND id <> {$params['id']}"; |
68 | } | |
6a488035 TO |
69 | CRM_Core_DAO::executeQuery($sql); |
70 | } | |
71 | ||
72 | // handle if email is on hold | |
73 | self::holdEmail($email); | |
74 | ||
75 | $email->save(); | |
76 | ||
da5e820b CW |
77 | $contactId = (int) ($email->contact_id ?? CRM_Core_DAO::getFieldValue(__CLASS__, $email->id, 'contact_id')); |
78 | if ($contactId && $email->is_primary) { | |
79 | $address = $email->email ?? CRM_Core_DAO::getFieldValue(__CLASS__, $email->id, 'email'); | |
80 | self::updateContactName($contactId, $address); | |
81 | } | |
82 | ||
6a488035 TO |
83 | CRM_Utils_Hook::post($hook, 'Email', $email->id, $email); |
84 | return $email; | |
85 | } | |
86 | ||
7a592535 CW |
87 | /** |
88 | * Event fired after modifying an Email. | |
89 | * @param \Civi\Core\Event\PostEvent $event | |
90 | */ | |
91 | public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) { | |
92 | if ($event->action !== 'delete' && !empty($event->object->is_primary) && !empty($event->object->contact_id)) { | |
93 | // update the UF user email if that has changed | |
94 | CRM_Core_BAO_UFMatch::updateUFName($event->object->contact_id); | |
95 | } | |
96 | } | |
97 | ||
ec47f7b4 | 98 | /** |
99 | * Takes an associative array and adds email. | |
100 | * | |
101 | * @param array $params | |
102 | * (reference ) an assoc array of name/value pairs. | |
103 | * | |
104 | * @return object | |
105 | * CRM_Core_BAO_Email object on success, null otherwise | |
106 | */ | |
107 | public static function add(&$params) { | |
108 | CRM_Core_Error::deprecatedFunctionWarning('apiv4 create'); | |
109 | return self::create($params); | |
110 | } | |
111 | ||
6a488035 TO |
112 | /** |
113 | * Given the list of params in the params array, fetch the object | |
114 | * and store the values in the values array | |
115 | * | |
6a0b768e TO |
116 | * @param array $entityBlock |
117 | * Input parameters to find object. | |
6a488035 | 118 | * |
cef76e5f | 119 | * @return array |
6a488035 | 120 | */ |
cef76e5f | 121 | public static function getValues($entityBlock) { |
6a488035 TO |
122 | return CRM_Core_BAO_Block::getValues('email', $entityBlock); |
123 | } | |
124 | ||
125 | /** | |
126 | * Get all the emails for a specified contact_id, with the primary email being first | |
127 | * | |
6a0b768e TO |
128 | * @param int $id |
129 | * The contact id. | |
6a488035 | 130 | * |
dd244018 EM |
131 | * @param bool $updateBlankLocInfo |
132 | * | |
a6c01b45 CW |
133 | * @return array |
134 | * the array of email id's | |
6a488035 | 135 | */ |
00be9182 | 136 | public static function allEmails($id, $updateBlankLocInfo = FALSE) { |
6a488035 TO |
137 | if (!$id) { |
138 | return NULL; | |
139 | } | |
140 | ||
141 | $query = " | |
142 | SELECT email, | |
143 | civicrm_location_type.name as locationType, | |
144 | civicrm_email.is_primary as is_primary, | |
145 | civicrm_email.on_hold as on_hold, | |
146 | civicrm_email.id as email_id, | |
147 | civicrm_email.location_type_id as locationTypeId | |
148 | FROM civicrm_contact | |
149 | LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id ) | |
150 | LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id ) | |
151 | WHERE civicrm_contact.id = %1 | |
152 | ORDER BY civicrm_email.is_primary DESC, email_id ASC "; | |
be2fb01f CW |
153 | $params = [ |
154 | 1 => [ | |
6a488035 TO |
155 | $id, |
156 | 'Integer', | |
be2fb01f CW |
157 | ], |
158 | ]; | |
6a488035 | 159 | |
be2fb01f | 160 | $emails = $values = []; |
353ffa53 TO |
161 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
162 | $count = 1; | |
6a488035 | 163 | while ($dao->fetch()) { |
be2fb01f | 164 | $values = [ |
6a488035 TO |
165 | 'locationType' => $dao->locationType, |
166 | 'is_primary' => $dao->is_primary, | |
167 | 'on_hold' => $dao->on_hold, | |
168 | 'id' => $dao->email_id, | |
169 | 'email' => $dao->email, | |
170 | 'locationTypeId' => $dao->locationTypeId, | |
be2fb01f | 171 | ]; |
6a488035 TO |
172 | |
173 | if ($updateBlankLocInfo) { | |
174 | $emails[$count++] = $values; | |
175 | } | |
176 | else { | |
177 | $emails[$dao->email_id] = $values; | |
178 | } | |
179 | } | |
180 | return $emails; | |
181 | } | |
182 | ||
183 | /** | |
184 | * Get all the emails for a specified location_block id, with the primary email being first | |
185 | * | |
6a0b768e TO |
186 | * @param array $entityElements |
187 | * The array containing entity_id and. | |
16b10e64 | 188 | * entity_table name |
6a488035 | 189 | * |
a6c01b45 CW |
190 | * @return array |
191 | * the array of email id's | |
6a488035 | 192 | */ |
00be9182 | 193 | public static function allEntityEmails(&$entityElements) { |
6a488035 TO |
194 | if (empty($entityElements)) { |
195 | return NULL; | |
196 | } | |
197 | ||
198 | $entityId = $entityElements['entity_id']; | |
199 | $entityTable = $entityElements['entity_table']; | |
200 | ||
6a488035 TO |
201 | $sql = " SELECT email, ltype.name as locationType, e.is_primary as is_primary, e.on_hold as on_hold,e.id as email_id, e.location_type_id as locationTypeId |
202 | FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev | |
203 | WHERE ev.id = %1 | |
204 | AND loc.id = ev.loc_block_id | |
205 | AND e.id IN (loc.email_id, loc.email_2_id) | |
206 | AND ltype.id = e.location_type_id | |
207 | ORDER BY e.is_primary DESC, email_id ASC "; | |
208 | ||
be2fb01f CW |
209 | $params = [ |
210 | 1 => [ | |
6a488035 TO |
211 | $entityId, |
212 | 'Integer', | |
be2fb01f CW |
213 | ], |
214 | ]; | |
6a488035 | 215 | |
be2fb01f | 216 | $emails = []; |
6a488035 TO |
217 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
218 | while ($dao->fetch()) { | |
be2fb01f | 219 | $emails[$dao->email_id] = [ |
6a488035 TO |
220 | 'locationType' => $dao->locationType, |
221 | 'is_primary' => $dao->is_primary, | |
222 | 'on_hold' => $dao->on_hold, | |
223 | 'id' => $dao->email_id, | |
224 | 'email' => $dao->email, | |
225 | 'locationTypeId' => $dao->locationTypeId, | |
be2fb01f | 226 | ]; |
6a488035 TO |
227 | } |
228 | ||
229 | return $emails; | |
230 | } | |
231 | ||
232 | /** | |
100fef9d | 233 | * Set / reset hold status for an email |
6a488035 | 234 | * |
6a0b768e TO |
235 | * @param object $email |
236 | * Email object. | |
6a488035 | 237 | */ |
00be9182 | 238 | public static function holdEmail(&$email) { |
7a5db44f | 239 | if ($email->id && $email->on_hold === NULL) { |
240 | // email is being updated but no change to on_hold. | |
241 | return; | |
916f11f8 | 242 | } |
7a5db44f | 243 | if ($email->on_hold === 'null' || $email->on_hold === NULL) { |
244 | // legacy handling, deprecated. | |
245 | $email->on_hold = 0; | |
246 | } | |
247 | $email->on_hold = (int) $email->on_hold; | |
248 | ||
6a488035 TO |
249 | //check for update mode |
250 | if ($email->id) { | |
be2fb01f | 251 | $params = [1 => [$email->id, 'Integer']]; |
7a5db44f | 252 | if ($email->on_hold) { |
6a488035 TO |
253 | $sql = " |
254 | SELECT id | |
255 | FROM civicrm_email | |
256 | WHERE id = %1 | |
257 | AND hold_date IS NULL | |
258 | "; | |
259 | if (CRM_Core_DAO::singleValueQuery($sql, $params)) { | |
260 | $email->hold_date = date('YmdHis'); | |
261 | $email->reset_date = 'null'; | |
262 | } | |
263 | } | |
7a5db44f | 264 | elseif ($email->on_hold === 0) { |
265 | // we do this lookup to see if reset_date should be changed. | |
6a488035 TO |
266 | $sql = " |
267 | SELECT id | |
268 | FROM civicrm_email | |
269 | WHERE id = %1 | |
270 | AND hold_date IS NOT NULL | |
271 | AND reset_date IS NULL | |
272 | "; | |
273 | if (CRM_Core_DAO::singleValueQuery($sql, $params)) { | |
274 | //set reset date only if it is not set and if hold date is set | |
353ffa53 TO |
275 | $email->on_hold = FALSE; |
276 | $email->hold_date = 'null'; | |
6a488035 TO |
277 | $email->reset_date = date('YmdHis'); |
278 | } | |
279 | } | |
280 | } | |
281 | else { | |
7a5db44f | 282 | if ($email->on_hold) { |
6a488035 TO |
283 | $email->hold_date = date('YmdHis'); |
284 | } | |
285 | } | |
286 | } | |
287 | ||
beac1417 MW |
288 | /** |
289 | * Generate an array of Domain email addresses. | |
290 | * @return array $domainEmails; | |
291 | */ | |
292 | public static function domainEmails() { | |
be2fb01f | 293 | $domainEmails = []; |
beac1417 MW |
294 | $domainFrom = (array) CRM_Core_OptionGroup::values('from_email_address'); |
295 | foreach (array_keys($domainFrom) as $k) { | |
296 | $domainEmail = $domainFrom[$k]; | |
297 | $domainEmails[$domainEmail] = htmlspecialchars($domainEmail); | |
298 | } | |
299 | return $domainEmails; | |
300 | } | |
301 | ||
6a488035 TO |
302 | /** |
303 | * Build From Email as the combination of all the email ids of the logged in user and | |
304 | * the domain email id | |
305 | * | |
a6c01b45 CW |
306 | * @return array |
307 | * an array of email ids | |
6a488035 | 308 | */ |
00be9182 | 309 | public static function getFromEmail() { |
c52a8fcc | 310 | // add all configured FROM email addresses |
beac1417 MW |
311 | $fromEmailValues = self::domainEmails(); |
312 | ||
313 | if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) { | |
314 | return $fromEmailValues; | |
c52a8fcc | 315 | } |
6a488035 | 316 | |
669f45ef | 317 | $contactFromEmails = []; |
6a488035 | 318 | // add logged in user's active email ids |
2dbdb9b9 | 319 | $contactID = CRM_Core_Session::getLoggedInContactID(); |
6a488035 TO |
320 | if ($contactID) { |
321 | $contactEmails = self::allEmails($contactID); | |
beac1417 | 322 | $fromDisplayName = CRM_Core_Session::singleton()->getLoggedInContactDisplayName(); |
6a488035 | 323 | |
beac1417 | 324 | foreach ($contactEmails as $emailId => $emailVal) { |
6a488035 TO |
325 | $email = trim($emailVal['email']); |
326 | if (!$email || $emailVal['on_hold']) { | |
327 | continue; | |
328 | } | |
329 | $fromEmail = "$fromDisplayName <$email>"; | |
330 | $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType']; | |
331 | ||
a7488080 | 332 | if (!empty($emailVal['is_primary'])) { |
6a488035 TO |
333 | $fromEmailHtml .= ' ' . ts('(preferred)'); |
334 | } | |
0cb9621a | 335 | $contactFromEmails[$emailId] = $fromEmailHtml; |
6a488035 TO |
336 | } |
337 | } | |
669f45ef | 338 | return CRM_Utils_Array::crmArrayMerge($contactFromEmails, $fromEmailValues); |
6a488035 TO |
339 | } |
340 | ||
b5c2afd0 EM |
341 | /** |
342 | * @return object | |
343 | */ | |
00be9182 | 344 | public static function isMultipleBulkMail() { |
84fb7424 | 345 | return Civi::settings()->get('civimail_multiple_bulk_emails'); |
6a488035 | 346 | } |
12445e1c CW |
347 | |
348 | /** | |
fe482240 | 349 | * Call common delete function. |
ad37ac8e | 350 | * |
7a592535 | 351 | * @see \CRM_Contact_BAO_Contact::on_hook_civicrm_post |
ad37ac8e | 352 | * |
7a592535 CW |
353 | * @param int $id |
354 | * @deprecated | |
ad37ac8e | 355 | * @return bool |
12445e1c | 356 | */ |
00be9182 | 357 | public static function del($id) { |
7a592535 | 358 | return (bool) self::deleteRecord(['id' => $id]); |
12445e1c | 359 | } |
96025800 | 360 | |
5b4b9509 | 361 | /** |
362 | * Get filters for entity reference fields. | |
363 | * | |
364 | * @return array | |
365 | */ | |
366 | public static function getEntityRefFilters() { | |
367 | $contactFields = CRM_Contact_BAO_Contact::getEntityRefFilters(); | |
368 | foreach ($contactFields as $index => &$contactField) { | |
369 | if (!empty($contactField['entity'])) { | |
370 | // For now email_getlist can't parse state, country etc. | |
371 | unset($contactFields[$index]); | |
372 | } | |
373 | elseif ($contactField['key'] !== 'contact_id') { | |
374 | $contactField['entity'] = 'Contact'; | |
375 | $contactField['key'] = 'contact_id.' . $contactField['key']; | |
376 | } | |
377 | } | |
378 | return $contactFields; | |
379 | } | |
380 | ||
da5e820b CW |
381 | /** |
382 | * | |
383 | * | |
384 | * @param int $contactId | |
385 | * @param string $primaryEmail | |
386 | */ | |
387 | public static function updateContactName($contactId, string $primaryEmail) { | |
388 | if (is_string($primaryEmail) && $primaryEmail !== '' && | |
389 | !CRM_Contact_BAO_Contact::hasName(['id' => $contactId]) | |
390 | ) { | |
391 | CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name', $primaryEmail); | |
392 | CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $contactId, 'sort_name', $primaryEmail); | |
393 | } | |
394 | } | |
395 | ||
d7b498bc EM |
396 | /** |
397 | * Get default text for a message with the signature from the email sender populated. | |
398 | * | |
399 | * @param int $emailID | |
400 | * | |
401 | * @return array | |
402 | * | |
403 | * @throws \API_Exception | |
404 | * @throws \Civi\API\Exception\UnauthorizedException | |
405 | */ | |
406 | public static function getEmailSignatureDefaults(int $emailID): array { | |
407 | // Add signature | |
408 | $defaultEmail = Email::get(FALSE) | |
409 | ->addSelect('signature_html', 'signature_text') | |
410 | ->addWhere('id', '=', $emailID)->execute()->first(); | |
411 | return [ | |
412 | 'html_message' => empty($defaultEmail['signature_html']) ? '' : '<br/><br/>--' . $defaultEmail['signature_html'], | |
413 | 'text_message' => empty($defaultEmail['signature_text']) ? '' : "\n\n--\n" . $defaultEmail['signature_text'], | |
414 | ]; | |
415 | } | |
416 | ||
6a488035 | 417 | } |