3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class contains functions for email handling.
21 class CRM_Core_BAO_Email
extends CRM_Core_DAO_Email
{
24 * Create email address.
26 * Note that the create function calls 'add' but has more business logic.
28 * @param array $params
33 public static function create($params) {
34 // if id is set & is_primary isn't we can assume no change
35 if (is_numeric(CRM_Utils_Array
::value('is_primary', $params)) ||
empty($params['id'])) {
36 CRM_Core_BAO_Block
::handlePrimary($params, get_class());
39 $email = CRM_Core_BAO_Email
::add($params);
45 * Takes an associative array and adds email.
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
51 * CRM_Core_BAO_Email object on success, null otherwise
53 public static function add(&$params) {
54 $hook = empty($params['id']) ?
'create' : 'edit';
55 CRM_Utils_Hook
::pre($hook, 'Email', CRM_Utils_Array
::value('id', $params), $params);
57 $email = new CRM_Core_DAO_Email();
58 $email->copyValues($params);
59 if (!empty($email->email
)) {
60 // lower case email field to optimize queries
61 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
62 $email->email
= $strtolower($email->email
);
66 * since we're setting bulkmail for 1 of this contact's emails, first reset all their other emails to is_bulkmail false
67 * We shouldn't not set the current email to false even though we
68 * are about to reset it to avoid contaminating the changelog if logging is enabled
69 * (only 1 email address can have is_bulkmail = true)
71 if ($email->is_bulkmail
!= 'null' && !empty($params['contact_id']) && !self
::isMultipleBulkMail()) {
75 WHERE contact_id = {$params['contact_id']}
77 if ($hook == 'edit') {
78 $sql .= " AND id <> {$params['id']}";
80 CRM_Core_DAO
::executeQuery($sql);
83 // handle if email is on hold
84 self
::holdEmail($email);
88 if ($email->is_primary
) {
89 // update the UF user email if that has changed
90 CRM_Core_BAO_UFMatch
::updateUFName($email->contact_id
);
93 CRM_Utils_Hook
::post($hook, 'Email', $email->id
, $email);
98 * Given the list of params in the params array, fetch the object
99 * and store the values in the values array
101 * @param array $entityBlock
102 * Input parameters to find object.
106 public static function getValues($entityBlock) {
107 return CRM_Core_BAO_Block
::getValues('email', $entityBlock);
111 * Get all the emails for a specified contact_id, with the primary email being first
116 * @param bool $updateBlankLocInfo
119 * the array of email id's
121 public static function allEmails($id, $updateBlankLocInfo = FALSE) {
128 civicrm_location_type.name as locationType,
129 civicrm_email.is_primary as is_primary,
130 civicrm_email.on_hold as on_hold,
131 civicrm_email.id as email_id,
132 civicrm_email.location_type_id as locationTypeId
134 LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id )
135 LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id )
136 WHERE civicrm_contact.id = %1
137 ORDER BY civicrm_email.is_primary DESC, email_id ASC ";
145 $emails = $values = [];
146 $dao = CRM_Core_DAO
::executeQuery($query, $params);
148 while ($dao->fetch()) {
150 'locationType' => $dao->locationType
,
151 'is_primary' => $dao->is_primary
,
152 'on_hold' => $dao->on_hold
,
153 'id' => $dao->email_id
,
154 'email' => $dao->email
,
155 'locationTypeId' => $dao->locationTypeId
,
158 if ($updateBlankLocInfo) {
159 $emails[$count++
] = $values;
162 $emails[$dao->email_id
] = $values;
169 * Get all the emails for a specified location_block id, with the primary email being first
171 * @param array $entityElements
172 * The array containing entity_id and.
176 * the array of email id's
178 public static function allEntityEmails(&$entityElements) {
179 if (empty($entityElements)) {
183 $entityId = $entityElements['entity_id'];
184 $entityTable = $entityElements['entity_table'];
186 $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
187 FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev
189 AND loc.id = ev.loc_block_id
190 AND e.id IN (loc.email_id, loc.email_2_id)
191 AND ltype.id = e.location_type_id
192 ORDER BY e.is_primary DESC, email_id ASC ";
202 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
203 while ($dao->fetch()) {
204 $emails[$dao->email_id
] = [
205 'locationType' => $dao->locationType
,
206 'is_primary' => $dao->is_primary
,
207 'on_hold' => $dao->on_hold
,
208 'id' => $dao->email_id
,
209 'email' => $dao->email
,
210 'locationTypeId' => $dao->locationTypeId
,
218 * Set / reset hold status for an email
220 * @param object $email
223 public static function holdEmail(&$email) {
224 if ($email->id
&& $email->on_hold
=== NULL) {
225 // email is being updated but no change to on_hold.
228 if ($email->on_hold
=== 'null' ||
$email->on_hold
=== NULL) {
229 // legacy handling, deprecated.
232 $email->on_hold
= (int) $email->on_hold
;
234 //check for update mode
236 $params = [1 => [$email->id
, 'Integer']];
237 if ($email->on_hold
) {
242 AND hold_date IS NULL
244 if (CRM_Core_DAO
::singleValueQuery($sql, $params)) {
245 $email->hold_date
= date('YmdHis');
246 $email->reset_date
= 'null';
249 elseif ($email->on_hold
=== 0) {
250 // we do this lookup to see if reset_date should be changed.
255 AND hold_date IS NOT NULL
256 AND reset_date IS NULL
258 if (CRM_Core_DAO
::singleValueQuery($sql, $params)) {
259 //set reset date only if it is not set and if hold date is set
260 $email->on_hold
= FALSE;
261 $email->hold_date
= 'null';
262 $email->reset_date
= date('YmdHis');
267 if ($email->on_hold
) {
268 $email->hold_date
= date('YmdHis');
274 * Generate an array of Domain email addresses.
275 * @return array $domainEmails;
277 public static function domainEmails() {
279 $domainFrom = (array) CRM_Core_OptionGroup
::values('from_email_address');
280 foreach (array_keys($domainFrom) as $k) {
281 $domainEmail = $domainFrom[$k];
282 $domainEmails[$domainEmail] = htmlspecialchars($domainEmail);
284 return $domainEmails;
288 * Build From Email as the combination of all the email ids of the logged in user and
289 * the domain email id
292 * an array of email ids
294 public static function getFromEmail() {
295 // add all configured FROM email addresses
296 $fromEmailValues = self
::domainEmails();
298 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
299 return $fromEmailValues;
302 $contactFromEmails = [];
303 // add logged in user's active email ids
304 $contactID = CRM_Core_Session
::singleton()->getLoggedInContactID();
306 $contactEmails = self
::allEmails($contactID);
307 $fromDisplayName = CRM_Core_Session
::singleton()->getLoggedInContactDisplayName();
309 foreach ($contactEmails as $emailId => $emailVal) {
310 $email = trim($emailVal['email']);
311 if (!$email ||
$emailVal['on_hold']) {
314 $fromEmail = "$fromDisplayName <$email>";
315 $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType'];
317 if (!empty($emailVal['is_primary'])) {
318 $fromEmailHtml .= ' ' . ts('(preferred)');
320 $contactFromEmails[$emailId] = $fromEmailHtml;
323 return CRM_Utils_Array
::crmArrayMerge($contactFromEmails, $fromEmailValues);
329 public static function isMultipleBulkMail() {
330 return Civi
::settings()->get('civimail_multiple_bulk_emails');
334 * Call common delete function.
340 public static function del($id) {
341 return CRM_Contact_BAO_Contact
::deleteObjectWithPrimary('Email', $id);