Only add in the additional metadata if we are also adding them to the form
[civicrm-core.git] / CRM / Core / BAO / Email.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class contains functions for email handling.
20 */
21 class CRM_Core_BAO_Email extends CRM_Core_DAO_Email {
22
23 /**
24 * Create email address.
25 *
26 * Note that the create function calls 'add' but has more business logic.
27 *
28 * @param array $params
29 * Input parameters.
30 *
31 * @return object
32 */
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());
37 }
38
39 $email = CRM_Core_BAO_Email::add($params);
40
41 return $email;
42 }
43
44 /**
45 * Takes an associative array and adds email.
46 *
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
49 *
50 * @return object
51 * CRM_Core_BAO_Email object on success, null otherwise
52 */
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);
56
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);
63 }
64
65 /*
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 */
71 if ($email->is_bulkmail != 'null' && !empty($params['contact_id']) && !self::isMultipleBulkMail()) {
72 $sql = "
73 UPDATE civicrm_email
74 SET is_bulkmail = 0
75 WHERE contact_id = {$params['contact_id']}
76 ";
77 if ($hook == 'edit') {
78 $sql .= " AND id <> {$params['id']}";
79 }
80 CRM_Core_DAO::executeQuery($sql);
81 }
82
83 // handle if email is on hold
84 self::holdEmail($email);
85
86 $email->save();
87
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
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 *
101 * @param array $entityBlock
102 * Input parameters to find object.
103 *
104 * @return array
105 */
106 public static function getValues($entityBlock) {
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 *
113 * @param int $id
114 * The contact id.
115 *
116 * @param bool $updateBlankLocInfo
117 *
118 * @return array
119 * the array of email id's
120 */
121 public static function allEmails($id, $updateBlankLocInfo = FALSE) {
122 if (!$id) {
123 return NULL;
124 }
125
126 $query = "
127 SELECT 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
133 FROM civicrm_contact
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 ";
138 $params = [
139 1 => [
140 $id,
141 'Integer',
142 ],
143 ];
144
145 $emails = $values = [];
146 $dao = CRM_Core_DAO::executeQuery($query, $params);
147 $count = 1;
148 while ($dao->fetch()) {
149 $values = [
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,
156 ];
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 *
171 * @param array $entityElements
172 * The array containing entity_id and.
173 * entity_table name
174 *
175 * @return array
176 * the array of email id's
177 */
178 public static function allEntityEmails(&$entityElements) {
179 if (empty($entityElements)) {
180 return NULL;
181 }
182
183 $entityId = $entityElements['entity_id'];
184 $entityTable = $entityElements['entity_table'];
185
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
188 WHERE ev.id = %1
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 ";
193
194 $params = [
195 1 => [
196 $entityId,
197 'Integer',
198 ],
199 ];
200
201 $emails = [];
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,
211 ];
212 }
213
214 return $emails;
215 }
216
217 /**
218 * Set / reset hold status for an email
219 *
220 * @param object $email
221 * Email object.
222 */
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.
226 return;
227 }
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
234 //check for update mode
235 if ($email->id) {
236 $params = [1 => [$email->id, 'Integer']];
237 if ($email->on_hold) {
238 $sql = "
239 SELECT id
240 FROM civicrm_email
241 WHERE id = %1
242 AND 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 }
249 elseif ($email->on_hold === 0) {
250 // we do this lookup to see if reset_date should be changed.
251 $sql = "
252 SELECT id
253 FROM civicrm_email
254 WHERE id = %1
255 AND hold_date IS NOT NULL
256 AND 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
260 $email->on_hold = FALSE;
261 $email->hold_date = 'null';
262 $email->reset_date = date('YmdHis');
263 }
264 }
265 }
266 else {
267 if ($email->on_hold) {
268 $email->hold_date = date('YmdHis');
269 }
270 }
271 }
272
273 /**
274 * Generate an array of Domain email addresses.
275 * @return array $domainEmails;
276 */
277 public static function domainEmails() {
278 $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);
283 }
284 return $domainEmails;
285 }
286
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 *
291 * @return array
292 * an array of email ids
293 */
294 public static function getFromEmail() {
295 // add all configured FROM email addresses
296 $fromEmailValues = self::domainEmails();
297
298 if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) {
299 return $fromEmailValues;
300 }
301
302 $contactFromEmails = [];
303 // add logged in user's active email ids
304 $contactID = CRM_Core_Session::singleton()->getLoggedInContactID();
305 if ($contactID) {
306 $contactEmails = self::allEmails($contactID);
307 $fromDisplayName = CRM_Core_Session::singleton()->getLoggedInContactDisplayName();
308
309 foreach ($contactEmails as $emailId => $emailVal) {
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
317 if (!empty($emailVal['is_primary'])) {
318 $fromEmailHtml .= ' ' . ts('(preferred)');
319 }
320 $contactFromEmails[$emailId] = $fromEmailHtml;
321 }
322 }
323 return CRM_Utils_Array::crmArrayMerge($contactFromEmails, $fromEmailValues);
324 }
325
326 /**
327 * @return object
328 */
329 public static function isMultipleBulkMail() {
330 return Civi::settings()->get('civimail_multiple_bulk_emails');
331 }
332
333 /**
334 * Call common delete function.
335 *
336 * @param int $id
337 *
338 * @return bool
339 */
340 public static function del($id) {
341 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Email', $id);
342 }
343
344 }