Merge pull request #4696 from colemanw/CRM-15669
[civicrm-core.git] / CRM / Core / BAO / Email.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class contains functions for email handling
38 */
39 class CRM_Core_BAO_Email extends CRM_Core_DAO_Email {
40
41 /**
42 * Create email address - note that the create function calls 'add' but
43 * has more business logic
44 *
45 * @param array $params 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 (reference ) an assoc array of name/value pairs
64 *
65 * @return object CRM_Core_BAO_Email object on success, null otherwise
66 * @static
67 */
68 public static function add(&$params) {
69 $hook = empty($params['id']) ? 'create' : 'edit';
70 CRM_Utils_Hook::pre($hook, 'Email', CRM_Utils_Array::value('id', $params), $params);
71
72 $email = new CRM_Core_DAO_Email();
73 $email->copyValues($params);
74
75 // lower case email field to optimize queries
76 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
77 $email->email = $strtolower($email->email);
78
79 /*
80 * since we're setting bulkmail for 1 of this contact's emails, first reset all their other emails to is_bulkmail false
81 * We shouldn't not set the current email to false even though we
82 * are about to reset it to avoid contaminating the changelog if logging is enabled
83 * (only 1 email address can have is_bulkmail = true)
84 */
85 if ($email->is_bulkmail != 'null' && $params['contact_id'] && !self::isMultipleBulkMail()) {
86 $sql = "
87 UPDATE civicrm_email
88 SET is_bulkmail = 0
89 WHERE contact_id = {$params['contact_id']}
90 ";
91 if($hook == 'edit'){
92 $sql .= " AND id <> {$params['id']}";
93 }
94 CRM_Core_DAO::executeQuery($sql);
95 }
96
97 // handle if email is on hold
98 self::holdEmail($email);
99
100 $email->save();
101
102 if ($email->is_primary) {
103 // update the UF user email if that has changed
104 CRM_Core_BAO_UFMatch::updateUFName($email->contact_id);
105 }
106
107 CRM_Utils_Hook::post($hook, 'Email', $email->id, $email);
108 return $email;
109 }
110
111 /**
112 * Given the list of params in the params array, fetch the object
113 * and store the values in the values array
114 *
115 * @param array $entityBlock input parameters to find object
116 *
117 * @return boolean
118 * @static
119 */
120 public static function &getValues($entityBlock) {
121 return CRM_Core_BAO_Block::getValues('email', $entityBlock);
122 }
123
124 /**
125 * Get all the emails for a specified contact_id, with the primary email being first
126 *
127 * @param int $id the contact id
128 *
129 * @param bool $updateBlankLocInfo
130 *
131 * @return array the array of email id's
132 * @static
133 */
134 public static function allEmails($id, $updateBlankLocInfo = FALSE) {
135 if (!$id) {
136 return NULL;
137 }
138
139 $query = "
140 SELECT email,
141 civicrm_location_type.name as locationType,
142 civicrm_email.is_primary as is_primary,
143 civicrm_email.on_hold as on_hold,
144 civicrm_email.id as email_id,
145 civicrm_email.location_type_id as locationTypeId
146 FROM civicrm_contact
147 LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id )
148 LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id )
149 WHERE civicrm_contact.id = %1
150 ORDER BY civicrm_email.is_primary DESC, email_id ASC ";
151 $params = array(
152 1 => array(
153 $id,
154 'Integer',
155 ),
156 );
157
158 $emails = $values = array();
159 $dao = CRM_Core_DAO::executeQuery($query, $params);
160 $count = 1;
161 while ($dao->fetch()) {
162 $values = array(
163 'locationType' => $dao->locationType,
164 'is_primary' => $dao->is_primary,
165 'on_hold' => $dao->on_hold,
166 'id' => $dao->email_id,
167 'email' => $dao->email,
168 'locationTypeId' => $dao->locationTypeId,
169 );
170
171 if ($updateBlankLocInfo) {
172 $emails[$count++] = $values;
173 }
174 else {
175 $emails[$dao->email_id] = $values;
176 }
177 }
178 return $emails;
179 }
180
181 /**
182 * Get all the emails for a specified location_block id, with the primary email being first
183 *
184 * @param array $entityElements the array containing entity_id and
185 * entity_table name
186 *
187 * @return array the array of email id's
188 * @static
189 */
190 public static function allEntityEmails(&$entityElements) {
191 if (empty($entityElements)) {
192 return NULL;
193 }
194
195 $entityId = $entityElements['entity_id'];
196 $entityTable = $entityElements['entity_table'];
197
198
199 $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
200 FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev
201 WHERE ev.id = %1
202 AND loc.id = ev.loc_block_id
203 AND e.id IN (loc.email_id, loc.email_2_id)
204 AND ltype.id = e.location_type_id
205 ORDER BY e.is_primary DESC, email_id ASC ";
206
207 $params = array(
208 1 => array(
209 $entityId,
210 'Integer',
211 ),
212 );
213
214 $emails = array();
215 $dao = CRM_Core_DAO::executeQuery($sql, $params);
216 while ($dao->fetch()) {
217 $emails[$dao->email_id] = array(
218 'locationType' => $dao->locationType,
219 'is_primary' => $dao->is_primary,
220 'on_hold' => $dao->on_hold,
221 'id' => $dao->email_id,
222 'email' => $dao->email,
223 'locationTypeId' => $dao->locationTypeId,
224 );
225 }
226
227 return $emails;
228 }
229
230 /**
231 * Set / reset hold status for an email
232 *
233 * @param object $email email object
234 *
235 * @return void
236 * @static
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 = "
244 SELECT id
245 FROM civicrm_email
246 WHERE id = %1
247 AND 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 = "
256 SELECT id
257 FROM civicrm_email
258 WHERE id = %1
259 AND hold_date IS NOT NULL
260 AND 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 an array of email ids
282 * @static
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 }