Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
daycontext.php
1<?php
2
3namespace Bitrix\Conversion;
4
12
14{
23 public function addCounter($day, $name, $value = null)
24 {
25 if (func_num_args() == 2)
26 {
27 $value = $name;
28 $name = $day;
29 $day = new Date();
30 }
31
32 $instance = self::getInstance();
33
34 if ($this->id === null && $this === $instance)
35 {
36 $pending =& self::$session['PENDING_COUNTERS'];
37
38 if (empty($pending[$name]))
39 $pending[$name] = 0;
40
41 $pending[$name] += (float) $value;
42 }
43 else
44 {
45 parent::addCounter($day, $name, $value);
46 }
47 }
48
56 public function addDayCounter($name, $value)
57 {
58 $instance = self::getInstance();
59 $session =& self::$session;
60
61 if ($this->id === null && $this === $instance)
62 {
63 $session['PENDING_DAY_COUNTERS'][$name] = (float) $value;
64 }
65 else
66 {
67 $unique =& $session['UNIQUE'];
68
69 if (!in_array($name, $unique, true))
70 {
71 $unique[] = $name;
72
73 $this->addCounter($name, $value);
74 $this->setCookie();
75 }
76 }
77 }
78
87 public function subDayCounter($day, $name, $value)
88 {
89 $this->subCounter($day, $name, $value);
90
91 // is today - clear session
92 $isToday = $day instanceof Date ? $day->format('dmY') === date('dmY') : false;
93 if ($isToday)
94 {
95 $unique =& self::$session['UNIQUE'];
96 if (($i = array_search($name, $unique, true)) !== false)
97 {
98 unset($unique[$i]);
99 $this->setCookie();
100 }
101 }
102 }
103
112 public function addCurrencyCounter($name, $value, $currency)
113 {
114 $this->addCounter($name, Utils::convertToBaseCurrency($value, $currency));
115 }
116
126 public function subCurrencyCounter($day, $name, $value, $currency)
127 {
128 $this->subCounter($day, $name, Utils::convertToBaseCurrency($value, $currency));
129 }
130
139 public function attachEntityItem($entity, $item)
140 {
141 if (! is_string($entity))
142 throw new ArgumentTypeException('entity', 'string');
143
144 if (! is_scalar($item))
145 throw new ArgumentTypeException('item', 'scalar');
146
147 $instance = self::getInstance();
148
149 if ($this->id === null && $this === $instance)
150 {
151 self::$session['PENDING_ENTITY_ITEMS'][$entity.':'.$item] = array('ENTITY' => $entity, 'ITEM' => $item);
152 }
153 else
154 {
155 try
156 {
157 Internals\ContextEntityItemTable::add(array(
158 'CONTEXT_ID' => $this->id,
159 'ENTITY' => $entity,
160 'ITEM' => $item,
161 ));
162 }
163 catch (\Bitrix\Main\DB\SqlQueryException $e)
164 {
165 }
166 }
167 }
168
176 public static function getEntityItemInstance($entity, $item)
177 {
178 $instance = self::getInstance();
179
180 $context = Internals\ContextEntityItemTable::getList(array(
181 'select' => array('CONTEXT_ID'),
182 'filter' => array('=ENTITY' => $entity, '=ITEM' => $item),
183 'limit' => 1,
184 ))->fetch();
185
186 $contextId = !empty($context['CONTEXT_ID']) ? $context['CONTEXT_ID'] : self::EMPTY_CONTEXT_ID;
187 if ($contextId !== $instance->id)
188 {
189 $instance = new self;
190 $instance->id = $contextId;
191 }
192
193 return $instance;
194 }
195
202 public static function getSiteInstance($siteId)
203 {
204 $instance = self::getInstance();
205
206 if (preg_match('/[a-z0-9_]{2}/i', $siteId) && self::getSiteId() != $siteId && \CSite::getById($siteId)->fetch())
207 {
208 $instance = new self;
209
210 foreach (EventManager::getInstance()->findEventHandlers('conversion', 'OnSetDayContextAttributes') as $handler)
211 ExecuteModuleEventEx($handler, array($instance));
212
213 $instance->setAttribute('conversion_site', $siteId);
214 $instance->save();
215 }
216
217 return $instance;
218 }
219
221 private static $instance;
222 private static $session;
223
229 public static function getInstance()
230 {
231 if (! self::$instance)
232 {
233 $instance = new self;
234 $varName = self::getVarName();
235 $session =& $_SESSION[$varName];
236 $expire = strtotime('today 23:59');
237
238 if (! (is_array($session) && is_int($session['ID']) && $session['EXPIRE'] === $expire))
239 {
240 $session = array('ID' => null, 'EXPIRE' => $expire, 'UNIQUE' => array());
241
242 if ($cookie = $_COOKIE[$varName])
243 {
244 try
245 {
246 $cookie = Json::decode($cookie);
247 }
248 catch (ArgumentException $e)
249 {
250 }
251 }
252
253 // check if cookie is valid
254 if ( is_array($cookie)
255 && is_array($cookie['UNIQUE'])
256 && $cookie['EXPIRE'] === $expire
257 && ($id = $cookie['ID']) !== null
258 && is_int($id)
259 && ($id === self::EMPTY_CONTEXT_ID || Internals\ContextTable::getByPrimary($id)->fetch())
260 )
261 {
262 $session['ID' ] = $id;
263 $session['UNIQUE'] = $cookie['UNIQUE'];
264 }
265 }
266
267 $instance->id = $session['ID'];
268 self::$session =& $session;
269 self::$instance = $instance;
270 }
271
272 return self::$instance;
273 }
274
276 private function setCookie()
277 {
278 $session = self::$session;
279
280 $cookie = new Main\Web\Cookie(
281 self::getVarName(),
282 Json::encode(array(
283 'ID' => $session['ID'],
284 'EXPIRE' => $session['EXPIRE'],
285 'UNIQUE' => $session['UNIQUE'],
286 )),
287 strtotime('+1 year'),
288 false
289 );
290 $cookie->setHttpOnly(false);
291
292 Main\Context::getCurrent()->getResponse()->addCookie($cookie);
293 }
294
296 public static function saveInstance()
297 {
298 $instance = self::getInstance();
299 $session =& self::$session;
300
301 if ($instance->id === null)
302 {
303 foreach (EventManager::getInstance()->findEventHandlers('conversion', 'OnSetDayContextAttributes') as $handler)
304 ExecuteModuleEventEx($handler, array($instance));
305
306 $instance->save();
307 }
308
309 $session['ID'] = $instance->id;
310 $instance->setCookie();
311
312 if (!empty($session['PENDING_COUNTERS']) && is_array($session['PENDING_COUNTERS']))
313 {
314 foreach ($session['PENDING_COUNTERS'] as $name => $value)
315 $instance->addCounter($name, $value);
316 }
317
318 if (!empty($session['PENDING_DAY_COUNTERS']) && is_array($session['PENDING_DAY_COUNTERS']))
319 {
320 foreach ($session['PENDING_DAY_COUNTERS'] as $name => $value)
321 $instance->addDayCounter($name, $value);
322 }
323
324 if (!empty($session['PENDING_ENTITY_ITEMS']) && is_array($session['PENDING_ENTITY_ITEMS']))
325 {
326 foreach ($session['PENDING_ENTITY_ITEMS'] as $i)
327 $instance->attachEntityItem($i['ENTITY'], $i['ITEM']);
328 }
329 }
330
332 public static function getVarName()
333 {
334 static $name;
335
336 if (! $name)
337 {
338 $name = 'BITRIX_CONVERSION_CONTEXT_'.self::getSiteId();
339 }
340
341 return $name;
342 }
343
345 public static function getSiteId()
346 {
347 static $siteId = null;
348
349 if ($siteId === null)
350 {
351 $siteId = '';
352
353 if (defined('ADMIN_SECTION') && ADMIN_SECTION === true)
354 {
355 if ($row = SiteTable::getList(array(
356 'select' => array('LID'),
357 'order' => array('DEF' => 'DESC', 'SORT' => 'ASC'),
358 'limit' => 1,
359 ))->fetch())
360 {
361 $siteId = $row['LID'];
362 }
363 }
364 else
365 {
366 $siteId = SITE_ID;
367 }
368 }
369
370 return $siteId;
371 }
372}
static getSiteInstance($siteId)
static getEntityItemInstance($entity, $item)
subDayCounter($day, $name, $value)
subCurrencyCounter($day, $name, $value, $currency)
attachEntityItem($entity, $item)
addCounter($day, $name, $value=null)
addCurrencyCounter($name, $value, $currency)
static convertToBaseCurrency($value, $currency)
Definition utils.php:10