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