1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
daycontext.php
См. документацию.
1<?php
2
3namespace Bitrix\Conversion;
4
5use Bitrix\Main;
6use Bitrix\Main\Application;
7use Bitrix\Main\Data\LocalStorage\SessionLocalStorage;
8use Bitrix\Main\SiteTable;
9use Bitrix\Main\EventManager;
10use Bitrix\Main\Web\Json;
11use Bitrix\Main\Type\Date;
12use Bitrix\Main\ArgumentException;
13use Bitrix\Main\ArgumentTypeException;
14
16{
18 private static self $instance;
19
20 private static array $contextData;
21
30 public function addCounter($day, $name, $value = null): void
31 {
32 if (func_num_args() === 2)
33 {
34 $value = $name;
35 $name = $day;
36 $day = new Date();
37 }
38
39 $instance = self::getInstance();
40
41 if ($this->getId() === null && $this === $instance)
42 {
43 $context = self::getContextData();
44 $context['PENDING_COUNTERS'] ??= [];
45 $context['PENDING_COUNTERS'][$name] ??= 0;
46 $context['PENDING_COUNTERS'][$name] += (float)$value;
47 self::setContextData($context);
48 unset($context);
49 }
50 else
51 {
52 parent::addCounter($day, $name, $value);
53 }
54 }
55
63 public function addDayCounter($name, $value): void
64 {
65 $instance = self::getInstance();
66
67 if ($this->getId() === null && $this === $instance)
68 {
69 $context = self::getContextData();
70 $context['PENDING_DAY_COUNTERS'] ??= [];
71 $context['PENDING_DAY_COUNTERS'][$name] = (float)$value;
72 self::setContextData($context);
73 unset($context);
74 }
75 else
76 {
77 $context = self::getContextData();
78 if (!in_array($name, $context['UNIQUE'], true))
79 {
80 $context['UNIQUE'][] = $name;
81 self::setContextData($context);
82
83 $this->addCounter(new Date(), $name, $value);
84 $this->setCookie();
85 }
86 unset($context);
87 }
88 }
89
98 public function subDayCounter($day, $name, $value): void
99 {
100 $this->subCounter($day, $name, $value);
101
102 // is today - clear session
103 $isToday = $day instanceof Date && $day->format('dmY') === date('dmY');
104 if ($isToday)
105 {
106 $context = self::getContextData();
107 $i = array_search($name, $context['UNIQUE'], true);
108 if ($i !== false)
109 {
110 unset($context['UNIQUE'][$i]);
111 self::setContextData($context);
112 $this->setCookie();
113 }
114 }
115 }
116
125 public function addCurrencyCounter($name, $value, $currency): void
126 {
128 }
129
139 public function subCurrencyCounter($day, $name, $value, $currency): void
140 {
142 }
143
152 public function attachEntityItem($entity, $item): void
153 {
154 if (!is_string($entity))
155 {
156 throw new ArgumentTypeException('entity', 'string');
157 }
158
159 if (!is_scalar($item))
160 {
161 throw new ArgumentTypeException('item', 'scalar');
162 }
163
164 $instance = self::getInstance();
165
166 if ($this->getId() === null && $this === $instance)
167 {
168 $context = self::getContextData();
169 $context['PENDING_ENTITY_ITEMS'] ??= [];
170 $context['PENDING_ENTITY_ITEMS'][$entity . ':' . $item] = [
171 'ENTITY' => $entity,
172 'ITEM' => $item,
173 ];
174 self::setContextData($context);
175 unset($context);
176 }
177 else
178 {
179 try
180 {
181 Internals\ContextEntityItemTable::add([
182 'CONTEXT_ID' => $this->id,
183 'ENTITY' => $entity,
184 'ITEM' => $item,
185 ]);
186 }
187 catch (\Bitrix\Main\DB\SqlQueryException)
188 {
189 }
190 }
191 }
192
200 public static function getEntityItemInstance($entity, $item): self
201 {
202 $instance = self::getInstance();
203
204 $context = Internals\ContextEntityItemTable::getRow([
205 'select' => [
206 'CONTEXT_ID',
207 ],
208 'filter' => [
209 '=ENTITY' => $entity,
210 '=ITEM' => $item,
211 ],
212 ]);
213
214 $contextId = (int)(!empty($context['CONTEXT_ID']) ? $context['CONTEXT_ID'] : self::EMPTY_CONTEXT_ID);
215 if ($contextId !== $instance->getId())
216 {
217 $instance = new self;
218 $instance->setId($contextId);
219 }
220
221 return $instance;
222 }
223
230 public static function getSiteInstance($siteId): self
231 {
232 $siteId = (string)$siteId;
233
234 $instance = self::getInstance();
235
236 if (preg_match('/[a-z0-9_]{2}/i', $siteId) && self::getSiteId() !== $siteId && \CSite::getById($siteId)->fetch())
237 {
238 $instance = new self;
239
240 $eventManager = EventManager::getInstance();
241 foreach ($eventManager->findEventHandlers('conversion', 'OnSetDayContextAttributes') as $handler)
242 {
244 $handler,
245 [
246 $instance,
247 ]
248 );
249 }
250 unset($eventManager);
251
252 $instance->setAttribute('conversion_site', $siteId);
253 $instance->save();
254 }
255
256 return $instance;
257 }
258
264 public static function getInstance(): self
265 {
266 if (!isset(self::$instance))
267 {
268 $instance = new self;
269
270 $currentContext = self::getDataFromStorage();
271 if ($currentContext === null)
272 {
273 $currentContext = self::getDataFromCookie();
274 }
275 if ($currentContext === null)
276 {
277 $currentContext = self::getDefaultData();
278 }
279 self::setContextData($currentContext);
280 $instance->setId($currentContext['ID']);
281
282 self::$instance = $instance;
283 }
284
285 return self::$instance;
286 }
287
289 private function setCookie(): void
290 {
291 //$session = self::$session;
292 $session = self::getContextData();
293
294 $cookie = new Main\Web\Cookie(
295 self::getVarName(),
296 Json::encode([
297 'ID' => $session['ID'],
298 'EXPIRE' => $session['EXPIRE'],
299 'UNIQUE' => $session['UNIQUE'],
300 ]),
301 strtotime('+1 year'),
302 false
303 );
304 $cookie->setHttpOnly(false);
305
306 Main\Context::getCurrent()->getResponse()->addCookie($cookie);
307 }
308
310 public static function saveInstance(): void
311 {
312 $instance = self::getInstance();
313
314 if ($instance->getId() === null)
315 {
316 $eventManager = EventManager::getInstance();
317 foreach ($eventManager->findEventHandlers('conversion', 'OnSetDayContextAttributes') as $handler)
318 {
320 $handler,
321 [
322 $instance,
323 ]
324 );
325 }
326 unset($eventManager);
327
328 $instance->save();
329 }
330
331 $session = self::getContextData();
332 $session['ID'] = $instance->getId();
333 self::setContextData($session);
334 $instance->setCookie();
335
336 if (!empty($session['PENDING_COUNTERS']) && is_array($session['PENDING_COUNTERS']))
337 {
338 $date = new Date();
339 foreach ($session['PENDING_COUNTERS'] as $name => $value)
340 {
341 $instance->addCounter($date, $name, $value);
342 }
343 unset($date);
344 }
345
346 if (!empty($session['PENDING_DAY_COUNTERS']) && is_array($session['PENDING_DAY_COUNTERS']))
347 {
348 foreach ($session['PENDING_DAY_COUNTERS'] as $name => $value)
349 {
350 $instance->addDayCounter($name, $value);
351 }
352 }
353
354 if (!empty($session['PENDING_ENTITY_ITEMS']) && is_array($session['PENDING_ENTITY_ITEMS']))
355 {
356 foreach ($session['PENDING_ENTITY_ITEMS'] as $i)
357 {
358 $instance->attachEntityItem($i['ENTITY'], $i['ITEM']);
359 }
360 }
361 }
362
364 public static function getVarName()
365 {
366 static $name;
367
368 if (!$name)
369 {
370 $name = 'BITRIX_CONVERSION_CONTEXT_' . self::getSiteId();
371 }
372
373 return $name;
374 }
375
377 public static function getSiteId()
378 {
379 static $siteId = null;
380
381 if ($siteId === null)
382 {
383 if (defined('ADMIN_SECTION') && ADMIN_SECTION === true)
384 {
385 $row = SiteTable::getRow([
386 'select' => [
387 'ID',
388 'DEF',
389 'SORT',
390 ],
391 'order' => [
392 'DEF' => 'DESC',
393 'SORT' => 'ASC',
394 ],
395 'cache' => [
396 'ttl' => 86400,
397 ],
398 ]);
399 if ($row)
400 {
401 $siteId = $row['ID'];
402 }
403 }
404 else
405 {
407 }
408 }
409
410 return $siteId;
411 }
412
413 private static function getLocalStorage(): SessionLocalStorage
414 {
415 return Application::getInstance()->getLocalSession(self::getVarName());
416 }
417
418 private static function getDataFromStorage(): ?array
419 {
420 $storage = self::getLocalStorage();
421 $data = $storage->getData();
422
423 return self::checkStorageData($data) ? $data : null;
424 }
425
426 private static function setDataToStorage(array $data): void
427 {
428 $storage = self::getLocalStorage();
429 $storage->setData($data);
430 }
431
432 private static function checkStorageData(mixed $data): bool
433 {
434 if (!is_array($data))
435 {
436 return false;
437 }
438 if (!is_int($data['ID'] ?? null))
439 {
440 return false;
441 }
442 if (($data['EXPIRE'] ?? null) !== self::getCurrentExpireValue())
443 {
444 return false;
445 }
446 if (!is_array($data['UNIQUE'] ?? null))
447 {
448 return false;
449 }
450
451 return true;
452 }
453
454 private static function getCurrentExpireValue(): int
455 {
456 $result = strtotime('today 23:59');
457
458 return $result === false ? 0 : $result;
459 }
460
461 private static function getDefaultData(): array
462 {
463 return [
464 'ID' => null,
465 'EXPIRE' => self::getCurrentExpireValue(),
466 'UNIQUE' => [],
467 ];
468 }
469
470 private static function getDataFromCookie(): ?array
471 {
472 $request = Main\Context::getCurrent()->getRequest();
473
474 $cookie = $request->getCookie(self::getVarName());
475 if ($cookie === null || $cookie === '')
476 {
477 return null;
478 }
479 try
480 {
481 $data = Json::decode($cookie);
482 }
483 catch (ArgumentException)
484 {
485 $data = null;
486 }
487
488 return self::checkCookieData($data) ? $data : null;
489 }
490
491 private static function checkCookieData(mixed $cookie): bool
492 {
493 if (!is_array($cookie))
494 {
495 return false;
496 }
497 if (!is_array($cookie['UNIQUE'] ?? null))
498 {
499 return false;
500 }
501 if (($cookie['EXPIRE'] ?? null) !== self::getCurrentExpireValue())
502 {
503 return false;
504 }
505
506 $id = $cookie['ID'] ?? null;
507 if (!is_int($id))
508 {
509 return false;
510 }
511 if ($id === self::EMPTY_CONTEXT_ID)
512 {
513 return true;
514 }
515
516 $row = Internals\ContextTable::getRow([
517 'select' => [
518 'ID',
519 ],
520 'filter' => [
521 '=ID' => $id,
522 ],
523 ]);
524
525 return $row !== null;
526 }
527
528 private static function setContextData(array $data): void
529 {
530 self::$contextData = $data;
531 self::setDataToStorage(self::$contextData);
532 }
533
534 private static function getContextData(): array
535 {
536 return self::$contextData;
537 }
538}
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
static getSiteInstance($siteId)
Определения daycontext.php:230
static getEntityItemInstance($entity, $item)
Определения daycontext.php:200
addDayCounter($name, $value)
Определения daycontext.php:63
subDayCounter($day, $name, $value)
Определения daycontext.php:98
subCurrencyCounter($day, $name, $value, $currency)
Определения daycontext.php:139
static getSiteId()
Определения daycontext.php:377
static saveInstance()
Определения daycontext.php:310
attachEntityItem($entity, $item)
Определения daycontext.php:152
addCounter($day, $name, $value=null)
Определения daycontext.php:30
static getInstance()
Определения daycontext.php:264
static getVarName()
Определения daycontext.php:364
addCurrencyCounter($name, $value, $currency)
Определения daycontext.php:125
subCounter($day, $name, $value=1)
Определения basecontext.php:100
static convertToBaseCurrency($value, $currency)
Определения utils.php:10
Определения date.php:9
format($format)
Определения date.php:110
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$entity
$context
Определения csv_new_setup.php:223
$siteId
Определения ajax.php:8
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
$name
Определения menu_edit.php:35
Определения arrayresult.php:2
if(empty($decryptedData)) $storage
Определения quickway.php:270
const ADMIN_SECTION
Определения rss.php:2
$i
Определения factura.php:643
$currency
Определения template.php:266
$eventManager
Определения include.php:412
const SITE_ID
Определения sonet_set_content_view.php:12