Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | ||
cac01cdc | 41 | /** |
6a488035 TO |
42 | * Create email address - note that the create function calls 'add' but |
43 | * has more business logic | |
44 | * | |
45 | * @param array $params input parameters | |
77b97be7 EM |
46 | * |
47 | * @return object | |
6a488035 TO |
48 | */ |
49 | 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 | /** | |
61 | * takes an associative array and adds email | |
62 | * | |
63 | * @param array $params (reference ) an assoc array of name/value pairs | |
64 | * | |
65 | * @return object CRM_Core_BAO_Email object on success, null otherwise | |
66 | * @access public | |
67 | * @static | |
68 | */ | |
69 | static function add(&$params) { | |
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); | |
75 | ||
76 | // lower case email field to optimize queries | |
77 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
78 | $email->email = $strtolower($email->email); | |
79 | ||
c2714028 | 80 | /* |
81 | * since we're setting bulkmail for 1 of this contact's emails, first reset all their other emails to is_bulkmail false | |
82 | * We shouldn't not set the current email to false even though we | |
83 | * are about to reset it to avoid contaminating the changelog if logging is enabled | |
84 | * (only 1 email address can have is_bulkmail = true) | |
85 | */ | |
6a488035 TO |
86 | if ($email->is_bulkmail != 'null' && $params['contact_id'] && !self::isMultipleBulkMail()) { |
87 | $sql = " | |
88 | UPDATE civicrm_email | |
89 | SET is_bulkmail = 0 | |
90 | WHERE contact_id = {$params['contact_id']} | |
91 | "; | |
c2714028 | 92 | if($hook == 'edit'){ |
93 | $sql .= " AND id <> {$params['id']}"; | |
94 | } | |
6a488035 TO |
95 | CRM_Core_DAO::executeQuery($sql); |
96 | } | |
97 | ||
98 | // handle if email is on hold | |
99 | self::holdEmail($email); | |
100 | ||
101 | $email->save(); | |
102 | ||
cac01cdc | 103 | if ($email->is_primary) { |
104 | // update the UF user email if that has changed | |
105 | CRM_Core_BAO_UFMatch::updateUFName($email->contact_id); | |
106 | } | |
107 | ||
6a488035 TO |
108 | CRM_Utils_Hook::post($hook, 'Email', $email->id, $email); |
109 | return $email; | |
110 | } | |
111 | ||
112 | /** | |
113 | * Given the list of params in the params array, fetch the object | |
114 | * and store the values in the values array | |
115 | * | |
116 | * @param array $entityBlock input parameters to find object | |
117 | * | |
118 | * @return boolean | |
119 | * @access public | |
120 | * @static | |
121 | */ | |
122 | static function &getValues($entityBlock) { | |
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 | * | |
129 | * @param int $id the contact id | |
130 | * | |
dd244018 EM |
131 | * @param bool $updateBlankLocInfo |
132 | * | |
6a488035 TO |
133 | * @return array the array of email id's |
134 | * @access public | |
135 | * @static | |
136 | */ | |
137 | static function allEmails($id, $updateBlankLocInfo = FALSE) { | |
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(); | |
162 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
163 | $count = 1; | |
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 | * | |
187 | * @param array $entityElements the array containing entity_id and | |
188 | * entity_table name | |
189 | * | |
190 | * @return array the array of email id's | |
191 | * @access public | |
192 | * @static | |
193 | */ | |
194 | static function allEntityEmails(&$entityElements) { | |
195 | if (empty($entityElements)) { | |
196 | return NULL; | |
197 | } | |
198 | ||
199 | $entityId = $entityElements['entity_id']; | |
200 | $entityTable = $entityElements['entity_table']; | |
201 | ||
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 | * Function to set / reset hold status for an email | |
236 | * | |
237 | * @param object $email email object | |
238 | * | |
239 | * @return void | |
240 | * @static | |
241 | */ | |
242 | static function holdEmail(&$email) { | |
243 | //check for update mode | |
244 | if ($email->id) { | |
245 | $params = array(1 => array($email->id, 'Integer')); | |
246 | if ($email->on_hold && $email->on_hold != 'null') { | |
247 | $sql = " | |
248 | SELECT id | |
249 | FROM civicrm_email | |
250 | WHERE id = %1 | |
251 | AND hold_date IS NULL | |
252 | "; | |
253 | if (CRM_Core_DAO::singleValueQuery($sql, $params)) { | |
254 | $email->hold_date = date('YmdHis'); | |
255 | $email->reset_date = 'null'; | |
256 | } | |
257 | } | |
258 | elseif ($email->on_hold == 'null') { | |
259 | $sql = " | |
260 | SELECT id | |
261 | FROM civicrm_email | |
262 | WHERE id = %1 | |
263 | AND hold_date IS NOT NULL | |
264 | AND reset_date IS NULL | |
265 | "; | |
266 | if (CRM_Core_DAO::singleValueQuery($sql, $params)) { | |
267 | //set reset date only if it is not set and if hold date is set | |
268 | $email->on_hold = FALSE; | |
269 | $email->hold_date = 'null'; | |
270 | $email->reset_date = date('YmdHis'); | |
271 | } | |
272 | } | |
273 | } | |
274 | else { | |
275 | if (($email->on_hold != 'null') && $email->on_hold) { | |
276 | $email->hold_date = date('YmdHis'); | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
281 | /** | |
282 | * Build From Email as the combination of all the email ids of the logged in user and | |
283 | * the domain email id | |
284 | * | |
285 | * @return array an array of email ids | |
286 | * @access public | |
287 | * @static | |
288 | */ | |
289 | static function getFromEmail() { | |
290 | $session = CRM_Core_Session::singleton(); | |
291 | $contactID = $session->get('userID'); | |
292 | $fromEmailValues = array(); | |
293 | ||
c52a8fcc DG |
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 | } | |
6a488035 TO |
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 | ||
a7488080 | 314 | if (!empty($emailVal['is_primary'])) { |
6a488035 TO |
315 | $fromEmailHtml .= ' ' . ts('(preferred)'); |
316 | } | |
317 | $fromEmailValues[$fromEmail] = $fromEmailHtml; | |
318 | } | |
319 | } | |
320 | return $fromEmailValues; | |
321 | } | |
322 | ||
b5c2afd0 EM |
323 | /** |
324 | * @return object | |
325 | */ | |
6a488035 TO |
326 | static function isMultipleBulkMail() { |
327 | return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'civimail_multiple_bulk_emails', NULL, FALSE); | |
328 | } | |
12445e1c CW |
329 | |
330 | /** | |
331 | * Call common delete function | |
332 | */ | |
333 | static function del($id) { | |
a65e2e55 | 334 | return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Email', $id); |
12445e1c | 335 | } |
6a488035 TO |
336 | } |
337 |