Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
relateddata.php
1<?php
8use Bitrix\Main\Entity\DataManager;
9
40final class RelatedDataTable extends DataManager
41{
43
49 public static function getTableName()
50 {
51 return 'b_sale_gift_related_data';
52 }
53
59 public static function getMap()
60 {
61 return array(
62 'ID' => array(
63 'data_type' => 'integer',
64 'primary' => true,
65 'autocomplete' => true,
66 ),
67 'DISCOUNT_ID' => array(
68 'data_type' => 'integer',
69 'required' => true,
70 ),
71 'DISCOUNT' => array(
72 'data_type' => '\Bitrix\Sale\Internals\DiscountTable',
73 'reference' => array(
74 '=this.DISCOUNT_ID' => 'ref.ID'
75 ),
76 'join_type' => 'INNER',
77 ),
78 'DISCOUNT_GROUP' => array(
79 'data_type' => '\Bitrix\Sale\Internals\DiscountGroupTable',
80 'reference' => array(
81 '=this.DISCOUNT_ID' => 'ref.DISCOUNT_ID'
82 ),
83 'join_type' => 'INNER',
84 ),
85 'ELEMENT_ID' => array(
86 'data_type' => 'integer',
87 ),
88 'SECTION_ID' => array(
89 'data_type' => 'integer',
90 ),
91 'MAIN_PRODUCT_SECTION_ID' => array(
92 'data_type' => 'integer',
93 ),
94 );
95 }
96
103 public static function deleteByDiscounts(array $discountIds)
104 {
105 if(empty($discountIds))
106 {
107 return;
108 }
109 $connection = Application::getConnection();
110 $helper = $connection->getSqlHelper();
111 $connection->queryExecute(
112 'delete from '.$helper->quote(self::getTableName()).
113 ' where ' . $helper->quote('DISCOUNT_ID') . ' in ('.implode(',', array_map('intval', $discountIds)).')'
114 );
115 }
116
123 public static function deleteByDiscount($discountId)
124 {
125 $discountId = (int)$discountId;
126 if($discountId <= 0)
127 {
128 return;
129 }
130 $connection = Application::getConnection();
131 $helper = $connection->getSqlHelper();
132 $connection->queryExecute('delete from ' . $helper->quote(self::getTableName()) . ' where ' . $helper->quote('DISCOUNT_ID') . ' = ' . $discountId);
133 }
134
141 public static function fillByDiscount(array $discount)
142 {
143 list($elementIds, $sectionIds) = static::getGiftsData($discount);
144 list($productElementIds, $productSectionIds) = static::getProductsData($discount);
145
146 //we works only with one section in condition.
147 $mainProductSectionId = reset($productSectionIds);
148 if(!is_int($mainProductSectionId))
149 {
150 $mainProductSectionId = null;
151 }
152
153 $items = array();
154 foreach($elementIds as $elementId)
155 {
156 $items[] = array(
157 'DISCOUNT_ID' => $discount['ID'],
158 'ELEMENT_ID' => $elementId,
159 'SECTION_ID' => null,
160 'MAIN_PRODUCT_SECTION_ID' => $mainProductSectionId,
161 );
162 }
163
164 foreach($sectionIds as $sectionId)
165 {
166 $items[] = array(
167 'DISCOUNT_ID' => $discount['ID'],
168 'ELEMENT_ID' => null,
169 'SECTION_ID' => $sectionId,
170 'MAIN_PRODUCT_SECTION_ID' => $mainProductSectionId,
171 );
172 }
173
174 static::insertBatch($items);
175 }
176
183 public static function getGiftsData(array $discount)
184 {
185 $sectionIds = $elementIds = array();
186
187 if (
188 (empty($discount['ACTIONS_LIST']) || !is_array($discount['ACTIONS_LIST']))
189 && checkSerializedData($discount['ACTIONS']))
190 {
191 $discount['ACTIONS_LIST'] = unserialize($discount['ACTIONS'], ['allowed_classes' => false]);
192 }
193
194 if(!isset($discount['ACTIONS_LIST']['CHILDREN']) && is_array($discount['ACTIONS_LIST']['CHILDREN']))
195 {
196 return array($elementIds, $sectionIds);
197 }
198
199 foreach($discount['ACTIONS_LIST']['CHILDREN'] as $child)
200 {
201 if(!isset($child['CLASS_ID']) || !isset($child['DATA']) || $child['CLASS_ID'] !== \CSaleActionGiftCtrlGroup::getControlID())
202 {
203 continue;
204 }
205 foreach($child['CHILDREN'] as $gifterChild)
206 {
207 switch($gifterChild['CLASS_ID'])
208 {
209 case 'GifterCondIBElement':
210 $elementIds = array_merge($elementIds, (array)$gifterChild['DATA']['Value']);
211 break;
212 case 'GifterCondIBSection':
213 $sectionIds = array_merge($sectionIds, (array)$gifterChild['DATA']['Value']);
214 break;
215 }
216 }
217 unset($gifterChild);
218 }
219 unset($child);
220
221 return array($elementIds, $sectionIds);
222 }
223
230 public static function getProductsData(array $discount)
231 {
232 $sectionIds = $elementIds = array();
233
234 if (
235 (empty($discount['CONDITIONS_LIST']) || !is_array($discount['CONDITIONS_LIST']))
236 && checkSerializedData($discount['CONDITIONS']))
237 {
238 $discount['CONDITIONS_LIST'] = unserialize($discount['CONDITIONS'], ['allowed_classes' => false]);
239 }
240
241 if(!isset($discount['CONDITIONS_LIST']['CLASS_ID']) || $discount['CONDITIONS_LIST']['CLASS_ID'] !== 'CondGroup')
242 {
243 return array($elementIds, $sectionIds);
244 }
245 if(empty($discount['CONDITIONS_LIST']['CHILDREN']))
246 {
247 return array($elementIds, $sectionIds);
248 }
249 if(count($discount['CONDITIONS_LIST']['CHILDREN']) > 1)
250 {
251 return array($elementIds, $sectionIds);
252 }
253 $child = reset($discount['CONDITIONS_LIST']['CHILDREN']);
254
255 if($child['CLASS_ID'] !== 'CondBsktProductGroup')
256 {
257 return array($elementIds, $sectionIds);
258 }
259
260 if(empty($child['CHILDREN']))
261 {
262 return array($elementIds, $sectionIds);
263 }
264 if(count($child['CHILDREN']) > 1)
265 {
266 return array($elementIds, $sectionIds);
267 }
268 $condition = reset($child['CHILDREN']);
269
270 if(!isset($condition['DATA']['logic']) || $condition['DATA']['logic'] !== 'Equal')
271 {
272 return array($elementIds, $sectionIds);
273 }
274
275 switch($condition['CLASS_ID'])
276 {
277 case 'CondIBElement':
278 $elementIds = (array)$condition['DATA']['value'];
279 break;
280 case 'CondIBSection':
281 $sectionIds = (array)$condition['DATA']['value'];
282 break;
283 }
284
285 return array($elementIds, $sectionIds);
286 }
287
293 private static function insertBatch(array $items)
294 {
295 $tableName = static::getTableName();
296 $connection = Application::getConnection();
297 $sqlHelper = $connection->getSqlHelper();
298
299 $query = $prefix = '';
300 if($connection instanceof MysqlCommonConnection)
301 {
302 foreach ($items as $item)
303 {
304 list($prefix, $values) = $sqlHelper->prepareInsert($tableName, $item);
305
306 $query .= ($query? ', ' : ' ') . '(' . $values . ')';
307 if(mb_strlen($query) > self::MAX_LENGTH_BATCH_MYSQL_QUERY)
308 {
309 $connection->queryExecute("INSERT INTO {$tableName} ({$prefix}) VALUES {$query}");
310 $query = '';
311 }
312 }
313 unset($item);
314
315 if($query && $prefix)
316 {
317 $connection->queryExecute("INSERT INTO {$tableName} ({$prefix}) VALUES {$query}");
318 }
319 }
320 elseif($connection instanceof MssqlConnection)
321 {
322 $valueData = array();
323 foreach ($items as $item)
324 {
325 list($prefix, $values) = $sqlHelper->prepareInsert($tableName, $item);
326 $valueData[] = "SELECT {$values}";
327 }
328 unset($item);
329
330 $valuesSql = implode(' UNION ALL ', $valueData);
331 if($valuesSql && $prefix)
332 {
333 $connection->queryExecute("INSERT INTO {$tableName} ({$prefix}) $valuesSql");
334 }
335 }
336 elseif($connection instanceof OracleConnection)
337 {
338 $valueData = array();
339 foreach ($items as $item)
340 {
341 list($prefix, $values) = $sqlHelper->prepareInsert($tableName, $item);
342 $valueData[] = "SELECT {$values} FROM dual";
343 }
344 unset($item);
345
346 $valuesSql = implode(' UNION ALL ', $valueData);
347 if($valuesSql && $prefix)
348 {
349 $connection->queryExecute("INSERT INTO {$tableName} ({$prefix}) $valuesSql");
350 }
351 }
352 }
353}
static getConnection($name="")