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 (is_numeric(CRM_Utils_Array
::value('is_primary', $params)) ||
49 // if id is set & is_primary isn't we can assume no change
52 CRM_Core_BAO_Block
::handlePrimary($params, get_class());
54 $email = CRM_Core_BAO_Email
::add($params);
60 * takes an associative array and adds email
62 * @param array $params (reference ) an assoc array of name/value pairs
64 * @return object CRM_Core_BAO_Email object on success, null otherwise
68 static function add(&$params) {
69 $hook = empty($params['id']) ?
'create' : 'edit';
70 CRM_Utils_Hook
::pre($hook, 'Email', CRM_Utils_Array
::value('id', $params), $params);
72 $email = new CRM_Core_DAO_Email();
73 $email->copyValues($params);
75 // lower case email field to optimize queries
76 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
77 $email->email
= $strtolower($email->email
);
79 // since we're setting bulkmail for 1 of this contact's emails, first reset all their emails to is_bulkmail false
80 // (only 1 email address can have is_bulkmail = true)
81 if ($email->is_bulkmail
!= 'null' && $params['contact_id'] && !self
::isMultipleBulkMail()) {
85 WHERE contact_id = {$params['contact_id']}
87 CRM_Core_DAO
::executeQuery($sql);
90 // handle if email is on hold
91 self
::holdEmail($email);
95 CRM_Utils_Hook
::post($hook, 'Email', $email->id
, $email);
100 * Given the list of params in the params array, fetch the object
101 * and store the values in the values array
103 * @param array $entityBlock input parameters to find object
109 static function &getValues($entityBlock) {
110 return CRM_Core_BAO_Block
::getValues('email', $entityBlock);
114 * Get all the emails for a specified contact_id, with the primary email being first
116 * @param int $id the contact id
118 * @return array the array of email id's
122 static function allEmails($id, $updateBlankLocInfo = FALSE) {
129 civicrm_location_type.name as locationType,
130 civicrm_email.is_primary as is_primary,
131 civicrm_email.on_hold as on_hold,
132 civicrm_email.id as email_id,
133 civicrm_email.location_type_id as locationTypeId
135 LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id )
136 LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id )
137 WHERE civicrm_contact.id = %1
138 ORDER BY civicrm_email.is_primary DESC, email_id ASC ";
146 $emails = $values = array();
147 $dao = CRM_Core_DAO
::executeQuery($query, $params);
149 while ($dao->fetch()) {
151 'locationType' => $dao->locationType
,
152 'is_primary' => $dao->is_primary
,
153 'on_hold' => $dao->on_hold
,
154 'id' => $dao->email_id
,
155 'email' => $dao->email
,
156 'locationTypeId' => $dao->locationTypeId
,
159 if ($updateBlankLocInfo) {
160 $emails[$count++
] = $values;
163 $emails[$dao->email_id
] = $values;
170 * Get all the emails for a specified location_block id, with the primary email being first
172 * @param array $entityElements the array containing entity_id and
175 * @return array the array of email id's
179 static function allEntityEmails(&$entityElements) {
180 if (empty($entityElements)) {
184 $entityId = $entityElements['entity_id'];
185 $entityTable = $entityElements['entity_table'];
188 $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
189 FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev
191 AND loc.id = ev.loc_block_id
192 AND e.id IN (loc.email_id, loc.email_2_id)
193 AND ltype.id = e.location_type_id
194 ORDER BY e.is_primary DESC, email_id ASC ";
204 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
205 while ($dao->fetch()) {
206 $emails[$dao->email_id
] = array(
207 'locationType' => $dao->locationType
,
208 'is_primary' => $dao->is_primary
,
209 'on_hold' => $dao->on_hold
,
210 'id' => $dao->email_id
,
211 'email' => $dao->email
,
212 'locationTypeId' => $dao->locationTypeId
,
220 * Function to set / reset hold status for an email
222 * @param object $email email object
227 static function holdEmail(&$email) {
228 //check for update mode
230 $params = array(1 => array($email->id
, 'Integer'));
231 if ($email->on_hold
&& $email->on_hold
!= 'null') {
236 AND hold_date IS NULL
238 if (CRM_Core_DAO
::singleValueQuery($sql, $params)) {
239 $email->hold_date
= date('YmdHis');
240 $email->reset_date
= 'null';
243 elseif ($email->on_hold
== 'null') {
248 AND hold_date IS NOT NULL
249 AND reset_date IS NULL
251 if (CRM_Core_DAO
::singleValueQuery($sql, $params)) {
252 //set reset date only if it is not set and if hold date is set
253 $email->on_hold
= FALSE;
254 $email->hold_date
= 'null';
255 $email->reset_date
= date('YmdHis');
260 if (($email->on_hold
!= 'null') && $email->on_hold
) {
261 $email->hold_date
= date('YmdHis');
267 * Build From Email as the combination of all the email ids of the logged in user and
268 * the domain email id
270 * @return array an array of email ids
274 static function getFromEmail() {
275 $session = CRM_Core_Session
::singleton();
276 $contactID = $session->get('userID');
277 $fromEmailValues = array();
279 // add the domain email id
280 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
281 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
282 $fromEmailValues[$domainEmail] = htmlspecialchars($domainEmail);
284 // add logged in user's active email ids
286 $contactEmails = self
::allEmails($contactID);
287 $fromDisplayName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'display_name');
289 foreach ($contactEmails as $emailId => $emailVal) {
290 $email = trim($emailVal['email']);
291 if (!$email ||
$emailVal['on_hold']) {
294 $fromEmail = "$fromDisplayName <$email>";
295 $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType'];
297 if (CRM_Utils_Array
::value('is_primary', $emailVal)) {
298 $fromEmailHtml .= ' ' . ts('(preferred)');
300 $fromEmailValues[$fromEmail] = $fromEmailHtml;
303 return $fromEmailValues;
306 static function isMultipleBulkMail() {
307 return CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
, 'civimail_multiple_bulk_emails', NULL, FALSE);
311 * Call common delete function
313 static function del($id) {
314 return CRM_Contact_BAO_Contact
::deleteObjectWithPrimary('Email', $id);