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
{
24 * Construct a new mailingab object.
26 * @params array $params
29 * @param array $params
31 * @return CRM_Mailing_DAO_MailingAB
33 public static function create(&$params) {
34 $transaction = new CRM_Core_Transaction();
36 $mailingab = self
::writeRecord($params);
38 if (is_a($mailingab, 'CRM_Core_Error')) {
39 $transaction->rollback();
42 $transaction->commit();
47 * Delete MailingAB and all its associated records.
50 * Id of the mail to delete.
52 public static function del($id) {
54 throw new CRM_Core_Exception(ts('No id passed to MailingAB del function'));
56 CRM_Core_Transaction
::create()->run(function () use ($id) {
57 CRM_Utils_Hook
::pre('delete', 'MailingAB', $id);
59 $dao = new CRM_Mailing_DAO_MailingAB();
61 if ($dao->find(TRUE)) {
62 $mailing_ids = [$dao->mailing_id_a
, $dao->mailing_id_b
, $dao->mailing_id_c
];
64 foreach ($mailing_ids as $mailing_id) {
66 CRM_Mailing_BAO_Mailing
::del($mailing_id);
71 CRM_Core_Session
::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
73 CRM_Utils_Hook
::post('delete', 'MailingAB', $id, $dao);
78 * Transfer recipients from the canonical mailing A to the other mailings.
80 * @param CRM_Mailing_DAO_MailingAB $dao
82 public static function distributeRecipients(CRM_Mailing_DAO_MailingAB
$dao) {
83 CRM_Mailing_BAO_Mailing
::getRecipients($dao->mailing_id_a
);
85 //calculate total number of random recipients for mail C from group_percentage selected
86 $totalCount = CRM_Mailing_BAO_Recipients
::mailingSize($dao->mailing_id_a
);
87 $totalSelected = max(1, round(($totalCount * $dao->group_percentage
) / 100));
89 CRM_Mailing_BAO_Recipients
::reassign($dao->mailing_id_a
, [
90 $dao->mailing_id_b
=> (2 * $totalSelected <= $totalCount) ?
$totalSelected : $totalCount - $totalSelected,
91 $dao->mailing_id_c
=> max(0, $totalCount - $totalSelected - $totalSelected),
97 * get abtest based on Mailing ID
99 * @param int $mailingID
104 public static function getABTest($mailingID) {
105 $query = "SELECT * FROM `civicrm_mailing_abtest` ab
106 where (ab.mailing_id_a = %1
107 OR ab.mailing_id_b = %1
108 OR ab.mailing_id_c = %1)
110 $params = [1 => [$mailingID, 'Integer']];
111 $abTest = CRM_Core_DAO
::executeQuery($query, $params);