| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | require_once 'Mail/mime.php'; |
| 36 | |
| 37 | /** |
| 38 | * Class CRM_Mailing_BAO_Mailing |
| 39 | */ |
| 40 | class CRM_Mailing_BAO_Mailing extends CRM_Mailing_DAO_Mailing { |
| 41 | |
| 42 | /** |
| 43 | * An array that holds the complete templates |
| 44 | * including any headers or footers that need to be prepended |
| 45 | * or appended to the body |
| 46 | */ |
| 47 | private $preparedTemplates = NULL; |
| 48 | |
| 49 | /** |
| 50 | * An array that holds the complete templates |
| 51 | * including any headers or footers that need to be prepended |
| 52 | * or appended to the body |
| 53 | */ |
| 54 | private $templates = NULL; |
| 55 | |
| 56 | /** |
| 57 | * An array that holds the tokens that are specifically found in our text and html bodies |
| 58 | */ |
| 59 | private $tokens = NULL; |
| 60 | |
| 61 | /** |
| 62 | * An array that holds the tokens that are specifically found in our text and html bodies |
| 63 | */ |
| 64 | private $flattenedTokens = NULL; |
| 65 | |
| 66 | /** |
| 67 | * The header associated with this mailing |
| 68 | */ |
| 69 | private $header = NULL; |
| 70 | |
| 71 | /** |
| 72 | * The footer associated with this mailing |
| 73 | */ |
| 74 | private $footer = NULL; |
| 75 | |
| 76 | /** |
| 77 | * The HTML content of the message |
| 78 | */ |
| 79 | private $html = NULL; |
| 80 | |
| 81 | /** |
| 82 | * The text content of the message |
| 83 | */ |
| 84 | private $text = NULL; |
| 85 | |
| 86 | /** |
| 87 | * Cached BAO for the domain |
| 88 | */ |
| 89 | private $_domain = NULL; |
| 90 | |
| 91 | /** |
| 92 | * Class constructor |
| 93 | */ |
| 94 | public function __construct() { |
| 95 | parent::__construct(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @deprecated |
| 100 | * |
| 101 | * @param int $job_id |
| 102 | * @param int $mailing_id |
| 103 | * |
| 104 | * @return int |
| 105 | */ |
| 106 | public static function getRecipientsCount($job_id, $mailing_id = NULL) { |
| 107 | // need this for backward compatibility, so we can get count for old mailings |
| 108 | // please do not use this function if possible |
| 109 | $eq = self::getRecipients($job_id, $mailing_id); |
| 110 | return $eq->N; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * note that $job_id is used only as a variable in the temp table construction |
| 115 | * and does not play a role in the queries generated |
| 116 | * @param int $job_id |
| 117 | * (misnomer) a nonce value used to name temporary tables. |
| 118 | * @param int $mailing_id |
| 119 | * @param bool $storeRecipients |
| 120 | * @param bool $dedupeEmail |
| 121 | * @param null $mode |
| 122 | * |
| 123 | * @return CRM_Mailing_Event_BAO_Queue|string |
| 124 | */ |
| 125 | public static function getRecipients( |
| 126 | $job_id, |
| 127 | $mailing_id = NULL, |
| 128 | $storeRecipients = FALSE, |
| 129 | $dedupeEmail = FALSE, |
| 130 | $mode = NULL) { |
| 131 | $mailingGroup = new CRM_Mailing_DAO_MailingGroup(); |
| 132 | |
| 133 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
| 134 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
| 135 | $mg = CRM_Mailing_DAO_MailingGroup::getTableName(); |
| 136 | $eq = CRM_Mailing_Event_DAO_Queue::getTableName(); |
| 137 | |
| 138 | $email = CRM_Core_DAO_Email::getTableName(); |
| 139 | if ($mode == 'sms') { |
| 140 | $phone = CRM_Core_DAO_Phone::getTableName(); |
| 141 | } |
| 142 | $contact = CRM_Contact_DAO_Contact::getTableName(); |
| 143 | |
| 144 | $group = CRM_Contact_DAO_Group::getTableName(); |
| 145 | $g2contact = CRM_Contact_DAO_GroupContact::getTableName(); |
| 146 | |
| 147 | $m = new CRM_Mailing_DAO_Mailing(); |
| 148 | $m->id = $mailing_id; |
| 149 | $m->find(TRUE); |
| 150 | |
| 151 | $email_selection_method = $m->email_selection_method; |
| 152 | $location_type_id = $m->location_type_id; |
| 153 | |
| 154 | // Note: When determining the ORDER that results are returned, it's |
| 155 | // the record that comes last that counts. That's because we are |
| 156 | // INSERT'ing INTO a table with a primary id so that last record |
| 157 | // over writes any previous record. |
| 158 | switch ($email_selection_method) { |
| 159 | case 'location-exclude': |
| 160 | $location_filter = "($email.location_type_id != $location_type_id)"; |
| 161 | // If there is more than one email that doesn't match the location, |
| 162 | // prefer the one marked is_bulkmail, followed by is_primary. |
| 163 | $order_by = "ORDER BY $email.is_bulkmail, $email.is_primary"; |
| 164 | break; |
| 165 | |
| 166 | case 'location-only': |
| 167 | $location_filter = "($email.location_type_id = $location_type_id)"; |
| 168 | // If there is more than one email of the desired location, prefer |
| 169 | // the one marked is_bulkmail, followed by is_primary. |
| 170 | $order_by = "ORDER BY $email.is_bulkmail, $email.is_primary"; |
| 171 | break; |
| 172 | |
| 173 | case 'location-prefer': |
| 174 | $location_filter = "($email.is_bulkmail = 1 OR $email.is_primary = 1 OR $email.location_type_id = $location_type_id)"; |
| 175 | |
| 176 | // ORDER BY is more complicated because we have to set an arbitrary |
| 177 | // order that prefers the location that we want. We do that using |
| 178 | // the FIELD function. For more info, see: |
| 179 | // https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field |
| 180 | // We assign the location type we want the value "1" by putting it |
| 181 | // in the first position after we name the field. All other location |
| 182 | // types are left out, so they will be assigned the value 0. That |
| 183 | // means, they will all be equally tied for first place, with our |
| 184 | // location being last. |
| 185 | $order_by = "ORDER BY FIELD($email.location_type_id, $location_type_id), $email.is_bulkmail, $email.is_primary"; |
| 186 | break; |
| 187 | |
| 188 | case 'automatic': |
| 189 | // fall through to default |
| 190 | default: |
| 191 | $location_filter = "($email.is_bulkmail = 1 OR $email.is_primary = 1)"; |
| 192 | $order_by = "ORDER BY $email.is_bulkmail"; |
| 193 | } |
| 194 | |
| 195 | /* Create a temp table for contact exclusion */ |
| 196 | $mailingGroup->query( |
| 197 | "CREATE TEMPORARY TABLE X_$job_id |
| 198 | (contact_id int primary key) |
| 199 | ENGINE=HEAP" |
| 200 | ); |
| 201 | |
| 202 | /* Add all the members of groups excluded from this mailing to the temp |
| 203 | * table */ |
| 204 | |
| 205 | $excludeSubGroup = "INSERT INTO X_$job_id (contact_id) |
| 206 | SELECT DISTINCT $g2contact.contact_id |
| 207 | FROM $g2contact |
| 208 | INNER JOIN $mg |
| 209 | ON $g2contact.group_id = $mg.entity_id AND $mg.entity_table = '$group' |
| 210 | WHERE |
| 211 | $mg.mailing_id = {$mailing_id} |
| 212 | AND $g2contact.status = 'Added' |
| 213 | AND $mg.group_type = 'Exclude'"; |
| 214 | $mailingGroup->query($excludeSubGroup); |
| 215 | |
| 216 | /* Add all unsubscribe members of base group from this mailing to the temp |
| 217 | * table */ |
| 218 | |
| 219 | $unSubscribeBaseGroup = "INSERT INTO X_$job_id (contact_id) |
| 220 | SELECT DISTINCT $g2contact.contact_id |
| 221 | FROM $g2contact |
| 222 | INNER JOIN $mg |
| 223 | ON $g2contact.group_id = $mg.entity_id AND $mg.entity_table = '$group' |
| 224 | WHERE |
| 225 | $mg.mailing_id = {$mailing_id} |
| 226 | AND $g2contact.status = 'Removed' |
| 227 | AND $mg.group_type = 'Base'"; |
| 228 | $mailingGroup->query($unSubscribeBaseGroup); |
| 229 | |
| 230 | /* Add all the (intended) recipients of an excluded prior mailing to |
| 231 | * the temp table */ |
| 232 | |
| 233 | $excludeSubMailing = "INSERT IGNORE INTO X_$job_id (contact_id) |
| 234 | SELECT DISTINCT $eq.contact_id |
| 235 | FROM $eq |
| 236 | INNER JOIN $job |
| 237 | ON $eq.job_id = $job.id |
| 238 | INNER JOIN $mg |
| 239 | ON $job.mailing_id = $mg.entity_id AND $mg.entity_table = '$mailing' |
| 240 | WHERE |
| 241 | $mg.mailing_id = {$mailing_id} |
| 242 | AND $mg.group_type = 'Exclude'"; |
| 243 | $mailingGroup->query($excludeSubMailing); |
| 244 | |
| 245 | // get all the saved searches AND hierarchical groups |
| 246 | // and load them in the cache |
| 247 | $sql = " |
| 248 | SELECT $group.id, $group.cache_date, $group.saved_search_id, $group.children |
| 249 | FROM $group |
| 250 | INNER JOIN $mg ON $mg.entity_id = $group.id |
| 251 | WHERE $mg.entity_table = '$group' |
| 252 | AND $mg.group_type = 'Exclude' |
| 253 | AND $mg.mailing_id = {$mailing_id} |
| 254 | AND ( saved_search_id != 0 |
| 255 | OR saved_search_id IS NOT NULL |
| 256 | OR children IS NOT NULL ) |
| 257 | "; |
| 258 | |
| 259 | $groupDAO = CRM_Core_DAO::executeQuery($sql); |
| 260 | while ($groupDAO->fetch()) { |
| 261 | if ($groupDAO->cache_date == NULL) { |
| 262 | CRM_Contact_BAO_GroupContactCache::load($groupDAO); |
| 263 | } |
| 264 | |
| 265 | $smartGroupExclude = " |
| 266 | INSERT IGNORE INTO X_$job_id (contact_id) |
| 267 | SELECT c.contact_id |
| 268 | FROM civicrm_group_contact_cache c |
| 269 | WHERE c.group_id = {$groupDAO->id} |
| 270 | "; |
| 271 | $mailingGroup->query($smartGroupExclude); |
| 272 | } |
| 273 | |
| 274 | $tempColumn = 'email_id'; |
| 275 | if ($mode == 'sms') { |
| 276 | $tempColumn = 'phone_id'; |
| 277 | } |
| 278 | |
| 279 | /* Get all the group contacts we want to include */ |
| 280 | |
| 281 | $mailingGroup->query( |
| 282 | "CREATE TEMPORARY TABLE I_$job_id |
| 283 | ($tempColumn int, contact_id int primary key) |
| 284 | ENGINE=HEAP" |
| 285 | ); |
| 286 | |
| 287 | /* Get the group contacts, but only those which are not in the |
| 288 | * exclusion temp table */ |
| 289 | |
| 290 | $query = "REPLACE INTO I_$job_id (email_id, contact_id) |
| 291 | |
| 292 | SELECT DISTINCT $email.id as email_id, |
| 293 | $contact.id as contact_id |
| 294 | FROM $email |
| 295 | INNER JOIN $contact |
| 296 | ON $email.contact_id = $contact.id |
| 297 | INNER JOIN $g2contact |
| 298 | ON $contact.id = $g2contact.contact_id |
| 299 | INNER JOIN $mg |
| 300 | ON $g2contact.group_id = $mg.entity_id |
| 301 | AND $mg.entity_table = '$group' |
| 302 | LEFT JOIN X_$job_id |
| 303 | ON $contact.id = X_$job_id.contact_id |
| 304 | WHERE |
| 305 | ($mg.group_type = 'Include') |
| 306 | AND $mg.search_id IS NULL |
| 307 | AND $g2contact.status = 'Added' |
| 308 | AND $contact.do_not_email = 0 |
| 309 | AND $contact.is_opt_out = 0 |
| 310 | AND $contact.is_deceased <> 1 |
| 311 | AND $location_filter |
| 312 | AND $email.email IS NOT NULL |
| 313 | AND $email.email != '' |
| 314 | AND $email.on_hold = 0 |
| 315 | AND $mg.mailing_id = {$mailing_id} |
| 316 | AND X_$job_id.contact_id IS null |
| 317 | $order_by"; |
| 318 | |
| 319 | if ($mode == 'sms') { |
| 320 | $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name'); |
| 321 | $query = "REPLACE INTO I_$job_id (phone_id, contact_id) |
| 322 | |
| 323 | SELECT DISTINCT $phone.id as phone_id, |
| 324 | $contact.id as contact_id |
| 325 | FROM $phone |
| 326 | INNER JOIN $contact |
| 327 | ON $phone.contact_id = $contact.id |
| 328 | INNER JOIN $g2contact |
| 329 | ON $contact.id = $g2contact.contact_id |
| 330 | INNER JOIN $mg |
| 331 | ON $g2contact.group_id = $mg.entity_id |
| 332 | AND $mg.entity_table = '$group' |
| 333 | LEFT JOIN X_$job_id |
| 334 | ON $contact.id = X_$job_id.contact_id |
| 335 | WHERE |
| 336 | ($mg.group_type = 'Include') |
| 337 | AND $mg.search_id IS NULL |
| 338 | AND $g2contact.status = 'Added' |
| 339 | AND $contact.do_not_sms = 0 |
| 340 | AND $contact.is_opt_out = 0 |
| 341 | AND $contact.is_deceased <> 1 |
| 342 | AND $phone.phone_type_id = {$phoneTypes['Mobile']} |
| 343 | AND $phone.phone IS NOT NULL |
| 344 | AND $phone.phone != '' |
| 345 | AND $mg.mailing_id = {$mailing_id} |
| 346 | AND X_$job_id.contact_id IS null"; |
| 347 | } |
| 348 | $mailingGroup->query($query); |
| 349 | |
| 350 | /* Query prior mailings */ |
| 351 | |
| 352 | $query = "REPLACE INTO I_$job_id (email_id, contact_id) |
| 353 | SELECT DISTINCT $email.id as email_id, |
| 354 | $contact.id as contact_id |
| 355 | FROM $email |
| 356 | INNER JOIN $contact |
| 357 | ON $email.contact_id = $contact.id |
| 358 | INNER JOIN $eq |
| 359 | ON $eq.contact_id = $contact.id |
| 360 | INNER JOIN $job |
| 361 | ON $eq.job_id = $job.id |
| 362 | INNER JOIN $mg |
| 363 | ON $job.mailing_id = $mg.entity_id AND $mg.entity_table = '$mailing' |
| 364 | LEFT JOIN X_$job_id |
| 365 | ON $contact.id = X_$job_id.contact_id |
| 366 | WHERE |
| 367 | ($mg.group_type = 'Include') |
| 368 | AND $contact.do_not_email = 0 |
| 369 | AND $contact.is_opt_out = 0 |
| 370 | AND $contact.is_deceased <> 1 |
| 371 | AND $location_filter |
| 372 | AND $email.on_hold = 0 |
| 373 | AND $mg.mailing_id = {$mailing_id} |
| 374 | AND X_$job_id.contact_id IS null |
| 375 | $order_by"; |
| 376 | |
| 377 | if ($mode == 'sms') { |
| 378 | $query = "REPLACE INTO I_$job_id (phone_id, contact_id) |
| 379 | SELECT DISTINCT $phone.id as phone_id, |
| 380 | $contact.id as contact_id |
| 381 | FROM $phone |
| 382 | INNER JOIN $contact |
| 383 | ON $phone.contact_id = $contact.id |
| 384 | INNER JOIN $eq |
| 385 | ON $eq.contact_id = $contact.id |
| 386 | INNER JOIN $job |
| 387 | ON $eq.job_id = $job.id |
| 388 | INNER JOIN $mg |
| 389 | ON $job.mailing_id = $mg.entity_id AND $mg.entity_table = '$mailing' |
| 390 | LEFT JOIN X_$job_id |
| 391 | ON $contact.id = X_$job_id.contact_id |
| 392 | WHERE |
| 393 | ($mg.group_type = 'Include') |
| 394 | AND $contact.do_not_sms = 0 |
| 395 | AND $contact.is_opt_out = 0 |
| 396 | AND $contact.is_deceased <> 1 |
| 397 | AND $phone.phone_type_id = {$phoneTypes['Mobile']} |
| 398 | AND $mg.mailing_id = {$mailing_id} |
| 399 | AND X_$job_id.contact_id IS null"; |
| 400 | } |
| 401 | $mailingGroup->query($query); |
| 402 | |
| 403 | $sql = " |
| 404 | SELECT $group.id, $group.cache_date, $group.saved_search_id, $group.children |
| 405 | FROM $group |
| 406 | INNER JOIN $mg ON $mg.entity_id = $group.id |
| 407 | WHERE $mg.entity_table = '$group' |
| 408 | AND $mg.group_type = 'Include' |
| 409 | AND $mg.search_id IS NULL |
| 410 | AND $mg.mailing_id = {$mailing_id} |
| 411 | AND ( saved_search_id != 0 |
| 412 | OR saved_search_id IS NOT NULL |
| 413 | OR children IS NOT NULL ) |
| 414 | "; |
| 415 | |
| 416 | $groupDAO = CRM_Core_DAO::executeQuery($sql); |
| 417 | while ($groupDAO->fetch()) { |
| 418 | if ($groupDAO->cache_date == NULL) { |
| 419 | CRM_Contact_BAO_GroupContactCache::load($groupDAO); |
| 420 | } |
| 421 | |
| 422 | $smartGroupInclude = " |
| 423 | REPLACE INTO I_$job_id (email_id, contact_id) |
| 424 | SELECT civicrm_email.id as email_id, c.id as contact_id |
| 425 | FROM civicrm_contact c |
| 426 | INNER JOIN civicrm_email ON civicrm_email.contact_id = c.id |
| 427 | INNER JOIN civicrm_group_contact_cache gc ON gc.contact_id = c.id |
| 428 | LEFT JOIN X_$job_id ON X_$job_id.contact_id = c.id |
| 429 | WHERE gc.group_id = {$groupDAO->id} |
| 430 | AND c.do_not_email = 0 |
| 431 | AND c.is_opt_out = 0 |
| 432 | AND c.is_deceased <> 1 |
| 433 | AND $location_filter |
| 434 | AND civicrm_email.on_hold = 0 |
| 435 | AND X_$job_id.contact_id IS null |
| 436 | $order_by |
| 437 | "; |
| 438 | if ($mode == 'sms') { |
| 439 | $smartGroupInclude = " |
| 440 | REPLACE INTO I_$job_id (phone_id, contact_id) |
| 441 | SELECT p.id as phone_id, c.id as contact_id |
| 442 | FROM civicrm_contact c |
| 443 | INNER JOIN civicrm_phone p ON p.contact_id = c.id |
| 444 | INNER JOIN civicrm_group_contact_cache gc ON gc.contact_id = c.id |
| 445 | LEFT JOIN X_$job_id ON X_$job_id.contact_id = c.id |
| 446 | WHERE gc.group_id = {$groupDAO->id} |
| 447 | AND c.do_not_sms = 0 |
| 448 | AND c.is_opt_out = 0 |
| 449 | AND c.is_deceased <> 1 |
| 450 | AND p.phone_type_id = {$phoneTypes['Mobile']} |
| 451 | AND X_$job_id.contact_id IS null"; |
| 452 | } |
| 453 | $mailingGroup->query($smartGroupInclude); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Construct the filtered search queries |
| 458 | */ |
| 459 | $query = " |
| 460 | SELECT search_id, search_args, entity_id |
| 461 | FROM $mg |
| 462 | WHERE $mg.search_id IS NOT NULL |
| 463 | AND $mg.mailing_id = {$mailing_id} |
| 464 | "; |
| 465 | $dao = CRM_Core_DAO::executeQuery($query); |
| 466 | while ($dao->fetch()) { |
| 467 | $customSQL = CRM_Contact_BAO_SearchCustom::civiMailSQL($dao->search_id, |
| 468 | $dao->search_args, |
| 469 | $dao->entity_id |
| 470 | ); |
| 471 | $query = "REPLACE INTO I_$job_id ({$tempColumn}, contact_id) |
| 472 | $customSQL"; |
| 473 | $mailingGroup->query($query); |
| 474 | } |
| 475 | |
| 476 | /* Get the emails with only location override */ |
| 477 | |
| 478 | $query = "REPLACE INTO I_$job_id (email_id, contact_id) |
| 479 | SELECT DISTINCT $email.id as local_email_id, |
| 480 | $contact.id as contact_id |
| 481 | FROM $email |
| 482 | INNER JOIN $contact |
| 483 | ON $email.contact_id = $contact.id |
| 484 | INNER JOIN $g2contact |
| 485 | ON $contact.id = $g2contact.contact_id |
| 486 | INNER JOIN $mg |
| 487 | ON $g2contact.group_id = $mg.entity_id |
| 488 | LEFT JOIN X_$job_id |
| 489 | ON $contact.id = X_$job_id.contact_id |
| 490 | WHERE |
| 491 | $mg.entity_table = '$group' |
| 492 | AND $mg.group_type = 'Include' |
| 493 | AND $g2contact.status = 'Added' |
| 494 | AND $contact.do_not_email = 0 |
| 495 | AND $contact.is_opt_out = 0 |
| 496 | AND $contact.is_deceased <> 1 |
| 497 | AND $location_filter |
| 498 | AND $email.on_hold = 0 |
| 499 | AND $mg.mailing_id = {$mailing_id} |
| 500 | AND X_$job_id.contact_id IS null |
| 501 | $order_by"; |
| 502 | if ($mode == "sms") { |
| 503 | $query = "REPLACE INTO I_$job_id (phone_id, contact_id) |
| 504 | SELECT DISTINCT $phone.id as phone_id, |
| 505 | $contact.id as contact_id |
| 506 | FROM $phone |
| 507 | INNER JOIN $contact |
| 508 | ON $phone.contact_id = $contact.id |
| 509 | INNER JOIN $g2contact |
| 510 | ON $contact.id = $g2contact.contact_id |
| 511 | INNER JOIN $mg |
| 512 | ON $g2contact.group_id = $mg.entity_id |
| 513 | LEFT JOIN X_$job_id |
| 514 | ON $contact.id = X_$job_id.contact_id |
| 515 | WHERE |
| 516 | $mg.entity_table = '$group' |
| 517 | AND $mg.group_type = 'Include' |
| 518 | AND $g2contact.status = 'Added' |
| 519 | AND $contact.do_not_sms = 0 |
| 520 | AND $contact.is_opt_out = 0 |
| 521 | AND $contact.is_deceased <> 1 |
| 522 | AND $phone.phone_type_id = {$phoneTypes['Mobile']} |
| 523 | AND $mg.mailing_id = {$mailing_id} |
| 524 | AND X_$job_id.contact_id IS null"; |
| 525 | } |
| 526 | $mailingGroup->query($query); |
| 527 | |
| 528 | $eq = new CRM_Mailing_Event_BAO_Queue(); |
| 529 | |
| 530 | list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause(); |
| 531 | $aclWhere = $aclWhere ? "WHERE {$aclWhere}" : ''; |
| 532 | |
| 533 | if ($storeRecipients && $mailing_id) { |
| 534 | $sql = " |
| 535 | DELETE |
| 536 | FROM civicrm_mailing_recipients |
| 537 | WHERE mailing_id = %1 |
| 538 | "; |
| 539 | $params = array(1 => array($mailing_id, 'Integer')); |
| 540 | CRM_Core_DAO::executeQuery($sql, $params); |
| 541 | |
| 542 | // CRM-3975 |
| 543 | $groupBy = $groupJoin = ''; |
| 544 | if ($dedupeEmail) { |
| 545 | $groupJoin = " INNER JOIN civicrm_email e ON e.id = i.email_id"; |
| 546 | $groupBy = " GROUP BY e.email "; |
| 547 | } |
| 548 | |
| 549 | $sql = " |
| 550 | INSERT INTO civicrm_mailing_recipients ( mailing_id, contact_id, {$tempColumn} ) |
| 551 | SELECT %1, i.contact_id, i.{$tempColumn} |
| 552 | FROM civicrm_contact contact_a |
| 553 | INNER JOIN I_$job_id i ON contact_a.id = i.contact_id |
| 554 | $groupJoin |
| 555 | {$aclFrom} |
| 556 | {$aclWhere} |
| 557 | $groupBy |
| 558 | ORDER BY i.contact_id, i.{$tempColumn} |
| 559 | "; |
| 560 | |
| 561 | CRM_Core_DAO::executeQuery($sql, $params); |
| 562 | |
| 563 | // if we need to add all emails marked bulk, do it as a post filter |
| 564 | // on the mailing recipients table |
| 565 | if (CRM_Core_BAO_Email::isMultipleBulkMail()) { |
| 566 | self::addMultipleEmails($mailing_id); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* Delete the temp table */ |
| 571 | |
| 572 | $mailingGroup->reset(); |
| 573 | $mailingGroup->query("DROP TEMPORARY TABLE X_$job_id"); |
| 574 | $mailingGroup->query("DROP TEMPORARY TABLE I_$job_id"); |
| 575 | |
| 576 | return $eq; |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * @param string $type |
| 581 | * |
| 582 | * @return array |
| 583 | */ |
| 584 | private function _getMailingGroupIds($type = 'Include') { |
| 585 | $mailingGroup = new CRM_Mailing_DAO_MailingGroup(); |
| 586 | $group = CRM_Contact_DAO_Group::getTableName(); |
| 587 | if (!isset($this->id)) { |
| 588 | // we're just testing tokens, so return any group |
| 589 | $query = "SELECT id AS entity_id |
| 590 | FROM $group |
| 591 | ORDER BY id |
| 592 | LIMIT 1"; |
| 593 | } |
| 594 | else { |
| 595 | $query = "SELECT entity_id |
| 596 | FROM $mg |
| 597 | WHERE mailing_id = {$this->id} |
| 598 | AND group_type = '$type' |
| 599 | AND entity_table = '$group'"; |
| 600 | } |
| 601 | $mailingGroup->query($query); |
| 602 | |
| 603 | $groupIds = array(); |
| 604 | while ($mailingGroup->fetch()) { |
| 605 | $groupIds[] = $mailingGroup->entity_id; |
| 606 | } |
| 607 | |
| 608 | return $groupIds; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Returns the regex patterns that are used for preparing the text and html templates. |
| 613 | */ |
| 614 | private function getPatterns($onlyHrefs = FALSE) { |
| 615 | |
| 616 | $patterns = array(); |
| 617 | |
| 618 | $protos = '(https?|ftp)'; |
| 619 | $letters = '\w'; |
| 620 | $gunk = '\{\}/#~:.?+=&;%@!\,\-\|\(\)\*'; |
| 621 | $punc = '.:?\-'; |
| 622 | $any = "{$letters}{$gunk}{$punc}"; |
| 623 | if ($onlyHrefs) { |
| 624 | $pattern = "\\bhref[ ]*=[ ]*([\"'])?(($protos:[$any]+?(?=[$punc]*[^$any]|$)))([\"'])?"; |
| 625 | } |
| 626 | else { |
| 627 | $pattern = "\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))"; |
| 628 | } |
| 629 | |
| 630 | $patterns[] = $pattern; |
| 631 | $patterns[] = '\\\\\{\w+\.\w+\\\\\}|\{\{\w+\.\w+\}\}'; |
| 632 | $patterns[] = '\{\w+\.\w+\}'; |
| 633 | |
| 634 | $patterns = '{' . implode('|', $patterns) . '}im'; |
| 635 | |
| 636 | return $patterns; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Returns an array that denotes the type of token that we are dealing with |
| 641 | * we use the type later on when we are doing a token replacement lookup |
| 642 | * |
| 643 | * @param string $token |
| 644 | * The token for which we will be doing adata lookup. |
| 645 | * |
| 646 | * @return array |
| 647 | * An array that holds the token itself and the type. |
| 648 | * the type will tell us which function to use for the data lookup |
| 649 | * if we need to do a lookup at all |
| 650 | */ |
| 651 | public function &getDataFunc($token) { |
| 652 | static $_categories = NULL; |
| 653 | static $_categoryString = NULL; |
| 654 | if (!$_categories) { |
| 655 | $_categories = array( |
| 656 | 'domain' => NULL, |
| 657 | 'action' => NULL, |
| 658 | 'mailing' => NULL, |
| 659 | 'contact' => NULL, |
| 660 | ); |
| 661 | |
| 662 | CRM_Utils_Hook::tokens($_categories); |
| 663 | $_categoryString = implode('|', array_keys($_categories)); |
| 664 | } |
| 665 | |
| 666 | $funcStruct = array('type' => NULL, 'token' => $token); |
| 667 | $matches = array(); |
| 668 | if ((preg_match('/^href/i', $token) || preg_match('/^http/i', $token))) { |
| 669 | // it is a url so we need to check to see if there are any tokens embedded |
| 670 | // if so then call this function again to get the token dataFunc |
| 671 | // and assign the type 'embedded' so that the data retrieving function |
| 672 | // will know what how to handle this token. |
| 673 | if (preg_match_all('/(\{\w+\.\w+\})/', $token, $matches)) { |
| 674 | $funcStruct['type'] = 'embedded_url'; |
| 675 | $funcStruct['embed_parts'] = $funcStruct['token'] = array(); |
| 676 | foreach ($matches[1] as $match) { |
| 677 | $preg_token = '/' . preg_quote($match, '/') . '/'; |
| 678 | $list = preg_split($preg_token, $token, 2); |
| 679 | $funcStruct['embed_parts'][] = $list[0]; |
| 680 | $token = $list[1]; |
| 681 | $funcStruct['token'][] = $this->getDataFunc($match); |
| 682 | } |
| 683 | // fixed truncated url, CRM-7113 |
| 684 | if ($token) { |
| 685 | $funcStruct['embed_parts'][] = $token; |
| 686 | } |
| 687 | } |
| 688 | else { |
| 689 | $funcStruct['type'] = 'url'; |
| 690 | } |
| 691 | } |
| 692 | elseif (preg_match('/^\{(' . $_categoryString . ')\.(\w+)\}$/', $token, $matches)) { |
| 693 | $funcStruct['type'] = $matches[1]; |
| 694 | $funcStruct['token'] = $matches[2]; |
| 695 | } |
| 696 | elseif (preg_match('/\\\\\{(\w+\.\w+)\\\\\}|\{\{(\w+\.\w+)\}\}/', $token, $matches)) { |
| 697 | // we are an escaped token |
| 698 | // so remove the escape chars |
| 699 | $unescaped_token = preg_replace('/\{\{|\}\}|\\\\\{|\\\\\}/', '', $matches[0]); |
| 700 | $funcStruct['token'] = '{' . $unescaped_token . '}'; |
| 701 | } |
| 702 | return $funcStruct; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * |
| 707 | * Prepares the text and html templates |
| 708 | * for generating the emails and returns a copy of the |
| 709 | * prepared templates |
| 710 | */ |
| 711 | private function getPreparedTemplates() { |
| 712 | if (!$this->preparedTemplates) { |
| 713 | $patterns['html'] = $this->getPatterns(TRUE); |
| 714 | $patterns['subject'] = $patterns['text'] = $this->getPatterns(); |
| 715 | $templates = $this->getTemplates(); |
| 716 | |
| 717 | $this->preparedTemplates = array(); |
| 718 | |
| 719 | foreach (array( |
| 720 | 'html', |
| 721 | 'text', |
| 722 | 'subject', |
| 723 | ) as $key) { |
| 724 | if (!isset($templates[$key])) { |
| 725 | continue; |
| 726 | } |
| 727 | |
| 728 | $matches = array(); |
| 729 | $tokens = array(); |
| 730 | $split_template = array(); |
| 731 | |
| 732 | $email = $templates[$key]; |
| 733 | preg_match_all($patterns[$key], $email, $matches, PREG_PATTERN_ORDER); |
| 734 | foreach ($matches[0] as $idx => $token) { |
| 735 | $preg_token = '/' . preg_quote($token, '/') . '/im'; |
| 736 | list($split_template[], $email) = preg_split($preg_token, $email, 2); |
| 737 | array_push($tokens, $this->getDataFunc($token)); |
| 738 | } |
| 739 | if ($email) { |
| 740 | $split_template[] = $email; |
| 741 | } |
| 742 | $this->preparedTemplates[$key]['template'] = $split_template; |
| 743 | $this->preparedTemplates[$key]['tokens'] = $tokens; |
| 744 | } |
| 745 | } |
| 746 | return ($this->preparedTemplates); |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * Retrieve a ref to an array that holds the email and text templates for this email |
| 751 | * assembles the complete template including the header and footer |
| 752 | * that the user has uploaded or declared (if they have dome that) |
| 753 | * |
| 754 | * @return array |
| 755 | * reference to an assoc array |
| 756 | */ |
| 757 | private function &getTemplates() { |
| 758 | if (!$this->templates) { |
| 759 | $this->getHeaderFooter(); |
| 760 | $this->templates = array(); |
| 761 | |
| 762 | if ($this->body_text) { |
| 763 | $template = array(); |
| 764 | if ($this->header) { |
| 765 | $template[] = $this->header->body_text; |
| 766 | } |
| 767 | |
| 768 | $template[] = $this->body_text; |
| 769 | |
| 770 | if ($this->footer) { |
| 771 | $template[] = $this->footer->body_text; |
| 772 | } |
| 773 | |
| 774 | $this->templates['text'] = implode("\n", $template); |
| 775 | } |
| 776 | |
| 777 | if ($this->body_html) { |
| 778 | |
| 779 | $template = array(); |
| 780 | if ($this->header) { |
| 781 | $template[] = $this->header->body_html; |
| 782 | } |
| 783 | |
| 784 | $template[] = $this->body_html; |
| 785 | |
| 786 | if ($this->footer) { |
| 787 | $template[] = $this->footer->body_html; |
| 788 | } |
| 789 | |
| 790 | $this->templates['html'] = implode("\n", $template); |
| 791 | |
| 792 | // this is where we create a text template from the html template if the text template did not exist |
| 793 | // this way we ensure that every recipient will receive an email even if the pref is set to text and the |
| 794 | // user uploads an html email only |
| 795 | if (!$this->body_text) { |
| 796 | $this->templates['text'] = CRM_Utils_String::htmlToText($this->templates['html']); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | if ($this->subject) { |
| 801 | $template = array(); |
| 802 | $template[] = $this->subject; |
| 803 | $this->templates['subject'] = implode("\n", $template); |
| 804 | } |
| 805 | } |
| 806 | return $this->templates; |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * |
| 811 | * Retrieve a ref to an array that holds all of the tokens in the email body |
| 812 | * where the keys are the type of token and the values are ordinal arrays |
| 813 | * that hold the token names (even repeated tokens) in the order in which |
| 814 | * they appear in the body of the email. |
| 815 | * |
| 816 | * note: the real work is done in the _getTokens() function |
| 817 | * |
| 818 | * this function needs to have some sort of a body assigned |
| 819 | * either text or html for this to have any meaningful impact |
| 820 | * |
| 821 | * @return array |
| 822 | * reference to an assoc array |
| 823 | */ |
| 824 | public function &getTokens() { |
| 825 | if (!$this->tokens) { |
| 826 | |
| 827 | $this->tokens = array('html' => array(), 'text' => array(), 'subject' => array()); |
| 828 | |
| 829 | if ($this->body_html) { |
| 830 | $this->_getTokens('html'); |
| 831 | if (!$this->body_text) { |
| 832 | // Since the text template was created from html, use the html tokens. |
| 833 | // @see CRM_Mailing_BAO_Mailing::getTemplates() |
| 834 | $this->tokens['text'] = $this->tokens['html']; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | if ($this->body_text) { |
| 839 | $this->_getTokens('text'); |
| 840 | } |
| 841 | |
| 842 | if ($this->subject) { |
| 843 | $this->_getTokens('subject'); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | return $this->tokens; |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * Returns the token set for all 3 parts as one set. This allows it to be sent to the |
| 852 | * hook in one call and standardizes it across other token workflows |
| 853 | * |
| 854 | * @return array |
| 855 | * reference to an assoc array |
| 856 | */ |
| 857 | public function &getFlattenedTokens() { |
| 858 | if (!$this->flattenedTokens) { |
| 859 | $tokens = $this->getTokens(); |
| 860 | |
| 861 | $this->flattenedTokens = CRM_Utils_Token::flattenTokens($tokens); |
| 862 | } |
| 863 | |
| 864 | return $this->flattenedTokens; |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * |
| 869 | * _getTokens parses out all of the tokens that have been |
| 870 | * included in the html and text bodies of the email |
| 871 | * we get the tokens and then separate them into an |
| 872 | * internal structure named tokens that has the same |
| 873 | * form as the static tokens property(?) of the CRM_Utils_Token class. |
| 874 | * The difference is that there might be repeated token names as we want the |
| 875 | * structures to represent the order in which tokens were found from left to right, top to bottom. |
| 876 | * |
| 877 | * |
| 878 | * @param string $prop name of the property that holds the text that we want to scan for tokens (html, text). |
| 879 | * Name of the property that holds the text that we want to scan for tokens (html, text). |
| 880 | * |
| 881 | * @return void |
| 882 | */ |
| 883 | private function _getTokens($prop) { |
| 884 | $templates = $this->getTemplates(); |
| 885 | |
| 886 | $newTokens = CRM_Utils_Token::getTokens($templates[$prop]); |
| 887 | |
| 888 | foreach ($newTokens as $type => $names) { |
| 889 | if (!isset($this->tokens[$prop][$type])) { |
| 890 | $this->tokens[$prop][$type] = array(); |
| 891 | } |
| 892 | foreach ($names as $key => $name) { |
| 893 | $this->tokens[$prop][$type][] = $name; |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * Generate an event queue for a test job. |
| 900 | * |
| 901 | * @param array $testParams |
| 902 | * Contains form values. |
| 903 | * |
| 904 | * @return void |
| 905 | */ |
| 906 | public function getTestRecipients($testParams) { |
| 907 | if (array_key_exists($testParams['test_group'], CRM_Core_PseudoConstant::group())) { |
| 908 | $contacts = civicrm_api('contact', 'get', array( |
| 909 | 'version' => 3, |
| 910 | 'group' => $testParams['test_group'], |
| 911 | 'return' => 'id', |
| 912 | 'options' => array( |
| 913 | 'limit' => 100000000000, |
| 914 | ), |
| 915 | ) |
| 916 | ); |
| 917 | |
| 918 | foreach (array_keys($contacts['values']) as $groupContact) { |
| 919 | $query = " |
| 920 | SELECT civicrm_email.id AS email_id, |
| 921 | civicrm_email.is_primary as is_primary, |
| 922 | civicrm_email.is_bulkmail as is_bulkmail |
| 923 | FROM civicrm_email |
| 924 | INNER JOIN civicrm_contact ON civicrm_email.contact_id = civicrm_contact.id |
| 925 | WHERE (civicrm_email.is_bulkmail = 1 OR civicrm_email.is_primary = 1) |
| 926 | AND civicrm_contact.id = {$groupContact} |
| 927 | AND civicrm_contact.do_not_email = 0 |
| 928 | AND civicrm_contact.is_deceased <> 1 |
| 929 | AND civicrm_email.on_hold = 0 |
| 930 | AND civicrm_contact.is_opt_out = 0 |
| 931 | GROUP BY civicrm_email.id |
| 932 | ORDER BY civicrm_email.is_bulkmail DESC |
| 933 | "; |
| 934 | $dao = CRM_Core_DAO::executeQuery($query); |
| 935 | if ($dao->fetch()) { |
| 936 | $params = array( |
| 937 | 'job_id' => $testParams['job_id'], |
| 938 | 'email_id' => $dao->email_id, |
| 939 | 'contact_id' => $groupContact, |
| 940 | ); |
| 941 | CRM_Mailing_Event_BAO_Queue::create($params); |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * Load this->header and this->footer. |
| 949 | */ |
| 950 | private function getHeaderFooter() { |
| 951 | if (!$this->header and $this->header_id) { |
| 952 | $this->header = new CRM_Mailing_BAO_Component(); |
| 953 | $this->header->id = $this->header_id; |
| 954 | $this->header->find(TRUE); |
| 955 | $this->header->free(); |
| 956 | } |
| 957 | |
| 958 | if (!$this->footer and $this->footer_id) { |
| 959 | $this->footer = new CRM_Mailing_BAO_Component(); |
| 960 | $this->footer->id = $this->footer_id; |
| 961 | $this->footer->find(TRUE); |
| 962 | $this->footer->free(); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * Given and array of headers and a prefix, job ID, event queue ID, and hash, |
| 968 | * add a Message-ID header if needed. |
| 969 | * |
| 970 | * i.e. if the global includeMessageId is set and there isn't already a |
| 971 | * Message-ID in the array. |
| 972 | * The message ID is structured the same way as a verp. However no interpretation |
| 973 | * is placed on the values received, so they do not need to follow the verp |
| 974 | * convention. |
| 975 | * |
| 976 | * @param array $headers |
| 977 | * Array of message headers to update, in-out. |
| 978 | * @param string $prefix |
| 979 | * Prefix for the message ID, use same prefixes as verp. |
| 980 | * wherever possible |
| 981 | * @param string $job_id |
| 982 | * Job ID component of the generated message ID. |
| 983 | * @param string $event_queue_id |
| 984 | * Event Queue ID component of the generated message ID. |
| 985 | * @param string $hash |
| 986 | * Hash component of the generated message ID. |
| 987 | * |
| 988 | * @return void |
| 989 | */ |
| 990 | public static function addMessageIdHeader(&$headers, $prefix, $job_id, $event_queue_id, $hash) { |
| 991 | $config = CRM_Core_Config::singleton(); |
| 992 | $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart(); |
| 993 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); |
| 994 | $includeMessageId = CRM_Core_BAO_MailSettings::includeMessageId(); |
| 995 | |
| 996 | if ($includeMessageId && (!array_key_exists('Message-ID', $headers))) { |
| 997 | $headers['Message-ID'] = '<' . implode($config->verpSeparator, |
| 998 | array( |
| 999 | $localpart . $prefix, |
| 1000 | $job_id, |
| 1001 | $event_queue_id, |
| 1002 | $hash, |
| 1003 | ) |
| 1004 | ) . "@{$emailDomain}>"; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Static wrapper for getting verp and urls. |
| 1010 | * |
| 1011 | * @param int $job_id |
| 1012 | * ID of the Job associated with this message. |
| 1013 | * @param int $event_queue_id |
| 1014 | * ID of the EventQueue. |
| 1015 | * @param string $hash |
| 1016 | * Hash of the EventQueue. |
| 1017 | * @param string $email |
| 1018 | * Destination address. |
| 1019 | * |
| 1020 | * @return array |
| 1021 | * (reference) array array ref that hold array refs to the verp info and urls |
| 1022 | */ |
| 1023 | public static function getVerpAndUrls($job_id, $event_queue_id, $hash, $email) { |
| 1024 | // create a skeleton object and set its properties that are required by getVerpAndUrlsAndHeaders() |
| 1025 | $config = CRM_Core_Config::singleton(); |
| 1026 | $bao = new CRM_Mailing_BAO_Mailing(); |
| 1027 | $bao->_domain = CRM_Core_BAO_Domain::getDomain(); |
| 1028 | $bao->from_name = $bao->from_email = $bao->subject = ''; |
| 1029 | |
| 1030 | // use $bao's instance method to get verp and urls |
| 1031 | list($verp, $urls, $_) = $bao->getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email); |
| 1032 | return array($verp, $urls); |
| 1033 | } |
| 1034 | |
| 1035 | /** |
| 1036 | * Get verp, urls and headers |
| 1037 | * |
| 1038 | * @param int $job_id |
| 1039 | * ID of the Job associated with this message. |
| 1040 | * @param int $event_queue_id |
| 1041 | * ID of the EventQueue. |
| 1042 | * @param string $hash |
| 1043 | * Hash of the EventQueue. |
| 1044 | * @param string $email |
| 1045 | * Destination address. |
| 1046 | * |
| 1047 | * @param bool $isForward |
| 1048 | * |
| 1049 | * @return array |
| 1050 | * array ref that hold array refs to the verp info, urls, and headers |
| 1051 | */ |
| 1052 | private function getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email, $isForward = FALSE) { |
| 1053 | $config = CRM_Core_Config::singleton(); |
| 1054 | |
| 1055 | /** |
| 1056 | * Inbound VERP keys: |
| 1057 | * reply: user replied to mailing |
| 1058 | * bounce: email address bounced |
| 1059 | * unsubscribe: contact opts out of all target lists for the mailing |
| 1060 | * resubscribe: contact opts back into all target lists for the mailing |
| 1061 | * optOut: contact unsubscribes from the domain |
| 1062 | */ |
| 1063 | $verp = array(); |
| 1064 | $verpTokens = array( |
| 1065 | 'reply' => 'r', |
| 1066 | 'bounce' => 'b', |
| 1067 | 'unsubscribe' => 'u', |
| 1068 | 'resubscribe' => 'e', |
| 1069 | 'optOut' => 'o', |
| 1070 | ); |
| 1071 | |
| 1072 | $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart(); |
| 1073 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); |
| 1074 | |
| 1075 | foreach ($verpTokens as $key => $value) { |
| 1076 | $verp[$key] = implode($config->verpSeparator, |
| 1077 | array( |
| 1078 | $localpart . $value, |
| 1079 | $job_id, |
| 1080 | $event_queue_id, |
| 1081 | $hash, |
| 1082 | ) |
| 1083 | ) . "@$emailDomain"; |
| 1084 | } |
| 1085 | |
| 1086 | //handle should override VERP address. |
| 1087 | $skipEncode = FALSE; |
| 1088 | |
| 1089 | if ($job_id && |
| 1090 | self::overrideVerp($job_id) |
| 1091 | ) { |
| 1092 | $verp['reply'] = "\"{$this->from_name}\" <{$this->from_email}>"; |
| 1093 | } |
| 1094 | |
| 1095 | $urls = array( |
| 1096 | 'forward' => CRM_Utils_System::url('civicrm/mailing/forward', |
| 1097 | "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", |
| 1098 | TRUE, NULL, TRUE, TRUE |
| 1099 | ), |
| 1100 | 'unsubscribeUrl' => CRM_Utils_System::url('civicrm/mailing/unsubscribe', |
| 1101 | "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", |
| 1102 | TRUE, NULL, TRUE, TRUE |
| 1103 | ), |
| 1104 | 'resubscribeUrl' => CRM_Utils_System::url('civicrm/mailing/resubscribe', |
| 1105 | "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", |
| 1106 | TRUE, NULL, TRUE, TRUE |
| 1107 | ), |
| 1108 | 'optOutUrl' => CRM_Utils_System::url('civicrm/mailing/optout', |
| 1109 | "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", |
| 1110 | TRUE, NULL, TRUE, TRUE |
| 1111 | ), |
| 1112 | 'subscribeUrl' => CRM_Utils_System::url('civicrm/mailing/subscribe', |
| 1113 | 'reset=1', |
| 1114 | TRUE, NULL, TRUE, TRUE |
| 1115 | ), |
| 1116 | ); |
| 1117 | |
| 1118 | $headers = array( |
| 1119 | 'Reply-To' => $verp['reply'], |
| 1120 | 'Return-Path' => $verp['bounce'], |
| 1121 | 'From' => "\"{$this->from_name}\" <{$this->from_email}>", |
| 1122 | 'Subject' => $this->subject, |
| 1123 | 'List-Unsubscribe' => "<mailto:{$verp['unsubscribe']}>", |
| 1124 | ); |
| 1125 | self::addMessageIdHeader($headers, 'm', $job_id, $event_queue_id, $hash); |
| 1126 | if ($isForward) { |
| 1127 | $headers['Subject'] = "[Fwd:{$this->subject}]"; |
| 1128 | } |
| 1129 | return array(&$verp, &$urls, &$headers); |
| 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * Compose a message. |
| 1134 | * |
| 1135 | * @param int $job_id |
| 1136 | * ID of the Job associated with this message. |
| 1137 | * @param int $event_queue_id |
| 1138 | * ID of the EventQueue. |
| 1139 | * @param string $hash |
| 1140 | * Hash of the EventQueue. |
| 1141 | * @param string $contactId |
| 1142 | * ID of the Contact. |
| 1143 | * @param string $email |
| 1144 | * Destination address. |
| 1145 | * @param string $recipient |
| 1146 | * To: of the recipient. |
| 1147 | * @param bool $test |
| 1148 | * Is this mailing a test?. |
| 1149 | * @param $contactDetails |
| 1150 | * @param $attachments |
| 1151 | * @param bool $isForward |
| 1152 | * Is this mailing compose for forward?. |
| 1153 | * @param string $fromEmail |
| 1154 | * Email address of who is forwardinf it. |
| 1155 | * |
| 1156 | * @param null $replyToEmail |
| 1157 | * |
| 1158 | * @return Mail_mime The mail object |
| 1159 | */ |
| 1160 | public function compose( |
| 1161 | $job_id, $event_queue_id, $hash, $contactId, |
| 1162 | $email, &$recipient, $test, |
| 1163 | $contactDetails, &$attachments, $isForward = FALSE, |
| 1164 | $fromEmail = NULL, $replyToEmail = NULL |
| 1165 | ) { |
| 1166 | $config = CRM_Core_Config::singleton(); |
| 1167 | $this->getTokens(); |
| 1168 | |
| 1169 | if ($this->_domain == NULL) { |
| 1170 | $this->_domain = CRM_Core_BAO_Domain::getDomain(); |
| 1171 | } |
| 1172 | |
| 1173 | list($verp, $urls, $headers) = $this->getVerpAndUrlsAndHeaders( |
| 1174 | $job_id, |
| 1175 | $event_queue_id, |
| 1176 | $hash, |
| 1177 | $email, |
| 1178 | $isForward |
| 1179 | ); |
| 1180 | |
| 1181 | //set from email who is forwarding it and not original one. |
| 1182 | if ($fromEmail) { |
| 1183 | unset($headers['From']); |
| 1184 | $headers['From'] = "<{$fromEmail}>"; |
| 1185 | } |
| 1186 | |
| 1187 | if ($replyToEmail && ($fromEmail != $replyToEmail)) { |
| 1188 | $headers['Reply-To'] = "{$replyToEmail}"; |
| 1189 | } |
| 1190 | |
| 1191 | if ($contactDetails) { |
| 1192 | $contact = $contactDetails; |
| 1193 | } |
| 1194 | elseif ($contactId === 0) { |
| 1195 | //anonymous user |
| 1196 | $contact = array(); |
| 1197 | CRM_Utils_Hook::tokenValues($contact, $contactId, $job_id); |
| 1198 | } |
| 1199 | else { |
| 1200 | $params = array(array('contact_id', '=', $contactId, 0, 0)); |
| 1201 | list($contact) = CRM_Contact_BAO_Query::apiQuery($params); |
| 1202 | |
| 1203 | //CRM-4524 |
| 1204 | $contact = reset($contact); |
| 1205 | |
| 1206 | if (!$contact || is_a($contact, 'CRM_Core_Error')) { |
| 1207 | CRM_Core_Error::debug_log_message(ts('CiviMail will not send email to a non-existent contact: %1', |
| 1208 | array(1 => $contactId) |
| 1209 | )); |
| 1210 | // setting this because function is called by reference |
| 1211 | //@todo test not calling function by reference |
| 1212 | $res = NULL; |
| 1213 | return $res; |
| 1214 | } |
| 1215 | |
| 1216 | // also call the hook to get contact details |
| 1217 | CRM_Utils_Hook::tokenValues($contact, $contactId, $job_id); |
| 1218 | } |
| 1219 | |
| 1220 | $pTemplates = $this->getPreparedTemplates(); |
| 1221 | $pEmails = array(); |
| 1222 | |
| 1223 | foreach ($pTemplates as $type => $pTemplate) { |
| 1224 | $html = ($type == 'html') ? TRUE : FALSE; |
| 1225 | $pEmails[$type] = array(); |
| 1226 | $pEmail = &$pEmails[$type]; |
| 1227 | $template = &$pTemplates[$type]['template']; |
| 1228 | $tokens = &$pTemplates[$type]['tokens']; |
| 1229 | $idx = 0; |
| 1230 | if (!empty($tokens)) { |
| 1231 | foreach ($tokens as $idx => $token) { |
| 1232 | $token_data = $this->getTokenData($token, $html, $contact, $verp, $urls, $event_queue_id); |
| 1233 | array_push($pEmail, $template[$idx]); |
| 1234 | array_push($pEmail, $token_data); |
| 1235 | } |
| 1236 | } |
| 1237 | else { |
| 1238 | array_push($pEmail, $template[$idx]); |
| 1239 | } |
| 1240 | |
| 1241 | if (isset($template[($idx + 1)])) { |
| 1242 | array_push($pEmail, $template[($idx + 1)]); |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | $html = NULL; |
| 1247 | if (isset($pEmails['html']) && is_array($pEmails['html']) && count($pEmails['html'])) { |
| 1248 | $html = &$pEmails['html']; |
| 1249 | } |
| 1250 | |
| 1251 | $text = NULL; |
| 1252 | if (isset($pEmails['text']) && is_array($pEmails['text']) && count($pEmails['text'])) { |
| 1253 | $text = &$pEmails['text']; |
| 1254 | } |
| 1255 | |
| 1256 | // push the tracking url on to the html email if necessary |
| 1257 | if ($this->open_tracking && $html) { |
| 1258 | array_push($html, "\n" . '<img src="' . $config->userFrameworkResourceURL . |
| 1259 | "extern/open.php?q=$event_queue_id\" width='1' height='1' alt='' border='0'>" |
| 1260 | ); |
| 1261 | } |
| 1262 | |
| 1263 | $message = new Mail_mime("\n"); |
| 1264 | |
| 1265 | $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY ? TRUE : FALSE; |
| 1266 | if ($useSmarty) { |
| 1267 | $smarty = CRM_Core_Smarty::singleton(); |
| 1268 | // also add the contact tokens to the template |
| 1269 | $smarty->assign_by_ref('contact', $contact); |
| 1270 | } |
| 1271 | |
| 1272 | $mailParams = $headers; |
| 1273 | if ($text && ($test || $contact['preferred_mail_format'] == 'Text' || |
| 1274 | $contact['preferred_mail_format'] == 'Both' || |
| 1275 | ($contact['preferred_mail_format'] == 'HTML' && !array_key_exists('html', $pEmails)) |
| 1276 | ) |
| 1277 | ) { |
| 1278 | $textBody = implode('', $text); |
| 1279 | if ($useSmarty) { |
| 1280 | $textBody = $smarty->fetch("string:$textBody"); |
| 1281 | } |
| 1282 | $mailParams['text'] = $textBody; |
| 1283 | } |
| 1284 | |
| 1285 | if ($html && ($test || ($contact['preferred_mail_format'] == 'HTML' || |
| 1286 | $contact['preferred_mail_format'] == 'Both' |
| 1287 | )) |
| 1288 | ) { |
| 1289 | $htmlBody = implode('', $html); |
| 1290 | if ($useSmarty) { |
| 1291 | $htmlBody = $smarty->fetch("string:$htmlBody"); |
| 1292 | } |
| 1293 | $mailParams['html'] = $htmlBody; |
| 1294 | } |
| 1295 | |
| 1296 | if (empty($mailParams['text']) && empty($mailParams['html'])) { |
| 1297 | // CRM-9833 |
| 1298 | // something went wrong, lets log it and return null (by reference) |
| 1299 | CRM_Core_Error::debug_log_message(ts('CiviMail will not send an empty mail body, Skipping: %1', |
| 1300 | array(1 => $email) |
| 1301 | )); |
| 1302 | $res = NULL; |
| 1303 | return $res; |
| 1304 | } |
| 1305 | |
| 1306 | $mailParams['attachments'] = $attachments; |
| 1307 | |
| 1308 | $mailingSubject = CRM_Utils_Array::value('subject', $pEmails); |
| 1309 | if (is_array($mailingSubject)) { |
| 1310 | $mailingSubject = implode('', $mailingSubject); |
| 1311 | } |
| 1312 | $mailParams['Subject'] = $mailingSubject; |
| 1313 | |
| 1314 | $mailParams['toName'] = CRM_Utils_Array::value('display_name', |
| 1315 | $contact |
| 1316 | ); |
| 1317 | $mailParams['toEmail'] = $email; |
| 1318 | |
| 1319 | // Add job ID to mailParams for external email delivery service to utilise |
| 1320 | $mailParams['job_id'] = $job_id; |
| 1321 | |
| 1322 | CRM_Utils_Hook::alterMailParams($mailParams, 'civimail'); |
| 1323 | |
| 1324 | // CRM-10699 support custom email headers |
| 1325 | if (!empty($mailParams['headers'])) { |
| 1326 | $headers = array_merge($headers, $mailParams['headers']); |
| 1327 | } |
| 1328 | //cycle through mailParams and set headers array |
| 1329 | foreach ($mailParams as $paramKey => $paramValue) { |
| 1330 | //exclude values not intended for the header |
| 1331 | if (!in_array($paramKey, array( |
| 1332 | 'text', |
| 1333 | 'html', |
| 1334 | 'attachments', |
| 1335 | 'toName', |
| 1336 | 'toEmail', |
| 1337 | )) |
| 1338 | ) { |
| 1339 | $headers[$paramKey] = $paramValue; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | if (!empty($mailParams['text'])) { |
| 1344 | $message->setTxtBody($mailParams['text']); |
| 1345 | } |
| 1346 | |
| 1347 | if (!empty($mailParams['html'])) { |
| 1348 | $message->setHTMLBody($mailParams['html']); |
| 1349 | } |
| 1350 | |
| 1351 | if (!empty($mailParams['attachments'])) { |
| 1352 | foreach ($mailParams['attachments'] as $fileID => $attach) { |
| 1353 | $message->addAttachment($attach['fullPath'], |
| 1354 | $attach['mime_type'], |
| 1355 | $attach['cleanName'] |
| 1356 | ); |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | //pickup both params from mail params. |
| 1361 | $toName = trim($mailParams['toName']); |
| 1362 | $toEmail = trim($mailParams['toEmail']); |
| 1363 | if ($toName == $toEmail || |
| 1364 | strpos($toName, '@') !== FALSE |
| 1365 | ) { |
| 1366 | $toName = NULL; |
| 1367 | } |
| 1368 | else { |
| 1369 | $toName = CRM_Utils_Mail::formatRFC2822Name($toName); |
| 1370 | } |
| 1371 | |
| 1372 | $headers['To'] = "$toName <$toEmail>"; |
| 1373 | |
| 1374 | $headers['Precedence'] = 'bulk'; |
| 1375 | // Will test in the mail processor if the X-VERP is set in the bounced email. |
| 1376 | // (As an option to replace real VERP for those that can't set it up) |
| 1377 | $headers['X-CiviMail-Bounce'] = $verp['bounce']; |
| 1378 | |
| 1379 | //CRM-5058 |
| 1380 | //token replacement of subject |
| 1381 | $headers['Subject'] = $mailingSubject; |
| 1382 | |
| 1383 | CRM_Utils_Mail::setMimeParams($message); |
| 1384 | $headers = $message->headers($headers); |
| 1385 | |
| 1386 | //get formatted recipient |
| 1387 | $recipient = $headers['To']; |
| 1388 | |
| 1389 | // make sure we unset a lot of stuff |
| 1390 | unset($verp); |
| 1391 | unset($urls); |
| 1392 | unset($params); |
| 1393 | unset($contact); |
| 1394 | unset($ids); |
| 1395 | |
| 1396 | return $message; |
| 1397 | } |
| 1398 | |
| 1399 | /** |
| 1400 | * |
| 1401 | * get mailing object and replaces subscribeInvite, |
| 1402 | * domain and mailing tokens |
| 1403 | */ |
| 1404 | public static function tokenReplace(&$mailing) { |
| 1405 | $domain = CRM_Core_BAO_Domain::getDomain(); |
| 1406 | |
| 1407 | foreach (array('text', 'html') as $type) { |
| 1408 | $tokens = $mailing->getTokens(); |
| 1409 | if (isset($mailing->templates[$type])) { |
| 1410 | $mailing->templates[$type] = CRM_Utils_Token::replaceSubscribeInviteTokens($mailing->templates[$type]); |
| 1411 | $mailing->templates[$type] = CRM_Utils_Token::replaceDomainTokens( |
| 1412 | $mailing->templates[$type], |
| 1413 | $domain, |
| 1414 | $type == 'html' ? TRUE : FALSE, |
| 1415 | $tokens[$type] |
| 1416 | ); |
| 1417 | $mailing->templates[$type] = CRM_Utils_Token::replaceMailingTokens($mailing->templates[$type], $mailing, NULL, $tokens[$type]); |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * |
| 1424 | * getTokenData receives a token from an email |
| 1425 | * and returns the appropriate data for the token |
| 1426 | */ |
| 1427 | private function getTokenData(&$token_a, $html = FALSE, &$contact, &$verp, &$urls, $event_queue_id) { |
| 1428 | $type = $token_a['type']; |
| 1429 | $token = $token_a['token']; |
| 1430 | $data = $token; |
| 1431 | |
| 1432 | $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY ? TRUE : FALSE; |
| 1433 | |
| 1434 | if ($type == 'embedded_url') { |
| 1435 | $embed_data = array(); |
| 1436 | foreach ($token as $t) { |
| 1437 | $embed_data[] = $this->getTokenData($t, $html = FALSE, $contact, $verp, $urls, $event_queue_id); |
| 1438 | } |
| 1439 | $numSlices = count($embed_data); |
| 1440 | $url = ''; |
| 1441 | for ($i = 0; $i < $numSlices; $i++) { |
| 1442 | $url .= "{$token_a['embed_parts'][$i]}{$embed_data[$i]}"; |
| 1443 | } |
| 1444 | if (isset($token_a['embed_parts'][$numSlices])) { |
| 1445 | $url .= $token_a['embed_parts'][$numSlices]; |
| 1446 | } |
| 1447 | // add trailing quote since we've gobbled it up in a previous regex |
| 1448 | // function getPatterns, line 431 |
| 1449 | if (preg_match('/^href[ ]*=[ ]*\'/', $url)) { |
| 1450 | $url .= "'"; |
| 1451 | } |
| 1452 | elseif (preg_match('/^href[ ]*=[ ]*\"/', $url)) { |
| 1453 | $url .= '"'; |
| 1454 | } |
| 1455 | $data = $url; |
| 1456 | } |
| 1457 | elseif ($type == 'url') { |
| 1458 | if ($this->url_tracking) { |
| 1459 | $data = CRM_Mailing_BAO_TrackableURL::getTrackerURL($token, $this->id, $event_queue_id); |
| 1460 | if (!empty($html)) { |
| 1461 | $data = htmlentities($data, ENT_NOQUOTES); |
| 1462 | } |
| 1463 | } |
| 1464 | else { |
| 1465 | $data = $token; |
| 1466 | } |
| 1467 | } |
| 1468 | elseif ($type == 'contact') { |
| 1469 | $data = CRM_Utils_Token::getContactTokenReplacement($token, $contact, FALSE, FALSE, $useSmarty); |
| 1470 | } |
| 1471 | elseif ($type == 'action') { |
| 1472 | $data = CRM_Utils_Token::getActionTokenReplacement($token, $verp, $urls, $html); |
| 1473 | } |
| 1474 | elseif ($type == 'domain') { |
| 1475 | $domain = CRM_Core_BAO_Domain::getDomain(); |
| 1476 | $data = CRM_Utils_Token::getDomainTokenReplacement($token, $domain, $html); |
| 1477 | } |
| 1478 | elseif ($type == 'mailing') { |
| 1479 | if ($token == 'name') { |
| 1480 | $data = $this->name; |
| 1481 | } |
| 1482 | elseif ($token == 'group') { |
| 1483 | $groups = $this->getGroupNames(); |
| 1484 | $data = implode(', ', $groups); |
| 1485 | } |
| 1486 | } |
| 1487 | else { |
| 1488 | $data = CRM_Utils_Array::value("{$type}.{$token}", $contact); |
| 1489 | } |
| 1490 | return $data; |
| 1491 | } |
| 1492 | |
| 1493 | /** |
| 1494 | * Return a list of group names for this mailing. Does not work with |
| 1495 | * prior-mailing targets. |
| 1496 | * |
| 1497 | * @return array |
| 1498 | * Names of groups receiving this mailing |
| 1499 | */ |
| 1500 | public function &getGroupNames() { |
| 1501 | if (!isset($this->id)) { |
| 1502 | return array(); |
| 1503 | } |
| 1504 | $mg = new CRM_Mailing_DAO_MailingGroup(); |
| 1505 | $mgtable = CRM_Mailing_DAO_MailingGroup::getTableName(); |
| 1506 | $group = CRM_Contact_BAO_Group::getTableName(); |
| 1507 | |
| 1508 | $mg->query("SELECT $group.title as name FROM $mgtable |
| 1509 | INNER JOIN $group ON $mgtable.entity_id = $group.id |
| 1510 | WHERE $mgtable.mailing_id = {$this->id} |
| 1511 | AND $mgtable.entity_table = '$group' |
| 1512 | AND $mgtable.group_type = 'Include' |
| 1513 | ORDER BY $group.name"); |
| 1514 | |
| 1515 | $groups = array(); |
| 1516 | while ($mg->fetch()) { |
| 1517 | $groups[] = $mg->name; |
| 1518 | } |
| 1519 | $mg->free(); |
| 1520 | return $groups; |
| 1521 | } |
| 1522 | |
| 1523 | /** |
| 1524 | * Add the mailings. |
| 1525 | * |
| 1526 | * @param array $params |
| 1527 | * Reference array contains the values submitted by the form. |
| 1528 | * @param array $ids |
| 1529 | * Reference array contains the id. |
| 1530 | * |
| 1531 | * |
| 1532 | * @return CRM_Mailing_DAO_Mailing |
| 1533 | */ |
| 1534 | public static function add(&$params, $ids = array()) { |
| 1535 | $id = CRM_Utils_Array::value('mailing_id', $ids, CRM_Utils_Array::value('id', $params)); |
| 1536 | |
| 1537 | if ($id) { |
| 1538 | CRM_Utils_Hook::pre('edit', 'Mailing', $id, $params); |
| 1539 | } |
| 1540 | else { |
| 1541 | CRM_Utils_Hook::pre('create', 'Mailing', NULL, $params); |
| 1542 | } |
| 1543 | |
| 1544 | $mailing = new static(); |
| 1545 | if ($id) { |
| 1546 | $mailing->id = $id; |
| 1547 | $mailing->find(TRUE); |
| 1548 | } |
| 1549 | $mailing->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); |
| 1550 | |
| 1551 | if (!isset($params['replyto_email']) && |
| 1552 | isset($params['from_email']) |
| 1553 | ) { |
| 1554 | $params['replyto_email'] = $params['from_email']; |
| 1555 | } |
| 1556 | |
| 1557 | $mailing->copyValues($params); |
| 1558 | |
| 1559 | $result = $mailing->save(); |
| 1560 | |
| 1561 | if (!empty($ids['mailing'])) { |
| 1562 | CRM_Utils_Hook::post('edit', 'Mailing', $mailing->id, $mailing); |
| 1563 | } |
| 1564 | else { |
| 1565 | CRM_Utils_Hook::post('create', 'Mailing', $mailing->id, $mailing); |
| 1566 | } |
| 1567 | |
| 1568 | return $result; |
| 1569 | } |
| 1570 | |
| 1571 | /** |
| 1572 | * Construct a new mailing object, along with job and mailing_group |
| 1573 | * objects, from the form values of the create mailing wizard. |
| 1574 | * |
| 1575 | * This function is a bit evil. It not only merges $params and saves |
| 1576 | * the mailing -- it also schedules the mailing and chooses the recipients. |
| 1577 | * Since it merges $params, it's also the only place to correctly trigger |
| 1578 | * multi-field validation. It should be broken up. |
| 1579 | * |
| 1580 | * In the mean time, use-cases which break under the weight of this |
| 1581 | * evil may find reprieve in these extra evil params: |
| 1582 | * |
| 1583 | * - _skip_evil_bao_auto_recipients_: bool |
| 1584 | * - _skip_evil_bao_auto_schedule_: bool |
| 1585 | * - _evil_bao_validator_: string|callable |
| 1586 | * |
| 1587 | * </twowrongsmakesaright> |
| 1588 | * |
| 1589 | * @params array $params |
| 1590 | * Form values. |
| 1591 | * |
| 1592 | * @param array $params |
| 1593 | * @param array $ids |
| 1594 | * |
| 1595 | * @return object |
| 1596 | * $mailing The new mailing object |
| 1597 | * @throws \Exception |
| 1598 | */ |
| 1599 | public static function create(&$params, $ids = array()) { |
| 1600 | // WTH $ids |
| 1601 | if (empty($ids) && isset($params['id'])) { |
| 1602 | $ids['mailing_id'] = $ids['id'] = $params['id']; |
| 1603 | } |
| 1604 | |
| 1605 | // CRM-12430 |
| 1606 | // Do the below only for an insert |
| 1607 | // for an update, we should not set the defaults |
| 1608 | if (!isset($ids['id']) && !isset($ids['mailing_id'])) { |
| 1609 | // Retrieve domain email and name for default sender |
| 1610 | $domain = civicrm_api( |
| 1611 | 'Domain', |
| 1612 | 'getsingle', |
| 1613 | array( |
| 1614 | 'version' => 3, |
| 1615 | 'current_domain' => 1, |
| 1616 | 'sequential' => 1, |
| 1617 | ) |
| 1618 | ); |
| 1619 | if (isset($domain['from_email'])) { |
| 1620 | $domain_email = $domain['from_email']; |
| 1621 | $domain_name = $domain['from_name']; |
| 1622 | } |
| 1623 | else { |
| 1624 | $domain_email = 'info@EXAMPLE.ORG'; |
| 1625 | $domain_name = 'EXAMPLE.ORG'; |
| 1626 | } |
| 1627 | if (!isset($params['created_id'])) { |
| 1628 | $session =& CRM_Core_Session::singleton(); |
| 1629 | $params['created_id'] = $session->get('userID'); |
| 1630 | } |
| 1631 | $defaults = array( |
| 1632 | // load the default config settings for each |
| 1633 | // eg reply_id, unsubscribe_id need to use |
| 1634 | // correct template IDs here |
| 1635 | 'override_verp' => TRUE, |
| 1636 | 'forward_replies' => FALSE, |
| 1637 | 'open_tracking' => TRUE, |
| 1638 | 'url_tracking' => TRUE, |
| 1639 | 'visibility' => 'Public Pages', |
| 1640 | 'replyto_email' => $domain_email, |
| 1641 | 'header_id' => CRM_Mailing_PseudoConstant::defaultComponent('header_id', ''), |
| 1642 | 'footer_id' => CRM_Mailing_PseudoConstant::defaultComponent('footer_id', ''), |
| 1643 | 'from_email' => $domain_email, |
| 1644 | 'from_name' => $domain_name, |
| 1645 | 'msg_template_id' => NULL, |
| 1646 | 'created_id' => $params['created_id'], |
| 1647 | 'approver_id' => NULL, |
| 1648 | 'auto_responder' => 0, |
| 1649 | 'created_date' => date('YmdHis'), |
| 1650 | 'scheduled_date' => NULL, |
| 1651 | 'approval_date' => NULL, |
| 1652 | ); |
| 1653 | |
| 1654 | // Get the default from email address, if not provided. |
| 1655 | if (empty($defaults['from_email'])) { |
| 1656 | $defaultAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); |
| 1657 | foreach ($defaultAddress as $id => $value) { |
| 1658 | if (preg_match('/"(.*)" <(.*)>/', $value, $match)) { |
| 1659 | $defaults['from_email'] = $match[2]; |
| 1660 | $defaults['from_name'] = $match[1]; |
| 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | $params = array_merge($defaults, $params); |
| 1666 | } |
| 1667 | |
| 1668 | /** |
| 1669 | * Could check and warn for the following cases: |
| 1670 | * |
| 1671 | * - groups OR mailings should be populated. |
| 1672 | * - body html OR body text should be populated. |
| 1673 | */ |
| 1674 | |
| 1675 | $transaction = new CRM_Core_Transaction(); |
| 1676 | |
| 1677 | $mailing = self::add($params, $ids); |
| 1678 | |
| 1679 | if (is_a($mailing, 'CRM_Core_Error')) { |
| 1680 | $transaction->rollback(); |
| 1681 | return $mailing; |
| 1682 | } |
| 1683 | // update mailings with hash values |
| 1684 | CRM_Contact_BAO_Contact_Utils::generateChecksum($mailing->id, NULL, NULL, NULL, 'mailing', 16); |
| 1685 | |
| 1686 | $groupTableName = CRM_Contact_BAO_Group::getTableName(); |
| 1687 | $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName(); |
| 1688 | |
| 1689 | /* Create the mailing group record */ |
| 1690 | $mg = new CRM_Mailing_DAO_MailingGroup(); |
| 1691 | $groupTypes = array('include' => 'Include', 'exclude' => 'Exclude', 'base' => 'Base'); |
| 1692 | foreach (array('groups', 'mailings') as $entity) { |
| 1693 | foreach (array('include', 'exclude', 'base') as $type) { |
| 1694 | if (isset($params[$entity][$type])) { |
| 1695 | self::replaceGroups($mailing->id, $groupTypes[$type], $entity, $params[$entity][$type]); |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | if (!empty($params['search_id']) && !empty($params['group_id'])) { |
| 1701 | $mg->reset(); |
| 1702 | $mg->mailing_id = $mailing->id; |
| 1703 | $mg->entity_table = $groupTableName; |
| 1704 | $mg->entity_id = $params['group_id']; |
| 1705 | $mg->search_id = $params['search_id']; |
| 1706 | $mg->search_args = $params['search_args']; |
| 1707 | $mg->group_type = 'Include'; |
| 1708 | $mg->save(); |
| 1709 | } |
| 1710 | |
| 1711 | // check and attach and files as needed |
| 1712 | CRM_Core_BAO_File::processAttachment($params, 'civicrm_mailing', $mailing->id); |
| 1713 | |
| 1714 | // If we're going to autosend, then check validity before saving. |
| 1715 | if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && !empty($params['_evil_bao_validator_'])) { |
| 1716 | $cb = Civi\Core\Resolver::singleton()->get($params['_evil_bao_validator_']); |
| 1717 | $errors = call_user_func($cb, $mailing); |
| 1718 | if (!empty($errors)) { |
| 1719 | $fields = implode(',', array_keys($errors)); |
| 1720 | throw new CRM_Core_Exception("Mailing cannot be sent. There are missing or invalid fields ($fields).", 'cannot-send', $errors); |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | $transaction->commit(); |
| 1725 | |
| 1726 | // Create parent job if not yet created. |
| 1727 | // Condition on the existence of a scheduled date. |
| 1728 | if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && empty($params['_skip_evil_bao_auto_schedule_'])) { |
| 1729 | $job = new CRM_Mailing_BAO_MailingJob(); |
| 1730 | $job->mailing_id = $mailing->id; |
| 1731 | $job->status = 'Scheduled'; |
| 1732 | $job->is_test = 0; |
| 1733 | |
| 1734 | if (!$job->find(TRUE)) { |
| 1735 | $job->scheduled_date = $params['scheduled_date']; |
| 1736 | $job->save(); |
| 1737 | } |
| 1738 | |
| 1739 | // Populate the recipients. |
| 1740 | if (empty($params['_skip_evil_bao_auto_recipients_'])) { |
| 1741 | self::getRecipients($job->id, $mailing->id, TRUE, $mailing->dedupe_email); |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | return $mailing; |
| 1746 | } |
| 1747 | |
| 1748 | /** |
| 1749 | * @param CRM_Mailing_DAO_Mailing $mailing |
| 1750 | * The mailing which may or may not be sendable. |
| 1751 | * @return array |
| 1752 | * List of error messages. |
| 1753 | */ |
| 1754 | public static function checkSendable($mailing) { |
| 1755 | $errors = array(); |
| 1756 | foreach (array('subject', 'name', 'from_name', 'from_email') as $field) { |
| 1757 | if (empty($mailing->{$field})) { |
| 1758 | $errors[$field] = ts('Field "%1" is required.', array( |
| 1759 | 1 => $field, |
| 1760 | )); |
| 1761 | } |
| 1762 | } |
| 1763 | if (empty($mailing->body_html) && empty($mailing->body_text)) { |
| 1764 | $errors['body'] = ts('Field "body_html" or "body_text" is required.'); |
| 1765 | } |
| 1766 | |
| 1767 | if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'disable_mandatory_tokens_check')) { |
| 1768 | $header = $mailing->header_id && $mailing->header_id != 'null' ? CRM_Mailing_BAO_Component::findById($mailing->header_id) : NULL; |
| 1769 | $footer = $mailing->footer_id && $mailing->footer_id != 'null' ? CRM_Mailing_BAO_Component::findById($mailing->footer_id) : NULL; |
| 1770 | foreach (array('body_html', 'body_text') as $field) { |
| 1771 | if (empty($mailing->{$field})) { |
| 1772 | continue; |
| 1773 | } |
| 1774 | $str = ($header ? $header->{$field} : '') . $mailing->{$field} . ($footer ? $footer->{$field} : ''); |
| 1775 | $err = CRM_Utils_Token::requiredTokens($str); |
| 1776 | if ($err !== TRUE) { |
| 1777 | foreach ($err as $token => $desc) { |
| 1778 | $errors["{$field}:{$token}"] = ts('This message is missing a required token - {%1}: %2', |
| 1779 | array(1 => $token, 2 => $desc) |
| 1780 | ); |
| 1781 | } |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | return $errors; |
| 1787 | } |
| 1788 | |
| 1789 | /** |
| 1790 | * Replace the list of recipients on a given mailing. |
| 1791 | * |
| 1792 | * @param int $mailingId |
| 1793 | * @param string $type |
| 1794 | * 'include' or 'exclude'. |
| 1795 | * @param string $entity |
| 1796 | * 'groups' or 'mailings'. |
| 1797 | * @param array <int> $entityIds |
| 1798 | * @throws CiviCRM_API3_Exception |
| 1799 | */ |
| 1800 | public static function replaceGroups($mailingId, $type, $entity, $entityIds) { |
| 1801 | $values = array(); |
| 1802 | foreach ($entityIds as $entityId) { |
| 1803 | $values[] = array('entity_id' => $entityId); |
| 1804 | } |
| 1805 | civicrm_api3('mailing_group', 'replace', array( |
| 1806 | 'mailing_id' => $mailingId, |
| 1807 | 'group_type' => $type, |
| 1808 | 'entity_table' => ($entity == 'groups') ? CRM_Contact_BAO_Group::getTableName() : CRM_Mailing_BAO_Mailing::getTableName(), |
| 1809 | 'values' => $values, |
| 1810 | )); |
| 1811 | } |
| 1812 | |
| 1813 | /** |
| 1814 | * Get hash value of the mailing. |
| 1815 | * |
| 1816 | * @param $id |
| 1817 | * |
| 1818 | * @return null|string |
| 1819 | */ |
| 1820 | public static function getMailingHash($id) { |
| 1821 | $hash = NULL; |
| 1822 | if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'hash_mailing_url')) { |
| 1823 | $hash = CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $id, 'hash', 'id'); |
| 1824 | } |
| 1825 | return $hash; |
| 1826 | } |
| 1827 | |
| 1828 | /** |
| 1829 | * Generate a report. Fetch event count information, mailing data, and job |
| 1830 | * status. |
| 1831 | * |
| 1832 | * @param int $id |
| 1833 | * The mailing id to report. |
| 1834 | * @param bool $skipDetails |
| 1835 | * Whether return all detailed report. |
| 1836 | * |
| 1837 | * @param bool $isSMS |
| 1838 | * |
| 1839 | * @return array |
| 1840 | * Associative array of reporting data |
| 1841 | */ |
| 1842 | public static function &report($id, $skipDetails = FALSE, $isSMS = FALSE) { |
| 1843 | $mailing_id = CRM_Utils_Type::escape($id, 'Integer'); |
| 1844 | |
| 1845 | $mailing = new CRM_Mailing_BAO_Mailing(); |
| 1846 | |
| 1847 | $t = array( |
| 1848 | 'mailing' => self::getTableName(), |
| 1849 | 'mailing_group' => CRM_Mailing_DAO_MailingGroup::getTableName(), |
| 1850 | 'group' => CRM_Contact_BAO_Group::getTableName(), |
| 1851 | 'job' => CRM_Mailing_BAO_MailingJob::getTableName(), |
| 1852 | 'queue' => CRM_Mailing_Event_BAO_Queue::getTableName(), |
| 1853 | 'delivered' => CRM_Mailing_Event_BAO_Delivered::getTableName(), |
| 1854 | 'opened' => CRM_Mailing_Event_BAO_Opened::getTableName(), |
| 1855 | 'reply' => CRM_Mailing_Event_BAO_Reply::getTableName(), |
| 1856 | 'unsubscribe' => CRM_Mailing_Event_BAO_Unsubscribe::getTableName(), |
| 1857 | 'bounce' => CRM_Mailing_Event_BAO_Bounce::getTableName(), |
| 1858 | 'forward' => CRM_Mailing_Event_BAO_Forward::getTableName(), |
| 1859 | 'url' => CRM_Mailing_BAO_TrackableURL::getTableName(), |
| 1860 | 'urlopen' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTableName(), |
| 1861 | 'component' => CRM_Mailing_BAO_Component::getTableName(), |
| 1862 | 'spool' => CRM_Mailing_BAO_Spool::getTableName(), |
| 1863 | ); |
| 1864 | |
| 1865 | $report = array(); |
| 1866 | $additionalWhereClause = " AND "; |
| 1867 | if (!$isSMS) { |
| 1868 | $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NULL "; |
| 1869 | } |
| 1870 | else { |
| 1871 | $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NOT NULL "; |
| 1872 | } |
| 1873 | |
| 1874 | /* Get the mailing info */ |
| 1875 | |
| 1876 | $mailing->query(" |
| 1877 | SELECT {$t['mailing']}.* |
| 1878 | FROM {$t['mailing']} |
| 1879 | WHERE {$t['mailing']}.id = $mailing_id {$additionalWhereClause}"); |
| 1880 | |
| 1881 | $mailing->fetch(); |
| 1882 | |
| 1883 | $report['mailing'] = array(); |
| 1884 | foreach (array_keys(self::fields()) as $field) { |
| 1885 | $report['mailing'][$field] = $mailing->$field; |
| 1886 | } |
| 1887 | |
| 1888 | //get the campaign |
| 1889 | if ($campaignId = CRM_Utils_Array::value('campaign_id', $report['mailing'])) { |
| 1890 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId); |
| 1891 | $report['mailing']['campaign'] = $campaigns[$campaignId]; |
| 1892 | } |
| 1893 | |
| 1894 | //mailing report is called by activity |
| 1895 | //we dont need all detail report |
| 1896 | if ($skipDetails) { |
| 1897 | return $report; |
| 1898 | } |
| 1899 | |
| 1900 | /* Get the component info */ |
| 1901 | |
| 1902 | $query = array(); |
| 1903 | |
| 1904 | $components = array( |
| 1905 | 'header' => ts('Header'), |
| 1906 | 'footer' => ts('Footer'), |
| 1907 | 'reply' => ts('Reply'), |
| 1908 | 'unsubscribe' => ts('Unsubscribe'), |
| 1909 | 'optout' => ts('Opt-Out'), |
| 1910 | ); |
| 1911 | foreach (array_keys($components) as $type) { |
| 1912 | $query[] = "SELECT {$t['component']}.name as name, |
| 1913 | '$type' as type, |
| 1914 | {$t['component']}.id as id |
| 1915 | FROM {$t['component']} |
| 1916 | INNER JOIN {$t['mailing']} |
| 1917 | ON {$t['mailing']}.{$type}_id = |
| 1918 | {$t['component']}.id |
| 1919 | WHERE {$t['mailing']}.id = $mailing_id"; |
| 1920 | } |
| 1921 | $q = '(' . implode(') UNION (', $query) . ')'; |
| 1922 | $mailing->query($q); |
| 1923 | |
| 1924 | $report['component'] = array(); |
| 1925 | while ($mailing->fetch()) { |
| 1926 | $report['component'][] = array( |
| 1927 | 'type' => $components[$mailing->type], |
| 1928 | 'name' => $mailing->name, |
| 1929 | 'link' => CRM_Utils_System::url('civicrm/mailing/component', |
| 1930 | "reset=1&action=update&id={$mailing->id}" |
| 1931 | ), |
| 1932 | ); |
| 1933 | } |
| 1934 | |
| 1935 | /* Get the recipient group info */ |
| 1936 | |
| 1937 | $mailing->query(" |
| 1938 | SELECT {$t['mailing_group']}.group_type as group_type, |
| 1939 | {$t['group']}.id as group_id, |
| 1940 | {$t['group']}.title as group_title, |
| 1941 | {$t['group']}.is_hidden as group_hidden, |
| 1942 | {$t['mailing']}.id as mailing_id, |
| 1943 | {$t['mailing']}.name as mailing_name |
| 1944 | FROM {$t['mailing_group']} |
| 1945 | LEFT JOIN {$t['group']} |
| 1946 | ON {$t['mailing_group']}.entity_id = {$t['group']}.id |
| 1947 | AND {$t['mailing_group']}.entity_table = |
| 1948 | '{$t['group']}' |
| 1949 | LEFT JOIN {$t['mailing']} |
| 1950 | ON {$t['mailing_group']}.entity_id = |
| 1951 | {$t['mailing']}.id |
| 1952 | AND {$t['mailing_group']}.entity_table = |
| 1953 | '{$t['mailing']}' |
| 1954 | |
| 1955 | WHERE {$t['mailing_group']}.mailing_id = $mailing_id |
| 1956 | "); |
| 1957 | |
| 1958 | $report['group'] = array('include' => array(), 'exclude' => array(), 'base' => array()); |
| 1959 | while ($mailing->fetch()) { |
| 1960 | $row = array(); |
| 1961 | if (isset($mailing->group_id)) { |
| 1962 | $row['id'] = $mailing->group_id; |
| 1963 | $row['name'] = $mailing->group_title; |
| 1964 | $row['link'] = CRM_Utils_System::url('civicrm/group/search', |
| 1965 | "reset=1&force=1&context=smog&gid={$row['id']}" |
| 1966 | ); |
| 1967 | } |
| 1968 | else { |
| 1969 | $row['id'] = $mailing->mailing_id; |
| 1970 | $row['name'] = $mailing->mailing_name; |
| 1971 | $row['mailing'] = TRUE; |
| 1972 | $row['link'] = CRM_Utils_System::url('civicrm/mailing/report', |
| 1973 | "mid={$row['id']}" |
| 1974 | ); |
| 1975 | } |
| 1976 | |
| 1977 | /* Rename hidden groups */ |
| 1978 | |
| 1979 | if ($mailing->group_hidden == 1) { |
| 1980 | $row['name'] = "Search Results"; |
| 1981 | } |
| 1982 | |
| 1983 | if ($mailing->group_type == 'Include') { |
| 1984 | $report['group']['include'][] = $row; |
| 1985 | } |
| 1986 | elseif ($mailing->group_type == 'Base') { |
| 1987 | $report['group']['base'][] = $row; |
| 1988 | } |
| 1989 | else { |
| 1990 | $report['group']['exclude'][] = $row; |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | /* Get the event totals, grouped by job (retries) */ |
| 1995 | |
| 1996 | $mailing->query(" |
| 1997 | SELECT {$t['job']}.*, |
| 1998 | COUNT(DISTINCT {$t['queue']}.id) as queue, |
| 1999 | COUNT(DISTINCT {$t['delivered']}.id) as delivered, |
| 2000 | COUNT(DISTINCT {$t['reply']}.id) as reply, |
| 2001 | COUNT(DISTINCT {$t['forward']}.id) as forward, |
| 2002 | COUNT(DISTINCT {$t['bounce']}.id) as bounce, |
| 2003 | COUNT(DISTINCT {$t['urlopen']}.id) as url, |
| 2004 | COUNT(DISTINCT {$t['spool']}.id) as spool |
| 2005 | FROM {$t['job']} |
| 2006 | LEFT JOIN {$t['queue']} |
| 2007 | ON {$t['queue']}.job_id = {$t['job']}.id |
| 2008 | LEFT JOIN {$t['reply']} |
| 2009 | ON {$t['reply']}.event_queue_id = {$t['queue']}.id |
| 2010 | LEFT JOIN {$t['forward']} |
| 2011 | ON {$t['forward']}.event_queue_id = {$t['queue']}.id |
| 2012 | LEFT JOIN {$t['bounce']} |
| 2013 | ON {$t['bounce']}.event_queue_id = {$t['queue']}.id |
| 2014 | LEFT JOIN {$t['delivered']} |
| 2015 | ON {$t['delivered']}.event_queue_id = {$t['queue']}.id |
| 2016 | AND {$t['bounce']}.id IS null |
| 2017 | LEFT JOIN {$t['urlopen']} |
| 2018 | ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id |
| 2019 | LEFT JOIN {$t['spool']} |
| 2020 | ON {$t['spool']}.job_id = {$t['job']}.id |
| 2021 | WHERE {$t['job']}.mailing_id = $mailing_id |
| 2022 | AND {$t['job']}.is_test = 0 |
| 2023 | GROUP BY {$t['job']}.id"); |
| 2024 | |
| 2025 | $report['jobs'] = array(); |
| 2026 | $report['event_totals'] = array(); |
| 2027 | $elements = array( |
| 2028 | 'queue', |
| 2029 | 'delivered', |
| 2030 | 'url', |
| 2031 | 'forward', |
| 2032 | 'reply', |
| 2033 | 'unsubscribe', |
| 2034 | 'optout', |
| 2035 | 'opened', |
| 2036 | 'total_opened', |
| 2037 | 'bounce', |
| 2038 | 'spool', |
| 2039 | ); |
| 2040 | |
| 2041 | // initialize various counters |
| 2042 | foreach ($elements as $field) { |
| 2043 | $report['event_totals'][$field] = 0; |
| 2044 | } |
| 2045 | |
| 2046 | while ($mailing->fetch()) { |
| 2047 | $row = array(); |
| 2048 | foreach ($elements as $field) { |
| 2049 | if (isset($mailing->$field)) { |
| 2050 | $row[$field] = $mailing->$field; |
| 2051 | $report['event_totals'][$field] += $mailing->$field; |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | // compute open total separately to discount duplicates |
| 2056 | // CRM-1258 |
| 2057 | $row['opened'] = CRM_Mailing_Event_BAO_Opened::getTotalCount($mailing_id, $mailing->id, TRUE); |
| 2058 | $report['event_totals']['opened'] += $row['opened']; |
| 2059 | $row['total_opened'] = CRM_Mailing_Event_BAO_Opened::getTotalCount($mailing_id, $mailing->id); |
| 2060 | $report['event_totals']['total_opened'] += $row['total_opened']; |
| 2061 | |
| 2062 | // compute unsub total separately to discount duplicates |
| 2063 | // CRM-1783 |
| 2064 | $row['unsubscribe'] = CRM_Mailing_Event_BAO_Unsubscribe::getTotalCount($mailing_id, $mailing->id, TRUE, TRUE); |
| 2065 | $report['event_totals']['unsubscribe'] += $row['unsubscribe']; |
| 2066 | |
| 2067 | $row['optout'] = CRM_Mailing_Event_BAO_Unsubscribe::getTotalCount($mailing_id, $mailing->id, TRUE, FALSE); |
| 2068 | $report['event_totals']['optout'] += $row['optout']; |
| 2069 | |
| 2070 | foreach (array_keys(CRM_Mailing_BAO_MailingJob::fields()) as $field) { |
| 2071 | $row[$field] = $mailing->$field; |
| 2072 | } |
| 2073 | |
| 2074 | if ($mailing->queue) { |
| 2075 | $row['delivered_rate'] = (100.0 * $mailing->delivered) / $mailing->queue; |
| 2076 | $row['bounce_rate'] = (100.0 * $mailing->bounce) / $mailing->queue; |
| 2077 | $row['unsubscribe_rate'] = (100.0 * $row['unsubscribe']) / $mailing->queue; |
| 2078 | $row['optout_rate'] = (100.0 * $row['optout']) / $mailing->queue; |
| 2079 | } |
| 2080 | else { |
| 2081 | $row['delivered_rate'] = 0; |
| 2082 | $row['bounce_rate'] = 0; |
| 2083 | $row['unsubscribe_rate'] = 0; |
| 2084 | $row['optout_rate'] = 0; |
| 2085 | } |
| 2086 | |
| 2087 | $row['links'] = array( |
| 2088 | 'clicks' => CRM_Utils_System::url( |
| 2089 | 'civicrm/mailing/report/event', |
| 2090 | "reset=1&event=click&mid=$mailing_id&jid={$mailing->id}" |
| 2091 | ), |
| 2092 | 'queue' => CRM_Utils_System::url( |
| 2093 | 'civicrm/mailing/report/event', |
| 2094 | "reset=1&event=queue&mid=$mailing_id&jid={$mailing->id}" |
| 2095 | ), |
| 2096 | 'delivered' => CRM_Utils_System::url( |
| 2097 | 'civicrm/mailing/report/event', |
| 2098 | "reset=1&event=delivered&mid=$mailing_id&jid={$mailing->id}" |
| 2099 | ), |
| 2100 | 'bounce' => CRM_Utils_System::url( |
| 2101 | 'civicrm/mailing/report/event', |
| 2102 | "reset=1&event=bounce&mid=$mailing_id&jid={$mailing->id}" |
| 2103 | ), |
| 2104 | 'unsubscribe' => CRM_Utils_System::url( |
| 2105 | 'civicrm/mailing/report/event', |
| 2106 | "reset=1&event=unsubscribe&mid=$mailing_id&jid={$mailing->id}" |
| 2107 | ), |
| 2108 | 'forward' => CRM_Utils_System::url( |
| 2109 | 'civicrm/mailing/report/event', |
| 2110 | "reset=1&event=forward&mid=$mailing_id&jid={$mailing->id}" |
| 2111 | ), |
| 2112 | 'reply' => CRM_Utils_System::url( |
| 2113 | 'civicrm/mailing/report/event', |
| 2114 | "reset=1&event=reply&mid=$mailing_id&jid={$mailing->id}" |
| 2115 | ), |
| 2116 | 'opened' => CRM_Utils_System::url( |
| 2117 | 'civicrm/mailing/report/event', |
| 2118 | "reset=1&event=opened&mid=$mailing_id&jid={$mailing->id}" |
| 2119 | ), |
| 2120 | ); |
| 2121 | |
| 2122 | foreach (array( |
| 2123 | 'scheduled_date', |
| 2124 | 'start_date', |
| 2125 | 'end_date', |
| 2126 | ) as $key) { |
| 2127 | $row[$key] = CRM_Utils_Date::customFormat($row[$key]); |
| 2128 | } |
| 2129 | $report['jobs'][] = $row; |
| 2130 | } |
| 2131 | |
| 2132 | $newTableSize = CRM_Mailing_BAO_Recipients::mailingSize($mailing_id); |
| 2133 | |
| 2134 | // we need to do this for backward compatibility, since old mailings did not |
| 2135 | // use the mailing_recipients table |
| 2136 | if ($newTableSize > 0) { |
| 2137 | $report['event_totals']['queue'] = $newTableSize; |
| 2138 | } |
| 2139 | else { |
| 2140 | $report['event_totals']['queue'] = self::getRecipientsCount($mailing_id, $mailing_id); |
| 2141 | } |
| 2142 | |
| 2143 | if (!empty($report['event_totals']['queue'])) { |
| 2144 | $report['event_totals']['delivered_rate'] = (100.0 * $report['event_totals']['delivered']) / $report['event_totals']['queue']; |
| 2145 | $report['event_totals']['bounce_rate'] = (100.0 * $report['event_totals']['bounce']) / $report['event_totals']['queue']; |
| 2146 | $report['event_totals']['unsubscribe_rate'] = (100.0 * $report['event_totals']['unsubscribe']) / $report['event_totals']['queue']; |
| 2147 | $report['event_totals']['optout_rate'] = (100.0 * $report['event_totals']['optout']) / $report['event_totals']['queue']; |
| 2148 | } |
| 2149 | else { |
| 2150 | $report['event_totals']['delivered_rate'] = 0; |
| 2151 | $report['event_totals']['bounce_rate'] = 0; |
| 2152 | $report['event_totals']['unsubscribe_rate'] = 0; |
| 2153 | $report['event_totals']['optout_rate'] = 0; |
| 2154 | } |
| 2155 | |
| 2156 | /* Get the click-through totals, grouped by URL */ |
| 2157 | |
| 2158 | $mailing->query(" |
| 2159 | SELECT {$t['url']}.url, |
| 2160 | {$t['url']}.id, |
| 2161 | COUNT({$t['urlopen']}.id) as clicks, |
| 2162 | COUNT(DISTINCT {$t['queue']}.id) as unique_clicks |
| 2163 | FROM {$t['url']} |
| 2164 | LEFT JOIN {$t['urlopen']} |
| 2165 | ON {$t['urlopen']}.trackable_url_id = {$t['url']}.id |
| 2166 | LEFT JOIN {$t['queue']} |
| 2167 | ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id |
| 2168 | LEFT JOIN {$t['job']} |
| 2169 | ON {$t['queue']}.job_id = {$t['job']}.id |
| 2170 | WHERE {$t['url']}.mailing_id = $mailing_id |
| 2171 | AND {$t['job']}.is_test = 0 |
| 2172 | GROUP BY {$t['url']}.id"); |
| 2173 | |
| 2174 | $report['click_through'] = array(); |
| 2175 | |
| 2176 | while ($mailing->fetch()) { |
| 2177 | $report['click_through'][] = array( |
| 2178 | 'url' => $mailing->url, |
| 2179 | 'link' => CRM_Utils_System::url( |
| 2180 | 'civicrm/mailing/report/event', |
| 2181 | "reset=1&event=click&mid=$mailing_id&uid={$mailing->id}" |
| 2182 | ), |
| 2183 | 'link_unique' => CRM_Utils_System::url( |
| 2184 | 'civicrm/mailing/report/event', |
| 2185 | "reset=1&event=click&mid=$mailing_id&uid={$mailing->id}&distinct=1" |
| 2186 | ), |
| 2187 | 'clicks' => $mailing->clicks, |
| 2188 | 'unique' => $mailing->unique_clicks, |
| 2189 | 'rate' => CRM_Utils_Array::value('delivered', $report['event_totals']) ? (100.0 * $mailing->unique_clicks) / $report['event_totals']['delivered'] : 0, |
| 2190 | ); |
| 2191 | } |
| 2192 | |
| 2193 | $report['event_totals']['links'] = array( |
| 2194 | 'clicks' => CRM_Utils_System::url( |
| 2195 | 'civicrm/mailing/report/event', |
| 2196 | "reset=1&event=click&mid=$mailing_id" |
| 2197 | ), |
| 2198 | 'clicks_unique' => CRM_Utils_System::url( |
| 2199 | 'civicrm/mailing/report/event', |
| 2200 | "reset=1&event=click&mid=$mailing_id&distinct=1" |
| 2201 | ), |
| 2202 | 'queue' => CRM_Utils_System::url( |
| 2203 | 'civicrm/mailing/report/event', |
| 2204 | "reset=1&event=queue&mid=$mailing_id" |
| 2205 | ), |
| 2206 | 'delivered' => CRM_Utils_System::url( |
| 2207 | 'civicrm/mailing/report/event', |
| 2208 | "reset=1&event=delivered&mid=$mailing_id" |
| 2209 | ), |
| 2210 | 'bounce' => CRM_Utils_System::url( |
| 2211 | 'civicrm/mailing/report/event', |
| 2212 | "reset=1&event=bounce&mid=$mailing_id" |
| 2213 | ), |
| 2214 | 'unsubscribe' => CRM_Utils_System::url( |
| 2215 | 'civicrm/mailing/report/event', |
| 2216 | "reset=1&event=unsubscribe&mid=$mailing_id" |
| 2217 | ), |
| 2218 | 'optout' => CRM_Utils_System::url( |
| 2219 | 'civicrm/mailing/report/event', |
| 2220 | "reset=1&event=optout&mid=$mailing_id" |
| 2221 | ), |
| 2222 | 'forward' => CRM_Utils_System::url( |
| 2223 | 'civicrm/mailing/report/event', |
| 2224 | "reset=1&event=forward&mid=$mailing_id" |
| 2225 | ), |
| 2226 | 'reply' => CRM_Utils_System::url( |
| 2227 | 'civicrm/mailing/report/event', |
| 2228 | "reset=1&event=reply&mid=$mailing_id" |
| 2229 | ), |
| 2230 | 'opened' => CRM_Utils_System::url( |
| 2231 | 'civicrm/mailing/report/event', |
| 2232 | "reset=1&event=opened&mid=$mailing_id" |
| 2233 | ), |
| 2234 | ); |
| 2235 | |
| 2236 | $actionLinks = array(CRM_Core_Action::VIEW => array('name' => ts('Report'))); |
| 2237 | if (CRM_Core_Permission::check('view all contacts')) { |
| 2238 | $actionLinks[CRM_Core_Action::ADVANCED] = array( |
| 2239 | 'name' => ts('Advanced Search'), |
| 2240 | 'url' => 'civicrm/contact/search/advanced', |
| 2241 | ); |
| 2242 | } |
| 2243 | $action = array_sum(array_keys($actionLinks)); |
| 2244 | |
| 2245 | $report['event_totals']['actionlinks'] = array(); |
| 2246 | foreach (array( |
| 2247 | 'clicks', |
| 2248 | 'clicks_unique', |
| 2249 | 'queue', |
| 2250 | 'delivered', |
| 2251 | 'bounce', |
| 2252 | 'unsubscribe', |
| 2253 | 'forward', |
| 2254 | 'reply', |
| 2255 | 'opened', |
| 2256 | 'optout', |
| 2257 | ) as $key) { |
| 2258 | $url = 'mailing/detail'; |
| 2259 | $reportFilter = "reset=1&mailing_id_value={$mailing_id}"; |
| 2260 | $searchFilter = "force=1&mailing_id=%%mid%%"; |
| 2261 | switch ($key) { |
| 2262 | case 'delivered': |
| 2263 | $reportFilter .= "&delivery_status_value=successful"; |
| 2264 | $searchFilter .= "&mailing_delivery_status=Y"; |
| 2265 | break; |
| 2266 | |
| 2267 | case 'bounce': |
| 2268 | $url = "mailing/bounce"; |
| 2269 | $searchFilter .= "&mailing_delivery_status=N"; |
| 2270 | break; |
| 2271 | |
| 2272 | case 'forward': |
| 2273 | $reportFilter .= "&is_forwarded_value=1"; |
| 2274 | $searchFilter .= "&mailing_forward=1"; |
| 2275 | break; |
| 2276 | |
| 2277 | case 'reply': |
| 2278 | $reportFilter .= "&is_replied_value=1"; |
| 2279 | $searchFilter .= "&mailing_reply_status=Y"; |
| 2280 | break; |
| 2281 | |
| 2282 | case 'unsubscribe': |
| 2283 | $reportFilter .= "&is_unsubscribed_value=1"; |
| 2284 | $searchFilter .= "&mailing_unsubscribe=1"; |
| 2285 | break; |
| 2286 | |
| 2287 | case 'optout': |
| 2288 | $reportFilter .= "&is_optout_value=1"; |
| 2289 | $searchFilter .= "&mailing_optout=1"; |
| 2290 | break; |
| 2291 | |
| 2292 | case 'opened': |
| 2293 | $url = "mailing/opened"; |
| 2294 | $searchFilter .= "&mailing_open_status=Y"; |
| 2295 | break; |
| 2296 | |
| 2297 | case 'clicks': |
| 2298 | case 'clicks_unique': |
| 2299 | $url = "mailing/clicks"; |
| 2300 | $searchFilter .= "&mailing_click_status=Y"; |
| 2301 | break; |
| 2302 | } |
| 2303 | $actionLinks[CRM_Core_Action::VIEW]['url'] = CRM_Report_Utils_Report::getNextUrl($url, $reportFilter, FALSE, TRUE); |
| 2304 | if (array_key_exists(CRM_Core_Action::ADVANCED, $actionLinks)) { |
| 2305 | $actionLinks[CRM_Core_Action::ADVANCED]['qs'] = $searchFilter; |
| 2306 | } |
| 2307 | $report['event_totals']['actionlinks'][$key] = CRM_Core_Action::formLink( |
| 2308 | $actionLinks, |
| 2309 | $action, |
| 2310 | array('mid' => $mailing_id), |
| 2311 | ts('more'), |
| 2312 | FALSE, |
| 2313 | 'mailing.report.action', |
| 2314 | 'Mailing', |
| 2315 | $mailing_id |
| 2316 | ); |
| 2317 | } |
| 2318 | |
| 2319 | return $report; |
| 2320 | } |
| 2321 | |
| 2322 | /** |
| 2323 | * Get the count of mailings. |
| 2324 | * |
| 2325 | * @param |
| 2326 | * |
| 2327 | * @return int |
| 2328 | * Count |
| 2329 | */ |
| 2330 | public function getCount() { |
| 2331 | $this->selectAdd(); |
| 2332 | $this->selectAdd('COUNT(id) as count'); |
| 2333 | |
| 2334 | $session = CRM_Core_Session::singleton(); |
| 2335 | $this->find(TRUE); |
| 2336 | |
| 2337 | return $this->count; |
| 2338 | } |
| 2339 | |
| 2340 | /** |
| 2341 | * @param int $id |
| 2342 | * |
| 2343 | * @throws Exception |
| 2344 | */ |
| 2345 | public static function checkPermission($id) { |
| 2346 | if (!$id) { |
| 2347 | return; |
| 2348 | } |
| 2349 | |
| 2350 | $mailingIDs = self::mailingACLIDs(); |
| 2351 | if ($mailingIDs === TRUE) { |
| 2352 | return; |
| 2353 | } |
| 2354 | |
| 2355 | if (!in_array($id, $mailingIDs)) { |
| 2356 | CRM_Core_Error::fatal(ts('You do not have permission to access this mailing report')); |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | /** |
| 2361 | * @param null $alias |
| 2362 | * |
| 2363 | * @return string |
| 2364 | */ |
| 2365 | public static function mailingACL($alias = NULL) { |
| 2366 | $mailingACL = " ( 0 ) "; |
| 2367 | |
| 2368 | $mailingIDs = self::mailingACLIDs(); |
| 2369 | if ($mailingIDs === TRUE) { |
| 2370 | return " ( 1 ) "; |
| 2371 | } |
| 2372 | |
| 2373 | if (!empty($mailingIDs)) { |
| 2374 | $mailingIDs = implode(',', $mailingIDs); |
| 2375 | $tableName = !$alias ? self::getTableName() : $alias; |
| 2376 | $mailingACL = " $tableName.id IN ( $mailingIDs ) "; |
| 2377 | } |
| 2378 | return $mailingACL; |
| 2379 | } |
| 2380 | |
| 2381 | /** |
| 2382 | * Returns all the mailings that this user can access. This is dependent on |
| 2383 | * all the groups that the user has access to. |
| 2384 | * However since most civi installs dont use ACL's we special case the condition |
| 2385 | * where the user has access to ALL groups, and hence ALL mailings and return a |
| 2386 | * value of TRUE (to avoid the downstream where clause with a list of mailing list IDs |
| 2387 | * |
| 2388 | * @return bool|array |
| 2389 | * TRUE if the user has access to all mailings, else array of mailing IDs (possibly empty). |
| 2390 | */ |
| 2391 | public static function mailingACLIDs() { |
| 2392 | // CRM-11633 |
| 2393 | // optimize common case where admin has access |
| 2394 | // to all mailings |
| 2395 | if ( |
| 2396 | CRM_Core_Permission::check('view all contacts') || |
| 2397 | CRM_Core_Permission::check('edit all contacts') |
| 2398 | ) { |
| 2399 | return TRUE; |
| 2400 | } |
| 2401 | |
| 2402 | $mailingIDs = array(); |
| 2403 | |
| 2404 | // get all the groups that this user can access |
| 2405 | // if they dont have universal access |
| 2406 | $groupNames = civicrm_api3('Group', 'get', array( |
| 2407 | 'is_active' => 1, |
| 2408 | 'check_permissions' => TRUE, |
| 2409 | 'return' => array('title', 'id'), |
| 2410 | 'options' => array('limit' => 0), |
| 2411 | )); |
| 2412 | foreach ($groupNames['values'] as $group) { |
| 2413 | $groups[$group['id']] = $group['title']; |
| 2414 | } |
| 2415 | if (!empty($groups)) { |
| 2416 | $groupIDs = implode(',', array_keys($groups)); |
| 2417 | $domain_id = CRM_Core_Config::domainID(); |
| 2418 | |
| 2419 | // get all the mailings that are in this subset of groups |
| 2420 | $query = " |
| 2421 | SELECT DISTINCT( m.id ) as id |
| 2422 | FROM civicrm_mailing m |
| 2423 | LEFT JOIN civicrm_mailing_group g ON g.mailing_id = m.id |
| 2424 | WHERE ( ( g.entity_table like 'civicrm_group%' AND g.entity_id IN ( $groupIDs ) ) |
| 2425 | OR ( g.entity_table IS NULL AND g.entity_id IS NULL AND m.domain_id = $domain_id ) ) |
| 2426 | "; |
| 2427 | $dao = CRM_Core_DAO::executeQuery($query); |
| 2428 | |
| 2429 | $mailingIDs = array(); |
| 2430 | while ($dao->fetch()) { |
| 2431 | $mailingIDs[] = $dao->id; |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | return $mailingIDs; |
| 2436 | } |
| 2437 | |
| 2438 | /** |
| 2439 | * Get the rows for a browse operation. |
| 2440 | * |
| 2441 | * @param int $offset |
| 2442 | * The row number to start from. |
| 2443 | * @param int $rowCount |
| 2444 | * The nmber of rows to return. |
| 2445 | * @param string $sort |
| 2446 | * The sql string that describes the sort order. |
| 2447 | * |
| 2448 | * @param null $additionalClause |
| 2449 | * @param array $additionalParams |
| 2450 | * |
| 2451 | * @return array |
| 2452 | * The rows |
| 2453 | */ |
| 2454 | public function &getRows($offset, $rowCount, $sort, $additionalClause = NULL, $additionalParams = NULL) { |
| 2455 | $mailing = self::getTableName(); |
| 2456 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
| 2457 | $group = CRM_Mailing_DAO_MailingGroup::getTableName(); |
| 2458 | $session = CRM_Core_Session::singleton(); |
| 2459 | |
| 2460 | $mailingACL = self::mailingACL(); |
| 2461 | |
| 2462 | //get all campaigns. |
| 2463 | $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE); |
| 2464 | |
| 2465 | // we only care about parent jobs, since that holds all the info on |
| 2466 | // the mailing |
| 2467 | $query = " |
| 2468 | SELECT $mailing.id, |
| 2469 | $mailing.name, |
| 2470 | $job.status, |
| 2471 | $mailing.approval_status_id, |
| 2472 | MIN($job.scheduled_date) as scheduled_date, |
| 2473 | MIN($job.start_date) as start_date, |
| 2474 | MAX($job.end_date) as end_date, |
| 2475 | createdContact.sort_name as created_by, |
| 2476 | scheduledContact.sort_name as scheduled_by, |
| 2477 | $mailing.created_id as created_id, |
| 2478 | $mailing.scheduled_id as scheduled_id, |
| 2479 | $mailing.is_archived as archived, |
| 2480 | $mailing.created_date as created_date, |
| 2481 | campaign_id, |
| 2482 | $mailing.sms_provider_id as sms_provider_id |
| 2483 | FROM $mailing |
| 2484 | LEFT JOIN $job ON ( $job.mailing_id = $mailing.id AND $job.is_test = 0 AND $job.parent_id IS NULL ) |
| 2485 | LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id ) |
| 2486 | LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id ) |
| 2487 | WHERE $mailingACL $additionalClause |
| 2488 | GROUP BY $mailing.id "; |
| 2489 | |
| 2490 | if ($sort) { |
| 2491 | $orderBy = trim($sort->orderBy()); |
| 2492 | if (!empty($orderBy)) { |
| 2493 | $query .= " ORDER BY $orderBy"; |
| 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | if ($rowCount) { |
| 2498 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
| 2499 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); |
| 2500 | |
| 2501 | $query .= " LIMIT $offset, $rowCount "; |
| 2502 | } |
| 2503 | |
| 2504 | if (!$additionalParams) { |
| 2505 | $additionalParams = array(); |
| 2506 | } |
| 2507 | |
| 2508 | $dao = CRM_Core_DAO::executeQuery($query, $additionalParams); |
| 2509 | |
| 2510 | $rows = array(); |
| 2511 | while ($dao->fetch()) { |
| 2512 | $rows[] = array( |
| 2513 | 'id' => $dao->id, |
| 2514 | 'name' => $dao->name, |
| 2515 | 'status' => $dao->status ? $dao->status : 'Not scheduled', |
| 2516 | 'created_date' => CRM_Utils_Date::customFormat($dao->created_date), |
| 2517 | 'scheduled' => CRM_Utils_Date::customFormat($dao->scheduled_date), |
| 2518 | 'scheduled_iso' => $dao->scheduled_date, |
| 2519 | 'start' => CRM_Utils_Date::customFormat($dao->start_date), |
| 2520 | 'end' => CRM_Utils_Date::customFormat($dao->end_date), |
| 2521 | 'created_by' => $dao->created_by, |
| 2522 | 'scheduled_by' => $dao->scheduled_by, |
| 2523 | 'created_id' => $dao->created_id, |
| 2524 | 'scheduled_id' => $dao->scheduled_id, |
| 2525 | 'archived' => $dao->archived, |
| 2526 | 'approval_status_id' => $dao->approval_status_id, |
| 2527 | 'campaign_id' => $dao->campaign_id, |
| 2528 | 'campaign' => empty($dao->campaign_id) ? NULL : $allCampaigns[$dao->campaign_id], |
| 2529 | 'sms_provider_id' => $dao->sms_provider_id, |
| 2530 | ); |
| 2531 | } |
| 2532 | return $rows; |
| 2533 | } |
| 2534 | |
| 2535 | /** |
| 2536 | * Show detail Mailing report. |
| 2537 | * |
| 2538 | * @param int $id |
| 2539 | * |
| 2540 | * @return string |
| 2541 | */ |
| 2542 | public static function showEmailDetails($id) { |
| 2543 | return CRM_Utils_System::url('civicrm/mailing/report', "mid=$id"); |
| 2544 | } |
| 2545 | |
| 2546 | /** |
| 2547 | * Delete Mails and all its associated records. |
| 2548 | * |
| 2549 | * @param int $id |
| 2550 | * Id of the mail to delete. |
| 2551 | * |
| 2552 | * @return void |
| 2553 | */ |
| 2554 | public static function del($id) { |
| 2555 | if (empty($id)) { |
| 2556 | CRM_Core_Error::fatal(); |
| 2557 | } |
| 2558 | |
| 2559 | CRM_Utils_Hook::pre('delete', 'Mailing', $id, CRM_Core_DAO::$_nullArray); |
| 2560 | |
| 2561 | // delete all file attachments |
| 2562 | CRM_Core_BAO_File::deleteEntityFile('civicrm_mailing', |
| 2563 | $id |
| 2564 | ); |
| 2565 | |
| 2566 | $dao = new CRM_Mailing_DAO_Mailing(); |
| 2567 | $dao->id = $id; |
| 2568 | $dao->delete(); |
| 2569 | |
| 2570 | CRM_Core_Session::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success'); |
| 2571 | |
| 2572 | CRM_Utils_Hook::post('delete', 'Mailing', $id, $dao); |
| 2573 | } |
| 2574 | |
| 2575 | /** |
| 2576 | * Delete Jobss and all its associated records |
| 2577 | * related to test Mailings |
| 2578 | * |
| 2579 | * @param int $id |
| 2580 | * Id of the Job to delete. |
| 2581 | * |
| 2582 | * @return void |
| 2583 | */ |
| 2584 | public static function delJob($id) { |
| 2585 | if (empty($id)) { |
| 2586 | CRM_Core_Error::fatal(); |
| 2587 | } |
| 2588 | |
| 2589 | $dao = new CRM_Mailing_BAO_MailingJob(); |
| 2590 | $dao->id = $id; |
| 2591 | $dao->delete(); |
| 2592 | } |
| 2593 | |
| 2594 | /** |
| 2595 | * @return array |
| 2596 | */ |
| 2597 | public function getReturnProperties() { |
| 2598 | $tokens = &$this->getTokens(); |
| 2599 | |
| 2600 | $properties = array(); |
| 2601 | if (isset($tokens['html']) && |
| 2602 | isset($tokens['html']['contact']) |
| 2603 | ) { |
| 2604 | $properties = array_merge($properties, $tokens['html']['contact']); |
| 2605 | } |
| 2606 | |
| 2607 | if (isset($tokens['text']) && |
| 2608 | isset($tokens['text']['contact']) |
| 2609 | ) { |
| 2610 | $properties = array_merge($properties, $tokens['text']['contact']); |
| 2611 | } |
| 2612 | |
| 2613 | if (isset($tokens['subject']) && |
| 2614 | isset($tokens['subject']['contact']) |
| 2615 | ) { |
| 2616 | $properties = array_merge($properties, $tokens['subject']['contact']); |
| 2617 | } |
| 2618 | |
| 2619 | $returnProperties = array(); |
| 2620 | $returnProperties['display_name'] = $returnProperties['contact_id'] = $returnProperties['preferred_mail_format'] = $returnProperties['hash'] = 1; |
| 2621 | |
| 2622 | foreach ($properties as $p) { |
| 2623 | $returnProperties[$p] = 1; |
| 2624 | } |
| 2625 | |
| 2626 | return $returnProperties; |
| 2627 | } |
| 2628 | |
| 2629 | /** |
| 2630 | * Build the compose mail form. |
| 2631 | * |
| 2632 | * @param CRM_Core_Form $form |
| 2633 | * |
| 2634 | * @return void |
| 2635 | */ |
| 2636 | public static function commonCompose(&$form) { |
| 2637 | //get the tokens. |
| 2638 | $tokens = array(); |
| 2639 | |
| 2640 | if (method_exists($form, 'listTokens')) { |
| 2641 | $tokens = array_merge($form->listTokens(), $tokens); |
| 2642 | } |
| 2643 | |
| 2644 | //sorted in ascending order tokens by ignoring word case |
| 2645 | $form->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens)); |
| 2646 | |
| 2647 | $templates = array(); |
| 2648 | |
| 2649 | $textFields = array('text_message' => ts('HTML Format'), 'sms_text_message' => ts('SMS Message')); |
| 2650 | $modePrefixes = array('Mail' => NULL, 'SMS' => 'SMS'); |
| 2651 | |
| 2652 | $className = CRM_Utils_System::getClassName($form); |
| 2653 | |
| 2654 | if ($className != 'CRM_SMS_Form_Upload' && $className != 'CRM_Contact_Form_Task_SMS' && |
| 2655 | $className != 'CRM_Contact_Form_Task_SMS' |
| 2656 | ) { |
| 2657 | $form->add('wysiwyg', 'html_message', |
| 2658 | ts('HTML Format'), |
| 2659 | array( |
| 2660 | 'cols' => '80', |
| 2661 | 'rows' => '8', |
| 2662 | 'onkeyup' => "return verify(this)", |
| 2663 | ) |
| 2664 | ); |
| 2665 | |
| 2666 | if ($className != 'CRM_Admin_Form_ScheduleReminders') { |
| 2667 | unset($modePrefixes['SMS']); |
| 2668 | } |
| 2669 | } |
| 2670 | else { |
| 2671 | unset($textFields['text_message']); |
| 2672 | unset($modePrefixes['Mail']); |
| 2673 | } |
| 2674 | |
| 2675 | //insert message Text by selecting "Select Template option" |
| 2676 | foreach ($textFields as $id => $label) { |
| 2677 | $prefix = NULL; |
| 2678 | if ($id == 'sms_text_message') { |
| 2679 | $prefix = "SMS"; |
| 2680 | $form->assign('max_sms_length', CRM_SMS_Provider::MAX_SMS_CHAR); |
| 2681 | } |
| 2682 | $form->add('textarea', $id, $label, |
| 2683 | array( |
| 2684 | 'cols' => '80', |
| 2685 | 'rows' => '8', |
| 2686 | 'onkeyup' => "return verify(this, '{$prefix}')", |
| 2687 | ) |
| 2688 | ); |
| 2689 | } |
| 2690 | |
| 2691 | foreach ($modePrefixes as $prefix) { |
| 2692 | if ($prefix == 'SMS') { |
| 2693 | $templates[$prefix] = CRM_Core_BAO_MessageTemplate::getMessageTemplates(FALSE, TRUE); |
| 2694 | } |
| 2695 | else { |
| 2696 | $templates[$prefix] = CRM_Core_BAO_MessageTemplate::getMessageTemplates(FALSE); |
| 2697 | } |
| 2698 | if (!empty($templates[$prefix])) { |
| 2699 | $form->assign('templates', TRUE); |
| 2700 | |
| 2701 | $form->add('select', "{$prefix}template", ts('Use Template'), |
| 2702 | array('' => ts('- select -')) + $templates[$prefix], FALSE, |
| 2703 | array('onChange' => "selectValue( this.value, '{$prefix}');") |
| 2704 | ); |
| 2705 | } |
| 2706 | $form->add('checkbox', "{$prefix}updateTemplate", ts('Update Template'), NULL); |
| 2707 | |
| 2708 | $form->add('checkbox', "{$prefix}saveTemplate", ts('Save As New Template'), NULL, FALSE, |
| 2709 | array('onclick' => "showSaveDetails(this, '{$prefix}');") |
| 2710 | ); |
| 2711 | $form->add('text', "{$prefix}saveTemplateName", ts('Template Title')); |
| 2712 | } |
| 2713 | |
| 2714 | // I'm not sure this is ever called. |
| 2715 | $action = CRM_Utils_Request::retrieve('action', 'String', $form, FALSE); |
| 2716 | if ((CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Task_PDF') && |
| 2717 | $action == CRM_Core_Action::VIEW |
| 2718 | ) { |
| 2719 | $form->freeze('html_message'); |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | /** |
| 2724 | * Get the search based mailing Ids. |
| 2725 | * |
| 2726 | * @return array |
| 2727 | * , searched base mailing ids. |
| 2728 | */ |
| 2729 | public function searchMailingIDs() { |
| 2730 | $group = CRM_Mailing_DAO_MailingGroup::getTableName(); |
| 2731 | $mailing = self::getTableName(); |
| 2732 | |
| 2733 | $query = " |
| 2734 | SELECT $mailing.id as mailing_id |
| 2735 | FROM $mailing, $group |
| 2736 | WHERE $group.mailing_id = $mailing.id |
| 2737 | AND $group.group_type = 'Base'"; |
| 2738 | |
| 2739 | $searchDAO = CRM_Core_DAO::executeQuery($query); |
| 2740 | $mailingIDs = array(); |
| 2741 | while ($searchDAO->fetch()) { |
| 2742 | $mailingIDs[] = $searchDAO->mailing_id; |
| 2743 | } |
| 2744 | |
| 2745 | return $mailingIDs; |
| 2746 | } |
| 2747 | |
| 2748 | /** |
| 2749 | * Get the content/components of mailing based on mailing Id |
| 2750 | * |
| 2751 | * @param array $report |
| 2752 | * of mailing report. |
| 2753 | * |
| 2754 | * @param $form |
| 2755 | * Reference of this. |
| 2756 | * |
| 2757 | * @param bool $isSMS |
| 2758 | * |
| 2759 | * @return array |
| 2760 | * array content/component. |
| 2761 | */ |
| 2762 | public static function getMailingContent(&$report, &$form, $isSMS = FALSE) { |
| 2763 | $htmlHeader = $textHeader = NULL; |
| 2764 | $htmlFooter = $textFooter = NULL; |
| 2765 | |
| 2766 | if (!$isSMS) { |
| 2767 | if ($report['mailing']['header_id']) { |
| 2768 | $header = new CRM_Mailing_BAO_Component(); |
| 2769 | $header->id = $report['mailing']['header_id']; |
| 2770 | $header->find(TRUE); |
| 2771 | $htmlHeader = $header->body_html; |
| 2772 | $textHeader = $header->body_text; |
| 2773 | } |
| 2774 | |
| 2775 | if ($report['mailing']['footer_id']) { |
| 2776 | $footer = new CRM_Mailing_BAO_Component(); |
| 2777 | $footer->id = $report['mailing']['footer_id']; |
| 2778 | $footer->find(TRUE); |
| 2779 | $htmlFooter = $footer->body_html; |
| 2780 | $textFooter = $footer->body_text; |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | $mailingKey = $form->_mailing_id; |
| 2785 | if (!$isSMS) { |
| 2786 | if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) { |
| 2787 | $mailingKey = $hash; |
| 2788 | } |
| 2789 | } |
| 2790 | |
| 2791 | if (!empty($report['mailing']['body_text'])) { |
| 2792 | $url = CRM_Utils_System::url('civicrm/mailing/view', 'reset=1&text=1&id=' . $mailingKey); |
| 2793 | $form->assign('textViewURL', $url); |
| 2794 | } |
| 2795 | |
| 2796 | if (!$isSMS) { |
| 2797 | if (!empty($report['mailing']['body_html'])) { |
| 2798 | $url = CRM_Utils_System::url('civicrm/mailing/view', 'reset=1&id=' . $mailingKey); |
| 2799 | $form->assign('htmlViewURL', $url); |
| 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | if (!$isSMS) { |
| 2804 | $report['mailing']['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $form->_mailing_id); |
| 2805 | } |
| 2806 | return $report; |
| 2807 | } |
| 2808 | |
| 2809 | /** |
| 2810 | * @param int $jobID |
| 2811 | * |
| 2812 | * @return mixed |
| 2813 | */ |
| 2814 | public static function overrideVerp($jobID) { |
| 2815 | static $_cache = array(); |
| 2816 | |
| 2817 | if (!isset($_cache[$jobID])) { |
| 2818 | $query = " |
| 2819 | SELECT override_verp |
| 2820 | FROM civicrm_mailing |
| 2821 | INNER JOIN civicrm_mailing_job ON civicrm_mailing.id = civicrm_mailing_job.mailing_id |
| 2822 | WHERE civicrm_mailing_job.id = %1 |
| 2823 | "; |
| 2824 | $params = array(1 => array($jobID, 'Integer')); |
| 2825 | $_cache[$jobID] = CRM_Core_DAO::singleValueQuery($query, $params); |
| 2826 | } |
| 2827 | return $_cache[$jobID]; |
| 2828 | } |
| 2829 | |
| 2830 | /** |
| 2831 | * @param null $mode |
| 2832 | * |
| 2833 | * @return bool |
| 2834 | * @throws Exception |
| 2835 | */ |
| 2836 | public static function processQueue($mode = NULL) { |
| 2837 | $config = CRM_Core_Config::singleton(); |
| 2838 | |
| 2839 | if ($mode == NULL && CRM_Core_BAO_MailSettings::defaultDomain() == "EXAMPLE.ORG") { |
| 2840 | throw new CRM_Core_Exception(ts('The <a href="%1">default mailbox</a> has not been configured. You will find <a href="%2">more info in the online user and administrator guide</a>', array( |
| 2841 | 1 => CRM_Utils_System::url('civicrm/admin/mailSettings', 'reset=1'), |
| 2842 | 2 => "http://book.civicrm.org/user/advanced-configuration/email-system-configuration/", |
| 2843 | ))); |
| 2844 | } |
| 2845 | |
| 2846 | // check if we are enforcing number of parallel cron jobs |
| 2847 | // CRM-8460 |
| 2848 | $gotCronLock = FALSE; |
| 2849 | |
| 2850 | if (property_exists($config, 'mailerJobsMax') && $config->mailerJobsMax && $config->mailerJobsMax > 0) { |
| 2851 | $lockArray = range(1, $config->mailerJobsMax); |
| 2852 | shuffle($lockArray); |
| 2853 | |
| 2854 | // check if we are using global locks |
| 2855 | foreach ($lockArray as $lockID) { |
| 2856 | $cronLock = Civi\Core\Container::singleton()->get('lockManager')->acquire("worker.mailing.send.{$lockID}"); |
| 2857 | if ($cronLock->isAcquired()) { |
| 2858 | $gotCronLock = TRUE; |
| 2859 | break; |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | // exit here since we have enuf cronjobs running |
| 2864 | if (!$gotCronLock) { |
| 2865 | CRM_Core_Error::debug_log_message('Returning early, since max number of cronjobs running'); |
| 2866 | return TRUE; |
| 2867 | } |
| 2868 | |
| 2869 | if (getenv('CIVICRM_CRON_HOLD')) { |
| 2870 | // In testing, we may need to simulate some slow activities. |
| 2871 | sleep(getenv('CIVICRM_CRON_HOLD')); |
| 2872 | } |
| 2873 | } |
| 2874 | |
| 2875 | // load bootstrap to call hooks |
| 2876 | |
| 2877 | // Split up the parent jobs into multiple child jobs |
| 2878 | $mailerJobSize = (property_exists($config, 'mailerJobSize')) ? $config->mailerJobSize : NULL; |
| 2879 | CRM_Mailing_BAO_MailingJob::runJobs_pre($mailerJobSize, $mode); |
| 2880 | CRM_Mailing_BAO_MailingJob::runJobs(NULL, $mode); |
| 2881 | CRM_Mailing_BAO_MailingJob::runJobs_post($mode); |
| 2882 | |
| 2883 | // lets release the global cron lock if we do have one |
| 2884 | if ($gotCronLock) { |
| 2885 | $cronLock->release(); |
| 2886 | } |
| 2887 | |
| 2888 | return TRUE; |
| 2889 | } |
| 2890 | |
| 2891 | /** |
| 2892 | * @param int $mailingID |
| 2893 | */ |
| 2894 | private static function addMultipleEmails($mailingID) { |
| 2895 | $sql = " |
| 2896 | INSERT INTO civicrm_mailing_recipients |
| 2897 | (mailing_id, email_id, contact_id) |
| 2898 | SELECT %1, e.id, e.contact_id FROM civicrm_email e |
| 2899 | WHERE e.on_hold = 0 |
| 2900 | AND e.is_bulkmail = 1 |
| 2901 | AND e.contact_id IN |
| 2902 | ( SELECT contact_id FROM civicrm_mailing_recipients mr WHERE mailing_id = %1 ) |
| 2903 | AND e.id NOT IN ( SELECT email_id FROM civicrm_mailing_recipients mr WHERE mailing_id = %1 ) |
| 2904 | "; |
| 2905 | $params = array(1 => array($mailingID, 'Integer')); |
| 2906 | |
| 2907 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 2908 | } |
| 2909 | |
| 2910 | /** |
| 2911 | * @param bool $isSMS |
| 2912 | * |
| 2913 | * @return mixed |
| 2914 | */ |
| 2915 | public static function getMailingsList($isSMS = FALSE) { |
| 2916 | static $list = array(); |
| 2917 | $where = " WHERE "; |
| 2918 | if (!$isSMS) { |
| 2919 | $where .= " civicrm_mailing.sms_provider_id IS NULL "; |
| 2920 | } |
| 2921 | else { |
| 2922 | $where .= " civicrm_mailing.sms_provider_id IS NOT NULL "; |
| 2923 | } |
| 2924 | |
| 2925 | if (empty($list)) { |
| 2926 | $query = " |
| 2927 | SELECT civicrm_mailing.id, civicrm_mailing.name, civicrm_mailing_job.end_date |
| 2928 | FROM civicrm_mailing |
| 2929 | INNER JOIN civicrm_mailing_job ON civicrm_mailing.id = civicrm_mailing_job.mailing_id {$where} |
| 2930 | ORDER BY civicrm_mailing.name"; |
| 2931 | $mailing = CRM_Core_DAO::executeQuery($query); |
| 2932 | |
| 2933 | while ($mailing->fetch()) { |
| 2934 | $list[$mailing->id] = "{$mailing->name} :: {$mailing->end_date}"; |
| 2935 | } |
| 2936 | } |
| 2937 | |
| 2938 | return $list; |
| 2939 | } |
| 2940 | |
| 2941 | /** |
| 2942 | * @param int $mid |
| 2943 | * |
| 2944 | * @return null|string |
| 2945 | */ |
| 2946 | public static function hiddenMailingGroup($mid) { |
| 2947 | $sql = " |
| 2948 | SELECT g.id |
| 2949 | FROM civicrm_mailing m |
| 2950 | INNER JOIN civicrm_mailing_group mg ON mg.mailing_id = m.id |
| 2951 | INNER JOIN civicrm_group g ON mg.entity_id = g.id AND mg.entity_table = 'civicrm_group' |
| 2952 | WHERE g.is_hidden = 1 |
| 2953 | AND mg.group_type = 'Include' |
| 2954 | AND m.id = %1 |
| 2955 | "; |
| 2956 | $params = array(1 => array($mid, 'Integer')); |
| 2957 | return CRM_Core_DAO::singleValueQuery($sql, $params); |
| 2958 | } |
| 2959 | |
| 2960 | /** |
| 2961 | * wrapper for ajax activity selector. |
| 2962 | * |
| 2963 | * @param array $params |
| 2964 | * Associated array for params record id. |
| 2965 | * |
| 2966 | * @return array |
| 2967 | * associated array of contact activities |
| 2968 | */ |
| 2969 | public static function getContactMailingSelector(&$params) { |
| 2970 | // format the params |
| 2971 | $params['offset'] = ($params['page'] - 1) * $params['rp']; |
| 2972 | $params['rowCount'] = $params['rp']; |
| 2973 | $params['sort'] = CRM_Utils_Array::value('sortBy', $params); |
| 2974 | $params['caseId'] = NULL; |
| 2975 | |
| 2976 | // get contact mailings |
| 2977 | $mailings = CRM_Mailing_BAO_Mailing::getContactMailings($params); |
| 2978 | |
| 2979 | // add total |
| 2980 | $params['total'] = CRM_Mailing_BAO_Mailing::getContactMailingsCount($params); |
| 2981 | |
| 2982 | //CRM-12814 |
| 2983 | if (!empty($mailings)) { |
| 2984 | $openCounts = CRM_Mailing_Event_BAO_Opened::getMailingContactCount(array_keys($mailings), $params['contact_id']); |
| 2985 | $clickCounts = CRM_Mailing_Event_BAO_TrackableURLOpen::getMailingContactCount(array_keys($mailings), $params['contact_id']); |
| 2986 | } |
| 2987 | |
| 2988 | // format params and add links |
| 2989 | $contactMailings = array(); |
| 2990 | foreach ($mailings as $mailingId => $values) { |
| 2991 | $mailing = array(); |
| 2992 | $mailing['subject'] = $values['subject']; |
| 2993 | $mailing['creator_name'] = CRM_Utils_System::href( |
| 2994 | $values['creator_name'], |
| 2995 | 'civicrm/contact/view', |
| 2996 | "reset=1&cid={$values['creator_id']}"); |
| 2997 | $mailing['recipients'] = CRM_Utils_System::href(ts('(recipients)'), 'civicrm/mailing/report/event', |
| 2998 | "mid={$values['mailing_id']}&reset=1&cid={$params['contact_id']}&event=queue&context=mailing"); |
| 2999 | $mailing['start_date'] = CRM_Utils_Date::customFormat($values['start_date']); |
| 3000 | //CRM-12814 |
| 3001 | $mailing['openstats'] = "Opens: " . |
| 3002 | CRM_Utils_Array::value($values['mailing_id'], $openCounts, 0) . |
| 3003 | "<br />Clicks: " . |
| 3004 | CRM_Utils_Array::value($values['mailing_id'], $clickCounts, 0); |
| 3005 | |
| 3006 | $actionLinks = array( |
| 3007 | CRM_Core_Action::VIEW => array( |
| 3008 | 'name' => ts('View'), |
| 3009 | 'url' => 'civicrm/mailing/view', |
| 3010 | 'qs' => "reset=1&id=%%mkey%%", |
| 3011 | 'title' => ts('View Mailing'), |
| 3012 | 'class' => 'crm-popup', |
| 3013 | ), |
| 3014 | CRM_Core_Action::BROWSE => array( |
| 3015 | 'name' => ts('Mailing Report'), |
| 3016 | 'url' => 'civicrm/mailing/report', |
| 3017 | 'qs' => "mid=%%mid%%&reset=1&cid=%%cid%%&context=mailing", |
| 3018 | 'title' => ts('View Mailing Report'), |
| 3019 | ), |
| 3020 | ); |
| 3021 | |
| 3022 | $mailingKey = $values['mailing_id']; |
| 3023 | if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) { |
| 3024 | $mailingKey = $hash; |
| 3025 | } |
| 3026 | |
| 3027 | $mailing['links'] = CRM_Core_Action::formLink( |
| 3028 | $actionLinks, |
| 3029 | NULL, |
| 3030 | array( |
| 3031 | 'mid' => $values['mailing_id'], |
| 3032 | 'cid' => $params['contact_id'], |
| 3033 | 'mkey' => $mailingKey, |
| 3034 | ), |
| 3035 | ts('more'), |
| 3036 | FALSE, |
| 3037 | 'mailing.contact.action', |
| 3038 | 'Mailing', |
| 3039 | $values['mailing_id'] |
| 3040 | ); |
| 3041 | |
| 3042 | array_push($contactMailings, $mailing); |
| 3043 | } |
| 3044 | |
| 3045 | $contactMailingsDT = array(); |
| 3046 | $contactMailingsDT['data'] = $contactMailings; |
| 3047 | $contactMailingsDT['recordsTotal'] = $params['total']; |
| 3048 | $contactMailingsDT['recordsFiltered'] = $params['total']; |
| 3049 | |
| 3050 | return $contactMailingsDT; |
| 3051 | } |
| 3052 | |
| 3053 | /** |
| 3054 | * Retrieve contact mailing. |
| 3055 | * |
| 3056 | * @param array $params |
| 3057 | * |
| 3058 | * @return array |
| 3059 | * Array of mailings for a contact |
| 3060 | * |
| 3061 | */ |
| 3062 | static public function getContactMailings(&$params) { |
| 3063 | $params['version'] = 3; |
| 3064 | $params['offset'] = ($params['page'] - 1) * $params['rp']; |
| 3065 | $params['limit'] = $params['rp']; |
| 3066 | $params['sort'] = CRM_Utils_Array::value('sortBy', $params); |
| 3067 | |
| 3068 | $result = civicrm_api('MailingContact', 'get', $params); |
| 3069 | return $result['values']; |
| 3070 | } |
| 3071 | |
| 3072 | /** |
| 3073 | * Retrieve contact mailing count. |
| 3074 | * |
| 3075 | * @param array $params |
| 3076 | * |
| 3077 | * @return int |
| 3078 | * count of mailings for a contact |
| 3079 | * |
| 3080 | */ |
| 3081 | static public function getContactMailingsCount(&$params) { |
| 3082 | $params['version'] = 3; |
| 3083 | return civicrm_api('MailingContact', 'getcount', $params); |
| 3084 | } |
| 3085 | |
| 3086 | /** |
| 3087 | * Get a list of permissions required for CRUD'ing each field |
| 3088 | * (when workflow is enabled). |
| 3089 | * |
| 3090 | * @return array |
| 3091 | * Array (string $fieldName => string $permName) |
| 3092 | */ |
| 3093 | public static function getWorkflowFieldPerms() { |
| 3094 | $fieldNames = array_keys(CRM_Mailing_DAO_Mailing::fields()); |
| 3095 | $fieldPerms = array(); |
| 3096 | foreach ($fieldNames as $fieldName) { |
| 3097 | if ($fieldName == 'id') { |
| 3098 | $fieldPerms[$fieldName] = array( |
| 3099 | array('access CiviMail', 'schedule mailings', 'approve mailings', 'create mailings'), // OR |
| 3100 | ); |
| 3101 | } |
| 3102 | elseif (in_array($fieldName, array('scheduled_date', 'scheduled_id'))) { |
| 3103 | $fieldPerms[$fieldName] = array( |
| 3104 | array('access CiviMail', 'schedule mailings'), // OR |
| 3105 | ); |
| 3106 | } |
| 3107 | elseif (in_array($fieldName, array('approval_date', 'approver_id', 'approval_status_id', 'approval_note'))) { |
| 3108 | $fieldPerms[$fieldName] = array( |
| 3109 | array('access CiviMail', 'approve mailings'), // OR |
| 3110 | ); |
| 3111 | } |
| 3112 | else { |
| 3113 | $fieldPerms[$fieldName] = array( |
| 3114 | array('access CiviMail', 'create mailings'), // OR |
| 3115 | ); |
| 3116 | } |
| 3117 | } |
| 3118 | return $fieldPerms; |
| 3119 | } |
| 3120 | |
| 3121 | } |