Merge pull request #17583 from civicrm/5.27
[civicrm-core.git] / CRM / Mailing / BAO / MailingAB.php
CommitLineData
4aef704e 1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
4aef704e 5 | |
bc77d7c0
TO
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 |
4aef704e 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
4aef704e 11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
4aef704e 16 */
17
18/**
19 * Class CRM_Mailing_BAO_MailingAB
20 */
21class CRM_Mailing_BAO_MailingAB extends CRM_Mailing_DAO_MailingAB {
22
23 /**
fe482240 24 * class constructor.
4aef704e 25 */
00be9182 26 public function __construct() {
4aef704e 27 parent::__construct();
28 }
29
30 /**
fe482240 31 * Construct a new mailingab object.
4aef704e 32 *
90c8230e
TO
33 * @params array $params
34 * Form values.
4aef704e 35 *
16b10e64 36 * @param array $params
4aef704e 37 *
a63d3a16 38 * @return CRM_Mailing_DAO_MailingAB
4aef704e 39 */
a63d3a16 40 public static function create(&$params) {
4aef704e 41 $transaction = new CRM_Core_Transaction();
42
a63d3a16 43 $mailingab = self::writeRecord($params);
4aef704e 44
45 if (is_a($mailingab, 'CRM_Core_Error')) {
46 $transaction->rollback();
47 return $mailingab;
48 }
49 $transaction->commit();
fd843187 50 return $mailingab;
4aef704e 51 }
52
4aef704e 53 /**
fe482240 54 * Delete MailingAB and all its associated records.
4aef704e 55 *
90c8230e
TO
56 * @param int $id
57 * Id of the mail to delete.
4aef704e 58 */
59 public static function del($id) {
60 if (empty($id)) {
2a7b8221 61 throw new CRM_Core_Exception(ts('No id passed to MailingAB del function'));
4aef704e 62 }
353ffa53 63 CRM_Core_Transaction::create()->run(function () use ($id) {
bf0dd3d8
TO
64 CRM_Utils_Hook::pre('delete', 'MailingAB', $id, CRM_Core_DAO::$_nullArray);
65
66 $dao = new CRM_Mailing_DAO_MailingAB();
67 $dao->id = $id;
68 if ($dao->find(TRUE)) {
be2fb01f 69 $mailing_ids = [$dao->mailing_id_a, $dao->mailing_id_b, $dao->mailing_id_c];
bf0dd3d8
TO
70 $dao->delete();
71 foreach ($mailing_ids as $mailing_id) {
72 if ($mailing_id) {
73 CRM_Mailing_BAO_Mailing::del($mailing_id);
74 }
75 }
76 }
77
78 CRM_Core_Session::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
79
80 CRM_Utils_Hook::post('delete', 'MailingAB', $id, $dao);
81 });
4aef704e 82 }
768c558c
TO
83
84 /**
85 * Transfer recipients from the canonical mailing A to the other mailings.
86 *
87 * @param CRM_Mailing_DAO_MailingAB $dao
88 */
89 public static function distributeRecipients(CRM_Mailing_DAO_MailingAB $dao) {
6dd717a6 90 CRM_Mailing_BAO_Mailing::getRecipients($dao->mailing_id_a);
768c558c
TO
91
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));
95
be2fb01f 96 CRM_Mailing_BAO_Recipients::reassign($dao->mailing_id_a, [
768c558c
TO
97 $dao->mailing_id_b => (2 * $totalSelected <= $totalCount) ? $totalSelected : $totalCount - $totalSelected,
98 $dao->mailing_id_c => max(0, $totalCount - $totalSelected - $totalSelected),
be2fb01f 99 ]);
768c558c
TO
100
101 }
96025800 102
e81bac46 103 /**
104 * get abtest based on Mailing ID
105 *
106 * @param int $mailingID
107 * Mailing ID.
108 *
f72b68c0 109 * @return object
e81bac46 110 */
e81bac46 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)
116 GROUP BY ab.id";
be2fb01f 117 $params = [1 => [$mailingID, 'Integer']];
e81bac46 118 $abTest = CRM_Core_DAO::executeQuery($query, $params);
119 $abTest->fetch();
120 return $abTest;
121 }
122
4aef704e 123}