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
47 static function create($params) {
48 // if id is set & is_primary isn't we can assume no change
49 if (is_numeric(CRM_Utils_Array
::value('is_primary', $params)) ||
empty($params['id'])) {
50 CRM_Core_BAO_Block
::handlePrimary($params, get_class());
53 $email = CRM_Core_BAO_Email
::add($params);
59 * takes an associative array and adds email
61 * @param array $params (reference ) an assoc array of name/value pairs
63 * @return object CRM_Core_BAO_Email object on success, null otherwise
67 static function add(&$params) {
68 $hook = empty($params['id']) ?
'create' : 'edit';
69 CRM_Utils_Hook
::pre($hook, 'Email', CRM_Utils_Array
::value('id', $params), $params);
71 $email = new CRM_Core_DAO_Email();
72 $email->copyValues($params);
74 // lower case email field to optimize queries
75 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
76 $email->email
= $strtolower($email->email
);
79 * since we're setting bulkmail for 1 of this contact's emails, first reset all their other emails to is_bulkmail false
80 * We shouldn't not set the current email to false even though we
81 * are about to reset it to avoid contaminating the changelog if logging is enabled
82 * (only 1 email address can have is_bulkmail = true)
84 if ($email->is_bulkmail
!= 'null' && $params['contact_id'] && !self
::isMultipleBulkMail()) {
88 WHERE contact_id = {$params['contact_id']}
91 $sql .= " AND id <> {$params['id']}";
93 CRM_Core_DAO
::executeQuery($sql);
96 // handle if email is on hold
97 self
::holdEmail($email);
101 if ($email->is_primary
) {
102 // update the UF user email if that has changed
103 CRM_Core_BAO_UFMatch
::updateUFName($email->contact_id
);
106 CRM_Utils_Hook
::post($hook, 'Email', $email->id
, $email);
111 * Given the list of params in the params array, fetch the object
112 * and store the values in the values array
114 * @param array $entityBlock input parameters to find object
120 static function &getValues($entityBlock) {
121 return CRM_Core_BAO_Block
::getValues('email', $entityBlock);
125 * Get all the emails for a specified contact_id, with the primary email being first
127 * @param int $id the contact id
129 * @return array the array of email id's
133 static function allEmails($id, $updateBlankLocInfo = FALSE) {
140 civicrm_location_type.name as locationType,
141 civicrm_email.is_primary as is_primary,
142 civicrm_email.on_hold as on_hold,
143 civicrm_email.id as email_id,
144 civicrm_email.location_type_id as locationTypeId
146 LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id )
147 LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id )
148 WHERE civicrm_contact.id = %1
149 ORDER BY civicrm_email.is_primary DESC, email_id ASC ";
157 $emails = $values = array();
158 $dao = CRM_Core_DAO
::executeQuery($query, $params);
160 while ($dao->fetch()) {
162 'locationType' => $dao->locationType
,
163 'is_primary' => $dao->is_primary
,
164 'on_hold' => $dao->on_hold
,
165 'id' => $dao->email_id
,
166 'email' => $dao->email
,
167 'locationTypeId' => $dao->locationTypeId
,
170 if ($updateBlankLocInfo) {
171 $emails[$count++
] = $values;
174 $emails[$dao->email_id
] = $values;
181 * Get all the emails for a specified location_block id, with the primary email being first
183 * @param array $entityElements the array containing entity_id and
186 * @return array the array of email id's
190 static function allEntityEmails(&$entityElements) {
191 if (empty($entityElements)) {
195 $entityId = $entityElements['entity_id'];
196 $entityTable = $entityElements['entity_table'];
199 $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
200 FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev
202 AND loc.id = ev.loc_block_id
203 AND e.id IN (loc.email_id, loc.email_2_id)
204 AND ltype.id = e.location_type_id
205 ORDER BY e.is_primary DESC, email_id ASC ";
215 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
216 while ($dao->fetch()) {
217 $emails[$dao->email_id
] = array(
218 'locationType' => $dao->locationType
,
219 'is_primary' => $dao->is_primary
,
220 'on_hold' => $dao->on_hold
,
221 'id' => $dao->email_id
,
222 'email' => $dao->email
,
223 'locationTypeId' => $dao->locationTypeId
,
231 * Function to set / reset hold status for an email
233 * @param object $email email object
238 static function holdEmail(&$email) {
239 //check for update mode
241 $params = array(1 => array($email->id
, 'Integer'));
242 if ($email->on_hold
&& $email->on_hold
!= 'null') {
247 AND hold_date IS NULL
249 if (CRM_Core_DAO
::singleValueQuery($sql, $params)) {
250 $email->hold_date
= date('YmdHis');
251 $email->reset_date
= 'null';
254 elseif ($email->on_hold
== 'null') {
259 AND hold_date IS NOT NULL
260 AND reset_date IS NULL
262 if (CRM_Core_DAO
::singleValueQuery($sql, $params)) {
263 //set reset date only if it is not set and if hold date is set
264 $email->on_hold
= FALSE;
265 $email->hold_date
= 'null';
266 $email->reset_date
= date('YmdHis');
271 if (($email->on_hold
!= 'null') && $email->on_hold
) {
272 $email->hold_date
= date('YmdHis');
278 * Build From Email as the combination of all the email ids of the logged in user and
279 * the domain email id
281 * @return array an array of email ids
285 static function getFromEmail() {
286 $session = CRM_Core_Session
::singleton();
287 $contactID = $session->get('userID');
288 $fromEmailValues = array();
290 // add all configured FROM email addresses
291 $domainFrom = CRM_Core_OptionGroup
::values('from_email_address');
292 foreach (array_keys($domainFrom) as $k) {
293 $domainEmail = $domainFrom[$k];
294 $fromEmailValues[$domainEmail] = htmlspecialchars($domainEmail);
297 // add logged in user's active email ids
299 $contactEmails = self
::allEmails($contactID);
300 $fromDisplayName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'display_name');
302 foreach ($contactEmails as $emailId => $emailVal) {
303 $email = trim($emailVal['email']);
304 if (!$email ||
$emailVal['on_hold']) {
307 $fromEmail = "$fromDisplayName <$email>";
308 $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType'];
310 if (!empty($emailVal['is_primary'])) {
311 $fromEmailHtml .= ' ' . ts('(preferred)');
313 $fromEmailValues[$fromEmail] = $fromEmailHtml;
316 return $fromEmailValues;
319 static function isMultipleBulkMail() {
320 return CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
, 'civimail_multiple_bulk_emails', NULL, FALSE);
324 * Call common delete function
326 static function del($id) {
327 return CRM_Contact_BAO_Contact
::deleteObjectWithPrimary('Email', $id);