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