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