887bc0cc9c6a594c555005fdacb691bfd6164a2e
[civicrm-core.git] / CRM / Mailing / Event / BAO / Forward.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Mailing_Event_BAO_Forward extends CRM_Mailing_Event_DAO_Forward {
36
37 /**
38 * Class constructor
39 */
40 public function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Create a new forward event, create a new contact if necessary
46 */
47 public 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();
62 $job = CRM_Mailing_BAO_MailingJob::getTableName();
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);
122 $formatted['onDuplicate'] = CRM_Import_Parser::DUPLICATE_SKIP;
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;
176 if (!empty($comment['body_html'])) {
177 $message->_htmlbody = $comment['body_html'] . '<br />---------------Original message---------------------<br />' . $message->_htmlbody;
178 }
179 }
180
181 $body = $message->get();
182 $headers = $message->headers();
183
184 $result = NULL;
185 if (is_object($mailer)) {
186 $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
187 $result = $mailer->send($recipient, $headers, $body);
188 unset($errorScope);
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 * @static
225 */
226 public static function getTotalCount($mailing_id, $job_id = NULL,
227 $is_distinct = FALSE
228 ) {
229 $dao = new CRM_Core_DAO();
230
231 $forward = self::getTableName();
232 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
233 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
234 $job = CRM_Mailing_BAO_MailingJob::getTableName();
235
236 $query = "
237 SELECT COUNT($forward.id) as forward
238 FROM $forward
239 INNER JOIN $queue
240 ON $forward.event_queue_id = $queue.id
241 INNER JOIN $job
242 ON $queue.job_id = $job.id
243 INNER JOIN $mailing
244 ON $job.mailing_id = $mailing.id
245 AND $job.is_test = 0
246 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
247
248 if (!empty($job_id)) {
249 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
250 }
251
252 if ($is_distinct) {
253 $query .= " GROUP BY $queue.id ";
254 }
255
256 // query was missing
257 $dao->query($query);
258
259 if ($dao->fetch()) {
260 return $dao->forward;
261 }
262
263 return NULL;
264 }
265
266 /**
267 * Get rows for the event browser
268 *
269 * @param int $mailing_id ID of the mailing
270 * @param int $job_id optional ID of the job
271 * @param boolean $is_distinct Group by queue id?
272 * @param int $offset Offset
273 * @param int $rowCount Number of rows
274 * @param array $sort sort array
275 *
276 * @return array Result set
277 * @static
278 */
279 public static function &getRows($mailing_id, $job_id = NULL,
280 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
281 ) {
282
283 $dao = new CRM_Core_Dao();
284
285 $forward = self::getTableName();
286 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
287 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
288 $job = CRM_Mailing_BAO_MailingJob::getTableName();
289 $contact = CRM_Contact_BAO_Contact::getTableName();
290 $email = CRM_Core_BAO_Email::getTableName();
291
292 $query = "
293 SELECT $contact.display_name as from_name,
294 $contact.id as from_id,
295 $email.email as from_email,
296 dest_contact.id as dest_id,
297 dest_email.email as dest_email,
298 $forward.time_stamp as date
299 FROM $contact
300 INNER JOIN $queue
301 ON $queue.contact_id = $contact.id
302 INNER JOIN $email
303 ON $queue.email_id = $email.id
304 INNER JOIN $forward
305 ON $forward.event_queue_id = $queue.id
306 INNER JOIN $queue as dest_queue
307 ON $forward.dest_queue_id = dest_queue.id
308 INNER JOIN $contact as dest_contact
309 ON dest_queue.contact_id = dest_contact.id
310 INNER JOIN $email as dest_email
311 ON dest_queue.email_id = dest_email.id
312 INNER JOIN $job
313 ON $queue.job_id = $job.id
314 INNER JOIN $mailing
315 ON $job.mailing_id = $mailing.id
316 AND $job.is_test = 0
317 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
318
319 if (!empty($job_id)) {
320 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
321 }
322
323 if ($is_distinct) {
324 $query .= " GROUP BY $queue.id ";
325 }
326
327 $orderBy = "sort_name ASC, {$forward}.time_stamp DESC";
328 if ($sort) {
329 if (is_string($sort)) {
330 $sort = CRM_Utils_Type::escape($sort, 'String');
331 $orderBy = $sort;
332 }
333 else {
334 $orderBy = trim($sort->orderBy());
335 }
336 }
337
338 $query .= " ORDER BY {$orderBy} ";
339
340 if ($offset || $rowCount) {
341 //Added "||$rowCount" to avoid displaying all records on first page
342 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
343 }
344
345 $dao->query($query);
346
347 $results = array();
348
349 while ($dao->fetch()) {
350 $from_url = CRM_Utils_System::url('civicrm/contact/view',
351 "reset=1&cid={$dao->from_id}"
352 );
353 $dest_url = CRM_Utils_System::url('civicrm/contact/view',
354 "reset=1&cid={$dao->dest_id}"
355 );
356 $results[] = array(
357 'from_name' => "<a href=\"$from_url\">{$dao->from_name}</a>",
358 'from_email' => $dao->from_email,
359 'dest_email' => "<a href=\"$dest_url\">{$dao->dest_email}</a>",
360 'date' => CRM_Utils_Date::customFormat($dao->date),
361 );
362 }
363 return $results;
364 }
365 }