Merge pull request #870 from colemanw/import
[civicrm-core.git] / CRM / Mailing / Event / BAO / Forward.php
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 class CRM_Mailing_Event_BAO_Forward extends CRM_Mailing_Event_DAO_Forward {
36
37 /**
38 * class constructor
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();
62 $job = CRM_Mailing_BAO_Job::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_Contact_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 (CRM_Utils_Array::value('body_html', $comment)) {
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 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK,
185 array('CRM_Core_Error', 'nullHandler')
186 );
187 $result = NULL;
188 if (is_object($mailer)) {
189 $result = $mailer->send($recipient, $headers, $body);
190 CRM_Core_Error::setCallback();
191 }
192
193 $params = array(
194 'event_queue_id' => $queue->id,
195 'job_id' => $job_id,
196 'hash' => $queue->hash,
197 );
198 if (is_a($result, 'PEAR_Error')) {
199 /* Register the bounce event */
200
201 $params = array_merge($params,
202 CRM_Mailing_BAO_BouncePattern::match($result->getMessage())
203 );
204 CRM_Mailing_Event_BAO_Bounce::create($params);
205 }
206 else {
207 $successfulForward = TRUE;
208 /* Register the delivery event */
209
210 CRM_Mailing_Event_BAO_Delivered::create($params);
211 }
212
213 $transaction->commit();
214
215 return $successfulForward;
216 }
217
218 /**
219 * Get row count for the event selector
220 *
221 * @param int $mailing_id ID of the mailing
222 * @param int $job_id Optional ID of a job to filter on
223 * @param boolean $is_distinct Group by queue ID?
224 *
225 * @return int Number of rows in result set
226 * @access public
227 * @static
228 */
229 public static function getTotalCount($mailing_id, $job_id = NULL,
230 $is_distinct = FALSE
231 ) {
232 $dao = new CRM_Core_DAO();
233
234 $forward = self::getTableName();
235 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
236 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
237 $job = CRM_Mailing_BAO_Job::getTableName();
238
239 $query = "
240 SELECT COUNT($forward.id) as forward
241 FROM $forward
242 INNER JOIN $queue
243 ON $forward.event_queue_id = $queue.id
244 INNER JOIN $job
245 ON $queue.job_id = $job.id
246 INNER JOIN $mailing
247 ON $job.mailing_id = $mailing.id
248 AND $job.is_test = 0
249 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
250
251 if (!empty($job_id)) {
252 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
253 }
254
255 if ($is_distinct) {
256 $query .= " GROUP BY $queue.id ";
257 }
258
259 // query was missing
260 $dao->query($query);
261
262 if ($dao->fetch()) {
263 return $dao->forward;
264 }
265
266 return NULL;
267 }
268
269 /**
270 * Get rows for the event browser
271 *
272 * @param int $mailing_id ID of the mailing
273 * @param int $job_id optional ID of the job
274 * @param boolean $is_distinct Group by queue id?
275 * @param int $offset Offset
276 * @param int $rowCount Number of rows
277 * @param array $sort sort array
278 *
279 * @return array Result set
280 * @access public
281 * @static
282 */
283 public static function &getRows($mailing_id, $job_id = NULL,
284 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
285 ) {
286
287 $dao = new CRM_Core_Dao();
288
289 $forward = self::getTableName();
290 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
291 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
292 $job = CRM_Mailing_BAO_Job::getTableName();
293 $contact = CRM_Contact_BAO_Contact::getTableName();
294 $email = CRM_Core_BAO_Email::getTableName();
295
296 $query = "
297 SELECT $contact.display_name as from_name,
298 $contact.id as from_id,
299 $email.email as from_email,
300 dest_contact.id as dest_id,
301 dest_email.email as dest_email,
302 $forward.time_stamp as date
303 FROM $contact
304 INNER JOIN $queue
305 ON $queue.contact_id = $contact.id
306 INNER JOIN $email
307 ON $queue.email_id = $email.id
308 INNER JOIN $forward
309 ON $forward.event_queue_id = $queue.id
310 INNER JOIN $queue as dest_queue
311 ON $forward.dest_queue_id = dest_queue.id
312 INNER JOIN $contact as dest_contact
313 ON dest_queue.contact_id = dest_contact.id
314 INNER JOIN $email as dest_email
315 ON dest_queue.email_id = dest_email.id
316 INNER JOIN $job
317 ON $queue.job_id = $job.id
318 INNER JOIN $mailing
319 ON $job.mailing_id = $mailing.id
320 AND $job.is_test = 0
321 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
322
323 if (!empty($job_id)) {
324 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
325 }
326
327 if ($is_distinct) {
328 $query .= " GROUP BY $queue.id ";
329 }
330
331 $orderBy = "sort_name ASC, {$forward}.time_stamp DESC";
332 if ($sort) {
333 if (is_string($sort)) {
334 $orderBy = $sort;
335 }
336 else {
337 $orderBy = trim($sort->orderBy());
338 }
339 }
340
341 $query .= " ORDER BY {$orderBy} ";
342
343 if ($offset || $rowCount) {
344 //Added "||$rowCount" to avoid displaying all records on first page
345 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
346 }
347
348 $dao->query($query);
349
350 $results = array();
351
352 while ($dao->fetch()) {
353 $from_url = CRM_Utils_System::url('civicrm/contact/view',
354 "reset=1&cid={$dao->from_id}"
355 );
356 $dest_url = CRM_Utils_System::url('civicrm/contact/view',
357 "reset=1&cid={$dao->dest_id}"
358 );
359 $results[] = array(
360 'from_name' => "<a href=\"$from_url\">{$dao->from_name}</a>",
361 'from_email' => $dao->from_email,
362 'dest_email' => "<a href=\"$dest_url\">{$dao->dest_email}</a>",
363 'date' => CRM_Utils_Date::customFormat($dao->date),
364 );
365 }
366 return $results;
367 }
368 }
369