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