CRM-15905 fix - API: problem sorting contacts on ID
[civicrm-core.git] / api / v3 / MailingAB.php
CommitLineData
4aef704e 1<?php
4aef704e 2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
4aef704e 5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * APIv3 functions for registering/processing mailing ab testing events.
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_MailingAB
34 * @copyright CiviCRM LLC (c) 2004-2014
35 * $Id$
4aef704e 36 */
37
4aef704e 38/**
1747ab99 39 * Handle a create mailing ab testing.
4aef704e 40 *
41 * @param array $params
4aef704e 42 *
a6c01b45 43 * @return array
72b3a70c 44 * API Success Array
4aef704e 45 */
fd843187 46function civicrm_api3_mailing_a_b_create($params) {
4aef704e 47 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
48}
49
50/**
51 * Handle a delete event.
52 *
53 * @param array $params
4aef704e 54 *
a6c01b45 55 * @return array
72b3a70c 56 * API Success Array
4aef704e 57 */
fd843187 58function civicrm_api3_mailing_a_b_delete($params) {
4aef704e 59 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
60}
61
62/**
63 * Handle a get event.
64 *
65 * @param array $params
dc64d047 66 *
4aef704e 67 * @return array
68 */
fd843187 69function civicrm_api3_mailing_a_b_get($params) {
4aef704e 70 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
71}
72
7811a84b 73/**
1747ab99 74 * Adjust Metadata for submit action.
7811a84b 75 *
0aa0303c
EM
76 * The metadata is used for setting defaults, documentation & validation.
77 *
cf470720
TO
78 * @param array $params
79 * Array or parameters determined by getfields.
7811a84b 80 */
768c558c
TO
81function _civicrm_api3_mailing_a_b_submit_spec(&$params) {
82 $mailingFields = CRM_Mailing_DAO_Mailing::fields();
83 $mailingAbFields = CRM_Mailing_DAO_MailingAB::fields();
84 $spec['id'] = $mailingAbFields['id'];
85 $spec['status'] = $mailingAbFields['status'];
86 $spec['scheduled_date'] = $mailingFields['scheduled_date'];
87 $spec['approval_date'] = $mailingFields['approval_date'];
88 $spec['approval_status_id'] = $mailingFields['approval_status_id'];
89 $spec['approval_note'] = $mailingFields['approval_note'];
90 // Note: we'll pass through approval_* fields to the underlying mailing, but they may be ignored
91 // if the user doesn't have suitable permission. If separate approvals are required, they must be provided
92 // outside the A/B Test UI.
7811a84b 93}
94
95/**
1747ab99 96 * Send A/B mail to A/B recipients respectively.
7811a84b 97 *
98 * @param array $params
1747ab99 99 *
7811a84b 100 * @return array
768c558c 101 * @throws API_Exception
7811a84b 102 */
768c558c
TO
103function civicrm_api3_mailing_a_b_submit($params) {
104 civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_MailingAB', array('id', 'status'));
105
106 if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) {
107 throw new API_Exception("Missing parameter scheduled_date and/or approval_date");
108 }
7811a84b 109
768c558c
TO
110 $dao = new CRM_Mailing_DAO_MailingAB();
111 $dao->id = $params['id'];
112 if (!$dao->find(TRUE)) {
113 throw new API_Exception("Failed to locate A/B test by ID");
7811a84b 114 }
768c558c
TO
115 if (empty($dao->mailing_id_a) || empty($dao->mailing_id_b) || empty($dao->mailing_id_c)) {
116 throw new API_Exception("Missing mailing IDs for A/B test");
7811a84b 117 }
118
768c558c
TO
119 $submitParams = CRM_Utils_Array::subset($params, array(
120 'scheduled_date',
121 'approval_date',
122 'approval_note',
123 'approval_status_id',
124 ));
125
126 switch ($params['status']) {
127 case 'Testing':
128 if (!empty($dao->status) && $dao->status != 'Draft') {
129 throw new API_Exception("Cannot transition to state 'Testing'");
130 }
131 civicrm_api3('Mailing', 'submit', $submitParams + array(
132 'id' => $dao->mailing_id_a,
133 '_skip_evil_bao_auto_recipients_' => 0,
134 ));
135 civicrm_api3('Mailing', 'submit', $submitParams + array(
136 'id' => $dao->mailing_id_b,
137 '_skip_evil_bao_auto_recipients_' => 1,
138 ));
139 CRM_Mailing_BAO_MailingAB::distributeRecipients($dao);
140 break;
7811a84b 141
768c558c
TO
142 case 'Final':
143 if ($dao->status != 'Testing') {
144 throw new API_Exception("Cannot transition to state 'Final'");
145 }
146 civicrm_api3('Mailing', 'submit', $submitParams + array(
147 'id' => $dao->mailing_id_c,
148 '_skip_evil_bao_auto_recipients_' => 1,
149 ));
150 break;
151
152 default:
153 throw new API_Exception("Unrecognized submission status");
7811a84b 154 }
155
768c558c
TO
156 return civicrm_api3('MailingAB', 'create', array(
157 'id' => $dao->id,
158 'status' => $params['status'],
159 'options' => array(
160 'reload' => 1,
161 ),
162 ));
467cd00c 163}
164
165/**
1747ab99 166 * Adjust Metadata for graph_stats action.
467cd00c 167 *
0aa0303c
EM
168 * The metadata is used for setting defaults, documentation & validation.
169 *
cf470720
TO
170 * @param array $params
171 * Array or parameters determined by getfields.
467cd00c 172 */
173function _civicrm_api3_mailing_a_b_graph_stats_spec(&$params) {
360aaa75
TO
174 $params['criteria']['title'] = 'Criteria';
175 $params['criteria']['default'] = 'Open';
176 // mailing_ab_winner_criteria
177 $params['target_date']['title'] = 'Target Date';
178 $params['target_date']['type'] = CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME;
bd6658bd 179 $params['split_count']['title'] = 'Split Count';
202ebbab 180 $params['split_count']['api.default'] = 6;
bd6658bd 181 $params['split_count_select']['title'] = 'Split Count Select';
202ebbab 182 $params['split_count_select']['api.required'] = 1;
360aaa75 183 $params['target_url']['title'] = 'Target URL';
202ebbab 184}
467cd00c 185
186/**
1747ab99 187 * Send graph detail for A/B tests mail.
467cd00c 188 *
189 * @param array $params
1747ab99 190 *
467cd00c 191 * @return array
360aaa75 192 * @throws API_Exception
467cd00c 193 */
194function civicrm_api3_mailing_a_b_graph_stats($params) {
202ebbab 195 civicrm_api3_verify_mandatory($params,
196 'CRM_Mailing_DAO_MailingAB',
197 array('id'),
198 FALSE
199 );
467cd00c 200
360aaa75
TO
201 $defaults = array(
202 'criteria' => 'Open',
203 'target_date' => CRM_Utils_Time::getTime('YmdHis'),
204 'split_count' => 6,
205 'split_count_select' => 1,
206 );
207 $params = array_merge($defaults, $params);
202ebbab 208
360aaa75 209 $mailingAB = civicrm_api3('MailingAB', 'getsingle', array('id' => $params['id']));
202ebbab 210 $graphStats = array();
467cd00c 211 $ABFormat = array('A' => 'mailing_id_a', 'B' => 'mailing_id_b');
212
202ebbab 213 foreach ($ABFormat as $name => $column) {
360aaa75
TO
214 switch (strtolower($params['criteria'])) {
215 case 'open':
768c558c
TO
216 $result = CRM_Mailing_Event_BAO_Opened::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_opened.time_stamp ASC");
217 $startDate = CRM_Utils_Date::processDate($result[0]['date']);
360aaa75
TO
218 $targetDate = CRM_Utils_Date::processDate($params['target_date']);
219 $dateDuration = round(round(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']);
202ebbab 220 $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']);
221 $toDate = date('YmdHis', $toDate);
222 $graphStats[$name] = array(
223 $params['split_count_select'] => array(
224 'count' => CRM_Mailing_Event_BAO_Opened::getTotalCount($mailingAB[$column], NULL, TRUE, $toDate),
21dfd5f5
TO
225 'time' => CRM_Utils_Date::customFormat($toDate),
226 ),
202ebbab 227 );
228 break;
ea100cb5 229
360aaa75 230 case 'total unique clicks':
202ebbab 231 $result = CRM_Mailing_Event_BAO_TrackableURLOpen::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_trackable_url_open.time_stamp ASC");
232 $startDate = CRM_Utils_Date::processDate($result[0]['date']);
360aaa75
TO
233 $targetDate = CRM_Utils_Date::processDate($params['target_date']);
234 $dateDuration = round(abs(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']);
202ebbab 235 $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']);
236 $toDate = date('YmdHis', $toDate);
237 $graphStats[$name] = array(
238 $params['split_count_select'] => array(
239 'count' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], NULL, FALSE, NULL, $toDate),
21dfd5f5
TO
240 'time' => CRM_Utils_Date::customFormat($toDate),
241 ),
202ebbab 242 );
243 break;
ea100cb5 244
360aaa75
TO
245 case 'total clicks on a particular link':
246 if (empty($params['target_url'])) {
247 throw new API_Exception("Provide url to get stats result for total clicks on a particular link");
202ebbab 248 }
360aaa75
TO
249 // FIXME: doesn't make sense to get url_id mailing_id_(a|b) while getting start date in mailing_id_a
250 $url_id = CRM_Mailing_BAO_TrackableURL::getTrackerURLId($mailingAB[$column], $params['target_url']);
202ebbab 251 $result = CRM_Mailing_Event_BAO_TrackableURLOpen::getRows($mailingAB['mailing_id_a'], NULL, FALSE, $url_id, 0, 1, "civicrm_mailing_event_trackable_url_open.time_stamp ASC");
252 $startDate = CRM_Utils_Date::processDate($result[0]['date']);
360aaa75
TO
253 $targetDate = CRM_Utils_Date::processDate($params['target_date']);
254 $dateDuration = round(abs(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']);
202ebbab 255 $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']);
256 $toDate = CRM_Utils_Date::processDate($toDate);
257 $graphStats[$name] = array(
258 $params['split_count_select'] => array(
259 'count' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], NULL, FALSE, $url_id, $toDate),
21dfd5f5
TO
260 'time' => CRM_Utils_Date::customFormat($toDate),
261 ),
202ebbab 262 );
263 break;
264 }
265 }
266
267 return civicrm_api3_create_success($graphStats);
b0f9e1df 268}