3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
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());
56 $email = CRM_Core_BAO_Email
::add($params);
62 * Takes an associative array and adds email
64 * @param array $params
65 * (reference ) an assoc array of name/value pairs.
68 * CRM_Core_BAO_Email object on success, null otherwise
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);
75 $email = new CRM_Core_DAO_Email();
76 $email->copyValues($params);
78 // lower case email field to optimize queries
79 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
80 $email->email
= $strtolower($email->email
);
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)
88 if ($email->is_bulkmail
!= 'null' && $params['contact_id'] && !self
::isMultipleBulkMail()) {
92 WHERE contact_id = {$params['contact_id']}
94 if ($hook == 'edit') {
95 $sql .= " AND id <> {$params['id']}";
97 CRM_Core_DAO
::executeQuery($sql);
100 // handle if email is on hold
101 self
::holdEmail($email);
105 if ($email->is_primary
) {
106 // update the UF user email if that has changed
107 CRM_Core_BAO_UFMatch
::updateUFName($email->contact_id
);
110 CRM_Utils_Hook
::post($hook, 'Email', $email->id
, $email);
115 * Given the list of params in the params array, fetch the object
116 * and store the values in the values array
118 * @param array $entityBlock
119 * Input parameters to find object.
124 public static function &getValues($entityBlock) {
125 return CRM_Core_BAO_Block
::getValues('email', $entityBlock);
129 * Get all the emails for a specified contact_id, with the primary email being first
134 * @param bool $updateBlankLocInfo
137 * the array of email id's
140 public static function allEmails($id, $updateBlankLocInfo = FALSE) {
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
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 ";
164 $emails = $values = array();
165 $dao = CRM_Core_DAO
::executeQuery($query, $params);
167 while ($dao->fetch()) {
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
,
177 if ($updateBlankLocInfo) {
178 $emails[$count++
] = $values;
181 $emails[$dao->email_id
] = $values;
188 * Get all the emails for a specified location_block id, with the primary email being first
190 * @param array $entityElements
191 * The array containing entity_id and.
195 * the array of email id's
198 public static function allEntityEmails(&$entityElements) {
199 if (empty($entityElements)) {
203 $entityId = $entityElements['entity_id'];
204 $entityTable = $entityElements['entity_table'];
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
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 ";
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
,
238 * Set / reset hold status for an email
240 * @param object $email
246 public static function holdEmail(&$email) {
247 //check for update mode
249 $params = array(1 => array($email->id
, 'Integer'));
250 if ($email->on_hold
&& $email->on_hold
!= 'null') {
255 AND hold_date IS NULL
257 if (CRM_Core_DAO
::singleValueQuery($sql, $params)) {
258 $email->hold_date
= date('YmdHis');
259 $email->reset_date
= 'null';
262 elseif ($email->on_hold
== 'null') {
267 AND hold_date IS NOT NULL
268 AND reset_date IS NULL
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');
279 if (($email->on_hold
!= 'null') && $email->on_hold
) {
280 $email->hold_date
= date('YmdHis');
286 * Build From Email as the combination of all the email ids of the logged in user and
287 * the domain email id
290 * an array of email ids
293 public static function getFromEmail() {
294 $session = CRM_Core_Session
::singleton();
295 $contactID = $session->get('userID');
296 $fromEmailValues = array();
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);
305 // add logged in user's active email ids
307 $contactEmails = self
::allEmails($contactID);
308 $fromDisplayName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'display_name');
310 foreach ($contactEmails as $emailId => $emailVal) {
311 $email = trim($emailVal['email']);
312 if (!$email ||
$emailVal['on_hold']) {
315 $fromEmail = "$fromDisplayName <$email>";
316 $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType'];
318 if (!empty($emailVal['is_primary'])) {
319 $fromEmailHtml .= ' ' . ts('(preferred)');
321 $fromEmailValues[$fromEmail] = $fromEmailHtml;
324 return $fromEmailValues;
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);
335 * Call common delete function
337 public static function del($id) {
338 return CRM_Contact_BAO_Contact
::deleteObjectWithPrimary('Email', $id);