Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
helper.php
1<?php
2
3namespace Bitrix\ABTest;
4
7
8class Helper
9{
10
16 public static function getActiveTest()
17 {
18 static $abtest;
19 static $defined;
20
21 if (!defined('SITE_ID') || !SITE_ID)
22 return null;
23
24 if (empty($defined))
25 {
26 $cache = new \CPHPCache();
27
28 if ($cache->initCache(30*24*3600, 'abtest_active_'.SITE_ID, 'abtest/'))
29 {
30 $abtest = $cache->getVars();
31 }
32 else
33 {
34 $abtest = ABTestTable::getList(array(
35 'order' => array('SORT' => 'ASC'),
36 'filter' => array(
37 '=SITE_ID' => SITE_ID,
38 '=ACTIVE' => 'Y',
39 '<=START_DATE' => new Type\DateTime()
40 ),
41 'limit' => 1
42 ))->fetch() ?: [];
43
44 $cache->startDataCache();
45 $cache->endDataCache($abtest);
46 }
47
48 $defined = true;
49
50 if (!empty($abtest))
51 {
52 if (!$abtest['MIN_AMOUNT'])
53 {
54 $capacity = AdminHelper::getSiteCapacity($abtest['SITE_ID']);
55 if ($capacity['min'] > 0)
56 {
57 $result = ABTestTable::update($abtest['ID'], array('MIN_AMOUNT' => $capacity['min']));
58 if ($result->isSuccess())
59 {
60 $cache->clean('abtest_active_'.SITE_ID, '/abtest');
61 $abtest['MIN_AMOUNT'] = $capacity['min'];
62 }
63 }
64 }
65
66 if (intval($abtest['DURATION']) == -1)
67 {
68 if (intval($abtest['MIN_AMOUNT']) > 0)
69 {
70 $capacity = AdminHelper::getTestCapacity($abtest['ID']);
71 if ($capacity['A'] >= $abtest['MIN_AMOUNT'] && $capacity['B'] >= $abtest['MIN_AMOUNT'])
72 {
73 Helper::stopTest($abtest['ID'], true);
74 $abtest = null;
75 }
76 }
77 }
78 else if (intval($abtest['DURATION']) > 0)
79 {
80 $end = clone $abtest['START_DATE'];
81 $end->add(intval($abtest['DURATION']).' days');
82
83 if (time() > $end->format('U'))
84 {
85 Helper::stopTest($abtest['ID'], true);
86 $abtest = null;
87 }
88 }
89 }
90 }
91
92 return $abtest;
93 }
94
102 private static function context($abtest, $section)
103 {
104 return array(
105 'abtest' => intval($abtest['ID']),
106 'section' => $section,
107 'data' => $abtest['TEST_DATA']
108 );
109 }
110
116 public static function getContext()
117 {
118 global $USER, $APPLICATION;
119
120 static $context;
121
122 if (!defined('SITE_ID') || !SITE_ID)
123 return null;
124
125 if (empty($context))
126 {
127 $activeTest = Helper::getActiveTest();
128
129 $isAbtestAdmin = is_object($USER) && $USER->canDoOperation('view_other_settings');
130 if ($isAbtestAdmin && !empty($_SESSION['ABTEST_MODE']))
131 {
132 if ($_SESSION['ABTEST_MODE'] == 'reset')
133 {
134 if (!empty($activeTest))
135 $context = Helper::context($activeTest, 'N');
136
137 unset($_SESSION['ABTEST_MODE']);
138 }
139 else if (preg_match('/^(\d+)\|(A|B|N)$/', $_SESSION['ABTEST_MODE'], $matches))
140 {
141 if (!empty($activeTest) && $activeTest['ID'] == intval($matches[1]))
142 {
143 $context = Helper::context($activeTest, $matches[2]);
144
145 unset($_SESSION['ABTEST_MODE']);
146 }
147 else
148 {
149 $abtest = ABTestTable::getList(array(
150 'filter' => array('=ID' => intval($matches[1]), 'ENABLED' => 'Y')
151 ))->fetch();
152
153 if (!empty($abtest) && $abtest['SITE_ID'] == SITE_ID)
154 $context = Helper::context($abtest, $matches[2]);
155 }
156 }
157 }
158
159 $cookieValue = $APPLICATION->get_cookie('ABTEST_'.SITE_ID);
160
161 if (empty($context) && !empty($activeTest))
162 {
163 $abtest = $activeTest;
164
165 if (!empty($cookieValue))
166 {
167 if (preg_match('/^'.intval($abtest['ID']).'\|(A|B|N)$/i', $cookieValue, $matches))
168 $section = $matches[1];
169 }
170
171 if (empty($section))
172 {
173 $dice = mt_rand(1, 100);
174
175 if ($dice <= intval($abtest['PORTION'])/2)
176 $section = 'A';
177 else if ($dice <= intval($abtest['PORTION']))
178 $section = 'B';
179 else
180 $section = 'N';
181 }
182
183 $context = Helper::context($abtest, $section);
184 }
185
186 if (empty($activeTest) || $activeTest['ID'] == $context['abtest'])
187 {
188 $newValue = empty($activeTest) ? null : sprintf('%u|%s', $context['abtest'], $context['section']);
189 if (!(empty($cookieValue) && empty($newValue)) && $cookieValue !== $newValue)
190 {
191 $cookie = new Main\Web\Cookie(sprintf('ABTEST_%s', SITE_ID), $newValue);
192
193 Main\Context::getCurrent()->getResponse()->addCookie($cookie);
194 }
195 }
196 }
197
198 return $context;
199 }
200
208 public static function getAlternative($type, $value)
209 {
210 $result = null;
211
212 if ($context = Helper::getContext())
213 {
214 foreach ($context['data']['list'] as $item)
215 {
216 if ($item['type'] == $type && $item['old_value'] == $value)
217 {
218 $result = $item['new_value'];
219 break;
220 }
221 }
222 }
223
224 return $result;
225 }
226
233 public static function startTest($id)
234 {
235 global $USER;
236
237 if ($abtest = ABTestTable::getById($id)->fetch())
238 {
239 $fields = array(
240 'START_DATE' => new Type\DateTime(),
241 'STOP_DATE' => null,
242 'ACTIVE' => 'Y',
243 'USER_ID' => $USER->getID()
244 );
245
246 if (!$abtest['MIN_AMOUNT'])
247 {
248 $capacity = AdminHelper::getSiteCapacity($abtest['SITE_ID']);
249 if ($capacity['min'] > 0)
250 $fields['MIN_AMOUNT'] = $capacity['min'];
251 }
252
253 $result = ABTestTable::update(intval($id), $fields);
254
255 if ($result->isSuccess())
256 {
257 Helper::clearCache($abtest['SITE_ID']);
258
259 return true;
260 }
261 }
262
263 return false;
264 }
265
273 public static function stopTest($id, $auto = false)
274 {
275 global $USER;
276
277 if ($abtest = ABTestTable::getById($id)->fetch())
278 {
279 $fields = array(
280 'STOP_DATE' => new Type\DateTime(),
281 'ACTIVE' => 'N',
282 );
283
284 if (!$auto)
285 $fields['USER_ID'] = $USER->getID();
286
287 $result = ABTestTable::update(intval($id), $fields);
288
289 if ($result->isSuccess())
290 {
291 Helper::clearCache($abtest['SITE_ID']);
292
293 return true;
294 }
295 }
296
297 return false;
298 }
299
306 public static function deleteTest($id)
307 {
308 if ($abtest = ABTestTable::getById($id)->fetch())
309 {
310 $result = ABTestTable::delete(intval($id));
311
312 if ($result->isSuccess())
313 {
314 if ($abtest['ACTIVE'] == 'Y')
315 Helper::clearCache($abtest['SITE_ID']);
316
317 return true;
318 }
319 }
320
321 return false;
322 }
323
330 public static function clearCache($siteId)
331 {
332 $cache = new \CPHPCache();
333 $cache->clean('abtest_active_'.$siteId, '/abtest');
334 }
335
336}
static clearCache($siteId)
Definition helper.php:330
static stopTest($id, $auto=false)
Definition helper.php:273
static getActiveTest()
Definition helper.php:16
static getContext()
Definition helper.php:116
static startTest($id)
Definition helper.php:233
static getAlternative($type, $value)
Definition helper.php:208
static deleteTest($id)
Definition helper.php:306