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
40 * $mailingab The new mailingab object
42 public static function create(&$params, $ids = []) {
43 $transaction = new CRM_Core_Transaction();
45 $mailingab = self
::add($params, $ids);
47 if (is_a($mailingab, 'CRM_Core_Error')) {
48 $transaction->rollback();
51 $transaction->commit();
56 * function to add the mailings.
58 * @param array $params
59 * Reference array contains the values submitted by the form.
61 * Reference array contains the id.
66 public static function add(&$params, $ids = []) {
67 $id = CRM_Utils_Array
::value('mailingab_id', $ids, CRM_Utils_Array
::value('id', $params));
70 CRM_Utils_Hook
::pre('edit', 'MailingAB', $id, $params);
73 CRM_Utils_Hook
::pre('create', 'MailingAB', NULL, $params);
76 $mailingab = new CRM_Mailing_DAO_MailingAB();
79 $mailingab->domain_id
= CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
82 $mailingab->copyValues($params);
84 $result = $mailingab->save();
87 CRM_Utils_Hook
::post('edit', 'MailingAB', $mailingab->id
, $mailingab);
90 CRM_Utils_Hook
::post('create', 'MailingAB', $mailingab->id
, $mailingab);
97 * Delete MailingAB and all its associated records.
100 * Id of the mail to delete.
102 public static function del($id) {
104 throw new CRM_Core_Exception(ts('No id passed to MailingAB del function'));
106 CRM_Core_Transaction
::create()->run(function () use ($id) {
107 CRM_Utils_Hook
::pre('delete', 'MailingAB', $id, CRM_Core_DAO
::$_nullArray);
109 $dao = new CRM_Mailing_DAO_MailingAB();
111 if ($dao->find(TRUE)) {
112 $mailing_ids = [$dao->mailing_id_a
, $dao->mailing_id_b
, $dao->mailing_id_c
];
114 foreach ($mailing_ids as $mailing_id) {
116 CRM_Mailing_BAO_Mailing
::del($mailing_id);
121 CRM_Core_Session
::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
123 CRM_Utils_Hook
::post('delete', 'MailingAB', $id, $dao);
128 * Transfer recipients from the canonical mailing A to the other mailings.
130 * @param CRM_Mailing_DAO_MailingAB $dao
132 public static function distributeRecipients(CRM_Mailing_DAO_MailingAB
$dao) {
133 CRM_Mailing_BAO_Mailing
::getRecipients($dao->mailing_id_a
);
135 //calculate total number of random recipients for mail C from group_percentage selected
136 $totalCount = CRM_Mailing_BAO_Recipients
::mailingSize($dao->mailing_id_a
);
137 $totalSelected = max(1, round(($totalCount * $dao->group_percentage
) / 100));
139 CRM_Mailing_BAO_Recipients
::reassign($dao->mailing_id_a
, [
140 $dao->mailing_id_b
=> (2 * $totalSelected <= $totalCount) ?
$totalSelected : $totalCount - $totalSelected,
141 $dao->mailing_id_c
=> max(0, $totalCount - $totalSelected - $totalSelected),
147 * get abtest based on Mailing ID
149 * @param int $mailingID
154 public static function getABTest($mailingID) {
155 $query = "SELECT * FROM `civicrm_mailing_abtest` ab
156 where (ab.mailing_id_a = %1
157 OR ab.mailing_id_b = %1
158 OR ab.mailing_id_c = %1)
160 $params = [1 => [$mailingID, 'Integer']];
161 $abTest = CRM_Core_DAO
::executeQuery($query, $params);