3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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
);
78 // since we're setting bulkmail for 1 of this contact's emails, first reset all their emails to is_bulkmail false
79 // (only 1 email address can have is_bulkmail = true)
80 if ($email->is_bulkmail
!= 'null' && $params['contact_id'] && !self
::isMultipleBulkMail()) {
84 WHERE contact_id = {$params['contact_id']}
86 CRM_Core_DAO
::executeQuery($sql);
89 // handle if email is on hold
90 self
::holdEmail($email);
94 if ($email->is_primary
) {
95 // update the UF user email if that has changed
96 CRM_Core_BAO_UFMatch
::updateUFName($email->contact_id
);
99 CRM_Utils_Hook
::post($hook, 'Email', $email->id
, $email);
104 * Given the list of params in the params array, fetch the object
105 * and store the values in the values array
107 * @param array $entityBlock input parameters to find object
113 static function &getValues($entityBlock) {
114 return CRM_Core_BAO_Block
::getValues('email', $entityBlock);
118 * Get all the emails for a specified contact_id, with the primary email being first
120 * @param int $id the contact id
122 * @return array the array of email id's
126 static function allEmails($id, $updateBlankLocInfo = FALSE) {
133 civicrm_location_type.name as locationType,
134 civicrm_email.is_primary as is_primary,
135 civicrm_email.on_hold as on_hold,
136 civicrm_email.id as email_id,
137 civicrm_email.location_type_id as locationTypeId
139 LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id )
140 LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id )
141 WHERE civicrm_contact.id = %1
142 ORDER BY civicrm_email.is_primary DESC, email_id ASC ";
150 $emails = $values = array();
151 $dao = CRM_Core_DAO
::executeQuery($query, $params);
153 while ($dao->fetch()) {
155 'locationType' => $dao->locationType
,
156 'is_primary' => $dao->is_primary
,
157 'on_hold' => $dao->on_hold
,
158 'id' => $dao->email_id
,
159 'email' => $dao->email
,
160 'locationTypeId' => $dao->locationTypeId
,
163 if ($updateBlankLocInfo) {
164 $emails[$count++
] = $values;
167 $emails[$dao->email_id
] = $values;
174 * Get all the emails for a specified location_block id, with the primary email being first
176 * @param array $entityElements the array containing entity_id and
179 * @return array the array of email id's
183 static function allEntityEmails(&$entityElements) {
184 if (empty($entityElements)) {
188 $entityId = $entityElements['entity_id'];
189 $entityTable = $entityElements['entity_table'];
192 $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
193 FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev
195 AND loc.id = ev.loc_block_id
196 AND e.id IN (loc.email_id, loc.email_2_id)
197 AND ltype.id = e.location_type_id
198 ORDER BY e.is_primary DESC, email_id ASC ";
208 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
209 while ($dao->fetch()) {
210 $emails[$dao->email_id
] = array(
211 'locationType' => $dao->locationType
,
212 'is_primary' => $dao->is_primary
,
213 'on_hold' => $dao->on_hold
,
214 'id' => $dao->email_id
,
215 'email' => $dao->email
,
216 'locationTypeId' => $dao->locationTypeId
,
224 * Function to set / reset hold status for an email
226 * @param object $email email object
231 static function holdEmail(&$email) {
232 //check for update mode
234 $params = array(1 => array($email->id
, 'Integer'));
235 if ($email->on_hold
&& $email->on_hold
!= 'null') {
240 AND hold_date IS NULL
242 if (CRM_Core_DAO
::singleValueQuery($sql, $params)) {
243 $email->hold_date
= date('YmdHis');
244 $email->reset_date
= 'null';
247 elseif ($email->on_hold
== 'null') {
252 AND hold_date IS NOT NULL
253 AND reset_date IS NULL
255 if (CRM_Core_DAO
::singleValueQuery($sql, $params)) {
256 //set reset date only if it is not set and if hold date is set
257 $email->on_hold
= FALSE;
258 $email->hold_date
= 'null';
259 $email->reset_date
= date('YmdHis');
264 if (($email->on_hold
!= 'null') && $email->on_hold
) {
265 $email->hold_date
= date('YmdHis');
271 * Build From Email as the combination of all the email ids of the logged in user and
272 * the domain email id
274 * @return array an array of email ids
278 static function getFromEmail() {
279 $session = CRM_Core_Session
::singleton();
280 $contactID = $session->get('userID');
281 $fromEmailValues = array();
283 // add the domain email id
284 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
285 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
286 $fromEmailValues[$domainEmail] = htmlspecialchars($domainEmail);
288 // add logged in user's active email ids
290 $contactEmails = self
::allEmails($contactID);
291 $fromDisplayName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'display_name');
293 foreach ($contactEmails as $emailId => $emailVal) {
294 $email = trim($emailVal['email']);
295 if (!$email ||
$emailVal['on_hold']) {
298 $fromEmail = "$fromDisplayName <$email>";
299 $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType'];
301 if (CRM_Utils_Array
::value('is_primary', $emailVal)) {
302 $fromEmailHtml .= ' ' . ts('(preferred)');
304 $fromEmailValues[$fromEmail] = $fromEmailHtml;
307 return $fromEmailValues;
310 static function isMultipleBulkMail() {
311 return CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
, 'civimail_multiple_bulk_emails', NULL, FALSE);
315 * Call common delete function
317 static function del($id) {
318 return CRM_Contact_BAO_Contact
::deleteObjectWithPrimary('Email', $id);