| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class contains functions for email handling |
| 38 | */ |
| 39 | class CRM_Core_BAO_Email extends CRM_Core_DAO_Email { |
| 40 | |
| 41 | /** |
| 42 | * Create email address - note that the create function calls 'add' but |
| 43 | * has more business logic |
| 44 | * |
| 45 | * @param array $params |
| 46 | * Input parameters. |
| 47 | * |
| 48 | * @return object |
| 49 | */ |
| 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()); |
| 54 | } |
| 55 | |
| 56 | $email = CRM_Core_BAO_Email::add($params); |
| 57 | |
| 58 | return $email; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Takes an associative array and adds email |
| 63 | * |
| 64 | * @param array $params |
| 65 | * (reference ) an assoc array of name/value pairs. |
| 66 | * |
| 67 | * @return object CRM_Core_BAO_Email object on success, null otherwise |
| 68 | * @static |
| 69 | */ |
| 70 | public static function add(&$params) { |
| 71 | $hook = empty($params['id']) ? 'create' : 'edit'; |
| 72 | CRM_Utils_Hook::pre($hook, 'Email', CRM_Utils_Array::value('id', $params), $params); |
| 73 | |
| 74 | $email = new CRM_Core_DAO_Email(); |
| 75 | $email->copyValues($params); |
| 76 | |
| 77 | // lower case email field to optimize queries |
| 78 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; |
| 79 | $email->email = $strtolower($email->email); |
| 80 | |
| 81 | /* |
| 82 | * since we're setting bulkmail for 1 of this contact's emails, first reset all their other emails to is_bulkmail false |
| 83 | * We shouldn't not set the current email to false even though we |
| 84 | * are about to reset it to avoid contaminating the changelog if logging is enabled |
| 85 | * (only 1 email address can have is_bulkmail = true) |
| 86 | */ |
| 87 | if ($email->is_bulkmail != 'null' && $params['contact_id'] && !self::isMultipleBulkMail()) { |
| 88 | $sql = " |
| 89 | UPDATE civicrm_email |
| 90 | SET is_bulkmail = 0 |
| 91 | WHERE contact_id = {$params['contact_id']} |
| 92 | "; |
| 93 | if ($hook == 'edit') { |
| 94 | $sql .= " AND id <> {$params['id']}"; |
| 95 | } |
| 96 | CRM_Core_DAO::executeQuery($sql); |
| 97 | } |
| 98 | |
| 99 | // handle if email is on hold |
| 100 | self::holdEmail($email); |
| 101 | |
| 102 | $email->save(); |
| 103 | |
| 104 | if ($email->is_primary) { |
| 105 | // update the UF user email if that has changed |
| 106 | CRM_Core_BAO_UFMatch::updateUFName($email->contact_id); |
| 107 | } |
| 108 | |
| 109 | CRM_Utils_Hook::post($hook, 'Email', $email->id, $email); |
| 110 | return $email; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Given the list of params in the params array, fetch the object |
| 115 | * and store the values in the values array |
| 116 | * |
| 117 | * @param array $entityBlock |
| 118 | * Input parameters to find object. |
| 119 | * |
| 120 | * @return boolean |
| 121 | * @static |
| 122 | */ |
| 123 | public static function &getValues($entityBlock) { |
| 124 | return CRM_Core_BAO_Block::getValues('email', $entityBlock); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get all the emails for a specified contact_id, with the primary email being first |
| 129 | * |
| 130 | * @param int $id |
| 131 | * The contact id. |
| 132 | * |
| 133 | * @param bool $updateBlankLocInfo |
| 134 | * |
| 135 | * @return array the array of email id's |
| 136 | * @static |
| 137 | */ |
| 138 | public static function allEmails($id, $updateBlankLocInfo = FALSE) { |
| 139 | if (!$id) { |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | $query = " |
| 144 | SELECT email, |
| 145 | civicrm_location_type.name as locationType, |
| 146 | civicrm_email.is_primary as is_primary, |
| 147 | civicrm_email.on_hold as on_hold, |
| 148 | civicrm_email.id as email_id, |
| 149 | civicrm_email.location_type_id as locationTypeId |
| 150 | FROM civicrm_contact |
| 151 | LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = civicrm_contact.id ) |
| 152 | LEFT JOIN civicrm_location_type ON ( civicrm_email.location_type_id = civicrm_location_type.id ) |
| 153 | WHERE civicrm_contact.id = %1 |
| 154 | ORDER BY civicrm_email.is_primary DESC, email_id ASC "; |
| 155 | $params = array( |
| 156 | 1 => array( |
| 157 | $id, |
| 158 | 'Integer', |
| 159 | ), |
| 160 | ); |
| 161 | |
| 162 | $emails = $values = array(); |
| 163 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 164 | $count = 1; |
| 165 | while ($dao->fetch()) { |
| 166 | $values = array( |
| 167 | 'locationType' => $dao->locationType, |
| 168 | 'is_primary' => $dao->is_primary, |
| 169 | 'on_hold' => $dao->on_hold, |
| 170 | 'id' => $dao->email_id, |
| 171 | 'email' => $dao->email, |
| 172 | 'locationTypeId' => $dao->locationTypeId, |
| 173 | ); |
| 174 | |
| 175 | if ($updateBlankLocInfo) { |
| 176 | $emails[$count++] = $values; |
| 177 | } |
| 178 | else { |
| 179 | $emails[$dao->email_id] = $values; |
| 180 | } |
| 181 | } |
| 182 | return $emails; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Get all the emails for a specified location_block id, with the primary email being first |
| 187 | * |
| 188 | * @param array $entityElements |
| 189 | * The array containing entity_id and. |
| 190 | * entity_table name |
| 191 | * |
| 192 | * @return array the array of email id's |
| 193 | * @static |
| 194 | */ |
| 195 | public static function allEntityEmails(&$entityElements) { |
| 196 | if (empty($entityElements)) { |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | $entityId = $entityElements['entity_id']; |
| 201 | $entityTable = $entityElements['entity_table']; |
| 202 | |
| 203 | $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 |
| 204 | FROM civicrm_loc_block loc, civicrm_email e, civicrm_location_type ltype, {$entityTable} ev |
| 205 | WHERE ev.id = %1 |
| 206 | AND loc.id = ev.loc_block_id |
| 207 | AND e.id IN (loc.email_id, loc.email_2_id) |
| 208 | AND ltype.id = e.location_type_id |
| 209 | ORDER BY e.is_primary DESC, email_id ASC "; |
| 210 | |
| 211 | $params = array( |
| 212 | 1 => array( |
| 213 | $entityId, |
| 214 | 'Integer', |
| 215 | ), |
| 216 | ); |
| 217 | |
| 218 | $emails = array(); |
| 219 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 220 | while ($dao->fetch()) { |
| 221 | $emails[$dao->email_id] = array( |
| 222 | 'locationType' => $dao->locationType, |
| 223 | 'is_primary' => $dao->is_primary, |
| 224 | 'on_hold' => $dao->on_hold, |
| 225 | 'id' => $dao->email_id, |
| 226 | 'email' => $dao->email, |
| 227 | 'locationTypeId' => $dao->locationTypeId, |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | return $emails; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Set / reset hold status for an email |
| 236 | * |
| 237 | * @param object $email |
| 238 | * Email object. |
| 239 | * |
| 240 | * @return void |
| 241 | * @static |
| 242 | */ |
| 243 | public static function holdEmail(&$email) { |
| 244 | //check for update mode |
| 245 | if ($email->id) { |
| 246 | $params = array(1 => array($email->id, 'Integer')); |
| 247 | if ($email->on_hold && $email->on_hold != 'null') { |
| 248 | $sql = " |
| 249 | SELECT id |
| 250 | FROM civicrm_email |
| 251 | WHERE id = %1 |
| 252 | AND hold_date IS NULL |
| 253 | "; |
| 254 | if (CRM_Core_DAO::singleValueQuery($sql, $params)) { |
| 255 | $email->hold_date = date('YmdHis'); |
| 256 | $email->reset_date = 'null'; |
| 257 | } |
| 258 | } |
| 259 | elseif ($email->on_hold == 'null') { |
| 260 | $sql = " |
| 261 | SELECT id |
| 262 | FROM civicrm_email |
| 263 | WHERE id = %1 |
| 264 | AND hold_date IS NOT NULL |
| 265 | AND reset_date IS NULL |
| 266 | "; |
| 267 | if (CRM_Core_DAO::singleValueQuery($sql, $params)) { |
| 268 | //set reset date only if it is not set and if hold date is set |
| 269 | $email->on_hold = FALSE; |
| 270 | $email->hold_date = 'null'; |
| 271 | $email->reset_date = date('YmdHis'); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | else { |
| 276 | if (($email->on_hold != 'null') && $email->on_hold) { |
| 277 | $email->hold_date = date('YmdHis'); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Build From Email as the combination of all the email ids of the logged in user and |
| 284 | * the domain email id |
| 285 | * |
| 286 | * @return array an array of email ids |
| 287 | * @static |
| 288 | */ |
| 289 | public static function getFromEmail() { |
| 290 | $session = CRM_Core_Session::singleton(); |
| 291 | $contactID = $session->get('userID'); |
| 292 | $fromEmailValues = array(); |
| 293 | |
| 294 | // add all configured FROM email addresses |
| 295 | $domainFrom = CRM_Core_OptionGroup::values('from_email_address'); |
| 296 | foreach (array_keys($domainFrom) as $k) { |
| 297 | $domainEmail = $domainFrom[$k]; |
| 298 | $fromEmailValues[$domainEmail] = htmlspecialchars($domainEmail); |
| 299 | } |
| 300 | |
| 301 | // add logged in user's active email ids |
| 302 | if ($contactID) { |
| 303 | $contactEmails = self::allEmails($contactID); |
| 304 | $fromDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'display_name'); |
| 305 | |
| 306 | foreach ($contactEmails as $emailId => $emailVal) { |
| 307 | $email = trim($emailVal['email']); |
| 308 | if (!$email || $emailVal['on_hold']) { |
| 309 | continue; |
| 310 | } |
| 311 | $fromEmail = "$fromDisplayName <$email>"; |
| 312 | $fromEmailHtml = htmlspecialchars($fromEmail) . ' ' . $emailVal['locationType']; |
| 313 | |
| 314 | if (!empty($emailVal['is_primary'])) { |
| 315 | $fromEmailHtml .= ' ' . ts('(preferred)'); |
| 316 | } |
| 317 | $fromEmailValues[$fromEmail] = $fromEmailHtml; |
| 318 | } |
| 319 | } |
| 320 | return $fromEmailValues; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * @return object |
| 325 | */ |
| 326 | public static function isMultipleBulkMail() { |
| 327 | return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'civimail_multiple_bulk_emails', NULL, FALSE); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Call common delete function |
| 332 | */ |
| 333 | public static function del($id) { |
| 334 | return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Email', $id); |
| 335 | } |
| 336 | } |