3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class contains functions for email handling
39 class CRM_Core_BAO_Email
extends CRM_Core_DAO_Email
{
42 * Create email address - note that the create function calls 'add' but
43 * has more business logic
45 * @param array $params input parameters
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());
55 $email = CRM_Core_BAO_Email
::add($params);
61 * takes an associative array and adds email
63 * @param array $params (reference ) an assoc array of name/value pairs
65 * @return object CRM_Core_BAO_Email object on success, null otherwise
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);
73 $email = new CRM_Core_DAO_Email();
74 $email->copyValues($params);
76 // lower case email field to optimize queries
77 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
78 $email->email
= $strtolower($email->email
);
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)
86 if ($email->is_bulkmail
!= 'null' && $params['contact_id'] && !self
::isMultipleBulkMail()) {
90 WHERE contact_id = {$params['contact_id']}
93 $sql .= " AND id <> {$params['id']}";
95 CRM_Core_DAO
::executeQuery($sql);
98 // handle if email is on hold
99 self
::holdEmail($email);
103 if ($email->is_primary
) {
104 // update the UF user email if that has changed
105 CRM_Core_BAO_UFMatch
::updateUFName($email->contact_id
);
108 CRM_Utils_Hook
::post($hook, 'Email', $email->id
, $email);
113 * Given the list of params in the params array, fetch the object
114 * and store the values in the values array
116 * @param array $entityBlock input parameters to find object
122 static function &getValues($entityBlock) {
123 return CRM_Core_BAO_Block
::getValues('email', $entityBlock);
127 * Get all the emails for a specified contact_id, with the primary email being first
129 * @param int $id the contact id
131 * @return array the array of email id's
135 static function allEmails($id, $updateBlankLocInfo = FALSE) {
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
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 ";
159 $emails = $values = array();
160 $dao = CRM_Core_DAO
::executeQuery($query, $params);
162 while ($dao->fetch()) {
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
,
172 if ($updateBlankLocInfo) {
173 $emails[$count++
] = $values;
176 $emails[$dao->email_id
] = $values;
183 * Get all the emails for a specified location_block id, with the primary email being first
185 * @param array $entityElements the array containing entity_id and
188 * @return array the array of email id's
192 static function allEntityEmails(&$entityElements) {
193 if (empty($entityElements)) {
197 $entityId = $entityElements['entity_id'];
198 $entityTable = $entityElements['entity_table'];
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
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 ";
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
,
233 * Function to set / reset hold status for an email
235 * @param object $email email object
240 static function holdEmail(&$email) {
241 //check for update mode
243 $params = array(1 => array($email->id
, 'Integer'));
244 if ($email->on_hold
&& $email->on_hold
!= 'null') {
249 AND hold_date IS NULL
251 if (CRM_Core_DAO
::singleValueQuery($sql, $params)) {
252 $email->hold_date
= date('YmdHis');
253 $email->reset_date
= 'null';
256 elseif ($email->on_hold
== 'null') {
261 AND hold_date IS NOT NULL
262 AND reset_date IS NULL
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');
273 if (($email->on_hold
!= 'null') && $email->on_hold
) {
274 $email->hold_date
= date('YmdHis');
280 * Build From Email as the combination of all the email ids of the logged in user and
281 * the domain email id
283 * @return array an array of email ids
287 static function getFromEmail() {
288 $session = CRM_Core_Session
::singleton();
289 $contactID = $session->get('userID');
290 $fromEmailValues = array();
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);
299 // add logged in user's active email ids
301 $contactEmails = self
::allEmails($contactID);
302 $fromDisplayName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'display_name');
304 foreach ($contactEmails as $emailId => $emailVal) {
305 $email = trim($emailVal['email']);
306 if (!$email ||
$emailVal['on_hold']) {
309 $fromEmail = "$fromDisplayName <$email>";
310 $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType'];
312 if (!empty($emailVal['is_primary'])) {
313 $fromEmailHtml .= ' ' . ts('(preferred)');
315 $fromEmailValues[$fromEmail] = $fromEmailHtml;
318 return $fromEmailValues;
321 static function isMultipleBulkMail() {
322 return CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
, 'civimail_multiple_bulk_emails', NULL, FALSE);
326 * Call common delete function
328 static function del($id) {
329 return CRM_Contact_BAO_Contact
::deleteObjectWithPrimary('Email', $id);