3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class CRM_Mailing_BAO_MailingAB
21 class CRM_Mailing_BAO_MailingAB
extends CRM_Mailing_DAO_MailingAB
{
26 public function __construct() {
27 parent
::__construct();
31 * Construct a new mailingab object.
33 * @params array $params
36 * @param array $params
38 * @return CRM_Mailing_DAO_MailingAB
40 public static function create(&$params) {
41 $transaction = new CRM_Core_Transaction();
43 $mailingab = self
::writeRecord($params);
45 if (is_a($mailingab, 'CRM_Core_Error')) {
46 $transaction->rollback();
49 $transaction->commit();
54 * Delete MailingAB and all its associated records.
57 * Id of the mail to delete.
59 public static function del($id) {
61 throw new CRM_Core_Exception(ts('No id passed to MailingAB del function'));
63 CRM_Core_Transaction
::create()->run(function () use ($id) {
64 CRM_Utils_Hook
::pre('delete', 'MailingAB', $id, CRM_Core_DAO
::$_nullArray);
66 $dao = new CRM_Mailing_DAO_MailingAB();
68 if ($dao->find(TRUE)) {
69 $mailing_ids = [$dao->mailing_id_a
, $dao->mailing_id_b
, $dao->mailing_id_c
];
71 foreach ($mailing_ids as $mailing_id) {
73 CRM_Mailing_BAO_Mailing
::del($mailing_id);
78 CRM_Core_Session
::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
80 CRM_Utils_Hook
::post('delete', 'MailingAB', $id, $dao);
85 * Transfer recipients from the canonical mailing A to the other mailings.
87 * @param CRM_Mailing_DAO_MailingAB $dao
89 public static function distributeRecipients(CRM_Mailing_DAO_MailingAB
$dao) {
90 CRM_Mailing_BAO_Mailing
::getRecipients($dao->mailing_id_a
);
92 //calculate total number of random recipients for mail C from group_percentage selected
93 $totalCount = CRM_Mailing_BAO_Recipients
::mailingSize($dao->mailing_id_a
);
94 $totalSelected = max(1, round(($totalCount * $dao->group_percentage
) / 100));
96 CRM_Mailing_BAO_Recipients
::reassign($dao->mailing_id_a
, [
97 $dao->mailing_id_b
=> (2 * $totalSelected <= $totalCount) ?
$totalSelected : $totalCount - $totalSelected,
98 $dao->mailing_id_c
=> max(0, $totalCount - $totalSelected - $totalSelected),
104 * get abtest based on Mailing ID
106 * @param int $mailingID
111 public static function getABTest($mailingID) {
112 $query = "SELECT * FROM `civicrm_mailing_abtest` ab
113 where (ab.mailing_id_a = %1
114 OR ab.mailing_id_b = %1
115 OR ab.mailing_id_c = %1)
117 $params = [1 => [$mailingID, 'Integer']];
118 $abTest = CRM_Core_DAO
::executeQuery($query, $params);