Merge pull request #22509 from braders/phpdoc-fixes
[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 * Construct a new mailingab object.
25 *
26 * @params array $params
27 * Form values.
28 *
29 * @param array $params
30 *
31 * @return CRM_Mailing_DAO_MailingAB
32 */
33 public static function create(&$params) {
34 $transaction = new CRM_Core_Transaction();
35
36 $mailingab = self::writeRecord($params);
37
38 if (is_a($mailingab, 'CRM_Core_Error')) {
39 $transaction->rollback();
40 return $mailingab;
41 }
42 $transaction->commit();
43 return $mailingab;
44 }
45
46 /**
47 * Delete MailingAB and all its associated records.
48 *
49 * @param int $id
50 * Id of the mail to delete.
51 */
52 public static function del($id) {
53 if (empty($id)) {
54 throw new CRM_Core_Exception(ts('No id passed to MailingAB del function'));
55 }
56 CRM_Core_Transaction::create()->run(function () use ($id) {
57 CRM_Utils_Hook::pre('delete', 'MailingAB', $id);
58
59 $dao = new CRM_Mailing_DAO_MailingAB();
60 $dao->id = $id;
61 if ($dao->find(TRUE)) {
62 $mailing_ids = [$dao->mailing_id_a, $dao->mailing_id_b, $dao->mailing_id_c];
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 });
75 }
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) {
83 CRM_Mailing_BAO_Mailing::getRecipients($dao->mailing_id_a);
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
89 CRM_Mailing_BAO_Recipients::reassign($dao->mailing_id_a, [
90 $dao->mailing_id_b => (2 * $totalSelected <= $totalCount) ? $totalSelected : $totalCount - $totalSelected,
91 $dao->mailing_id_c => max(0, $totalCount - $totalSelected - $totalSelected),
92 ]);
93
94 }
95
96 /**
97 * get abtest based on Mailing ID
98 *
99 * @param int $mailingID
100 * Mailing ID.
101 *
102 * @return object
103 */
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";
110 $params = [1 => [$mailingID, 'Integer']];
111 $abTest = CRM_Core_DAO::executeQuery($query, $params);
112 $abTest->fetch();
113 return $abTest;
114 }
115
116 }