| 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 | * APIv3 functions for registering/processing mailing events. |
| 31 | * |
| 32 | * @package CiviCRM_APIv3 |
| 33 | */ |
| 34 | |
| 35 | /** |
| 36 | * Handle a create event. |
| 37 | * |
| 38 | * @param array $params |
| 39 | * |
| 40 | * @return array |
| 41 | * API Success Array |
| 42 | * @throws \API_Exception |
| 43 | * @throws \Civi\API\Exception\UnauthorizedException |
| 44 | */ |
| 45 | function civicrm_api3_mailing_create($params) { |
| 46 | if (CRM_Mailing_Info::workflowEnabled()) { |
| 47 | if (!CRM_Core_Permission::check('create mailings')) { |
| 48 | throw new \Civi\API\Exception\UnauthorizedException("This system uses advanced CiviMail workflows which require additional permissions"); |
| 49 | } |
| 50 | if (!CRM_Core_Permission::check('schedule mailings')) { |
| 51 | unset($params['scheduled_date']); |
| 52 | unset($params['scheduled_id']); |
| 53 | } |
| 54 | if (!CRM_Core_Permission::check('approve mailings')) { |
| 55 | unset($params['approval_date']); |
| 56 | unset($params['approver_id']); |
| 57 | unset($params['approval_status_id']); |
| 58 | unset($params['approval_note']); |
| 59 | } |
| 60 | } |
| 61 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get mailing token. |
| 66 | * |
| 67 | * @param array $params |
| 68 | * |
| 69 | * @return array |
| 70 | * @throws \API_Exception |
| 71 | */ |
| 72 | function civicrm_api3_mailing_get_token($params) { |
| 73 | if (!array_key_exists("usage", $params)) { |
| 74 | throw new API_Exception('Mandatory keys missing from params array: entity'); |
| 75 | } |
| 76 | |
| 77 | $tokens = CRM_Core_SelectValues::contactTokens(); |
| 78 | switch ($params['usage']) { |
| 79 | case 'Mailing': |
| 80 | $tokens = array_merge(CRM_Core_SelectValues::mailingTokens(), $tokens); |
| 81 | break; |
| 82 | |
| 83 | case 'ScheduleEventReminder': |
| 84 | $tokens = array_merge(CRM_Core_SelectValues::activityTokens(), $tokens); |
| 85 | $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens); |
| 86 | $tokens = array_merge(CRM_Core_SelectValues::membershipTokens(), $tokens); |
| 87 | break; |
| 88 | |
| 89 | case 'ManageEventScheduleReminder': |
| 90 | $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens); |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | return CRM_Utils_Token::formatTokensForDisplay($tokens); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Adjust Metadata for Create action. |
| 99 | * |
| 100 | * The metadata is used for setting defaults, documentation & validation. |
| 101 | * |
| 102 | * @param array $params |
| 103 | * Array of parameters determined by getfields. |
| 104 | */ |
| 105 | function _civicrm_api3_mailing_create_spec(&$params) { |
| 106 | $params['created_id']['api.required'] = 1; |
| 107 | $params['created_id']['api.default'] = 'user_contact_id'; |
| 108 | $params['api.mailing_job.create']['api.default'] = 1; |
| 109 | $params['api.mailing_job.create']['title'] = 'Schedule Mailing?'; |
| 110 | |
| 111 | $params['override_verp']['api.default'] = !CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, |
| 112 | 'track_civimail_replies', NULL, FALSE |
| 113 | ); |
| 114 | $params['visibility']['api.default'] = 'Public Pages'; |
| 115 | $params['dedupe_email']['api.default'] = TRUE; |
| 116 | |
| 117 | $params['forward_replies']['api.default'] = FALSE; |
| 118 | $params['auto_responder']['api.default'] = FALSE; |
| 119 | $params['open_tracking']['api.default'] = TRUE; |
| 120 | $params['url_tracking']['api.default'] = TRUE; |
| 121 | |
| 122 | $params['header_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('Header', ''); |
| 123 | $params['footer_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('Footer', ''); |
| 124 | $params['optout_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('OptOut', ''); |
| 125 | $params['reply_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('Reply', ''); |
| 126 | $params['resubscribe_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('Resubscribe', ''); |
| 127 | $params['unsubscribe_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('Unsubscribe', ''); |
| 128 | |
| 129 | $defaultAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); |
| 130 | foreach ($defaultAddress as $id => $value) { |
| 131 | if (preg_match('/"(.*)" <(.*)>/', $value, $match)) { |
| 132 | $params['from_email']['api.default'] = $match[2]; |
| 133 | $params['from_name']['api.default'] = $match[1]; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Handle a delete event. |
| 140 | * |
| 141 | * @param array $params |
| 142 | * |
| 143 | * @return array |
| 144 | * API Success Array |
| 145 | */ |
| 146 | function civicrm_api3_mailing_delete($params) { |
| 147 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Handle a get event. |
| 152 | * |
| 153 | * @param array $params |
| 154 | * |
| 155 | * @return array |
| 156 | */ |
| 157 | function civicrm_api3_mailing_get($params) { |
| 158 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Adjust metadata for mailing submit api function. |
| 163 | * |
| 164 | * @param array $spec |
| 165 | */ |
| 166 | function _civicrm_api3_mailing_submit_spec(&$spec) { |
| 167 | $mailingFields = CRM_Mailing_DAO_Mailing::fields(); |
| 168 | $spec['id'] = $mailingFields['id']; |
| 169 | $spec['scheduled_date'] = $mailingFields['scheduled_date']; |
| 170 | $spec['approval_date'] = $mailingFields['approval_date']; |
| 171 | $spec['approval_status_id'] = $mailingFields['approval_status_id']; |
| 172 | $spec['approval_note'] = $mailingFields['approval_note']; |
| 173 | // _skip_evil_bao_auto_recipients_: bool |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Mailing submit. |
| 178 | * |
| 179 | * @param array $params |
| 180 | * |
| 181 | * @return array |
| 182 | * @throws API_Exception |
| 183 | */ |
| 184 | function civicrm_api3_mailing_submit($params) { |
| 185 | civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_Mailing', array('id')); |
| 186 | |
| 187 | if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) { |
| 188 | throw new API_Exception("Missing parameter scheduled_date and/or approval_date"); |
| 189 | } |
| 190 | if (!is_numeric(CRM_Core_Session::getLoggedInContactID())) { |
| 191 | throw new API_Exception("Failed to determine current user"); |
| 192 | } |
| 193 | |
| 194 | $updateParams = array(); |
| 195 | $updateParams['id'] = $params['id']; |
| 196 | |
| 197 | // The BAO will auto-create the job - note: exact match to API default. |
| 198 | $updateParams['api.mailing_job.create'] = 0; |
| 199 | |
| 200 | // Note: we'll pass along scheduling/approval fields, but they may get ignored |
| 201 | // if we don't have permission. |
| 202 | if (isset($params['scheduled_date'])) { |
| 203 | $updateParams['scheduled_date'] = $params['scheduled_date']; |
| 204 | $updateParams['scheduled_id'] = CRM_Core_Session::getLoggedInContactID(); |
| 205 | } |
| 206 | if (isset($params['approval_date'])) { |
| 207 | $updateParams['approval_date'] = $params['approval_date']; |
| 208 | $updateParams['approver_id'] = CRM_Core_Session::getLoggedInContactID(); |
| 209 | $updateParams['approval_status_id'] = CRM_Utils_Array::value('approval_status_id', $updateParams, CRM_Core_OptionGroup::getDefaultValue('mail_approval_status')); |
| 210 | } |
| 211 | if (isset($params['approval_note'])) { |
| 212 | $updateParams['approval_note'] = $params['approval_note']; |
| 213 | } |
| 214 | if (isset($params['_skip_evil_bao_auto_recipients_'])) { |
| 215 | $updateParams['_skip_evil_bao_auto_recipients_'] = $params['_skip_evil_bao_auto_recipients_']; |
| 216 | } |
| 217 | |
| 218 | $updateParams['options']['reload'] = 1; |
| 219 | return civicrm_api3('Mailing', 'create', $updateParams); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Process a bounce event by passing through to the BAOs. |
| 224 | * |
| 225 | * @param array $params |
| 226 | * |
| 227 | * @throws API_Exception |
| 228 | * @return array |
| 229 | */ |
| 230 | function civicrm_api3_mailing_event_bounce($params) { |
| 231 | $body = $params['body']; |
| 232 | unset($params['body']); |
| 233 | |
| 234 | $params += CRM_Mailing_BAO_BouncePattern::match($body); |
| 235 | |
| 236 | if (CRM_Mailing_Event_BAO_Bounce::create($params)) { |
| 237 | return civicrm_api3_create_success($params); |
| 238 | } |
| 239 | else { |
| 240 | throw new API_Exception(ts('Queue event could not be found'), 'no_queue_event |
| 241 | '); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Adjust Metadata for bounce_spec action. |
| 247 | * |
| 248 | * The metadata is used for setting defaults, documentation & validation. |
| 249 | * |
| 250 | * @param array $params |
| 251 | * Array of parameters determined by getfields. |
| 252 | */ |
| 253 | function _civicrm_api3_mailing_event_bounce_spec(&$params) { |
| 254 | $params['job_id']['api.required'] = 1; |
| 255 | $params['job_id']['title'] = 'Job ID'; |
| 256 | $params['event_queue_id']['api.required'] = 1; |
| 257 | $params['event_queue_id']['title'] = 'Event Queue ID'; |
| 258 | $params['hash']['api.required'] = 1; |
| 259 | $params['hash']['title'] = 'Hash'; |
| 260 | $params['body']['api.required'] = 1; |
| 261 | $params['body']['title'] = 'Body'; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Handle a confirm event. |
| 266 | * |
| 267 | * @deprecated |
| 268 | * |
| 269 | * @param array $params |
| 270 | * |
| 271 | * @return array |
| 272 | */ |
| 273 | function civicrm_api3_mailing_event_confirm($params) { |
| 274 | return civicrm_api('mailing_event_confirm', 'create', $params); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Declare deprecated functions. |
| 279 | * |
| 280 | * @deprecated api notice |
| 281 | * @return array |
| 282 | * Array of deprecated actions |
| 283 | */ |
| 284 | function _civicrm_api3_mailing_deprecation() { |
| 285 | return array('event_confirm' => 'Mailing api "event_confirm" action is deprecated. Use the mailing_event_confirm api instead.'); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Handle a reply event. |
| 290 | * |
| 291 | * @param array $params |
| 292 | * |
| 293 | * @return array |
| 294 | */ |
| 295 | function civicrm_api3_mailing_event_reply($params) { |
| 296 | $job = $params['job_id']; |
| 297 | $queue = $params['event_queue_id']; |
| 298 | $hash = $params['hash']; |
| 299 | $replyto = $params['replyTo']; |
| 300 | $bodyTxt = CRM_Utils_Array::value('bodyTxt', $params); |
| 301 | $bodyHTML = CRM_Utils_Array::value('bodyHTML', $params); |
| 302 | $fullEmail = CRM_Utils_Array::value('fullEmail', $params); |
| 303 | |
| 304 | $mailing = CRM_Mailing_Event_BAO_Reply::reply($job, $queue, $hash, $replyto); |
| 305 | |
| 306 | if (empty($mailing)) { |
| 307 | return civicrm_api3_create_error('Queue event could not be found'); |
| 308 | } |
| 309 | |
| 310 | CRM_Mailing_Event_BAO_Reply::send($queue, $mailing, $bodyTxt, $replyto, $bodyHTML, $fullEmail); |
| 311 | |
| 312 | return civicrm_api3_create_success($params); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Adjust Metadata for event_reply action. |
| 317 | * |
| 318 | * The metadata is used for setting defaults, documentation & validation. |
| 319 | * |
| 320 | * @param array $params |
| 321 | * Array of parameters determined by getfields. |
| 322 | */ |
| 323 | function _civicrm_api3_mailing_event_reply_spec(&$params) { |
| 324 | $params['job_id']['api.required'] = 1; |
| 325 | $params['job_id']['title'] = 'Job ID'; |
| 326 | $params['event_queue_id']['api.required'] = 1; |
| 327 | $params['event_queue_id']['title'] = 'Event Queue ID'; |
| 328 | $params['hash']['api.required'] = 1; |
| 329 | $params['hash']['title'] = 'Hash'; |
| 330 | $params['replyTo']['api.required'] = 0; |
| 331 | $params['replyTo']['title'] = 'Reply To';//doesn't really explain adequately |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Handle a forward event. |
| 336 | * |
| 337 | * @param array $params |
| 338 | * |
| 339 | * @return array |
| 340 | */ |
| 341 | function civicrm_api3_mailing_event_forward($params) { |
| 342 | $job = $params['job_id']; |
| 343 | $queue = $params['event_queue_id']; |
| 344 | $hash = $params['hash']; |
| 345 | $email = $params['email']; |
| 346 | $fromEmail = CRM_Utils_Array::value('fromEmail', $params); |
| 347 | $params = CRM_Utils_Array::value('params', $params); |
| 348 | |
| 349 | $forward = CRM_Mailing_Event_BAO_Forward::forward($job, $queue, $hash, $email, $fromEmail, $params); |
| 350 | |
| 351 | if ($forward) { |
| 352 | return civicrm_api3_create_success($params); |
| 353 | } |
| 354 | |
| 355 | return civicrm_api3_create_error('Queue event could not be found'); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Adjust Metadata for event_forward action. |
| 360 | * |
| 361 | * The metadata is used for setting defaults, documentation & validation. |
| 362 | * |
| 363 | * @param array $params |
| 364 | * Array of parameters determined by getfields. |
| 365 | */ |
| 366 | function _civicrm_api3_mailing_event_forward_spec(&$params) { |
| 367 | $params['job_id']['api.required'] = 1; |
| 368 | $params['job_id']['title'] = 'Job ID'; |
| 369 | $params['event_queue_id']['api.required'] = 1; |
| 370 | $params['event_queue_id']['title'] = 'Event Queue ID'; |
| 371 | $params['hash']['api.required'] = 1; |
| 372 | $params['hash']['title'] = 'Hash'; |
| 373 | $params['email']['api.required'] = 1; |
| 374 | $params['email']['title'] = 'Forwarded to Email'; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Handle a click event. |
| 379 | * |
| 380 | * @param array $params |
| 381 | * |
| 382 | * @return array |
| 383 | */ |
| 384 | function civicrm_api3_mailing_event_click($params) { |
| 385 | civicrm_api3_verify_mandatory($params, |
| 386 | 'CRM_Mailing_Event_DAO_TrackableURLOpen', |
| 387 | array('event_queue_id', 'url_id'), |
| 388 | FALSE |
| 389 | ); |
| 390 | |
| 391 | $url_id = $params['url_id']; |
| 392 | $queue = $params['event_queue_id']; |
| 393 | |
| 394 | $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($queue, $url_id); |
| 395 | |
| 396 | $values = array(); |
| 397 | $values['url'] = $url; |
| 398 | $values['is_error'] = 0; |
| 399 | |
| 400 | return civicrm_api3_create_success($values); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Handle an open event. |
| 405 | * |
| 406 | * @param array $params |
| 407 | * |
| 408 | * @return array |
| 409 | */ |
| 410 | function civicrm_api3_mailing_event_open($params) { |
| 411 | |
| 412 | civicrm_api3_verify_mandatory($params, |
| 413 | 'CRM_Mailing_Event_DAO_Opened', |
| 414 | array('event_queue_id'), |
| 415 | FALSE |
| 416 | ); |
| 417 | |
| 418 | $queue = $params['event_queue_id']; |
| 419 | $success = CRM_Mailing_Event_BAO_Opened::open($queue); |
| 420 | |
| 421 | if (!$success) { |
| 422 | return civicrm_api3_create_error('mailing open event failed'); |
| 423 | } |
| 424 | |
| 425 | return civicrm_api3_create_success($params); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Preview mailing. |
| 430 | * |
| 431 | * @param array $params |
| 432 | * Array per getfields metadata. |
| 433 | * |
| 434 | * @return array |
| 435 | * @throws \API_Exception |
| 436 | */ |
| 437 | function civicrm_api3_mailing_preview($params) { |
| 438 | civicrm_api3_verify_mandatory($params, |
| 439 | 'CRM_Mailing_DAO_Mailing', |
| 440 | array('id'), |
| 441 | FALSE |
| 442 | ); |
| 443 | |
| 444 | $fromEmail = NULL; |
| 445 | if (!empty($params['from_email'])) { |
| 446 | $fromEmail = $params['from_email']; |
| 447 | } |
| 448 | |
| 449 | $session = CRM_Core_Session::singleton(); |
| 450 | $mailing = new CRM_Mailing_BAO_Mailing(); |
| 451 | $mailing->id = $params['id']; |
| 452 | $mailing->find(TRUE); |
| 453 | |
| 454 | CRM_Mailing_BAO_Mailing::tokenReplace($mailing); |
| 455 | |
| 456 | // get and format attachments |
| 457 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id); |
| 458 | |
| 459 | $returnProperties = $mailing->getReturnProperties(); |
| 460 | $contactID = CRM_Utils_Array::value('contact_id', $params); |
| 461 | if (!$contactID) { |
| 462 | $contactID = $session->get('userID'); |
| 463 | } |
| 464 | $mailingParams = array('contact_id' => $contactID); |
| 465 | |
| 466 | $details = CRM_Utils_Token::getTokenDetails($mailingParams, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens()); |
| 467 | |
| 468 | $mime = &$mailing->compose(NULL, NULL, NULL, $session->get('userID'), $fromEmail, $fromEmail, |
| 469 | TRUE, $details[0][$contactID], $attachments |
| 470 | ); |
| 471 | |
| 472 | return civicrm_api3_create_success(array( |
| 473 | 'id' => $params['id'], |
| 474 | 'contact_id' => $contactID, |
| 475 | 'subject' => $mime->_headers['Subject'], |
| 476 | 'body_html' => $mime->getHTMLBody(), |
| 477 | 'body_text' => $mime->getTXTBody(), |
| 478 | )); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Adjust metadata for send test function. |
| 483 | * |
| 484 | * @param array $spec |
| 485 | */ |
| 486 | function _civicrm_api3_mailing_send_test_spec(&$spec) { |
| 487 | $spec['test_group']['title'] = 'Test Group ID'; |
| 488 | $spec['test_email']['title'] = 'Test Email Address'; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Send test mailing. |
| 493 | * |
| 494 | * @param array $params |
| 495 | * |
| 496 | * @return array |
| 497 | * @throws \API_Exception |
| 498 | * @throws \CiviCRM_API3_Exception |
| 499 | */ |
| 500 | function civicrm_api3_mailing_send_test($params) { |
| 501 | if (!array_key_exists('test_group', $params) && !array_key_exists('test_email', $params)) { |
| 502 | throw new API_Exception("Mandatory key(s) missing from params array: test_group and/or test_email field are required"); |
| 503 | } |
| 504 | civicrm_api3_verify_mandatory($params, |
| 505 | 'CRM_Mailing_DAO_MailingJob', |
| 506 | array('mailing_id'), |
| 507 | FALSE |
| 508 | ); |
| 509 | |
| 510 | $testEmailParams = _civicrm_api3_generic_replace_base_params($params); |
| 511 | $testEmailParams['is_test'] = 1; |
| 512 | $job = civicrm_api3('MailingJob', 'create', $testEmailParams); |
| 513 | $testEmailParams['job_id'] = $job['id']; |
| 514 | $testEmailParams['emails'] = explode(',', $testEmailParams['test_email']); |
| 515 | if (!empty($params['test_email'])) { |
| 516 | $query = CRM_Utils_SQL_Select::from('civicrm_email e') |
| 517 | ->select(array('e.id', 'e.contact_id', 'e.email')) |
| 518 | ->join('c', 'INNER JOIN civicrm_contact c ON e.contact_id = c.id') |
| 519 | ->where('e.email IN (@emails)', array('@emails' => $testEmailParams['emails'])) |
| 520 | ->where('e.on_hold = 0') |
| 521 | ->where('c.is_opt_out = 0') |
| 522 | ->where('c.do_not_email = 0') |
| 523 | ->where('c.is_deceased = 0') |
| 524 | ->groupBy('e.id') |
| 525 | ->orderBy(array('e.is_bulkmail DESC', 'e.is_primary DESC')) |
| 526 | ->toSQL(); |
| 527 | $dao = CRM_Core_DAO::executeQuery($query); |
| 528 | $emailDetail = array(); |
| 529 | // fetch contact_id and email id for all existing emails |
| 530 | while ($dao->fetch()) { |
| 531 | $emailDetail[$dao->email] = array( |
| 532 | 'contact_id' => $dao->contact_id, |
| 533 | 'email_id' => $dao->id, |
| 534 | ); |
| 535 | } |
| 536 | $dao->free(); |
| 537 | foreach ($testEmailParams['emails'] as $key => $email) { |
| 538 | $email = trim($email); |
| 539 | $contactId = $emailId = NULL; |
| 540 | if (array_key_exists($email, $emailDetail)) { |
| 541 | $emailId = $emailDetail[$email]['email_id']; |
| 542 | $contactId = $emailDetail[$email]['contact_id']; |
| 543 | } |
| 544 | if (!$contactId) { |
| 545 | //create new contact. |
| 546 | $contact = civicrm_api3('Contact', 'create', |
| 547 | array( |
| 548 | 'contact_type' => 'Individual', |
| 549 | 'email' => $email, |
| 550 | 'api.Email.get' => array('return' => 'id'), |
| 551 | ) |
| 552 | ); |
| 553 | $contactId = $contact['id']; |
| 554 | $emailId = $contact['values'][$contactId]['api.Email.get']['id']; |
| 555 | } |
| 556 | civicrm_api3('MailingEventQueue', 'create', |
| 557 | array( |
| 558 | 'job_id' => $job['id'], |
| 559 | 'email_id' => $emailId, |
| 560 | 'contact_id' => $contactId, |
| 561 | ) |
| 562 | ); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | $isComplete = FALSE; |
| 567 | $config = CRM_Core_Config::singleton(); |
| 568 | $mailerJobSize = (property_exists($config, 'mailerJobSize')) ? $config->mailerJobSize : NULL; |
| 569 | while (!$isComplete) { |
| 570 | // Q: In CRM_Mailing_BAO_Mailing::processQueue(), the three runJobs*() |
| 571 | // functions are all called. Why does Mailing.send_test only call one? |
| 572 | // CRM_Mailing_BAO_MailingJob::runJobs_pre($mailerJobSize, NULL); |
| 573 | $isComplete = CRM_Mailing_BAO_MailingJob::runJobs($testEmailParams); |
| 574 | // CRM_Mailing_BAO_MailingJob::runJobs_post(NULL); |
| 575 | } |
| 576 | |
| 577 | //return delivered mail info |
| 578 | $mailDelivered = CRM_Mailing_Event_BAO_Delivered::getRows($params['mailing_id'], $job['id'], TRUE, NULL, NULL, NULL, TRUE); |
| 579 | |
| 580 | return civicrm_api3_create_success($mailDelivered); |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Adjust Metadata for send_mail action. |
| 585 | * |
| 586 | * The metadata is used for setting defaults, documentation & validation. |
| 587 | * |
| 588 | * @param array $params |
| 589 | * Array of parameters determined by getfields. |
| 590 | */ |
| 591 | function _civicrm_api3_mailing_stats_spec(&$params) { |
| 592 | $params['date']['api.default'] = 'now'; |
| 593 | $params['date']['title'] = 'Date'; |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Function which needs to be explained. |
| 598 | * |
| 599 | * @param array $params |
| 600 | * |
| 601 | * @return array |
| 602 | * @throws \API_Exception |
| 603 | */ |
| 604 | function civicrm_api3_mailing_stats($params) { |
| 605 | civicrm_api3_verify_mandatory($params, |
| 606 | 'CRM_Mailing_DAO_MailingJob', |
| 607 | array('mailing_id'), |
| 608 | FALSE |
| 609 | ); |
| 610 | |
| 611 | if ($params['date'] == 'now') { |
| 612 | $params['date'] = date('YmdHis'); |
| 613 | } |
| 614 | else { |
| 615 | $params['date'] = CRM_Utils_Date::processDate($params['date'] . ' ' . $params['date_time']); |
| 616 | } |
| 617 | |
| 618 | $stats[$params['mailing_id']] = array(); |
| 619 | if (empty($params['job_id'])) { |
| 620 | $params['job_id'] = NULL; |
| 621 | } |
| 622 | foreach (array('Delivered', 'Bounces', 'Unsubscribers', 'Unique Clicks', 'Opened') as $detail) { |
| 623 | switch ($detail) { |
| 624 | case 'Delivered': |
| 625 | $stats[$params['mailing_id']] += array( |
| 626 | $detail => CRM_Mailing_Event_BAO_Delivered::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, $params['date']), |
| 627 | ); |
| 628 | break; |
| 629 | |
| 630 | case 'Bounces': |
| 631 | $stats[$params['mailing_id']] += array( |
| 632 | $detail => CRM_Mailing_Event_BAO_Bounce::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, $params['date']), |
| 633 | ); |
| 634 | break; |
| 635 | |
| 636 | case 'Unsubscribers': |
| 637 | $stats[$params['mailing_id']] += array( |
| 638 | $detail => CRM_Mailing_Event_BAO_Unsubscribe::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, NULL, $params['date']), |
| 639 | ); |
| 640 | break; |
| 641 | |
| 642 | case 'Unique Clicks': |
| 643 | $stats[$params['mailing_id']] += array( |
| 644 | $detail => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, NULL, $params['date']), |
| 645 | ); |
| 646 | break; |
| 647 | |
| 648 | case 'Opened': |
| 649 | $stats[$params['mailing_id']] += array( |
| 650 | $detail => CRM_Mailing_Event_BAO_Opened::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, $params['date']), |
| 651 | ); |
| 652 | break; |
| 653 | } |
| 654 | } |
| 655 | return civicrm_api3_create_success($stats); |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Fix the reset dates on the email record based on when a mail was last delivered. |
| 660 | * |
| 661 | * We only consider mailings that were completed and finished in the last 3 to 7 days |
| 662 | * Both the min and max days can be set via the params |
| 663 | * |
| 664 | * @param array $params |
| 665 | * |
| 666 | * @return array |
| 667 | */ |
| 668 | function civicrm_api3_mailing_update_email_resetdate($params) { |
| 669 | CRM_Mailing_Event_BAO_Delivered::updateEmailResetDate( |
| 670 | CRM_Utils_Array::value('minDays', $params, 3), |
| 671 | CRM_Utils_Array::value('maxDays', $params, 3) |
| 672 | ); |
| 673 | return civicrm_api3_create_success(); |
| 674 | } |