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