Merge pull request #15988 from jensschuppe/dev_core_1425
[civicrm-core.git] / CRM / Core / BAO / Email.php
CommitLineData
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
18/**
192d36c5 19 * This class contains functions for email handling.
6a488035
TO
20 */
21class CRM_Core_BAO_Email extends CRM_Core_DAO_Email {
22
cac01cdc 23 /**
192d36c5 24 * Create email address.
25 *
26 * Note that the create function calls 'add' but has more business logic.
6a488035 27 *
6a0b768e
TO
28 * @param array $params
29 * Input parameters.
77b97be7
EM
30 *
31 * @return object
6a488035 32 */
00be9182 33 public static function create($params) {
4b6bf55f 34 // if id is set & is_primary isn't we can assume no change
cac01cdc 35 if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) || empty($params['id'])) {
6a488035
TO
36 CRM_Core_BAO_Block::handlePrimary($params, get_class());
37 }
4b6bf55f 38
6a488035
TO
39 $email = CRM_Core_BAO_Email::add($params);
40
41 return $email;
42 }
43
44 /**
fe482240 45 * Takes an associative array and adds email.
6a488035 46 *
6a0b768e
TO
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
6a488035 49 *
a6c01b45
CW
50 * @return object
51 * CRM_Core_BAO_Email object on success, null otherwise
6a488035 52 */
00be9182 53 public static function add(&$params) {
6a488035
TO
54 $hook = empty($params['id']) ? 'create' : 'edit';
55 CRM_Utils_Hook::pre($hook, 'Email', CRM_Utils_Array::value('id', $params), $params);
56
57 $email = new CRM_Core_DAO_Email();
58 $email->copyValues($params);
7629d5a6 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);
63 }
6a488035 64
c2714028 65 /*
e70a7fc0
TO
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)
70 */
7629d5a6 71 if ($email->is_bulkmail != 'null' && !empty($params['contact_id']) && !self::isMultipleBulkMail()) {
6a488035
TO
72 $sql = "
73UPDATE civicrm_email
74SET is_bulkmail = 0
75WHERE contact_id = {$params['contact_id']}
76";
9b873358 77 if ($hook == 'edit') {
2aa397bc
TO
78 $sql .= " AND id <> {$params['id']}";
79 }
6a488035
TO
80 CRM_Core_DAO::executeQuery($sql);
81 }
82
83 // handle if email is on hold
84 self::holdEmail($email);
85
86 $email->save();
87
cac01cdc 88 if ($email->is_primary) {
89 // update the UF user email if that has changed
90 CRM_Core_BAO_UFMatch::updateUFName($email->contact_id);
91 }
92
6a488035
TO
93 CRM_Utils_Hook::post($hook, 'Email', $email->id, $email);
94 return $email;
95 }
96
97 /**
98 * Given the list of params in the params array, fetch the object
99 * and store the values in the values array
100 *
6a0b768e
TO
101 * @param array $entityBlock
102 * Input parameters to find object.
6a488035 103 *
cef76e5f 104 * @return array
6a488035 105 */
cef76e5f 106 public static function getValues($entityBlock) {
6a488035
TO
107 return CRM_Core_BAO_Block::getValues('email', $entityBlock);
108 }
109
110 /**
111 * Get all the emails for a specified contact_id, with the primary email being first
112 *
6a0b768e
TO
113 * @param int $id
114 * The contact id.
6a488035 115 *
dd244018
EM
116 * @param bool $updateBlankLocInfo
117 *
a6c01b45
CW
118 * @return array
119 * the array of email id's
6a488035 120 */
00be9182 121 public static function allEmails($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
122 if (!$id) {
123 return NULL;
124 }
125
126 $query = "
127SELECT email,
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
133FROM civicrm_contact
134LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id )
135LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id )
136WHERE civicrm_contact.id = %1
137ORDER BY civicrm_email.is_primary DESC, email_id ASC ";
be2fb01f
CW
138 $params = [
139 1 => [
6a488035
TO
140 $id,
141 'Integer',
be2fb01f
CW
142 ],
143 ];
6a488035 144
be2fb01f 145 $emails = $values = [];
353ffa53
TO
146 $dao = CRM_Core_DAO::executeQuery($query, $params);
147 $count = 1;
6a488035 148 while ($dao->fetch()) {
be2fb01f 149 $values = [
6a488035
TO
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,
be2fb01f 156 ];
6a488035
TO
157
158 if ($updateBlankLocInfo) {
159 $emails[$count++] = $values;
160 }
161 else {
162 $emails[$dao->email_id] = $values;
163 }
164 }
165 return $emails;
166 }
167
168 /**
169 * Get all the emails for a specified location_block id, with the primary email being first
170 *
6a0b768e
TO
171 * @param array $entityElements
172 * The array containing entity_id and.
16b10e64 173 * entity_table name
6a488035 174 *
a6c01b45
CW
175 * @return array
176 * the array of email id's
6a488035 177 */
00be9182 178 public static function allEntityEmails(&$entityElements) {
6a488035
TO
179 if (empty($entityElements)) {
180 return NULL;
181 }
182
183 $entityId = $entityElements['entity_id'];
184 $entityTable = $entityElements['entity_table'];
185
6a488035
TO
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
187FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev
188WHERE ev.id = %1
189AND loc.id = ev.loc_block_id
190AND e.id IN (loc.email_id, loc.email_2_id)
191AND ltype.id = e.location_type_id
192ORDER BY e.is_primary DESC, email_id ASC ";
193
be2fb01f
CW
194 $params = [
195 1 => [
6a488035
TO
196 $entityId,
197 'Integer',
be2fb01f
CW
198 ],
199 ];
6a488035 200
be2fb01f 201 $emails = [];
6a488035
TO
202 $dao = CRM_Core_DAO::executeQuery($sql, $params);
203 while ($dao->fetch()) {
be2fb01f 204 $emails[$dao->email_id] = [
6a488035
TO
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,
be2fb01f 211 ];
6a488035
TO
212 }
213
214 return $emails;
215 }
216
217 /**
100fef9d 218 * Set / reset hold status for an email
6a488035 219 *
6a0b768e
TO
220 * @param object $email
221 * Email object.
6a488035 222 */
00be9182 223 public static function holdEmail(&$email) {
7a5db44f 224 if ($email->id && $email->on_hold === NULL) {
225 // email is being updated but no change to on_hold.
226 return;
916f11f8 227 }
7a5db44f 228 if ($email->on_hold === 'null' || $email->on_hold === NULL) {
229 // legacy handling, deprecated.
230 $email->on_hold = 0;
231 }
232 $email->on_hold = (int) $email->on_hold;
233
6a488035
TO
234 //check for update mode
235 if ($email->id) {
be2fb01f 236 $params = [1 => [$email->id, 'Integer']];
7a5db44f 237 if ($email->on_hold) {
6a488035
TO
238 $sql = "
239SELECT id
240FROM civicrm_email
241WHERE id = %1
242AND hold_date IS NULL
243";
244 if (CRM_Core_DAO::singleValueQuery($sql, $params)) {
245 $email->hold_date = date('YmdHis');
246 $email->reset_date = 'null';
247 }
248 }
7a5db44f 249 elseif ($email->on_hold === 0) {
250 // we do this lookup to see if reset_date should be changed.
6a488035
TO
251 $sql = "
252SELECT id
253FROM civicrm_email
254WHERE id = %1
255AND hold_date IS NOT NULL
256AND reset_date IS NULL
257";
258 if (CRM_Core_DAO::singleValueQuery($sql, $params)) {
259 //set reset date only if it is not set and if hold date is set
353ffa53
TO
260 $email->on_hold = FALSE;
261 $email->hold_date = 'null';
6a488035
TO
262 $email->reset_date = date('YmdHis');
263 }
264 }
265 }
266 else {
7a5db44f 267 if ($email->on_hold) {
6a488035
TO
268 $email->hold_date = date('YmdHis');
269 }
270 }
271 }
272
beac1417
MW
273 /**
274 * Generate an array of Domain email addresses.
275 * @return array $domainEmails;
276 */
277 public static function domainEmails() {
be2fb01f 278 $domainEmails = [];
beac1417
MW
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);
283 }
284 return $domainEmails;
285 }
286
6a488035
TO
287 /**
288 * Build From Email as the combination of all the email ids of the logged in user and
289 * the domain email id
290 *
a6c01b45
CW
291 * @return array
292 * an array of email ids
6a488035 293 */
00be9182 294 public static function getFromEmail() {
c52a8fcc 295 // add all configured FROM email addresses
beac1417
MW
296 $fromEmailValues = self::domainEmails();
297
298 if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) {
299 return $fromEmailValues;
c52a8fcc 300 }
6a488035 301
669f45ef 302 $contactFromEmails = [];
6a488035 303 // add logged in user's active email ids
beac1417 304 $contactID = CRM_Core_Session::singleton()->getLoggedInContactID();
6a488035
TO
305 if ($contactID) {
306 $contactEmails = self::allEmails($contactID);
beac1417 307 $fromDisplayName = CRM_Core_Session::singleton()->getLoggedInContactDisplayName();
6a488035 308
beac1417 309 foreach ($contactEmails as $emailId => $emailVal) {
6a488035
TO
310 $email = trim($emailVal['email']);
311 if (!$email || $emailVal['on_hold']) {
312 continue;
313 }
314 $fromEmail = "$fromDisplayName <$email>";
315 $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType'];
316
a7488080 317 if (!empty($emailVal['is_primary'])) {
6a488035
TO
318 $fromEmailHtml .= ' ' . ts('(preferred)');
319 }
0cb9621a 320 $contactFromEmails[$emailId] = $fromEmailHtml;
6a488035
TO
321 }
322 }
669f45ef 323 return CRM_Utils_Array::crmArrayMerge($contactFromEmails, $fromEmailValues);
6a488035
TO
324 }
325
b5c2afd0
EM
326 /**
327 * @return object
328 */
00be9182 329 public static function isMultipleBulkMail() {
84fb7424 330 return Civi::settings()->get('civimail_multiple_bulk_emails');
6a488035 331 }
12445e1c
CW
332
333 /**
fe482240 334 * Call common delete function.
ad37ac8e 335 *
336 * @param int $id
337 *
338 * @return bool
12445e1c 339 */
00be9182 340 public static function del($id) {
a65e2e55 341 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Email', $id);
12445e1c 342 }
96025800 343
6a488035 344}