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 | class CRM_Mailing_Event_BAO_Forward extends CRM_Mailing_Event_DAO_Forward { | |
36 | ||
37 | /** | |
100fef9d | 38 | * Class constructor |
6a488035 TO |
39 | */ |
40 | function __construct() { | |
41 | parent::__construct(); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Create a new forward event, create a new contact if necessary | |
46 | */ | |
47 | static function &forward($job_id, $queue_id, $hash, $forward_email, $fromEmail = NULL, $comment = NULL) { | |
48 | $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); | |
49 | ||
50 | $successfulForward = FALSE; | |
51 | $contact_id = NULL; | |
52 | if (!$q) { | |
53 | return $successfulForward; | |
54 | } | |
55 | ||
56 | /* Find the email address/contact, if it exists */ | |
57 | ||
58 | $contact = CRM_Contact_BAO_Contact::getTableName(); | |
59 | $location = CRM_Core_BAO_Location::getTableName(); | |
60 | $email = CRM_Core_BAO_Email::getTableName(); | |
61 | $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
9da8dc8c | 62 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
63 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
64 | $forward = self::getTableName(); | |
65 | ||
66 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
67 | ||
68 | $dao = new CRM_Core_Dao(); | |
69 | $dao->query(" | |
70 | SELECT $contact.id as contact_id, | |
71 | $email.id as email_id, | |
72 | $contact.do_not_email as do_not_email, | |
73 | $queueTable.id as queue_id | |
74 | FROM ($email, $job as temp_job) | |
75 | INNER JOIN $contact | |
76 | ON $email.contact_id = $contact.id | |
77 | LEFT JOIN $queueTable | |
78 | ON $email.id = $queueTable.email_id | |
79 | LEFT JOIN $job | |
80 | ON $queueTable.job_id = $job.id | |
81 | AND temp_job.mailing_id = $job.mailing_id | |
82 | WHERE $queueTable.job_id = $job_id | |
83 | AND $email.email = '" . | |
84 | CRM_Utils_Type::escape($forward_email, 'String') . "'" | |
85 | ); | |
86 | ||
87 | $dao->fetch(); | |
88 | ||
89 | $transaction = new CRM_Core_Transaction(); | |
90 | ||
91 | if (isset($dao->queue_id) || | |
92 | (isset($dao->do_not_email) && $dao->do_not_email == 1) | |
93 | ) { | |
94 | /* We already sent this mailing to $forward_email, or we should | |
95 | * never email this contact. Give up. */ | |
96 | ||
97 | return $successfulForward; | |
98 | } | |
99 | ||
100 | require_once 'api/api.php'; | |
101 | $contactParams = array( | |
102 | 'email' => $forward_email, | |
103 | 'version' => 3, | |
104 | ); | |
105 | $contactValues = civicrm_api('contact', 'get', $contactParams); | |
106 | $count = $contactValues['count']; | |
107 | ||
108 | if ($count == 0) { | |
109 | /* If the contact does not exist, create one. */ | |
110 | ||
111 | $formatted = array( | |
112 | 'contact_type' => 'Individual', | |
113 | 'version' => 3, | |
114 | ); | |
115 | $locationType = CRM_Core_BAO_LocationType::getDefault(); | |
116 | $value = array( | |
117 | 'email' => $forward_email, | |
118 | 'location_type_id' => $locationType->id, | |
119 | ); | |
120 | require_once 'CRM/Utils/DeprecatedUtils.php'; | |
121 | _civicrm_api3_deprecated_add_formatted_param($value, $formatted); | |
a05662ef | 122 | $formatted['onDuplicate'] = CRM_Import_Parser::DUPLICATE_SKIP; |
6a488035 TO |
123 | $formatted['fixAddress'] = TRUE; |
124 | $contact = civicrm_api('contact', 'create', $formatted); | |
125 | if (civicrm_error($contact)) { | |
126 | return $successfulForward; | |
127 | } | |
128 | $contact_id = $contact['id']; | |
129 | } | |
130 | $email = new CRM_Core_DAO_Email(); | |
131 | $email->email = $forward_email; | |
132 | $email->find(TRUE); | |
133 | $email_id = $email->id; | |
134 | if (!$contact_id) { | |
135 | $contact_id = $email->contact_id; | |
136 | } | |
137 | ||
138 | /* Create a new queue event */ | |
139 | ||
140 | $queue_params = array( | |
141 | 'email_id' => $email_id, | |
142 | 'contact_id' => $contact_id, | |
143 | 'job_id' => $job_id, | |
144 | ); | |
145 | ||
146 | $queue = CRM_Mailing_Event_BAO_Queue::create($queue_params); | |
147 | ||
148 | $forward = new CRM_Mailing_Event_BAO_Forward(); | |
149 | $forward->time_stamp = date('YmdHis'); | |
150 | $forward->event_queue_id = $queue_id; | |
151 | $forward->dest_queue_id = $queue->id; | |
152 | $forward->save(); | |
153 | ||
154 | $dao->reset(); | |
155 | $dao->query(" SELECT $job.mailing_id as mailing_id | |
156 | FROM $job | |
157 | WHERE $job.id = " . | |
158 | CRM_Utils_Type::escape($job_id, 'Integer') | |
159 | ); | |
160 | $dao->fetch(); | |
161 | $mailing_obj = new CRM_Mailing_BAO_Mailing(); | |
162 | $mailing_obj->id = $dao->mailing_id; | |
163 | $mailing_obj->find(TRUE); | |
164 | ||
165 | $config = CRM_Core_Config::singleton(); | |
166 | $mailer = $config->getMailer(); | |
167 | ||
168 | $recipient = NULL; | |
169 | $attachments = NULL; | |
170 | $message = $mailing_obj->compose($job_id, $queue->id, $queue->hash, | |
171 | $queue->contact_id, $forward_email, $recipient, FALSE, NULL, $attachments, TRUE, $fromEmail | |
172 | ); | |
173 | //append comment if added while forwarding. | |
174 | if (count($comment)) { | |
175 | $message->_txtbody = CRM_Utils_Array::value('body_text', $comment) . $message->_txtbody; | |
a7488080 | 176 | if (!empty($comment['body_html'])) { |
6a488035 TO |
177 | $message->_htmlbody = $comment['body_html'] . '<br />---------------Original message---------------------<br />' . $message->_htmlbody; |
178 | } | |
179 | } | |
180 | ||
181 | $body = $message->get(); | |
182 | $headers = $message->headers(); | |
183 | ||
6a488035 TO |
184 | $result = NULL; |
185 | if (is_object($mailer)) { | |
6a4257d4 | 186 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 187 | $result = $mailer->send($recipient, $headers, $body); |
6a4257d4 | 188 | unset($errorScope); |
6a488035 TO |
189 | } |
190 | ||
191 | $params = array( | |
192 | 'event_queue_id' => $queue->id, | |
193 | 'job_id' => $job_id, | |
194 | 'hash' => $queue->hash, | |
195 | ); | |
196 | if (is_a($result, 'PEAR_Error')) { | |
197 | /* Register the bounce event */ | |
198 | ||
199 | $params = array_merge($params, | |
200 | CRM_Mailing_BAO_BouncePattern::match($result->getMessage()) | |
201 | ); | |
202 | CRM_Mailing_Event_BAO_Bounce::create($params); | |
203 | } | |
204 | else { | |
205 | $successfulForward = TRUE; | |
206 | /* Register the delivery event */ | |
207 | ||
208 | CRM_Mailing_Event_BAO_Delivered::create($params); | |
209 | } | |
210 | ||
211 | $transaction->commit(); | |
212 | ||
213 | return $successfulForward; | |
214 | } | |
215 | ||
216 | /** | |
217 | * Get row count for the event selector | |
218 | * | |
219 | * @param int $mailing_id ID of the mailing | |
220 | * @param int $job_id Optional ID of a job to filter on | |
221 | * @param boolean $is_distinct Group by queue ID? | |
222 | * | |
223 | * @return int Number of rows in result set | |
224 | * @access public | |
225 | * @static | |
226 | */ | |
227 | public static function getTotalCount($mailing_id, $job_id = NULL, | |
228 | $is_distinct = FALSE | |
229 | ) { | |
230 | $dao = new CRM_Core_DAO(); | |
231 | ||
232 | $forward = self::getTableName(); | |
233 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
234 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 235 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
236 | |
237 | $query = " | |
238 | SELECT COUNT($forward.id) as forward | |
239 | FROM $forward | |
240 | INNER JOIN $queue | |
241 | ON $forward.event_queue_id = $queue.id | |
242 | INNER JOIN $job | |
243 | ON $queue.job_id = $job.id | |
244 | INNER JOIN $mailing | |
245 | ON $job.mailing_id = $mailing.id | |
246 | AND $job.is_test = 0 | |
247 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
248 | ||
249 | if (!empty($job_id)) { | |
250 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
251 | } | |
252 | ||
253 | if ($is_distinct) { | |
254 | $query .= " GROUP BY $queue.id "; | |
255 | } | |
256 | ||
257 | // query was missing | |
258 | $dao->query($query); | |
259 | ||
260 | if ($dao->fetch()) { | |
261 | return $dao->forward; | |
262 | } | |
263 | ||
264 | return NULL; | |
265 | } | |
266 | ||
267 | /** | |
268 | * Get rows for the event browser | |
269 | * | |
270 | * @param int $mailing_id ID of the mailing | |
271 | * @param int $job_id optional ID of the job | |
272 | * @param boolean $is_distinct Group by queue id? | |
273 | * @param int $offset Offset | |
274 | * @param int $rowCount Number of rows | |
275 | * @param array $sort sort array | |
276 | * | |
277 | * @return array Result set | |
278 | * @access public | |
279 | * @static | |
280 | */ | |
281 | public static function &getRows($mailing_id, $job_id = NULL, | |
282 | $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL | |
283 | ) { | |
284 | ||
285 | $dao = new CRM_Core_Dao(); | |
286 | ||
287 | $forward = self::getTableName(); | |
288 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
289 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 290 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
291 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
292 | $email = CRM_Core_BAO_Email::getTableName(); | |
293 | ||
294 | $query = " | |
295 | SELECT $contact.display_name as from_name, | |
296 | $contact.id as from_id, | |
297 | $email.email as from_email, | |
298 | dest_contact.id as dest_id, | |
299 | dest_email.email as dest_email, | |
300 | $forward.time_stamp as date | |
301 | FROM $contact | |
302 | INNER JOIN $queue | |
303 | ON $queue.contact_id = $contact.id | |
304 | INNER JOIN $email | |
305 | ON $queue.email_id = $email.id | |
306 | INNER JOIN $forward | |
307 | ON $forward.event_queue_id = $queue.id | |
308 | INNER JOIN $queue as dest_queue | |
309 | ON $forward.dest_queue_id = dest_queue.id | |
310 | INNER JOIN $contact as dest_contact | |
311 | ON dest_queue.contact_id = dest_contact.id | |
312 | INNER JOIN $email as dest_email | |
313 | ON dest_queue.email_id = dest_email.id | |
314 | INNER JOIN $job | |
315 | ON $queue.job_id = $job.id | |
316 | INNER JOIN $mailing | |
317 | ON $job.mailing_id = $mailing.id | |
318 | AND $job.is_test = 0 | |
319 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
320 | ||
321 | if (!empty($job_id)) { | |
322 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
323 | } | |
324 | ||
325 | if ($is_distinct) { | |
326 | $query .= " GROUP BY $queue.id "; | |
327 | } | |
328 | ||
329 | $orderBy = "sort_name ASC, {$forward}.time_stamp DESC"; | |
330 | if ($sort) { | |
331 | if (is_string($sort)) { | |
21d32567 | 332 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
333 | $orderBy = $sort; |
334 | } | |
335 | else { | |
336 | $orderBy = trim($sort->orderBy()); | |
337 | } | |
338 | } | |
339 | ||
340 | $query .= " ORDER BY {$orderBy} "; | |
341 | ||
342 | if ($offset || $rowCount) { | |
343 | //Added "||$rowCount" to avoid displaying all records on first page | |
344 | $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); | |
345 | } | |
346 | ||
347 | $dao->query($query); | |
348 | ||
349 | $results = array(); | |
350 | ||
351 | while ($dao->fetch()) { | |
352 | $from_url = CRM_Utils_System::url('civicrm/contact/view', | |
353 | "reset=1&cid={$dao->from_id}" | |
354 | ); | |
355 | $dest_url = CRM_Utils_System::url('civicrm/contact/view', | |
356 | "reset=1&cid={$dao->dest_id}" | |
357 | ); | |
358 | $results[] = array( | |
359 | 'from_name' => "<a href=\"$from_url\">{$dao->from_name}</a>", | |
360 | 'from_email' => $dao->from_email, | |
361 | 'dest_email' => "<a href=\"$dest_url\">{$dao->dest_email}</a>", | |
362 | 'date' => CRM_Utils_Date::customFormat($dao->date), | |
363 | ); | |
364 | } | |
365 | return $results; | |
366 | } | |
367 | } | |
368 |