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 TO |
22 | */ |
23 | class CRM_Core_BAO_Email extends CRM_Core_DAO_Email { | |
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 | ||
cac01cdc | 83 | if ($email->is_primary) { |
84 | // update the UF user email if that has changed | |
85 | CRM_Core_BAO_UFMatch::updateUFName($email->contact_id); | |
86 | } | |
87 | ||
6a488035 TO |
88 | CRM_Utils_Hook::post($hook, 'Email', $email->id, $email); |
89 | return $email; | |
90 | } | |
91 | ||
ec47f7b4 | 92 | /** |
93 | * Takes an associative array and adds email. | |
94 | * | |
95 | * @param array $params | |
96 | * (reference ) an assoc array of name/value pairs. | |
97 | * | |
98 | * @return object | |
99 | * CRM_Core_BAO_Email object on success, null otherwise | |
100 | */ | |
101 | public static function add(&$params) { | |
102 | CRM_Core_Error::deprecatedFunctionWarning('apiv4 create'); | |
103 | return self::create($params); | |
104 | } | |
105 | ||
6a488035 TO |
106 | /** |
107 | * Given the list of params in the params array, fetch the object | |
108 | * and store the values in the values array | |
109 | * | |
6a0b768e TO |
110 | * @param array $entityBlock |
111 | * Input parameters to find object. | |
6a488035 | 112 | * |
cef76e5f | 113 | * @return array |
6a488035 | 114 | */ |
cef76e5f | 115 | public static function getValues($entityBlock) { |
6a488035 TO |
116 | return CRM_Core_BAO_Block::getValues('email', $entityBlock); |
117 | } | |
118 | ||
119 | /** | |
120 | * Get all the emails for a specified contact_id, with the primary email being first | |
121 | * | |
6a0b768e TO |
122 | * @param int $id |
123 | * The contact id. | |
6a488035 | 124 | * |
dd244018 EM |
125 | * @param bool $updateBlankLocInfo |
126 | * | |
a6c01b45 CW |
127 | * @return array |
128 | * the array of email id's | |
6a488035 | 129 | */ |
00be9182 | 130 | public static function allEmails($id, $updateBlankLocInfo = FALSE) { |
6a488035 TO |
131 | if (!$id) { |
132 | return NULL; | |
133 | } | |
134 | ||
135 | $query = " | |
136 | SELECT email, | |
137 | civicrm_location_type.name as locationType, | |
138 | civicrm_email.is_primary as is_primary, | |
139 | civicrm_email.on_hold as on_hold, | |
140 | civicrm_email.id as email_id, | |
141 | civicrm_email.location_type_id as locationTypeId | |
142 | FROM civicrm_contact | |
143 | LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id ) | |
144 | LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id ) | |
145 | WHERE civicrm_contact.id = %1 | |
146 | ORDER BY civicrm_email.is_primary DESC, email_id ASC "; | |
be2fb01f CW |
147 | $params = [ |
148 | 1 => [ | |
6a488035 TO |
149 | $id, |
150 | 'Integer', | |
be2fb01f CW |
151 | ], |
152 | ]; | |
6a488035 | 153 | |
be2fb01f | 154 | $emails = $values = []; |
353ffa53 TO |
155 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
156 | $count = 1; | |
6a488035 | 157 | while ($dao->fetch()) { |
be2fb01f | 158 | $values = [ |
6a488035 TO |
159 | 'locationType' => $dao->locationType, |
160 | 'is_primary' => $dao->is_primary, | |
161 | 'on_hold' => $dao->on_hold, | |
162 | 'id' => $dao->email_id, | |
163 | 'email' => $dao->email, | |
164 | 'locationTypeId' => $dao->locationTypeId, | |
be2fb01f | 165 | ]; |
6a488035 TO |
166 | |
167 | if ($updateBlankLocInfo) { | |
168 | $emails[$count++] = $values; | |
169 | } | |
170 | else { | |
171 | $emails[$dao->email_id] = $values; | |
172 | } | |
173 | } | |
174 | return $emails; | |
175 | } | |
176 | ||
177 | /** | |
178 | * Get all the emails for a specified location_block id, with the primary email being first | |
179 | * | |
6a0b768e TO |
180 | * @param array $entityElements |
181 | * The array containing entity_id and. | |
16b10e64 | 182 | * entity_table name |
6a488035 | 183 | * |
a6c01b45 CW |
184 | * @return array |
185 | * the array of email id's | |
6a488035 | 186 | */ |
00be9182 | 187 | public static function allEntityEmails(&$entityElements) { |
6a488035 TO |
188 | if (empty($entityElements)) { |
189 | return NULL; | |
190 | } | |
191 | ||
192 | $entityId = $entityElements['entity_id']; | |
193 | $entityTable = $entityElements['entity_table']; | |
194 | ||
6a488035 TO |
195 | $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 |
196 | FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev | |
197 | WHERE ev.id = %1 | |
198 | AND loc.id = ev.loc_block_id | |
199 | AND e.id IN (loc.email_id, loc.email_2_id) | |
200 | AND ltype.id = e.location_type_id | |
201 | ORDER BY e.is_primary DESC, email_id ASC "; | |
202 | ||
be2fb01f CW |
203 | $params = [ |
204 | 1 => [ | |
6a488035 TO |
205 | $entityId, |
206 | 'Integer', | |
be2fb01f CW |
207 | ], |
208 | ]; | |
6a488035 | 209 | |
be2fb01f | 210 | $emails = []; |
6a488035 TO |
211 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
212 | while ($dao->fetch()) { | |
be2fb01f | 213 | $emails[$dao->email_id] = [ |
6a488035 TO |
214 | 'locationType' => $dao->locationType, |
215 | 'is_primary' => $dao->is_primary, | |
216 | 'on_hold' => $dao->on_hold, | |
217 | 'id' => $dao->email_id, | |
218 | 'email' => $dao->email, | |
219 | 'locationTypeId' => $dao->locationTypeId, | |
be2fb01f | 220 | ]; |
6a488035 TO |
221 | } |
222 | ||
223 | return $emails; | |
224 | } | |
225 | ||
226 | /** | |
100fef9d | 227 | * Set / reset hold status for an email |
6a488035 | 228 | * |
6a0b768e TO |
229 | * @param object $email |
230 | * Email object. | |
6a488035 | 231 | */ |
00be9182 | 232 | public static function holdEmail(&$email) { |
7a5db44f | 233 | if ($email->id && $email->on_hold === NULL) { |
234 | // email is being updated but no change to on_hold. | |
235 | return; | |
916f11f8 | 236 | } |
7a5db44f | 237 | if ($email->on_hold === 'null' || $email->on_hold === NULL) { |
238 | // legacy handling, deprecated. | |
239 | $email->on_hold = 0; | |
240 | } | |
241 | $email->on_hold = (int) $email->on_hold; | |
242 | ||
6a488035 TO |
243 | //check for update mode |
244 | if ($email->id) { | |
be2fb01f | 245 | $params = [1 => [$email->id, 'Integer']]; |
7a5db44f | 246 | if ($email->on_hold) { |
6a488035 TO |
247 | $sql = " |
248 | SELECT id | |
249 | FROM civicrm_email | |
250 | WHERE id = %1 | |
251 | AND hold_date IS NULL | |
252 | "; | |
253 | if (CRM_Core_DAO::singleValueQuery($sql, $params)) { | |
254 | $email->hold_date = date('YmdHis'); | |
255 | $email->reset_date = 'null'; | |
256 | } | |
257 | } | |
7a5db44f | 258 | elseif ($email->on_hold === 0) { |
259 | // we do this lookup to see if reset_date should be changed. | |
6a488035 TO |
260 | $sql = " |
261 | SELECT id | |
262 | FROM civicrm_email | |
263 | WHERE id = %1 | |
264 | AND hold_date IS NOT NULL | |
265 | AND reset_date IS NULL | |
266 | "; | |
267 | if (CRM_Core_DAO::singleValueQuery($sql, $params)) { | |
268 | //set reset date only if it is not set and if hold date is set | |
353ffa53 TO |
269 | $email->on_hold = FALSE; |
270 | $email->hold_date = 'null'; | |
6a488035 TO |
271 | $email->reset_date = date('YmdHis'); |
272 | } | |
273 | } | |
274 | } | |
275 | else { | |
7a5db44f | 276 | if ($email->on_hold) { |
6a488035 TO |
277 | $email->hold_date = date('YmdHis'); |
278 | } | |
279 | } | |
280 | } | |
281 | ||
beac1417 MW |
282 | /** |
283 | * Generate an array of Domain email addresses. | |
284 | * @return array $domainEmails; | |
285 | */ | |
286 | public static function domainEmails() { | |
be2fb01f | 287 | $domainEmails = []; |
beac1417 MW |
288 | $domainFrom = (array) CRM_Core_OptionGroup::values('from_email_address'); |
289 | foreach (array_keys($domainFrom) as $k) { | |
290 | $domainEmail = $domainFrom[$k]; | |
291 | $domainEmails[$domainEmail] = htmlspecialchars($domainEmail); | |
292 | } | |
293 | return $domainEmails; | |
294 | } | |
295 | ||
6a488035 TO |
296 | /** |
297 | * Build From Email as the combination of all the email ids of the logged in user and | |
298 | * the domain email id | |
299 | * | |
a6c01b45 CW |
300 | * @return array |
301 | * an array of email ids | |
6a488035 | 302 | */ |
00be9182 | 303 | public static function getFromEmail() { |
c52a8fcc | 304 | // add all configured FROM email addresses |
beac1417 MW |
305 | $fromEmailValues = self::domainEmails(); |
306 | ||
307 | if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) { | |
308 | return $fromEmailValues; | |
c52a8fcc | 309 | } |
6a488035 | 310 | |
669f45ef | 311 | $contactFromEmails = []; |
6a488035 | 312 | // add logged in user's active email ids |
2dbdb9b9 | 313 | $contactID = CRM_Core_Session::getLoggedInContactID(); |
6a488035 TO |
314 | if ($contactID) { |
315 | $contactEmails = self::allEmails($contactID); | |
beac1417 | 316 | $fromDisplayName = CRM_Core_Session::singleton()->getLoggedInContactDisplayName(); |
6a488035 | 317 | |
beac1417 | 318 | foreach ($contactEmails as $emailId => $emailVal) { |
6a488035 TO |
319 | $email = trim($emailVal['email']); |
320 | if (!$email || $emailVal['on_hold']) { | |
321 | continue; | |
322 | } | |
323 | $fromEmail = "$fromDisplayName <$email>"; | |
324 | $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType']; | |
325 | ||
a7488080 | 326 | if (!empty($emailVal['is_primary'])) { |
6a488035 TO |
327 | $fromEmailHtml .= ' ' . ts('(preferred)'); |
328 | } | |
0cb9621a | 329 | $contactFromEmails[$emailId] = $fromEmailHtml; |
6a488035 TO |
330 | } |
331 | } | |
669f45ef | 332 | return CRM_Utils_Array::crmArrayMerge($contactFromEmails, $fromEmailValues); |
6a488035 TO |
333 | } |
334 | ||
b5c2afd0 EM |
335 | /** |
336 | * @return object | |
337 | */ | |
00be9182 | 338 | public static function isMultipleBulkMail() { |
84fb7424 | 339 | return Civi::settings()->get('civimail_multiple_bulk_emails'); |
6a488035 | 340 | } |
12445e1c CW |
341 | |
342 | /** | |
fe482240 | 343 | * Call common delete function. |
ad37ac8e | 344 | * |
345 | * @param int $id | |
346 | * | |
347 | * @return bool | |
12445e1c | 348 | */ |
00be9182 | 349 | public static function del($id) { |
a65e2e55 | 350 | return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Email', $id); |
12445e1c | 351 | } |
96025800 | 352 | |
5b4b9509 | 353 | /** |
354 | * Get filters for entity reference fields. | |
355 | * | |
356 | * @return array | |
357 | */ | |
358 | public static function getEntityRefFilters() { | |
359 | $contactFields = CRM_Contact_BAO_Contact::getEntityRefFilters(); | |
360 | foreach ($contactFields as $index => &$contactField) { | |
361 | if (!empty($contactField['entity'])) { | |
362 | // For now email_getlist can't parse state, country etc. | |
363 | unset($contactFields[$index]); | |
364 | } | |
365 | elseif ($contactField['key'] !== 'contact_id') { | |
366 | $contactField['entity'] = 'Contact'; | |
367 | $contactField['key'] = 'contact_id.' . $contactField['key']; | |
368 | } | |
369 | } | |
370 | return $contactFields; | |
371 | } | |
372 | ||
da5e820b CW |
373 | /** |
374 | * | |
375 | * | |
376 | * @param int $contactId | |
377 | * @param string $primaryEmail | |
378 | */ | |
379 | public static function updateContactName($contactId, string $primaryEmail) { | |
380 | if (is_string($primaryEmail) && $primaryEmail !== '' && | |
381 | !CRM_Contact_BAO_Contact::hasName(['id' => $contactId]) | |
382 | ) { | |
383 | CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name', $primaryEmail); | |
384 | CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $contactId, 'sort_name', $primaryEmail); | |
385 | } | |
386 | } | |
387 | ||
d7b498bc EM |
388 | /** |
389 | * Get default text for a message with the signature from the email sender populated. | |
390 | * | |
391 | * @param int $emailID | |
392 | * | |
393 | * @return array | |
394 | * | |
395 | * @throws \API_Exception | |
396 | * @throws \Civi\API\Exception\UnauthorizedException | |
397 | */ | |
398 | public static function getEmailSignatureDefaults(int $emailID): array { | |
399 | // Add signature | |
400 | $defaultEmail = Email::get(FALSE) | |
401 | ->addSelect('signature_html', 'signature_text') | |
402 | ->addWhere('id', '=', $emailID)->execute()->first(); | |
403 | return [ | |
404 | 'html_message' => empty($defaultEmail['signature_html']) ? '' : '<br/><br/>--' . $defaultEmail['signature_html'], | |
405 | 'text_message' => empty($defaultEmail['signature_text']) ? '' : "\n\n--\n" . $defaultEmail['signature_text'], | |
406 | ]; | |
407 | } | |
408 | ||
6a488035 | 409 | } |