Merge pull request #14222 from mfb/debug-var
[civicrm-core.git] / CRM / Core / BAO / Email.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
192d36c5 35 * This class contains functions for email handling.
6a488035
TO
36 */
37class CRM_Core_BAO_Email extends CRM_Core_DAO_Email {
38
cac01cdc 39 /**
192d36c5 40 * Create email address.
41 *
42 * Note that the create function calls 'add' but has more business logic.
6a488035 43 *
6a0b768e
TO
44 * @param array $params
45 * Input parameters.
77b97be7
EM
46 *
47 * @return object
6a488035 48 */
00be9182 49 public static function create($params) {
4b6bf55f 50 // if id is set & is_primary isn't we can assume no change
cac01cdc 51 if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) || empty($params['id'])) {
6a488035
TO
52 CRM_Core_BAO_Block::handlePrimary($params, get_class());
53 }
4b6bf55f 54
6a488035
TO
55 $email = CRM_Core_BAO_Email::add($params);
56
57 return $email;
58 }
59
60 /**
fe482240 61 * Takes an associative array and adds email.
6a488035 62 *
6a0b768e
TO
63 * @param array $params
64 * (reference ) an assoc array of name/value pairs.
6a488035 65 *
a6c01b45
CW
66 * @return object
67 * CRM_Core_BAO_Email object on success, null otherwise
6a488035 68 */
00be9182 69 public static function add(&$params) {
6a488035
TO
70 $hook = empty($params['id']) ? 'create' : 'edit';
71 CRM_Utils_Hook::pre($hook, 'Email', CRM_Utils_Array::value('id', $params), $params);
72
73 $email = new CRM_Core_DAO_Email();
74 $email->copyValues($params);
7629d5a6 75 if (!empty($email->email)) {
76 // lower case email field to optimize queries
77 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
78 $email->email = $strtolower($email->email);
79 }
6a488035 80
c2714028 81 /*
e70a7fc0
TO
82 * since we're setting bulkmail for 1 of this contact's emails, first reset all their other emails to is_bulkmail false
83 * We shouldn't not set the current email to false even though we
84 * are about to reset it to avoid contaminating the changelog if logging is enabled
85 * (only 1 email address can have is_bulkmail = true)
86 */
7629d5a6 87 if ($email->is_bulkmail != 'null' && !empty($params['contact_id']) && !self::isMultipleBulkMail()) {
6a488035
TO
88 $sql = "
89UPDATE civicrm_email
90SET is_bulkmail = 0
91WHERE contact_id = {$params['contact_id']}
92";
9b873358 93 if ($hook == 'edit') {
2aa397bc
TO
94 $sql .= " AND id <> {$params['id']}";
95 }
6a488035
TO
96 CRM_Core_DAO::executeQuery($sql);
97 }
98
99 // handle if email is on hold
100 self::holdEmail($email);
101
102 $email->save();
103
cac01cdc 104 if ($email->is_primary) {
105 // update the UF user email if that has changed
106 CRM_Core_BAO_UFMatch::updateUFName($email->contact_id);
107 }
108
6a488035
TO
109 CRM_Utils_Hook::post($hook, 'Email', $email->id, $email);
110 return $email;
111 }
112
113 /**
114 * Given the list of params in the params array, fetch the object
115 * and store the values in the values array
116 *
6a0b768e
TO
117 * @param array $entityBlock
118 * Input parameters to find object.
6a488035 119 *
cef76e5f 120 * @return array
6a488035 121 */
cef76e5f 122 public static function getValues($entityBlock) {
6a488035
TO
123 return CRM_Core_BAO_Block::getValues('email', $entityBlock);
124 }
125
126 /**
127 * Get all the emails for a specified contact_id, with the primary email being first
128 *
6a0b768e
TO
129 * @param int $id
130 * The contact id.
6a488035 131 *
dd244018
EM
132 * @param bool $updateBlankLocInfo
133 *
a6c01b45
CW
134 * @return array
135 * the array of email id's
6a488035 136 */
00be9182 137 public static function allEmails($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
138 if (!$id) {
139 return NULL;
140 }
141
142 $query = "
143SELECT email,
144 civicrm_location_type.name as locationType,
145 civicrm_email.is_primary as is_primary,
146 civicrm_email.on_hold as on_hold,
147 civicrm_email.id as email_id,
148 civicrm_email.location_type_id as locationTypeId
149FROM civicrm_contact
150LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id )
151LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id )
152WHERE civicrm_contact.id = %1
153ORDER BY civicrm_email.is_primary DESC, email_id ASC ";
be2fb01f
CW
154 $params = [
155 1 => [
6a488035
TO
156 $id,
157 'Integer',
be2fb01f
CW
158 ],
159 ];
6a488035 160
be2fb01f 161 $emails = $values = [];
353ffa53
TO
162 $dao = CRM_Core_DAO::executeQuery($query, $params);
163 $count = 1;
6a488035 164 while ($dao->fetch()) {
be2fb01f 165 $values = [
6a488035
TO
166 'locationType' => $dao->locationType,
167 'is_primary' => $dao->is_primary,
168 'on_hold' => $dao->on_hold,
169 'id' => $dao->email_id,
170 'email' => $dao->email,
171 'locationTypeId' => $dao->locationTypeId,
be2fb01f 172 ];
6a488035
TO
173
174 if ($updateBlankLocInfo) {
175 $emails[$count++] = $values;
176 }
177 else {
178 $emails[$dao->email_id] = $values;
179 }
180 }
181 return $emails;
182 }
183
184 /**
185 * Get all the emails for a specified location_block id, with the primary email being first
186 *
6a0b768e
TO
187 * @param array $entityElements
188 * The array containing entity_id and.
16b10e64 189 * entity_table name
6a488035 190 *
a6c01b45
CW
191 * @return array
192 * the array of email id's
6a488035 193 */
00be9182 194 public static function allEntityEmails(&$entityElements) {
6a488035
TO
195 if (empty($entityElements)) {
196 return NULL;
197 }
198
199 $entityId = $entityElements['entity_id'];
200 $entityTable = $entityElements['entity_table'];
201
6a488035
TO
202 $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
203FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev
204WHERE ev.id = %1
205AND loc.id = ev.loc_block_id
206AND e.id IN (loc.email_id, loc.email_2_id)
207AND ltype.id = e.location_type_id
208ORDER BY e.is_primary DESC, email_id ASC ";
209
be2fb01f
CW
210 $params = [
211 1 => [
6a488035
TO
212 $entityId,
213 'Integer',
be2fb01f
CW
214 ],
215 ];
6a488035 216
be2fb01f 217 $emails = [];
6a488035
TO
218 $dao = CRM_Core_DAO::executeQuery($sql, $params);
219 while ($dao->fetch()) {
be2fb01f 220 $emails[$dao->email_id] = [
6a488035
TO
221 'locationType' => $dao->locationType,
222 'is_primary' => $dao->is_primary,
223 'on_hold' => $dao->on_hold,
224 'id' => $dao->email_id,
225 'email' => $dao->email,
226 'locationTypeId' => $dao->locationTypeId,
be2fb01f 227 ];
6a488035
TO
228 }
229
230 return $emails;
231 }
232
233 /**
100fef9d 234 * Set / reset hold status for an email
6a488035 235 *
6a0b768e
TO
236 * @param object $email
237 * Email object.
6a488035 238 */
00be9182 239 public static function holdEmail(&$email) {
7a5db44f 240 if ($email->id && $email->on_hold === NULL) {
241 // email is being updated but no change to on_hold.
242 return;
916f11f8 243 }
7a5db44f 244 if ($email->on_hold === 'null' || $email->on_hold === NULL) {
245 // legacy handling, deprecated.
246 $email->on_hold = 0;
247 }
248 $email->on_hold = (int) $email->on_hold;
249
6a488035
TO
250 //check for update mode
251 if ($email->id) {
be2fb01f 252 $params = [1 => [$email->id, 'Integer']];
7a5db44f 253 if ($email->on_hold) {
6a488035
TO
254 $sql = "
255SELECT id
256FROM civicrm_email
257WHERE id = %1
258AND hold_date IS NULL
259";
260 if (CRM_Core_DAO::singleValueQuery($sql, $params)) {
261 $email->hold_date = date('YmdHis');
262 $email->reset_date = 'null';
263 }
264 }
7a5db44f 265 elseif ($email->on_hold === 0) {
266 // we do this lookup to see if reset_date should be changed.
6a488035
TO
267 $sql = "
268SELECT id
269FROM civicrm_email
270WHERE id = %1
271AND hold_date IS NOT NULL
272AND reset_date IS NULL
273";
274 if (CRM_Core_DAO::singleValueQuery($sql, $params)) {
275 //set reset date only if it is not set and if hold date is set
353ffa53
TO
276 $email->on_hold = FALSE;
277 $email->hold_date = 'null';
6a488035
TO
278 $email->reset_date = date('YmdHis');
279 }
280 }
281 }
282 else {
7a5db44f 283 if ($email->on_hold) {
6a488035
TO
284 $email->hold_date = date('YmdHis');
285 }
286 }
287 }
288
beac1417
MW
289 /**
290 * Generate an array of Domain email addresses.
291 * @return array $domainEmails;
292 */
293 public static function domainEmails() {
be2fb01f 294 $domainEmails = [];
beac1417
MW
295 $domainFrom = (array) CRM_Core_OptionGroup::values('from_email_address');
296 foreach (array_keys($domainFrom) as $k) {
297 $domainEmail = $domainFrom[$k];
298 $domainEmails[$domainEmail] = htmlspecialchars($domainEmail);
299 }
300 return $domainEmails;
301 }
302
6a488035
TO
303 /**
304 * Build From Email as the combination of all the email ids of the logged in user and
305 * the domain email id
306 *
a6c01b45
CW
307 * @return array
308 * an array of email ids
6a488035 309 */
00be9182 310 public static function getFromEmail() {
c52a8fcc 311 // add all configured FROM email addresses
beac1417
MW
312 $fromEmailValues = self::domainEmails();
313
314 if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) {
315 return $fromEmailValues;
c52a8fcc 316 }
6a488035 317
669f45ef 318 $contactFromEmails = [];
6a488035 319 // add logged in user's active email ids
beac1417 320 $contactID = CRM_Core_Session::singleton()->getLoggedInContactID();
6a488035
TO
321 if ($contactID) {
322 $contactEmails = self::allEmails($contactID);
beac1417 323 $fromDisplayName = CRM_Core_Session::singleton()->getLoggedInContactDisplayName();
6a488035 324
beac1417 325 foreach ($contactEmails as $emailId => $emailVal) {
6a488035
TO
326 $email = trim($emailVal['email']);
327 if (!$email || $emailVal['on_hold']) {
328 continue;
329 }
330 $fromEmail = "$fromDisplayName <$email>";
331 $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType'];
332
a7488080 333 if (!empty($emailVal['is_primary'])) {
6a488035
TO
334 $fromEmailHtml .= ' ' . ts('(preferred)');
335 }
0cb9621a 336 $contactFromEmails[$emailId] = $fromEmailHtml;
6a488035
TO
337 }
338 }
669f45ef 339 return CRM_Utils_Array::crmArrayMerge($contactFromEmails, $fromEmailValues);
6a488035
TO
340 }
341
b5c2afd0
EM
342 /**
343 * @return object
344 */
00be9182 345 public static function isMultipleBulkMail() {
84fb7424 346 return Civi::settings()->get('civimail_multiple_bulk_emails');
6a488035 347 }
12445e1c
CW
348
349 /**
fe482240 350 * Call common delete function.
ad37ac8e 351 *
352 * @param int $id
353 *
354 * @return bool
12445e1c 355 */
00be9182 356 public static function del($id) {
a65e2e55 357 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Email', $id);
12445e1c 358 }
96025800 359
6a488035 360}