Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
generatorcontext.php
1<?php
2
3namespace Bitrix\Conversion;
4
9
12{
13 private function setAttributes(array $attributes)
14 {
15 foreach ($attributes as $name => $value)
16 {
17 self::setAttribute($name, $value);
18 }
19 }
20
21 private static function appendCounters(array & $one, array $two)
22 {
23 foreach ($two as $name => $value)
24 {
25 if ($counter =& $one[$name])
26 {
27 $counter += $value;
28 }
29 else
30 {
31 $counter = $value;
32 }
33 }
34 }
35
36 private static function appendDayCounters(array & $one, array $two)
37 {
38 foreach ($two as $day => $twoCounters)
39 {
40 if ($oneCounters =& $one[$day])
41 {
42 self::appendCounters($oneCounters, $twoCounters);
43 }
44 else
45 {
46 $oneCounters = $twoCounters;
47 }
48 }
49 }
50
51 static public function generateInitialData(Date $from)
52 {
53 if ( ($to = Option::get('conversion', 'START_DATE_TIME', 'undefined')) != 'undefined'
54 && DateTime::isCorrect($to, 'Y-m-d H:i:s')
55 && ($to = new DateTime($to, 'Y-m-d H:i:s'))
56 && $to->format('Y-m-d H:i:s') > $from->format('Y-m-d H:i:s')
57 && Option::get('conversion', 'GENERATE_INITIAL_DATA', 'undefined') == 'undefined')
58 {
59 Option::set('conversion', 'GENERATE_INITIAL_DATA', 'generated');
60
61 $context = new self;
62
63 // generate data
64
65 $data = array();
66
67 foreach (EventManager::getInstance()->findEventHandlers('conversion', 'OnGenerateInitialData') as $handler)
68 {
69 $result = ExecuteModuleEventEx($handler, array($from, $to)); // TODO validate
70
71 foreach ($result as $row)
72 {
73 $context->id = null;
74 $context->attributes = array();
75 $context->setAttributes($row['ATTRIBUTES']);
76 $context->save();
77
78 if ($dayCounters =& $data[$context->id])
79 {
80 self::appendDayCounters($dayCounters, $row['DAY_COUNTERS']);
81 }
82 else
83 {
84 $dayCounters = $row['DAY_COUNTERS'];
85 }
86 }
87 }
88
89 unset($dayCounters);
90
91 // save data to database
92
93 $numerators = CounterManager::getTypes(array('GROUP' => 'day'));
94 unset($numerators['conversion_visit_day']);
95
96 foreach ($data as $id => $dayCounters)
97 {
98 $context->id = $id;
99
100 foreach ($dayCounters as $day => $counters)
101 {
102 $day = new Date($day, 'Y-m-d');
103
104 $visitSum = 0;
105 $visitQuantity = 0;
106
107 unset($counters['conversion_visit_day']);
108
109 foreach ($counters as $name => $value)
110 {
111 $context->addCounter($day, $name, $value);
112
113 if ($numerators[$name])
114 {
115 $visitSum += $value;
116 $visitQuantity += 1;
117 }
118 }
119
120 $context->addCounter($day, 'conversion_visit_day', $visitQuantity ? round($visitSum / $visitQuantity * 100) + 1 : 1);
121 }
122 }
123 }
124 }
125}
static getTypes(array $filter=null)
static isCorrect($time, $format=null)
Definition date.php:356