Merge pull request #22540 from civicrm/5.46
[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
4aef704e 23 /**
fe482240 24 * Construct a new mailingab object.
4aef704e 25 *
90c8230e
TO
26 * @params array $params
27 * Form values.
4aef704e 28 *
16b10e64 29 * @param array $params
4aef704e 30 *
a63d3a16 31 * @return CRM_Mailing_DAO_MailingAB
4aef704e 32 */
a63d3a16 33 public static function create(&$params) {
4aef704e 34 $transaction = new CRM_Core_Transaction();
35
a63d3a16 36 $mailingab = self::writeRecord($params);
4aef704e 37
38 if (is_a($mailingab, 'CRM_Core_Error')) {
39 $transaction->rollback();
40 return $mailingab;
41 }
42 $transaction->commit();
fd843187 43 return $mailingab;
4aef704e 44 }
45
4aef704e 46 /**
fe482240 47 * Delete MailingAB and all its associated records.
4aef704e 48 *
90c8230e
TO
49 * @param int $id
50 * Id of the mail to delete.
4aef704e 51 */
52 public static function del($id) {
53 if (empty($id)) {
2a7b8221 54 throw new CRM_Core_Exception(ts('No id passed to MailingAB del function'));
4aef704e 55 }
353ffa53 56 CRM_Core_Transaction::create()->run(function () use ($id) {
be12df5a 57 CRM_Utils_Hook::pre('delete', 'MailingAB', $id);
bf0dd3d8
TO
58
59 $dao = new CRM_Mailing_DAO_MailingAB();
60 $dao->id = $id;
61 if ($dao->find(TRUE)) {
be2fb01f 62 $mailing_ids = [$dao->mailing_id_a, $dao->mailing_id_b, $dao->mailing_id_c];
bf0dd3d8
TO
63 $dao->delete();
64 foreach ($mailing_ids as $mailing_id) {
65 if ($mailing_id) {
66 CRM_Mailing_BAO_Mailing::del($mailing_id);
67 }
68 }
69 }
70
71 CRM_Core_Session::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
72
73 CRM_Utils_Hook::post('delete', 'MailingAB', $id, $dao);
74 });
4aef704e 75 }
768c558c
TO
76
77 /**
78 * Transfer recipients from the canonical mailing A to the other mailings.
79 *
80 * @param CRM_Mailing_DAO_MailingAB $dao
81 */
82 public static function distributeRecipients(CRM_Mailing_DAO_MailingAB $dao) {
6dd717a6 83 CRM_Mailing_BAO_Mailing::getRecipients($dao->mailing_id_a);
768c558c
TO
84
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));
88
be2fb01f 89 CRM_Mailing_BAO_Recipients::reassign($dao->mailing_id_a, [
768c558c
TO
90 $dao->mailing_id_b => (2 * $totalSelected <= $totalCount) ? $totalSelected : $totalCount - $totalSelected,
91 $dao->mailing_id_c => max(0, $totalCount - $totalSelected - $totalSelected),
be2fb01f 92 ]);
768c558c
TO
93
94 }
96025800 95
e81bac46 96 /**
97 * get abtest based on Mailing ID
98 *
99 * @param int $mailingID
100 * Mailing ID.
101 *
f72b68c0 102 * @return object
e81bac46 103 */
e81bac46 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)
109 GROUP BY ab.id";
be2fb01f 110 $params = [1 => [$mailingID, 'Integer']];
e81bac46 111 $abTest = CRM_Core_DAO::executeQuery($query, $params);
112 $abTest->fetch();
113 return $abTest;
114 }
115
4aef704e 116}