Merge pull request #15338 from totten/master-poc-postcommit
[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 * @param array $ids
38 *
39 * @return object
40 * $mailingab The new mailingab object
41 */
42 public static function create(&$params, $ids = []) {
43 $transaction = new CRM_Core_Transaction();
44
45 $mailingab = self::add($params, $ids);
46
47 if (is_a($mailingab, 'CRM_Core_Error')) {
48 $transaction->rollback();
49 return $mailingab;
50 }
51 $transaction->commit();
52 return $mailingab;
53 }
54
55 /**
56 * function to add the mailings.
57 *
58 * @param array $params
59 * Reference array contains the values submitted by the form.
60 * @param array $ids
61 * Reference array contains the id.
62 *
63 *
64 * @return object
65 */
66 public static function add(&$params, $ids = []) {
67 $id = CRM_Utils_Array::value('mailingab_id', $ids, CRM_Utils_Array::value('id', $params));
68
69 if ($id) {
70 CRM_Utils_Hook::pre('edit', 'MailingAB', $id, $params);
71 }
72 else {
73 CRM_Utils_Hook::pre('create', 'MailingAB', NULL, $params);
74 }
75
76 $mailingab = new CRM_Mailing_DAO_MailingAB();
77 $mailingab->id = $id;
78 if (!$id) {
79 $mailingab->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
80 }
81
82 $mailingab->copyValues($params);
83
84 $result = $mailingab->save();
85
86 if ($id) {
87 CRM_Utils_Hook::post('edit', 'MailingAB', $mailingab->id, $mailingab);
88 }
89 else {
90 CRM_Utils_Hook::post('create', 'MailingAB', $mailingab->id, $mailingab);
91 }
92
93 return $result;
94 }
95
96 /**
97 * Delete MailingAB and all its associated records.
98 *
99 * @param int $id
100 * Id of the mail to delete.
101 */
102 public static function del($id) {
103 if (empty($id)) {
104 throw new CRM_Core_Exception(ts('No id passed to MailingAB del function'));
105 }
106 CRM_Core_Transaction::create()->run(function () use ($id) {
107 CRM_Utils_Hook::pre('delete', 'MailingAB', $id, CRM_Core_DAO::$_nullArray);
108
109 $dao = new CRM_Mailing_DAO_MailingAB();
110 $dao->id = $id;
111 if ($dao->find(TRUE)) {
112 $mailing_ids = [$dao->mailing_id_a, $dao->mailing_id_b, $dao->mailing_id_c];
113 $dao->delete();
114 foreach ($mailing_ids as $mailing_id) {
115 if ($mailing_id) {
116 CRM_Mailing_BAO_Mailing::del($mailing_id);
117 }
118 }
119 }
120
121 CRM_Core_Session::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
122
123 CRM_Utils_Hook::post('delete', 'MailingAB', $id, $dao);
124 });
125 }
126
127 /**
128 * Transfer recipients from the canonical mailing A to the other mailings.
129 *
130 * @param CRM_Mailing_DAO_MailingAB $dao
131 */
132 public static function distributeRecipients(CRM_Mailing_DAO_MailingAB $dao) {
133 CRM_Mailing_BAO_Mailing::getRecipients($dao->mailing_id_a);
134
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));
138
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),
142 ]);
143
144 }
145
146 /**
147 * get abtest based on Mailing ID
148 *
149 * @param int $mailingID
150 * Mailing ID.
151 *
152 * @return object
153 */
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)
159 GROUP BY ab.id";
160 $params = [1 => [$mailingID, 'Integer']];
161 $abTest = CRM_Core_DAO::executeQuery($query, $params);
162 $abTest->fetch();
163 return $abTest;
164 }
165
166 }