Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
auto.php
1<?php
2namespace Bitrix\Seo\Adv;
3
7
8class Auto
9{
10 public static function checkQuantity($ID, $productFields)
11 {
12 $checkNeed = (
13 isset($productFields['QUANTITY'])
14 || isset($productFields['QUANTITY_TRACE'])
15 || isset($productFields['CAN_BUY_ZERO'])
16 );
17
18 if (!$checkNeed)
19 {
20 return;
21 }
22
23 $productIterator = ProductTable::getList(array(
24 'filter' => array('=ID' => $ID),
25 'select' => array('QUANTITY', 'QUANTITY_TRACE', 'CAN_BUY_ZERO'),
26 ));
27 $product = $productIterator->fetch();
28 if($product)
29 {
30 if($product["QUANTITY_TRACE"] == "Y" && $product['CAN_BUY_ZERO'] == 'N')
31 {
32 $linkIterator = LinkTable::getList(array(
33 "filter" => array(
34 "=LINK_TYPE" => LinkTable::TYPE_IBLOCK_ELEMENT,
35 "=LINK_ID" => $ID,
36 array(
37 array(
38 "=BANNER.AUTO_QUANTITY_OFF" => YandexBannerTable::ACTIVE,
39 ),
40 array(
41 "=BANNER.AUTO_QUANTITY_ON" => YandexBannerTable::ACTIVE,
42 ),
43 'LOGIC' => "OR",
44 ),
45 ),
46 "select" => array(
47 "BANNER_ID",
48 "AUTO_QUANTITY_ON" => "BANNER.AUTO_QUANTITY_ON",
49 "AUTO_QUANTITY_OFF" => "BANNER.AUTO_QUANTITY_OFF",
50 ),
51 ));
52
53 $zeroQuantity = $product['QUANTITY'] <= 0;
54
55 $linkIdMark = array();
56 $linkIdUnMark = array();
57 while($link = $linkIterator->fetch())
58 {
59 $linkIdMark[] = $link["BANNER_ID"];
60
61 if(
62 $zeroQuantity && $link["AUTO_QUANTITY_ON"] == YandexBannerTable::MARKED
63 || !$zeroQuantity && $link["AUTO_QUANTITY_OFF"] == YandexBannerTable::MARKED
64 )
65 {
66 $linkIdUnMark[] = $link["BANNER_ID"];
67 }
68 }
69
70 if(count($linkIdMark) > 0)
71 {
72 if($zeroQuantity)
73 {
75 }
76 else
77 {
79 }
80
81 if(count($linkIdUnMark) > 0)
82 {
83 if($zeroQuantity)
84 {
86 }
87 else
88 {
90 }
91 }
92 }
93 }
94 }
95 }
96
97 public static function checkQuantityAgent()
98 {
99 if(!IsModuleInstalled("catalog"))
100 {
101 return __CLASS__."::checkQuantityAgent();";
102 }
103
104 $dbRes = YandexBannerTable::getList(array(
105 'filter' => array(
106 array(
107 '=AUTO_QUANTITY_ON' => YandexBannerTable::MARKED,
108 ),
109 array(
110 '=AUTO_QUANTITY_OFF' => YandexBannerTable::MARKED,
111 ),
112 'LOGIC' => "OR"
113 ),
114 'select' => array(
115 'ID', 'XML_ID', 'CAMPAIGN_ID', 'CAMPAIGN_XML_ID' => 'CAMPAIGN.XML_ID',
116 'AUTO_QUANTITY_ON', 'AUTO_QUANTITY_OFF',
117 ),
118 ));
119
120 try
121 {
122 $engine = new YandexDirect();
123 }
124 catch (\Bitrix\Main\SystemException $e)
125 {
126 return __CLASS__."::checkQuantityAgent();";
127 }
128
129 $bannersListToStop = array();
130 $bannersListToResume = array();
131 $bannersListToUnMarkStopped = array();
132 $bannersListToUnMarkResumed = array();
133
134 $bannersLogData = array();
135 while($banner = $dbRes->fetch())
136 {
137 if($banner["AUTO_QUANTITY_ON"] == YandexBannerTable::MARKED)
138 {
139 if(!isset($bannersListToResume[$banner["CAMPAIGN_XML_ID"]]))
140 {
141 $bannersListToResume[$banner["CAMPAIGN_XML_ID"]] = array();
142 }
143
144 $bannersListToResume[$banner["CAMPAIGN_XML_ID"]][$banner["ID"]] = $banner["XML_ID"];
146
147 if($banner["AUTO_QUANTITY_OFF"] == YandexBannerTable::MARKED)
148 {
149 $bannersListToUnMarkStopped[] = $banner["ID"];
150 }
151 }
152 else
153 {
154 if(!isset($bannersListToResume[$banner["CAMPAIGN_XML_ID"]]))
155 {
156 $bannersListToStop[$banner["CAMPAIGN_XML_ID"]] = array();
157 }
158
159 $bannersListToStop[$banner["CAMPAIGN_XML_ID"]][$banner["ID"]] = $banner["XML_ID"];
161
162 if($banner["AUTO_QUANTITY_ON"] == YandexBannerTable::MARKED)
163 {
164 $bannersListToUnMarkResumed[] = $banner["ID"];
165 }
166 }
167
168 $bannersLogData[$banner["ID"]] = array(
169 'CAMPAIGN_ID' => $banner['CAMPAIGN_ID'],
170 'CAMPAIGN_XML_ID' => $banner['CAMPAIGN_XML_ID'],
171 'BANNER_ID' => $banner['ID'],
172 'BANNER_XML_ID' => $banner['XML_ID'],
173 'CAUSE_CODE' => $causeCode,
174 );
175 }
176
177 if(count($bannersLogData) > 0)
178 {
179 foreach($bannersListToResume as $campaignId => $bannersList)
180 {
181 if(count($bannersList) > 0)
182 {
183 $success = true;
184
185 try
186 {
187 $engine->resumeBanners($campaignId, array_values($bannersList));
188 }
189 catch(YandexDirectException $e)
190 {
191 $success = false;
192 }
193
194 foreach($bannersList as $bannerId => $bannerXmlId)
195 {
196 $logEntry = $bannersLogData[$bannerId];
197 $logEntry['ENGINE_ID'] = $engine->getId();
198 $logEntry['SUCCESS'] = $success ? AutologTable::SUCCESS : AutologTable::FAILURE;
199
200 AutologTable::add($logEntry);
201 }
202
203 $bannersListToUnMarkResumed = array_merge(
204 $bannersListToUnMarkResumed,
205 array_keys($bannersList)
206 );
207 }
208 }
209
210 foreach($bannersListToStop as $campaignId => $bannersList)
211 {
212 if(count($bannersList) > 0)
213 {
214 $success = true;
215 try
216 {
217 $engine->stopBanners($campaignId, array_values($bannersList));
218 }
219 catch(YandexDirectException $e)
220 {
221 $success = false;
222 }
223
224 foreach($bannersList as $bannerId => $bannerXmlId)
225 {
226 $logEntry = $bannersLogData[$bannerId];
227 $logEntry['ENGINE_ID'] = $engine->getId();
228 $logEntry['SUCCESS'] = $success ? AutologTable::SUCCESS : AutologTable::FAILURE;
229
230 AutologTable::add($logEntry);
231 }
232
233 $bannersListToUnMarkStopped = array_merge(
234 $bannersListToUnMarkStopped,
235 array_keys($bannersList)
236 );
237 }
238 }
239
240 if(count($bannersListToUnMarkStopped) > 0)
241 {
242 YandexBannerTable::unMarkStopped($bannersListToUnMarkStopped);
243 }
244
245 if(count($bannersListToUnMarkResumed) > 0)
246 {
247 YandexBannerTable::unMarkResumed($bannersListToUnMarkResumed);
248 }
249 }
250
251 return __CLASS__."::checkQuantityAgent();";
252 }
253}
254
static checkQuantity($ID, $productFields)
Definition auto.php:10
static checkQuantityAgent()
Definition auto.php:97
static unMarkStopped(array $idList)
static markResumed(array $idList)
static markStopped(array $idList)
static unMarkResumed(array $idList)