Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Core / BAO / Email.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33
34/**
35 * This class contains functions for email handling.
36 */
37class CRM_Core_BAO_Email extends CRM_Core_DAO_Email {
38
39 /**
40 * Create email address.
41 *
42 * Note that the create function calls 'add' but has more business logic.
43 *
44 * @param array $params
45 * Input parameters.
46 *
47 * @return object
48 */
49 public static function create($params) {
50 // if id is set & is_primary isn't we can assume no change
51 if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) || empty($params['id'])) {
52 CRM_Core_BAO_Block::handlePrimary($params, get_class());
53 }
54
55 $email = CRM_Core_BAO_Email::add($params);
56
57 return $email;
58 }
59
60 /**
61 * Takes an associative array and adds email.
62 *
63 * @param array $params
64 * (reference ) an assoc array of name/value pairs.
65 *
66 * @return object
67 * CRM_Core_BAO_Email object on success, null otherwise
68 */
69 public static function add(&$params) {
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);
75
76 // lower case email field to optimize queries
77 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
78 $email->email = $strtolower($email->email);
79
80 /*
81 * since we're setting bulkmail for 1 of this contact's emails, first reset all their other emails to is_bulkmail false
82 * We shouldn't not set the current email to false even though we
83 * are about to reset it to avoid contaminating the changelog if logging is enabled
84 * (only 1 email address can have is_bulkmail = true)
85 */
86 if ($email->is_bulkmail != 'null' && $params['contact_id'] && !self::isMultipleBulkMail()) {
87 $sql = "
88UPDATE civicrm_email
89SET is_bulkmail = 0
90WHERE contact_id = {$params['contact_id']}
91";
92 if ($hook == 'edit') {
93 $sql .= " AND id <> {$params['id']}";
94 }
95 CRM_Core_DAO::executeQuery($sql);
96 }
97
98 // handle if email is on hold
99 self::holdEmail($email);
100
101 $email->save();
102
103 if ($email->is_primary) {
104 // update the UF user email if that has changed
105 CRM_Core_BAO_UFMatch::updateUFName($email->contact_id);
106 }
107
108 CRM_Utils_Hook::post($hook, 'Email', $email->id, $email);
109 return $email;
110 }
111
112 /**
113 * Given the list of params in the params array, fetch the object
114 * and store the values in the values array
115 *
116 * @param array $entityBlock
117 * Input parameters to find object.
118 *
119 * @return bool
120 */
121 public static function &getValues($entityBlock) {
122 return CRM_Core_BAO_Block::getValues('email', $entityBlock);
123 }
124
125 /**
126 * Get all the emails for a specified contact_id, with the primary email being first
127 *
128 * @param int $id
129 * The contact id.
130 *
131 * @param bool $updateBlankLocInfo
132 *
133 * @return array
134 * the array of email id's
135 */
136 public static function allEmails($id, $updateBlankLocInfo = FALSE) {
137 if (!$id) {
138 return NULL;
139 }
140
141 $query = "
142SELECT email,
143 civicrm_location_type.name as locationType,
144 civicrm_email.is_primary as is_primary,
145 civicrm_email.on_hold as on_hold,
146 civicrm_email.id as email_id,
147 civicrm_email.location_type_id as locationTypeId
148FROM civicrm_contact
149LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id )
150LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id )
151WHERE civicrm_contact.id = %1
152ORDER BY civicrm_email.is_primary DESC, email_id ASC ";
153 $params = array(
154 1 => array(
155 $id,
156 'Integer',
157 ),
158 );
159
160 $emails = $values = array();
161 $dao = CRM_Core_DAO::executeQuery($query, $params);
162 $count = 1;
163 while ($dao->fetch()) {
164 $values = array(
165 'locationType' => $dao->locationType,
166 'is_primary' => $dao->is_primary,
167 'on_hold' => $dao->on_hold,
168 'id' => $dao->email_id,
169 'email' => $dao->email,
170 'locationTypeId' => $dao->locationTypeId,
171 );
172
173 if ($updateBlankLocInfo) {
174 $emails[$count++] = $values;
175 }
176 else {
177 $emails[$dao->email_id] = $values;
178 }
179 }
180 return $emails;
181 }
182
183 /**
184 * Get all the emails for a specified location_block id, with the primary email being first
185 *
186 * @param array $entityElements
187 * The array containing entity_id and.
188 * entity_table name
189 *
190 * @return array
191 * the array of email id's
192 */
193 public static function allEntityEmails(&$entityElements) {
194 if (empty($entityElements)) {
195 return NULL;
196 }
197
198 $entityId = $entityElements['entity_id'];
199 $entityTable = $entityElements['entity_table'];
200
201 $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
202FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev
203WHERE ev.id = %1
204AND loc.id = ev.loc_block_id
205AND e.id IN (loc.email_id, loc.email_2_id)
206AND ltype.id = e.location_type_id
207ORDER BY e.is_primary DESC, email_id ASC ";
208
209 $params = array(
210 1 => array(
211 $entityId,
212 'Integer',
213 ),
214 );
215
216 $emails = array();
217 $dao = CRM_Core_DAO::executeQuery($sql, $params);
218 while ($dao->fetch()) {
219 $emails[$dao->email_id] = array(
220 'locationType' => $dao->locationType,
221 'is_primary' => $dao->is_primary,
222 'on_hold' => $dao->on_hold,
223 'id' => $dao->email_id,
224 'email' => $dao->email,
225 'locationTypeId' => $dao->locationTypeId,
226 );
227 }
228
229 return $emails;
230 }
231
232 /**
233 * Set / reset hold status for an email
234 *
235 * @param object $email
236 * Email object.
237 */
238 public static function holdEmail(&$email) {
239 //check for update mode
240 if ($email->id) {
241 $params = array(1 => array($email->id, 'Integer'));
242 if ($email->on_hold && $email->on_hold != 'null') {
243 $sql = "
244SELECT id
245FROM civicrm_email
246WHERE id = %1
247AND hold_date IS NULL
248";
249 if (CRM_Core_DAO::singleValueQuery($sql, $params)) {
250 $email->hold_date = date('YmdHis');
251 $email->reset_date = 'null';
252 }
253 }
254 elseif ($email->on_hold == 'null') {
255 $sql = "
256SELECT id
257FROM civicrm_email
258WHERE id = %1
259AND hold_date IS NOT NULL
260AND reset_date IS NULL
261";
262 if (CRM_Core_DAO::singleValueQuery($sql, $params)) {
263 //set reset date only if it is not set and if hold date is set
264 $email->on_hold = FALSE;
265 $email->hold_date = 'null';
266 $email->reset_date = date('YmdHis');
267 }
268 }
269 }
270 else {
271 if (($email->on_hold != 'null') && $email->on_hold) {
272 $email->hold_date = date('YmdHis');
273 }
274 }
275 }
276
277 /**
278 * Build From Email as the combination of all the email ids of the logged in user and
279 * the domain email id
280 *
281 * @return array
282 * an array of email ids
283 */
284 public static function getFromEmail() {
285 $session = CRM_Core_Session::singleton();
286 $contactID = $session->get('userID');
287 $fromEmailValues = array();
288
289 // add all configured FROM email addresses
290 $domainFrom = CRM_Core_OptionGroup::values('from_email_address');
291 foreach (array_keys($domainFrom) as $k) {
292 $domainEmail = $domainFrom[$k];
293 $fromEmailValues[$domainEmail] = htmlspecialchars($domainEmail);
294 }
295
296 // add logged in user's active email ids
297 if ($contactID) {
298 $contactEmails = self::allEmails($contactID);
299 $fromDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'display_name');
300
301 foreach ($contactEmails as $emailId => $emailVal) {
302 $email = trim($emailVal['email']);
303 if (!$email || $emailVal['on_hold']) {
304 continue;
305 }
306 $fromEmail = "$fromDisplayName <$email>";
307 $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType'];
308
309 if (!empty($emailVal['is_primary'])) {
310 $fromEmailHtml .= ' ' . ts('(preferred)');
311 }
312 $fromEmailValues[$fromEmail] = $fromEmailHtml;
313 }
314 }
315 return $fromEmailValues;
316 }
317
318 /**
319 * @return object
320 */
321 public static function isMultipleBulkMail() {
322 return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'civimail_multiple_bulk_emails', NULL, FALSE);
323 }
324
325 /**
326 * Call common delete function.
327 */
328 public static function del($id) {
329 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Email', $id);
330 }
331
332}