Merge pull request #3209 from eileenmcnaughton/comments
[civicrm-core.git] / CRM / Core / BAO / Email.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 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 * @access public
67 * @static
68 */
69 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 = "
88 UPDATE civicrm_email
89 SET is_bulkmail = 0
90 WHERE 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 input parameters to find object
117 *
118 * @return boolean
119 * @access public
120 * @static
121 */
122 static function &getValues($entityBlock) {
123 return CRM_Core_BAO_Block::getValues('email', $entityBlock);
124 }
125
126 /**
127 * Get all the emails for a specified contact_id, with the primary email being first
128 *
129 * @param int $id the contact id
130 *
131 * @return array the array of email id's
132 * @access public
133 * @static
134 */
135 static function allEmails($id, $updateBlankLocInfo = FALSE) {
136 if (!$id) {
137 return NULL;
138 }
139
140 $query = "
141 SELECT email,
142 civicrm_location_type.name as locationType,
143 civicrm_email.is_primary as is_primary,
144 civicrm_email.on_hold as on_hold,
145 civicrm_email.id as email_id,
146 civicrm_email.location_type_id as locationTypeId
147 FROM civicrm_contact
148 LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id )
149 LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id )
150 WHERE civicrm_contact.id = %1
151 ORDER BY civicrm_email.is_primary DESC, email_id ASC ";
152 $params = array(
153 1 => array(
154 $id,
155 'Integer',
156 ),
157 );
158
159 $emails = $values = array();
160 $dao = CRM_Core_DAO::executeQuery($query, $params);
161 $count = 1;
162 while ($dao->fetch()) {
163 $values = array(
164 'locationType' => $dao->locationType,
165 'is_primary' => $dao->is_primary,
166 'on_hold' => $dao->on_hold,
167 'id' => $dao->email_id,
168 'email' => $dao->email,
169 'locationTypeId' => $dao->locationTypeId,
170 );
171
172 if ($updateBlankLocInfo) {
173 $emails[$count++] = $values;
174 }
175 else {
176 $emails[$dao->email_id] = $values;
177 }
178 }
179 return $emails;
180 }
181
182 /**
183 * Get all the emails for a specified location_block id, with the primary email being first
184 *
185 * @param array $entityElements the array containing entity_id and
186 * entity_table name
187 *
188 * @return array the array of email id's
189 * @access public
190 * @static
191 */
192 static function allEntityEmails(&$entityElements) {
193 if (empty($entityElements)) {
194 return NULL;
195 }
196
197 $entityId = $entityElements['entity_id'];
198 $entityTable = $entityElements['entity_table'];
199
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
202 FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev
203 WHERE ev.id = %1
204 AND loc.id = ev.loc_block_id
205 AND e.id IN (loc.email_id, loc.email_2_id)
206 AND ltype.id = e.location_type_id
207 ORDER 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 * Function to set / reset hold status for an email
234 *
235 * @param object $email email object
236 *
237 * @return void
238 * @static
239 */
240 static function holdEmail(&$email) {
241 //check for update mode
242 if ($email->id) {
243 $params = array(1 => array($email->id, 'Integer'));
244 if ($email->on_hold && $email->on_hold != 'null') {
245 $sql = "
246 SELECT id
247 FROM civicrm_email
248 WHERE id = %1
249 AND hold_date IS NULL
250 ";
251 if (CRM_Core_DAO::singleValueQuery($sql, $params)) {
252 $email->hold_date = date('YmdHis');
253 $email->reset_date = 'null';
254 }
255 }
256 elseif ($email->on_hold == 'null') {
257 $sql = "
258 SELECT id
259 FROM civicrm_email
260 WHERE id = %1
261 AND hold_date IS NOT NULL
262 AND reset_date IS NULL
263 ";
264 if (CRM_Core_DAO::singleValueQuery($sql, $params)) {
265 //set reset date only if it is not set and if hold date is set
266 $email->on_hold = FALSE;
267 $email->hold_date = 'null';
268 $email->reset_date = date('YmdHis');
269 }
270 }
271 }
272 else {
273 if (($email->on_hold != 'null') && $email->on_hold) {
274 $email->hold_date = date('YmdHis');
275 }
276 }
277 }
278
279 /**
280 * Build From Email as the combination of all the email ids of the logged in user and
281 * the domain email id
282 *
283 * @return array an array of email ids
284 * @access public
285 * @static
286 */
287 static function getFromEmail() {
288 $session = CRM_Core_Session::singleton();
289 $contactID = $session->get('userID');
290 $fromEmailValues = array();
291
292 // add all configured FROM email addresses
293 $domainFrom = CRM_Core_OptionGroup::values('from_email_address');
294 foreach (array_keys($domainFrom) as $k) {
295 $domainEmail = $domainFrom[$k];
296 $fromEmailValues[$domainEmail] = htmlspecialchars($domainEmail);
297 }
298
299 // add logged in user's active email ids
300 if ($contactID) {
301 $contactEmails = self::allEmails($contactID);
302 $fromDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'display_name');
303
304 foreach ($contactEmails as $emailId => $emailVal) {
305 $email = trim($emailVal['email']);
306 if (!$email || $emailVal['on_hold']) {
307 continue;
308 }
309 $fromEmail = "$fromDisplayName <$email>";
310 $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType'];
311
312 if (!empty($emailVal['is_primary'])) {
313 $fromEmailHtml .= ' ' . ts('(preferred)');
314 }
315 $fromEmailValues[$fromEmail] = $fromEmailHtml;
316 }
317 }
318 return $fromEmailValues;
319 }
320
321 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 static function del($id) {
329 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Email', $id);
330 }
331 }
332