Short array syntax - auto-format CRM directory
[civicrm-core.git] / CRM / Mailing / BAO / MailingAB.php
CommitLineData
4aef704e 1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
4aef704e 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
4aef704e 7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
4aef704e 27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
4aef704e 32 */
33
34/**
35 * Class CRM_Mailing_BAO_MailingAB
36 */
37class CRM_Mailing_BAO_MailingAB extends CRM_Mailing_DAO_MailingAB {
38
39 /**
fe482240 40 * class constructor.
4aef704e 41 */
00be9182 42 public function __construct() {
4aef704e 43 parent::__construct();
44 }
45
46 /**
fe482240 47 * Construct a new mailingab object.
4aef704e 48 *
90c8230e
TO
49 * @params array $params
50 * Form values.
4aef704e 51 *
16b10e64 52 * @param array $params
4aef704e 53 * @param array $ids
54 *
a6c01b45
CW
55 * @return object
56 * $mailingab The new mailingab object
4aef704e 57 */
be2fb01f 58 public static function create(&$params, $ids = []) {
4aef704e 59 $transaction = new CRM_Core_Transaction();
60
61 $mailingab = self::add($params, $ids);
62
63 if (is_a($mailingab, 'CRM_Core_Error')) {
64 $transaction->rollback();
65 return $mailingab;
66 }
67 $transaction->commit();
fd843187 68 return $mailingab;
4aef704e 69 }
70
71 /**
fe482240 72 * function to add the mailings.
4aef704e 73 *
90c8230e
TO
74 * @param array $params
75 * Reference array contains the values submitted by the form.
76 * @param array $ids
77 * Reference array contains the id.
4aef704e 78 *
4aef704e 79 *
80 * @return object
81 */
be2fb01f 82 public static function add(&$params, $ids = []) {
4aef704e 83 $id = CRM_Utils_Array::value('mailingab_id', $ids, CRM_Utils_Array::value('id', $params));
84
85 if ($id) {
86 CRM_Utils_Hook::pre('edit', 'MailingAB', $id, $params);
87 }
88 else {
89 CRM_Utils_Hook::pre('create', 'MailingAB', NULL, $params);
90 }
91
92 $mailingab = new CRM_Mailing_DAO_MailingAB();
93 $mailingab->id = $id;
bd6658bd
TO
94 if (!$id) {
95 $mailingab->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
96 }
4aef704e 97
98 $mailingab->copyValues($params);
99
100 $result = $mailingab->save();
101
102 if ($id) {
103 CRM_Utils_Hook::post('edit', 'MailingAB', $mailingab->id, $mailingab);
104 }
105 else {
106 CRM_Utils_Hook::post('create', 'MailingAB', $mailingab->id, $mailingab);
107 }
108
109 return $result;
110 }
111
112
113 /**
fe482240 114 * Delete MailingAB and all its associated records.
4aef704e 115 *
90c8230e
TO
116 * @param int $id
117 * Id of the mail to delete.
4aef704e 118 */
119 public static function del($id) {
120 if (empty($id)) {
121 CRM_Core_Error::fatal();
122 }
353ffa53 123 CRM_Core_Transaction::create()->run(function () use ($id) {
bf0dd3d8
TO
124 CRM_Utils_Hook::pre('delete', 'MailingAB', $id, CRM_Core_DAO::$_nullArray);
125
126 $dao = new CRM_Mailing_DAO_MailingAB();
127 $dao->id = $id;
128 if ($dao->find(TRUE)) {
be2fb01f 129 $mailing_ids = [$dao->mailing_id_a, $dao->mailing_id_b, $dao->mailing_id_c];
bf0dd3d8
TO
130 $dao->delete();
131 foreach ($mailing_ids as $mailing_id) {
132 if ($mailing_id) {
133 CRM_Mailing_BAO_Mailing::del($mailing_id);
134 }
135 }
136 }
137
138 CRM_Core_Session::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
139
140 CRM_Utils_Hook::post('delete', 'MailingAB', $id, $dao);
141 });
4aef704e 142 }
768c558c
TO
143
144 /**
145 * Transfer recipients from the canonical mailing A to the other mailings.
146 *
147 * @param CRM_Mailing_DAO_MailingAB $dao
148 */
149 public static function distributeRecipients(CRM_Mailing_DAO_MailingAB $dao) {
6dd717a6 150 CRM_Mailing_BAO_Mailing::getRecipients($dao->mailing_id_a);
768c558c
TO
151
152 //calculate total number of random recipients for mail C from group_percentage selected
153 $totalCount = CRM_Mailing_BAO_Recipients::mailingSize($dao->mailing_id_a);
154 $totalSelected = max(1, round(($totalCount * $dao->group_percentage) / 100));
155
be2fb01f 156 CRM_Mailing_BAO_Recipients::reassign($dao->mailing_id_a, [
768c558c
TO
157 $dao->mailing_id_b => (2 * $totalSelected <= $totalCount) ? $totalSelected : $totalCount - $totalSelected,
158 $dao->mailing_id_c => max(0, $totalCount - $totalSelected - $totalSelected),
be2fb01f 159 ]);
768c558c
TO
160
161 }
96025800 162
e81bac46 163 /**
164 * get abtest based on Mailing ID
165 *
166 * @param int $mailingID
167 * Mailing ID.
168 *
f72b68c0 169 * @return object
e81bac46 170 */
e81bac46 171 public static function getABTest($mailingID) {
172 $query = "SELECT * FROM `civicrm_mailing_abtest` ab
173 where (ab.mailing_id_a = %1
174 OR ab.mailing_id_b = %1
175 OR ab.mailing_id_c = %1)
176 GROUP BY ab.id";
be2fb01f 177 $params = [1 => [$mailingID, 'Integer']];
e81bac46 178 $abTest = CRM_Core_DAO::executeQuery($query, $params);
179 $abTest->fetch();
180 return $abTest;
181 }
182
4aef704e 183}