Cleanup MailingAB BAO
[civicrm-core.git] / CRM / Mailing / BAO / MailingAB.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class CRM_Mailing_BAO_MailingAB
20 */
21 class CRM_Mailing_BAO_MailingAB extends CRM_Mailing_DAO_MailingAB {
22
23 /**
24 * class constructor.
25 */
26 public function __construct() {
27 parent::__construct();
28 }
29
30 /**
31 * Construct a new mailingab object.
32 *
33 * @params array $params
34 * Form values.
35 *
36 * @param array $params
37 *
38 * @return CRM_Mailing_DAO_MailingAB
39 */
40 public static function create(&$params) {
41 $transaction = new CRM_Core_Transaction();
42
43 $mailingab = self::writeRecord($params);
44
45 if (is_a($mailingab, 'CRM_Core_Error')) {
46 $transaction->rollback();
47 return $mailingab;
48 }
49 $transaction->commit();
50 return $mailingab;
51 }
52
53 /**
54 * Delete MailingAB and all its associated records.
55 *
56 * @param int $id
57 * Id of the mail to delete.
58 */
59 public static function del($id) {
60 if (empty($id)) {
61 throw new CRM_Core_Exception(ts('No id passed to MailingAB del function'));
62 }
63 CRM_Core_Transaction::create()->run(function () use ($id) {
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)) {
69 $mailing_ids = [$dao->mailing_id_a, $dao->mailing_id_b, $dao->mailing_id_c];
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 });
82 }
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) {
90 CRM_Mailing_BAO_Mailing::getRecipients($dao->mailing_id_a);
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
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),
99 ]);
100
101 }
102
103 /**
104 * get abtest based on Mailing ID
105 *
106 * @param int $mailingID
107 * Mailing ID.
108 *
109 * @return object
110 */
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";
117 $params = [1 => [$mailingID, 'Integer']];
118 $abTest = CRM_Core_DAO::executeQuery($query, $params);
119 $abTest->fetch();
120 return $abTest;
121 }
122
123 }