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