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