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 | ||
36 | require_once 'Mail.php'; | |
9da8dc8c | 37 | class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob { |
6a488035 TO |
38 | CONST MAX_CONTACTS_TO_PROCESS = 1000; |
39 | ||
40 | /** | |
41 | * class constructor | |
42 | */ | |
43 | function __construct() { | |
44 | parent::__construct(); | |
45 | } | |
46 | ||
af4c09bc EM |
47 | /** |
48 | * @param $params | |
49 | * | |
50 | * @return CRM_Mailing_BAO_MailingJob | |
51 | */ | |
84dd5db6 | 52 | static public function create($params) { |
9da8dc8c | 53 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
54 | $job->mailing_id = $params['mailing_id']; |
55 | $job->status = $params['status']; | |
56 | $job->scheduled_date = $params['scheduled_date']; | |
57 | $job->is_test = $params['is_test']; | |
58 | $job->save(); | |
59 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
84dd5db6 | 60 | $mailing->getRecipients($job->id, $params['mailing_id'], NULL, NULL, TRUE, FALSE); |
6a488035 TO |
61 | return $job; |
62 | } | |
af4c09bc | 63 | |
6a488035 TO |
64 | /** |
65 | * Initiate all pending/ready jobs | |
66 | * | |
af4c09bc EM |
67 | * @param null $testParams |
68 | * @param null $mode | |
69 | * | |
6a488035 TO |
70 | * @return void |
71 | * @access public | |
72 | * @static | |
73 | */ | |
74 | public static function runJobs($testParams = NULL, $mode = NULL) { | |
9da8dc8c | 75 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
76 | |
77 | $config = CRM_Core_Config::singleton(); | |
9da8dc8c | 78 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); |
6a488035 TO |
79 | $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); |
80 | ||
81 | if (!empty($testParams)) { | |
82 | $query = " | |
83 | SELECT * | |
84 | FROM $jobTable | |
85 | WHERE id = {$testParams['job_id']}"; | |
86 | $job->query($query); | |
87 | } | |
88 | else { | |
89 | $currentTime = date('YmdHis'); | |
90 | $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m'); | |
91 | $domainID = CRM_Core_Config::domainID(); | |
92 | ||
93 | $modeClause = 'AND m.sms_provider_id IS NULL'; | |
94 | if ($mode == 'sms') { | |
95 | $modeClause = 'AND m.sms_provider_id IS NOT NULL'; | |
96 | } | |
97 | ||
98 | // Select the first child job that is scheduled | |
99 | // CRM-6835 | |
100 | $query = " | |
101 | SELECT j.* | |
102 | FROM $jobTable j, | |
103 | $mailingTable m | |
104 | WHERE m.id = j.mailing_id AND m.domain_id = {$domainID} | |
105 | {$modeClause} | |
106 | AND j.is_test = 0 | |
107 | AND ( ( j.start_date IS null | |
108 | AND j.scheduled_date <= $currentTime | |
109 | AND j.status = 'Scheduled' ) | |
110 | OR ( j.status = 'Running' | |
111 | AND j.end_date IS null ) ) | |
112 | AND (j.job_type = 'child') | |
113 | AND {$mailingACL} | |
114 | ORDER BY j.mailing_id, | |
115 | j.id | |
116 | "; | |
117 | ||
118 | $job->query($query); | |
119 | } | |
120 | ||
121 | ||
122 | while ($job->fetch()) { | |
123 | // still use job level lock for each child job | |
124 | $lockName = "civimail.job.{$job->id}"; | |
125 | ||
126 | $lock = new CRM_Core_Lock($lockName); | |
127 | if (!$lock->isAcquired()) { | |
128 | continue; | |
129 | } | |
130 | ||
131 | // for test jobs we do not change anything, since its on a short-circuit path | |
132 | if (empty($testParams)) { | |
133 | // we've got the lock, but while we were waiting and processing | |
134 | // other emails, this job might have changed under us | |
135 | // lets get the job status again and check | |
1a5727bd | 136 | $job->status = CRM_Core_DAO::getFieldValue( |
cbe30f5a | 137 | 'CRM_Mailing_DAO_MailingJob', |
6a488035 | 138 | $job->id, |
6606b2e1 DL |
139 | 'status', |
140 | 'id', | |
141 | TRUE | |
6a488035 TO |
142 | ); |
143 | ||
1a5727bd DL |
144 | if ( |
145 | $job->status != 'Running' && | |
6a488035 TO |
146 | $job->status != 'Scheduled' |
147 | ) { | |
148 | // this includes Cancelled and other statuses, CRM-4246 | |
149 | $lock->release(); | |
150 | continue; | |
151 | } | |
152 | } | |
153 | ||
154 | /* Queue up recipients for the child job being launched */ | |
155 | ||
156 | if ($job->status != 'Running') { | |
157 | $transaction = new CRM_Core_Transaction(); | |
158 | ||
159 | // have to queue it up based on the offset and limits | |
160 | // get the parent ID, and limit and offset | |
161 | $job->queue($testParams); | |
162 | ||
163 | // Mark up the starting time | |
9da8dc8c | 164 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
6a488035 TO |
165 | $saveJob->id = $job->id; |
166 | $saveJob->start_date = date('YmdHis'); | |
167 | $saveJob->status = 'Running'; | |
168 | $saveJob->save(); | |
169 | ||
170 | $transaction->commit(); | |
171 | } | |
172 | ||
173 | // Get the mailer | |
174 | // make it a persistent connection, CRM-9349 | |
175 | if ($mode === NULL) { | |
176 | $mailer = $config->getMailer(TRUE); | |
177 | } | |
178 | elseif ($mode == 'sms') { | |
179 | $mailer = CRM_SMS_Provider::singleton(array('mailing_id' => $job->mailing_id)); | |
180 | } | |
181 | ||
182 | // Compose and deliver each child job | |
183 | $isComplete = $job->deliver($mailer, $testParams); | |
184 | ||
185 | CRM_Utils_Hook::post('create', 'CRM_Mailing_DAO_Spool', $job->id, $isComplete); | |
186 | ||
187 | // Mark the child complete | |
188 | if ($isComplete) { | |
189 | /* Finish the job */ | |
190 | ||
191 | $transaction = new CRM_Core_Transaction(); | |
192 | ||
9da8dc8c | 193 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
6a488035 TO |
194 | $saveJob->id = $job->id; |
195 | $saveJob->end_date = date('YmdHis'); | |
196 | $saveJob->status = 'Complete'; | |
197 | $saveJob->save(); | |
198 | ||
199 | $transaction->commit(); | |
200 | ||
201 | // don't mark the mailing as complete | |
202 | } | |
203 | ||
204 | // Release the child joblock | |
205 | $lock->release(); | |
206 | ||
207 | if ($testParams) { | |
208 | return $isComplete; | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
213 | // post process to determine if the parent job | |
214 | // as well as the mailing is complete after the run | |
215 | public static function runJobs_post($mode = NULL) { | |
216 | ||
9da8dc8c | 217 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
218 | |
219 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
220 | ||
221 | $config = CRM_Core_Config::singleton(); | |
9da8dc8c | 222 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); |
6a488035 TO |
223 | $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); |
224 | ||
225 | $currentTime = date('YmdHis'); | |
226 | $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m'); | |
227 | $domainID = CRM_Core_Config::domainID(); | |
228 | ||
229 | $query = " | |
230 | SELECT j.* | |
231 | FROM $jobTable j, | |
232 | $mailingTable m | |
233 | WHERE m.id = j.mailing_id AND m.domain_id = {$domainID} | |
234 | AND j.is_test = 0 | |
235 | AND j.scheduled_date <= $currentTime | |
236 | AND j.status = 'Running' | |
237 | AND j.end_date IS null | |
238 | AND (j.job_type != 'child' OR j.job_type is NULL) | |
239 | ORDER BY j.scheduled_date, | |
240 | j.start_date"; | |
241 | ||
242 | $job->query($query); | |
243 | ||
244 | // For each parent job that is running, let's look at their child jobs | |
245 | while ($job->fetch()) { | |
246 | ||
9da8dc8c | 247 | $child_job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
248 | |
249 | $child_job_sql = " | |
250 | SELECT count(j.id) | |
251 | FROM civicrm_mailing_job j, civicrm_mailing m | |
252 | WHERE m.id = j.mailing_id | |
253 | AND j.job_type = 'child' | |
254 | AND j.parent_id = %1 | |
255 | AND j.status <> 'Complete'"; | |
256 | $params = array(1 => array($job->id, 'Integer')); | |
257 | ||
258 | $anyChildLeft = CRM_Core_DAO::singleValueQuery($child_job_sql, $params); | |
259 | ||
260 | // all of the child jobs are complete, update | |
261 | // the parent job as well as the mailing status | |
262 | if (!$anyChildLeft) { | |
263 | ||
264 | $transaction = new CRM_Core_Transaction(); | |
265 | ||
9da8dc8c | 266 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
6a488035 TO |
267 | $saveJob->id = $job->id; |
268 | $saveJob->end_date = date('YmdHis'); | |
269 | $saveJob->status = 'Complete'; | |
270 | $saveJob->save(); | |
271 | ||
272 | $mailing->reset(); | |
273 | $mailing->id = $job->mailing_id; | |
274 | $mailing->is_completed = TRUE; | |
275 | $mailing->save(); | |
276 | $transaction->commit(); | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
281 | ||
282 | // before we run jobs, we need to split the jobs | |
283 | public static function runJobs_pre($offset = 200, $mode = NULL) { | |
9da8dc8c | 284 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
285 | |
286 | $config = CRM_Core_Config::singleton(); | |
9da8dc8c | 287 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); |
6a488035 TO |
288 | $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); |
289 | ||
290 | $currentTime = date('YmdHis'); | |
291 | $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m'); | |
292 | ||
293 | ||
9da8dc8c | 294 | $workflowClause = CRM_Mailing_BAO_MailingJob::workflowClause(); |
6a488035 TO |
295 | |
296 | $domainID = CRM_Core_Config::domainID(); | |
297 | ||
298 | $modeClause = 'AND m.sms_provider_id IS NULL'; | |
299 | if ($mode == 'sms') { | |
300 | $modeClause = 'AND m.sms_provider_id IS NOT NULL'; | |
301 | } | |
302 | ||
303 | // Select all the mailing jobs that are created from | |
304 | // when the mailing is submitted or scheduled. | |
305 | $query = " | |
306 | SELECT j.* | |
307 | FROM $jobTable j, | |
308 | $mailingTable m | |
309 | WHERE m.id = j.mailing_id AND m.domain_id = {$domainID} | |
310 | $workflowClause | |
311 | $modeClause | |
312 | AND j.is_test = 0 | |
313 | AND ( ( j.start_date IS null | |
314 | AND j.scheduled_date <= $currentTime | |
315 | AND j.status = 'Scheduled' | |
316 | AND j.end_date IS null ) ) | |
317 | AND ((j.job_type is NULL) OR (j.job_type <> 'child')) | |
318 | ORDER BY j.scheduled_date, | |
319 | j.start_date"; | |
320 | ||
321 | ||
322 | $job->query($query); | |
323 | ||
324 | ||
325 | // For each of the "Parent Jobs" we find, we split them into | |
326 | // X Number of child jobs | |
327 | while ($job->fetch()) { | |
328 | // still use job level lock for each child job | |
329 | $lockName = "civimail.job.{$job->id}"; | |
330 | ||
331 | $lock = new CRM_Core_Lock($lockName); | |
332 | if (!$lock->isAcquired()) { | |
333 | continue; | |
334 | } | |
335 | ||
336 | // Re-fetch the job status in case things | |
337 | // changed between the first query and now | |
338 | // to avoid race conditions | |
6606b2e1 | 339 | $job->status = CRM_Core_DAO::getFieldValue( |
cbe30f5a | 340 | 'CRM_Mailing_DAO_MailingJob', |
6a488035 | 341 | $job->id, |
6606b2e1 DL |
342 | 'status', |
343 | 'id', | |
344 | TRUE | |
6a488035 TO |
345 | ); |
346 | if ($job->status != 'Scheduled') { | |
347 | $lock->release(); | |
348 | continue; | |
349 | } | |
350 | ||
351 | $job->split_job($offset); | |
352 | ||
353 | // update the status of the parent job | |
354 | $transaction = new CRM_Core_Transaction(); | |
355 | ||
9da8dc8c | 356 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
6a488035 TO |
357 | $saveJob->id = $job->id; |
358 | $saveJob->start_date = date('YmdHis'); | |
359 | $saveJob->status = 'Running'; | |
360 | $saveJob->save(); | |
361 | ||
362 | $transaction->commit(); | |
363 | ||
364 | // Release the job lock | |
365 | $lock->release(); | |
366 | } | |
367 | } | |
368 | ||
369 | // Split the parent job into n number of child job based on an offset | |
370 | // If null or 0 , we create only one child job | |
371 | public function split_job($offset = 200) { | |
372 | $recipient_count = CRM_Mailing_BAO_Recipients::mailingSize($this->mailing_id); | |
373 | ||
9da8dc8c | 374 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); |
6a488035 TO |
375 | |
376 | ||
377 | $dao = new CRM_Core_DAO(); | |
378 | ||
379 | $sql = " | |
380 | INSERT INTO civicrm_mailing_job | |
381 | (`mailing_id`, `scheduled_date`, `status`, `job_type`, `parent_id`, `job_offset`, `job_limit`) | |
382 | VALUES (%1, %2, %3, %4, %5, %6, %7) | |
383 | "; | |
384 | $params = array(1 => array($this->mailing_id, 'Integer'), | |
385 | 2 => array($this->scheduled_date, 'String'), | |
386 | 3 => array('Scheduled', 'String'), | |
387 | 4 => array('child', 'String'), | |
388 | 5 => array($this->id, 'Integer'), | |
389 | 6 => array(0, 'Integer'), | |
390 | 7 => array($recipient_count, 'Integer'), | |
391 | ); | |
392 | ||
393 | // create one child job if the mailing size is less than the offset | |
cbe30f5a | 394 | // probably use a CRM_Mailing_DAO_MailingJob( ); |
6a488035 TO |
395 | if (empty($offset) || |
396 | $recipient_count <= $offset | |
397 | ) { | |
398 | CRM_Core_DAO::executeQuery($sql, $params); | |
399 | } | |
400 | else { | |
401 | // Creating 'child jobs' | |
402 | for ($i = 0; $i < $recipient_count; $i = $i + $offset) { | |
403 | $params[6][0] = $i; | |
404 | $params[7][0] = $offset; | |
405 | CRM_Core_DAO::executeQuery($sql, $params); | |
406 | } | |
407 | } | |
408 | } | |
409 | ||
410 | public function queue($testParams = NULL) { | |
411 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
412 | $mailing->id = $this->mailing_id; | |
413 | if (!empty($testParams)) { | |
414 | $mailing->getTestRecipients($testParams); | |
415 | } | |
416 | else { | |
417 | // We are still getting all the recipients from the parent job | |
418 | // so we don't mess with the include/exclude logic. | |
419 | $recipients = CRM_Mailing_BAO_Recipients::mailingQuery($this->mailing_id, $this->job_offset, $this->job_limit); | |
420 | ||
421 | // FIXME: this is not very smart, we should move this to one DB call | |
422 | // INSERT INTO ... SELECT FROM .. | |
423 | // the thing we need to figure out is how to generate the hash automatically | |
424 | $now = time(); | |
425 | $params = array(); | |
426 | $count = 0; | |
427 | while ($recipients->fetch()) { | |
428 | if ($recipients->phone_id) { | |
429 | $recipients->email_id = "null"; | |
430 | } | |
431 | else { | |
432 | $recipients->phone_id = "null"; | |
433 | } | |
434 | ||
435 | $params[] = array( | |
436 | $this->id, | |
437 | $recipients->email_id, | |
438 | $recipients->contact_id, | |
439 | $recipients->phone_id, | |
440 | ); | |
441 | $count++; | |
442 | if ($count % CRM_Core_DAO::BULK_MAIL_INSERT_COUNT == 0) { | |
443 | CRM_Mailing_Event_BAO_Queue::bulkCreate($params, $now); | |
444 | $count = 0; | |
445 | $params = array(); | |
446 | } | |
447 | } | |
448 | ||
449 | if (!empty($params)) { | |
450 | CRM_Mailing_Event_BAO_Queue::bulkCreate($params, $now); | |
451 | } | |
452 | } | |
453 | } | |
454 | ||
455 | /** | |
456 | * Send the mailing | |
457 | * | |
2a6da8d7 EM |
458 | * @param object $mailer A Mail object to send the messages |
459 | * | |
460 | * @param null $testParams | |
6a488035 TO |
461 | * |
462 | * @return void | |
463 | * @access public | |
464 | */ | |
465 | public function deliver(&$mailer, $testParams = NULL) { | |
466 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
467 | $mailing->id = $this->mailing_id; | |
468 | $mailing->find(TRUE); | |
469 | $mailing->free(); | |
470 | ||
471 | $eq = new CRM_Mailing_Event_BAO_Queue(); | |
472 | $eqTable = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
473 | $emailTable = CRM_Core_BAO_Email::getTableName(); | |
474 | $phoneTable = CRM_Core_DAO_Phone::getTableName(); | |
475 | $contactTable = CRM_Contact_BAO_Contact::getTableName(); | |
476 | $edTable = CRM_Mailing_Event_BAO_Delivered::getTableName(); | |
477 | $ebTable = CRM_Mailing_Event_BAO_Bounce::getTableName(); | |
478 | ||
479 | $query = " SELECT $eqTable.id, | |
480 | $emailTable.email as email, | |
481 | $eqTable.contact_id, | |
482 | $eqTable.hash, | |
483 | NULL as phone | |
484 | FROM $eqTable | |
485 | INNER JOIN $emailTable | |
486 | ON $eqTable.email_id = $emailTable.id | |
487 | INNER JOIN $contactTable | |
488 | ON $contactTable.id = $emailTable.contact_id | |
489 | LEFT JOIN $edTable | |
490 | ON $eqTable.id = $edTable.event_queue_id | |
491 | LEFT JOIN $ebTable | |
492 | ON $eqTable.id = $ebTable.event_queue_id | |
493 | WHERE $eqTable.job_id = " . $this->id . " | |
494 | AND $edTable.id IS null | |
495 | AND $ebTable.id IS null | |
496 | AND $contactTable.is_opt_out = 0"; | |
497 | ||
498 | if ($mailing->sms_provider_id) { | |
499 | $query = " | |
500 | SELECT $eqTable.id, | |
501 | $phoneTable.phone as phone, | |
502 | $eqTable.contact_id, | |
503 | $eqTable.hash, | |
504 | NULL as email | |
505 | FROM $eqTable | |
506 | INNER JOIN $phoneTable | |
507 | ON $eqTable.phone_id = $phoneTable.id | |
508 | INNER JOIN $contactTable | |
509 | ON $contactTable.id = $phoneTable.contact_id | |
510 | LEFT JOIN $edTable | |
511 | ON $eqTable.id = $edTable.event_queue_id | |
512 | LEFT JOIN $ebTable | |
513 | ON $eqTable.id = $ebTable.event_queue_id | |
514 | WHERE $eqTable.job_id = " . $this->id . " | |
515 | AND $edTable.id IS null | |
516 | AND $ebTable.id IS null | |
517 | AND $contactTable.is_opt_out = 0"; | |
518 | } | |
519 | $eq->query($query); | |
520 | ||
521 | static $config = NULL; | |
522 | static $mailsProcessed = 0; | |
523 | ||
524 | if ($config == NULL) { | |
525 | $config = CRM_Core_Config::singleton(); | |
526 | } | |
527 | ||
528 | $job_date = CRM_Utils_Date::isoToMysql($this->scheduled_date); | |
529 | $fields = array(); | |
530 | ||
531 | if (!empty($testParams)) { | |
a5db8e6f | 532 | $mailing->subject = ts('[CiviMail Draft]') . ' ' . $mailing->subject; |
6a488035 TO |
533 | } |
534 | ||
535 | CRM_Mailing_BAO_Mailing::tokenReplace($mailing); | |
536 | ||
537 | // get and format attachments | |
538 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id); | |
539 | ||
540 | if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { | |
541 | CRM_Core_Smarty::registerStringResource(); | |
542 | } | |
543 | ||
df7f4ae1 DL |
544 | // CRM-12376 |
545 | // This handles the edge case scenario where all the mails | |
546 | // have been delivered in prior jobs | |
547 | $isDelivered = TRUE; | |
6a488035 TO |
548 | |
549 | // make sure that there's no more than $config->mailerBatchLimit mails processed in a run | |
550 | while ($eq->fetch()) { | |
551 | // if ( ( $mailsProcessed % 100 ) == 0 ) { | |
552 | // CRM_Utils_System::xMemory( "$mailsProcessed: " ); | |
553 | // } | |
554 | ||
df7f4ae1 DL |
555 | if ( |
556 | $config->mailerBatchLimit > 0 && | |
6a488035 TO |
557 | $mailsProcessed >= $config->mailerBatchLimit |
558 | ) { | |
559 | if (!empty($fields)) { | |
560 | $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); | |
561 | } | |
562 | $eq->free(); | |
563 | return FALSE; | |
564 | } | |
565 | $mailsProcessed++; | |
566 | ||
567 | $fields[] = array( | |
568 | 'id' => $eq->id, | |
569 | 'hash' => $eq->hash, | |
570 | 'contact_id' => $eq->contact_id, | |
571 | 'email' => $eq->email, | |
572 | 'phone' => $eq->phone, | |
573 | ); | |
574 | if (count($fields) == self::MAX_CONTACTS_TO_PROCESS) { | |
575 | $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); | |
576 | if (!$isDelivered) { | |
577 | $eq->free(); | |
578 | return $isDelivered; | |
579 | } | |
580 | $fields = array(); | |
581 | } | |
582 | } | |
583 | ||
584 | $eq->free(); | |
585 | ||
586 | if (!empty($fields)) { | |
587 | $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); | |
588 | } | |
589 | return $isDelivered; | |
590 | } | |
591 | ||
592 | public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) { | |
593 | static $smtpConnectionErrors = 0; | |
594 | ||
1a5727bd | 595 | if (!is_object($mailer) || empty($fields)) { |
6a488035 TO |
596 | CRM_Core_Error::fatal(); |
597 | } | |
598 | ||
599 | // get the return properties | |
600 | $returnProperties = $mailing->getReturnProperties(); | |
601 | $params = $targetParams = $deliveredParams = array(); | |
602 | $count = 0; | |
603 | ||
604 | foreach ($fields as $key => $field) { | |
605 | $params[] = $field['contact_id']; | |
606 | } | |
607 | ||
608 | $details = CRM_Utils_Token::getTokenDetails( | |
609 | $params, | |
610 | $returnProperties, | |
611 | TRUE, TRUE, NULL, | |
612 | $mailing->getFlattenedTokens(), | |
613 | get_class($this), | |
614 | $this->id | |
615 | ); | |
616 | ||
617 | $config = CRM_Core_Config::singleton(); | |
618 | foreach ($fields as $key => $field) { | |
619 | $contactID = $field['contact_id']; | |
620 | if (!array_key_exists($contactID, $details[0])) { | |
621 | $details[0][$contactID] = array(); | |
622 | } | |
6a488035 | 623 | |
1a5727bd | 624 | /* Compose the mailing */ |
6a488035 TO |
625 | $recipient = $replyToEmail = NULL; |
626 | $replyValue = strcmp($mailing->replyto_email, $mailing->from_email); | |
627 | if ($replyValue) { | |
628 | $replyToEmail = $mailing->replyto_email; | |
629 | } | |
630 | ||
1a5727bd DL |
631 | $message = &$mailing->compose( |
632 | $this->id, $field['id'], $field['hash'], | |
6a488035 TO |
633 | $field['contact_id'], $field['email'], |
634 | $recipient, FALSE, $details[0][$contactID], $attachments, | |
635 | FALSE, NULL, $replyToEmail | |
636 | ); | |
637 | if (empty($message)) { | |
638 | // lets keep the message in the queue | |
639 | // most likely a permissions related issue with smarty templates | |
640 | // or a bad contact id? CRM-9833 | |
641 | continue; | |
642 | } | |
643 | ||
644 | /* Send the mailing */ | |
645 | ||
646 | $body = &$message->get(); | |
647 | $headers = &$message->headers(); | |
648 | ||
649 | if ($mailing->sms_provider_id) { | |
650 | $provider = CRM_SMS_Provider::singleton(array('mailing_id' => $mailing->id)); | |
651 | $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]); | |
652 | $headers = $provider->getRecipientDetails($field, $details[0][$contactID]); | |
653 | } | |
654 | ||
655 | // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743 | |
656 | $recipient = $headers['To']; | |
657 | $result = NULL; | |
658 | ||
659 | // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744 | |
660 | if ($job_date) { | |
6a4257d4 | 661 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 TO |
662 | } |
663 | ||
664 | $result = $mailer->send($recipient, $headers, $body, $this->id); | |
665 | ||
666 | if ($job_date) { | |
6a4257d4 | 667 | unset($errorScope); |
6a488035 TO |
668 | } |
669 | ||
c5a6413b DS |
670 | // FIXME: for now we skipping bounce handling for sms |
671 | if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id) { | |
6a488035 TO |
672 | // CRM-9191 |
673 | $message = $result->getMessage(); | |
674 | if (strpos($message, | |
675 | 'Failed to write to socket' | |
676 | ) !== FALSE) { | |
677 | // lets log this message and code | |
678 | $code = $result->getCode(); | |
679 | CRM_Core_Error::debug_log_message("SMTP Socket Error. Message: $message, Code: $code"); | |
680 | ||
681 | // these are socket write errors which most likely means smtp connection errors | |
682 | // lets skip them | |
683 | $smtpConnectionErrors++; | |
684 | if ($smtpConnectionErrors <= 5) { | |
685 | continue; | |
686 | } | |
687 | ||
6a488035 TO |
688 | // seems like we have too many of them in a row, we should |
689 | // write stuff to disk and abort the cron job | |
2ede60ec DL |
690 | $this->writeToDB( |
691 | $deliveredParams, | |
6a488035 TO |
692 | $targetParams, |
693 | $mailing, | |
694 | $job_date | |
695 | ); | |
696 | ||
697 | CRM_Core_Error::debug_log_message("Too many SMTP Socket Errors. Exiting"); | |
698 | CRM_Utils_System::civiExit(); | |
699 | } | |
700 | ||
701 | /* Register the bounce event */ | |
702 | ||
703 | $params = array( | |
704 | 'event_queue_id' => $field['id'], | |
705 | 'job_id' => $this->id, | |
706 | 'hash' => $field['hash'], | |
707 | ); | |
708 | $params = array_merge($params, | |
709 | CRM_Mailing_BAO_BouncePattern::match($result->getMessage()) | |
710 | ); | |
711 | CRM_Mailing_Event_BAO_Bounce::create($params); | |
712 | } | |
713 | else { | |
714 | /* Register the delivery event */ | |
6a488035 TO |
715 | $deliveredParams[] = $field['id']; |
716 | $targetParams[] = $field['contact_id']; | |
717 | ||
718 | $count++; | |
719 | if ($count % CRM_Core_DAO::BULK_MAIL_INSERT_COUNT == 0) { | |
2ede60ec DL |
720 | $this->writeToDB( |
721 | $deliveredParams, | |
6a488035 TO |
722 | $targetParams, |
723 | $mailing, | |
724 | $job_date | |
725 | ); | |
726 | $count = 0; | |
727 | ||
728 | // hack to stop mailing job at run time, CRM-4246. | |
729 | // to avoid making too many DB calls for this rare case | |
730 | // lets do it when we snapshot | |
1a5727bd | 731 | $status = CRM_Core_DAO::getFieldValue( |
cbe30f5a | 732 | 'CRM_Mailing_DAO_MailingJob', |
6a488035 | 733 | $this->id, |
1a5727bd DL |
734 | 'status', |
735 | 'id', | |
736 | TRUE | |
6a488035 | 737 | ); |
1a5727bd | 738 | |
6a488035 TO |
739 | if ($status != 'Running') { |
740 | return FALSE; | |
741 | } | |
742 | } | |
743 | } | |
744 | ||
745 | unset($result); | |
746 | ||
747 | // seems like a successful delivery or bounce, lets decrement error count | |
748 | // only if we have smtp connection errors | |
749 | if ($smtpConnectionErrors > 0) { | |
750 | $smtpConnectionErrors--; | |
751 | } | |
752 | ||
753 | // If we have enabled the Throttle option, this is the time to enforce it. | |
754 | if (isset($config->mailThrottleTime) && $config->mailThrottleTime > 0) { | |
755 | usleep((int ) $config->mailThrottleTime); | |
756 | } | |
757 | } | |
758 | ||
2ede60ec DL |
759 | $result = $this->writeToDB( |
760 | $deliveredParams, | |
6a488035 TO |
761 | $targetParams, |
762 | $mailing, | |
763 | $job_date | |
764 | ); | |
765 | ||
766 | return $result; | |
767 | } | |
768 | ||
769 | /** | |
770 | * cancel a mailing | |
771 | * | |
772 | * @param int $mailingId the id of the mailing to be canceled | |
773 | * @static | |
774 | */ | |
775 | public static function cancel($mailingId) { | |
776 | $sql = " | |
777 | SELECT * | |
778 | FROM civicrm_mailing_job | |
779 | WHERE mailing_id = %1 | |
780 | AND is_test = 0 | |
781 | AND ( ( job_type IS NULL ) OR | |
782 | job_type <> 'child' ) | |
783 | "; | |
784 | $params = array(1 => array($mailingId, 'Integer')); | |
785 | $job = CRM_Core_DAO::executeQuery($sql, $params); | |
786 | if ($job->fetch() && | |
787 | in_array($job->status, array('Scheduled', 'Running', 'Paused')) | |
788 | ) { | |
789 | ||
9da8dc8c | 790 | $newJob = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
791 | $newJob->id = $job->id; |
792 | $newJob->end_date = date('YmdHis'); | |
793 | $newJob->status = 'Canceled'; | |
794 | $newJob->save(); | |
795 | ||
796 | // also cancel all child jobs | |
797 | $sql = " | |
798 | UPDATE civicrm_mailing_job | |
799 | SET status = 'Canceled', | |
800 | end_date = %2 | |
801 | WHERE parent_id = %1 | |
802 | AND is_test = 0 | |
803 | AND job_type = 'child' | |
804 | AND status IN ( 'Scheduled', 'Running', 'Paused' ) | |
805 | "; | |
806 | $params = array(1 => array($job->id, 'Integer'), | |
807 | 2 => array(date('YmdHis'), 'Timestamp'), | |
808 | ); | |
809 | CRM_Core_DAO::executeQuery($sql, $params); | |
810 | ||
811 | CRM_Core_Session::setStatus(ts('The mailing has been canceled.'), ts('Canceled'), 'success'); | |
812 | } | |
813 | } | |
814 | ||
815 | /** | |
816 | * Return a translated status enum string | |
817 | * | |
818 | * @param string $status The status enum | |
819 | * | |
820 | * @return string The translated version | |
821 | * @access public | |
822 | * @static | |
823 | */ | |
824 | public static function status($status) { | |
825 | static $translation = NULL; | |
826 | ||
827 | if (empty($translation)) { | |
828 | $translation = array( | |
829 | 'Scheduled' => ts('Scheduled'), | |
830 | 'Running' => ts('Running'), | |
831 | 'Complete' => ts('Complete'), | |
832 | 'Paused' => ts('Paused'), | |
833 | 'Canceled' => ts('Canceled'), | |
834 | ); | |
835 | } | |
836 | return CRM_Utils_Array::value($status, $translation, ts('Not scheduled')); | |
837 | } | |
838 | ||
839 | /** | |
840 | * Return a workflow clause for use in SQL queries, | |
841 | * to only process jobs that are approved. | |
842 | * | |
843 | * @return string For use in a WHERE clause | |
844 | * @access public | |
845 | * @static | |
846 | */ | |
847 | public static function workflowClause() { | |
848 | // add an additional check and only process | |
849 | // jobs that are approved | |
850 | if (CRM_Mailing_Info::workflowEnabled()) { | |
851 | $approveOptionID = CRM_Core_OptionGroup::getValue('mail_approval_status', | |
852 | 'Approved', | |
853 | 'name' | |
854 | ); | |
855 | if ($approveOptionID) { | |
856 | return " AND m.approval_status_id = $approveOptionID "; | |
857 | } | |
858 | } | |
859 | return ''; | |
860 | } | |
861 | ||
2ede60ec DL |
862 | public function writeToDB( |
863 | &$deliveredParams, | |
6a488035 TO |
864 | &$targetParams, |
865 | &$mailing, | |
866 | $job_date | |
867 | ) { | |
868 | static $activityTypeID = NULL; | |
2ede60ec | 869 | static $writeActivity = NULL; |
6a488035 TO |
870 | |
871 | if (!empty($deliveredParams)) { | |
872 | CRM_Mailing_Event_BAO_Delivered::bulkCreate($deliveredParams); | |
873 | $deliveredParams = array(); | |
874 | } | |
875 | ||
2ede60ec DL |
876 | if ($writeActivity === NULL) { |
877 | $writeActivity = CRM_Core_BAO_Setting::getItem( | |
878 | CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, | |
879 | 'write_activity_record', | |
880 | NULL, | |
881 | TRUE | |
882 | ); | |
883 | } | |
6a488035 | 884 | |
2ede60ec DL |
885 | if (!$writeActivity) { |
886 | return TRUE; | |
887 | } | |
888 | ||
889 | $result = TRUE; | |
890 | if (!empty($targetParams) && !empty($mailing->scheduled_id)) { | |
6a488035 | 891 | if (!$activityTypeID) { |
6a488035 TO |
892 | if ($mailing->sms_provider_id) { |
893 | $mailing->subject = $mailing->name; | |
2ede60ec DL |
894 | $activityTypeID = CRM_Core_OptionGroup::getValue( |
895 | 'activity_type', | |
6a488035 TO |
896 | 'Mass SMS', |
897 | 'name' | |
898 | ); | |
899 | } | |
2ede60ec DL |
900 | else { |
901 | $activityTypeID = CRM_Core_OptionGroup::getValue( | |
902 | 'activity_type', | |
903 | 'Bulk Email', | |
904 | 'name' | |
905 | ); | |
906 | } | |
6a488035 TO |
907 | if (!$activityTypeID) { |
908 | CRM_Core_Error::fatal(); | |
909 | } | |
910 | } | |
911 | ||
6a488035 TO |
912 | $activity = array( |
913 | 'source_contact_id' => $mailing->scheduled_id, | |
914 | // CRM-9519 | |
915 | 'target_contact_id' => array_unique($targetParams), | |
6a488035 TO |
916 | 'activity_type_id' => $activityTypeID, |
917 | 'source_record_id' => $this->mailing_id, | |
918 | 'activity_date_time' => $job_date, | |
919 | 'subject' => $mailing->subject, | |
920 | 'status_id' => 2, | |
921 | 'deleteActivityTarget' => FALSE, | |
922 | 'campaign_id' => $mailing->campaign_id, | |
923 | ); | |
924 | ||
925 | //check whether activity is already created for this mailing. | |
926 | //if yes then create only target contact record. | |
927 | $query = " | |
928 | SELECT id | |
929 | FROM civicrm_activity | |
930 | WHERE civicrm_activity.activity_type_id = %1 | |
2ede60ec DL |
931 | AND civicrm_activity.source_record_id = %2 |
932 | "; | |
6a488035 | 933 | |
2ede60ec DL |
934 | $queryParams = array( |
935 | 1 => array($activityTypeID, 'Integer'), | |
6a488035 TO |
936 | 2 => array($this->mailing_id, 'Integer'), |
937 | ); | |
2ede60ec | 938 | $activityID = CRM_Core_DAO::singleValueQuery($query, $queryParams); |
6a488035 TO |
939 | |
940 | if ($activityID) { | |
941 | $activity['id'] = $activityID; | |
942 | ||
943 | // CRM-9519 | |
944 | if (CRM_Core_BAO_Email::isMultipleBulkMail()) { | |
b561230a DL |
945 | static $targetRecordID = NULL; |
946 | if (!$targetRecordID) { | |
947 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); | |
948 | $targetRecordID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
949 | } | |
950 | ||
6a488035 TO |
951 | // make sure we don't attempt to duplicate the target activity |
952 | foreach ($activity['target_contact_id'] as $key => $targetID) { | |
b561230a DL |
953 | $sql = " |
954 | SELECT id | |
955 | FROM civicrm_activity_contact | |
956 | WHERE activity_id = $activityID | |
957 | AND contact_id = $targetID | |
958 | AND record_type_id = $targetRecordID | |
959 | "; | |
6a488035 TO |
960 | if (CRM_Core_DAO::singleValueQuery($sql)) { |
961 | unset($activity['target_contact_id'][$key]); | |
962 | } | |
963 | } | |
964 | } | |
965 | } | |
966 | ||
2ede60ec | 967 | if (is_a(CRM_Activity_BAO_Activity::create($activity), 'CRM_Core_Error')) { |
6a488035 TO |
968 | $result = FALSE; |
969 | } | |
970 | ||
971 | $targetParams = array(); | |
972 | } | |
973 | ||
974 | return $result; | |
975 | } | |
976 | } | |
977 |