Commit | Line | Data |
---|---|---|
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 | */ | |
21 | class 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 | * @param array $ids |
38 | * | |
a6c01b45 CW |
39 | * @return object |
40 | * $mailingab The new mailingab object | |
4aef704e | 41 | */ |
be2fb01f | 42 | public static function create(&$params, $ids = []) { |
4aef704e | 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(); | |
fd843187 | 52 | return $mailingab; |
4aef704e | 53 | } |
54 | ||
55 | /** | |
fe482240 | 56 | * function to add the mailings. |
4aef704e | 57 | * |
90c8230e TO |
58 | * @param array $params |
59 | * Reference array contains the values submitted by the form. | |
60 | * @param array $ids | |
61 | * Reference array contains the id. | |
4aef704e | 62 | * |
4aef704e | 63 | * |
64 | * @return object | |
65 | */ | |
be2fb01f | 66 | public static function add(&$params, $ids = []) { |
4aef704e | 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; | |
bd6658bd TO |
78 | if (!$id) { |
79 | $mailingab->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); | |
80 | } | |
4aef704e | 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 | ||
4aef704e | 96 | /** |
fe482240 | 97 | * Delete MailingAB and all its associated records. |
4aef704e | 98 | * |
90c8230e TO |
99 | * @param int $id |
100 | * Id of the mail to delete. | |
4aef704e | 101 | */ |
102 | public static function del($id) { | |
103 | if (empty($id)) { | |
2a7b8221 | 104 | throw new CRM_Core_Exception(ts('No id passed to MailingAB del function')); |
4aef704e | 105 | } |
353ffa53 | 106 | CRM_Core_Transaction::create()->run(function () use ($id) { |
bf0dd3d8 TO |
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)) { | |
be2fb01f | 112 | $mailing_ids = [$dao->mailing_id_a, $dao->mailing_id_b, $dao->mailing_id_c]; |
bf0dd3d8 TO |
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 | }); | |
4aef704e | 125 | } |
768c558c TO |
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) { | |
6dd717a6 | 133 | CRM_Mailing_BAO_Mailing::getRecipients($dao->mailing_id_a); |
768c558c TO |
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 | ||
be2fb01f | 139 | CRM_Mailing_BAO_Recipients::reassign($dao->mailing_id_a, [ |
768c558c TO |
140 | $dao->mailing_id_b => (2 * $totalSelected <= $totalCount) ? $totalSelected : $totalCount - $totalSelected, |
141 | $dao->mailing_id_c => max(0, $totalCount - $totalSelected - $totalSelected), | |
be2fb01f | 142 | ]); |
768c558c TO |
143 | |
144 | } | |
96025800 | 145 | |
e81bac46 | 146 | /** |
147 | * get abtest based on Mailing ID | |
148 | * | |
149 | * @param int $mailingID | |
150 | * Mailing ID. | |
151 | * | |
f72b68c0 | 152 | * @return object |
e81bac46 | 153 | */ |
e81bac46 | 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"; | |
be2fb01f | 160 | $params = [1 => [$mailingID, 'Integer']]; |
e81bac46 | 161 | $abTest = CRM_Core_DAO::executeQuery($query, $params); |
162 | $abTest->fetch(); | |
163 | return $abTest; | |
164 | } | |
165 | ||
4aef704e | 166 | } |