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