Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
2
4
5
8
9final class Manager
10{
14 private static $instance;
15
16
21 public static function getInstance()
22 {
23 if (!isset(self::$instance))
24 {
25 self::$instance = new self;
26 }
27
28 return self::$instance;
29 }
30
31 private function __construct()
32 {
33 $this->errorCollection = new ErrorCollection;
34 }
35
36 private function __clone()
37 {}
38
39 public function dropIndex($discountId)
40 {
43 }
44
45 public function indexDiscount(array $discount)
46 {
47 if(empty($discount['ID']))
48 {
49 return false;
50 }
51
52 $condition = $this->getConditionStructure($discount);
53 if(!$condition)
54 {
55 return false;
56 }
57
58 list($elementIds, $sectionIds) = $this->extractElementsAndSections($condition);
59
60 $this->dropIndex($discount['ID']);
61
62 if(!$elementIds && !$sectionIds)
63 {
64 return false;
65 }
66
67 if($elementIds)
68 {
69 IndexElementTable::fillByDiscount($discount['ID'], $elementIds);
70 }
71
72 if($sectionIds)
73 {
74 IndexSectionTable::fillByDiscount($discount['ID'], $sectionIds);
75 }
76
77 return true;
78 }
79
80 public function hasDataToIndex(array $discount)
81 {
82 list($elementIds, $sectionIds) = $this->extractElementsAndSections($discount['CONDITIONS']);
83
84 return !empty($elementIds) || !empty($sectionIds);
85 }
86
87 private function getConditionStructure(array $discount)
88 {
89 if(empty($discount['CONDITIONS']) && empty($discount['CONDITIONS_LIST']))
90 {
91 return null;
92 }
93
94 if(!empty($discount['CONDITIONS_LIST']))
95 {
96 $conditions = $discount['CONDITIONS_LIST'];
97 }
98 else
99 {
100 $conditions = $discount['CONDITIONS'];
101 }
102
103 if(is_string($conditions))
104 {
105 $conditions = unserialize($conditions, ['allowed_classes' => false]);
106 }
107
108 if(!$conditions || !is_array($conditions))
109 {
110 return null;
111 }
112
113 return $conditions;
114 }
115
116 private function extractElementsAndSections(array $condition)
117 {
118 if(empty($condition['CLASS_ID']))
119 {
120 return null;
121 }
122
123 if($condition['CLASS_ID'] !== 'CondGroup' || empty($condition['DATA']))
124 {
125 return null;
126 }
127
128 if(empty($condition['CHILDREN']))
129 {
130 return null;
131 }
132
133 if(
134 !($condition['DATA']['All'] === 'AND' && $condition['DATA']['True'] === 'True') &&
135 !($condition['DATA']['All'] === 'OR' && $condition['DATA']['True'] === 'True')
136 )
137 {
138 return null;
139 }
140
141 $elementIds = $sectionIds = array();
142 foreach($condition['CHILDREN'] as $child)
143 {
144 $onlyOneCondition =
145 isset($child['CHILDREN'])
146 && is_array($child['CHILDREN'])
147 && count($child['CHILDREN']) === 1
148 && $child['DATA']['All'] === 'AND'
149 && $child['DATA']['Found'] === 'Found';
150
151 if(
152 $child['CLASS_ID'] === 'CondBsktProductGroup' &&
153 (
154 ($child['DATA']['All'] === 'OR' && $child['DATA']['Found'] === 'Found') ||
155 $onlyOneCondition
156 )
157 )
158 {
159 foreach($child['CHILDREN'] as $grandchild)
160 {
161 switch($grandchild['CLASS_ID'])
162 {
163 case 'CondIBElement':
164 if (is_array($grandchild['DATA']['value']))
165 {
166 $elementIds = array_merge($elementIds, $grandchild['DATA']['value']);
167 }
168 else
169 {
170 $elementIds[] = $grandchild['DATA']['value'];
171 }
172 break;
173 case 'CondIBSection':
174 if($grandchild['DATA']['logic'] === 'Equal')
175 {
176 $sectionIds[] = $grandchild['DATA']['value'];
177 }
178 break;
179 }
180 }
181 }
182 }
183
184 $elementIds = $this->convertSkuToMainProducts($elementIds);
185
186 return array($elementIds, $sectionIds);
187 }
188
189 private function convertSkuToMainProducts(array $elementIds)
190 {
191 if (!Loader::includeModule('catalog'))
192 {
193 return $elementIds;
194 }
195
196 $products = \CCatalogSKU::getProductList($elementIds);
197 if (empty($products))
198 {
199 return $elementIds;
200 }
201
202 $newElementIds = array_combine($elementIds, $elementIds);
203 foreach($products as $offerId => $product)
204 {
205 if(isset($newElementIds[$offerId]))
206 {
207 $newElementIds[$product['ID']] = $product['ID'];
208 unset($newElementIds[$offerId]);
209 }
210 }
211
212 return array_values($newElementIds);
213 }
214}
static fillByDiscount($discountId, array $indexData)
static fillByDiscount($discountId, array $indexData)
hasDataToIndex(array $discount)
Definition manager.php:80