3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Class CRM_Mailing_BAO_MailingAB
37 class CRM_Mailing_BAO_MailingAB
extends CRM_Mailing_DAO_MailingAB
{
42 public function __construct() {
43 parent
::__construct();
47 * Construct a new mailingab object.
49 * @params array $params
52 * @param array $params
56 * $mailingab The new mailingab object
58 public static function create(&$params, $ids = []) {
59 $transaction = new CRM_Core_Transaction();
61 $mailingab = self
::add($params, $ids);
63 if (is_a($mailingab, 'CRM_Core_Error')) {
64 $transaction->rollback();
67 $transaction->commit();
72 * function to add the mailings.
74 * @param array $params
75 * Reference array contains the values submitted by the form.
77 * Reference array contains the id.
82 public static function add(&$params, $ids = []) {
83 $id = CRM_Utils_Array
::value('mailingab_id', $ids, CRM_Utils_Array
::value('id', $params));
86 CRM_Utils_Hook
::pre('edit', 'MailingAB', $id, $params);
89 CRM_Utils_Hook
::pre('create', 'MailingAB', NULL, $params);
92 $mailingab = new CRM_Mailing_DAO_MailingAB();
95 $mailingab->domain_id
= CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
98 $mailingab->copyValues($params);
100 $result = $mailingab->save();
103 CRM_Utils_Hook
::post('edit', 'MailingAB', $mailingab->id
, $mailingab);
106 CRM_Utils_Hook
::post('create', 'MailingAB', $mailingab->id
, $mailingab);
113 * Delete MailingAB and all its associated records.
116 * Id of the mail to delete.
118 public static function del($id) {
120 throw new CRM_Core_Exception(ts('No id passed to MailingAB del function'));
122 CRM_Core_Transaction
::create()->run(function () use ($id) {
123 CRM_Utils_Hook
::pre('delete', 'MailingAB', $id, CRM_Core_DAO
::$_nullArray);
125 $dao = new CRM_Mailing_DAO_MailingAB();
127 if ($dao->find(TRUE)) {
128 $mailing_ids = [$dao->mailing_id_a
, $dao->mailing_id_b
, $dao->mailing_id_c
];
130 foreach ($mailing_ids as $mailing_id) {
132 CRM_Mailing_BAO_Mailing
::del($mailing_id);
137 CRM_Core_Session
::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
139 CRM_Utils_Hook
::post('delete', 'MailingAB', $id, $dao);
144 * Transfer recipients from the canonical mailing A to the other mailings.
146 * @param CRM_Mailing_DAO_MailingAB $dao
148 public static function distributeRecipients(CRM_Mailing_DAO_MailingAB
$dao) {
149 CRM_Mailing_BAO_Mailing
::getRecipients($dao->mailing_id_a
);
151 //calculate total number of random recipients for mail C from group_percentage selected
152 $totalCount = CRM_Mailing_BAO_Recipients
::mailingSize($dao->mailing_id_a
);
153 $totalSelected = max(1, round(($totalCount * $dao->group_percentage
) / 100));
155 CRM_Mailing_BAO_Recipients
::reassign($dao->mailing_id_a
, [
156 $dao->mailing_id_b
=> (2 * $totalSelected <= $totalCount) ?
$totalSelected : $totalCount - $totalSelected,
157 $dao->mailing_id_c
=> max(0, $totalCount - $totalSelected - $totalSelected),
163 * get abtest based on Mailing ID
165 * @param int $mailingID
170 public static function getABTest($mailingID) {
171 $query = "SELECT * FROM `civicrm_mailing_abtest` ab
172 where (ab.mailing_id_a = %1
173 OR ab.mailing_id_b = %1
174 OR ab.mailing_id_c = %1)
176 $params = [1 => [$mailingID, 'Integer']];
177 $abTest = CRM_Core_DAO
::executeQuery($query, $params);