Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
2
4
5use \Bitrix\Main\SystemException;
10use \Bitrix\Sale\TradingPlatform\Timer;
11use \Bitrix\Sale\TradingPlatform\Vk;
12use \Bitrix\Sale\TradingPlatform;
13
21{
22 const DEFAULT_PROCESS_TYPE = 'ALL';
23 const FIRST_PROCESS_TYPE = 'ALBUMS';
24 const SECOND_PROCESS_TYPE = 'PRODUCTS';
27 const MAX_EXEC_COUNT = 3;
28
29 public static function runProcess($exportId, $processType)
30 {
31 $result = array();
32
33 if (empty($exportId))
34 throw new ArgumentNullException('exportId');
35 $exportId = \EscapePHPString($exportId);
36
37 $currProcess = Vk\Journal::getCurrentProcess($exportId);
38 $startPosition = $currProcess ? $currProcess['START_POSITION'] : self::DEFAULT_START_POSITION;
39 $execCount = $currProcess ? $currProcess['EXEC_COUNT'] : self::DEFAULT_EXEC_COUNT;
40// if we run ALL export - first we must add ALBUMS. After this we create PRODUCTS
41 $currProcessType = self::prepareType($processType);
42 if (!in_array($currProcessType, Vk\Vk::getExportTypes()))
43 throw new ArgumentOutOfRangeException('currProcessType');
44
45 $journal = new Vk\Journal($exportId, $currProcessType);
46
47// if first running - start journal
48 if ($startPosition == self::DEFAULT_START_POSITION)
49 $journal->start();
50
51 $vk = Vk\Vk::getInstance();
52 $vk->log(
53 TradingPlatform\Logger::LOG_LEVEL_DEBUG,
54 "VK_PROCESS__START",
55 'FEED_' . $currProcessType,
56 "VKontakte export of " . $currProcessType . " started. Export profile: " . $exportId . ", start position: " . $startPosition
57 );
58
59 try
60 {
61 $timelimit = $vk->getTimelimit($exportId);
62 $feed = Manager::createFeed($currProcessType, $exportId, $timelimit, $startPosition);
63 $feed->processData($exportId);
64 }
65
66 catch (TimeIsOverException $e)
67 {
68 $endPosition = $e->getEndPosition();
69// control of slow export
70 if ($startPosition == $endPosition)
71 {
72 $execCount++;
73 if ($execCount >= self::MAX_EXEC_COUNT)
74 $result['TOO_MUCH_TIMES'] = Vk\Journal::getTooMuchTimeExportMessage();
75 }
76 else
77 {
78 $execCount = self::DEFAULT_EXEC_COUNT;
79 }
80
81 $vk->log(
82 TradingPlatform\Logger::LOG_LEVEL_DEBUG,
83 "VK_PROCESS__TIMELIMIT",
84 'FEED_' . $currProcessType,
85 "VKontakte export of " . $currProcessType . " for profile " . $exportId . " takes too long and was finished at position '" . $startPosition . "'."
86 );
87 }
88
89 catch (Vk\ExecuteException $e)
90 {
91// reload to show errors
92 $result['ERRORS_CRITICAL'] = true;
93 $msg = $e->getFullMessage() ? $e->getFullMessage() : Loc::getMessage("SALE_VK__UNKNOWN_ERROR");
94 $vk->log(TradingPlatform\Logger::LOG_LEVEL_ERROR, "VK_FEED__FEED_ERRORS", 'FEED_' . $currProcessType, $msg);
95 }
96
97 catch (\Exception $e)
98 {
99// todo: need create normal errors desc
100 $vk->log(
101 TradingPlatform\Logger::LOG_LEVEL_ERROR,
102 "VK_PROCESS__ERRORS", 'FEED_' . $currProcessType,
103 "VKontakte export of " . $currProcessType . " for profile " . $exportId . " finished with some errors. " .
104 $e->getMessage()
105 );
106 }
107
108 $journal = new Vk\Journal($exportId, $currProcessType);
109// If export not set endPosition - we catch finish element.
110 if (!isset($endPosition))
111 {
112// close journal for current type and write to log
113 $journal->end();
114 $vk->log(
115 TradingPlatform\Logger::LOG_LEVEL_DEBUG,
116 "VK_PROCESS__FINISH",
117 'FEED_' . $currProcessType,
118 "VKontakte export of " . $currProcessType . " for profile " . $exportId . " was finished."
119 );
120
121// if ALL export - after ALBUMS run PRODUCTS
122 if ($currProcessType == self::FIRST_PROCESS_TYPE && $processType == self::DEFAULT_PROCESS_TYPE)
123 {
124 $processTypeToSave = self::SECOND_PROCESS_TYPE;
125 $positionToSave = self::DEFAULT_START_POSITION;
126 $execCountToSave = self::DEFAULT_EXEC_COUNT;
127
128 $result['CONTINUE'] = true;
129 $result['TYPE'] = $processTypeToSave;
130 }
131
132// end of export process sovsem
133 else
134 {
135 $processTypeToSave = false;
136 $positionToSave = false;
137 $execCountToSave = false;
138
139 $result['CONTINUE'] = false;
140 }
141 }
142
143// CONTINUE export in current type
144 else
145 {
146// if ALBUM - save ALL value ($processType)
147 $processTypeToSave = $processType;
148 $positionToSave = $endPosition;
149 $execCountToSave = $execCount;
150
151 $result['CONTINUE'] = true;
152 $result['TYPE'] = $processTypeToSave;
153 }
154
155// SAVE params of process
156 $journal->saveProcessParams($exportId, $processTypeToSave, $positionToSave, $execCountToSave);
157
158 return $result;
159 }
160
161
172 private static function createFeed($feedType, $exportId, $timeLimit = 0, $startPosition = '')
173 {
174 $feedParams = array(
175 "TIMER" => new Timer($timeLimit, false),
176 "FEED_TYPE" => $feedType,
177 );
178
179 switch ($feedType)
180 {
181 case 'PRODUCTS':
182 $feedParams["DATA_SOURCE"] = new Data\Sources\Product($exportId, $startPosition);
183 $feedParams["DATA_CONVERTER"] = new Data\Converters\Product($exportId);
184 $feedParams["DATA_PROCESSOR"] = new Data\Processors\ProductAdd($exportId);
185 break;
186
187 case 'PRODUCTS_DELETE':
188 $feedParams["DATA_SOURCE"] = false;
189 $feedParams["DATA_CONVERTER"] = false;
190 $feedParams["DATA_PROCESSOR"] = new Data\Processors\ProductsDelete($exportId);
191 break;
192
193 case 'PRODUCTS_DELETE_ALL':
194 $feedParams["DATA_SOURCE"] = false;
195 $feedParams["DATA_CONVERTER"] = false;
196 $feedParams["DATA_PROCESSOR"] = new Data\Processors\ProductsDeleteAll($exportId);
197 break;
198
199 case 'ALBUMS':
200 $feedParams["DATA_SOURCE"] = new Data\Sources\Section($exportId, $startPosition);
201 $feedParams["DATA_CONVERTER"] = new Data\Converters\Album($exportId);
202 $feedParams["DATA_PROCESSOR"] = new Data\Processors\AlbumAdd($exportId);
203 break;
204
205 case 'ALBUMS_DELETE':
206 $feedParams["DATA_SOURCE"] = false;
207 $feedParams["DATA_CONVERTER"] = false;
208 $feedParams["DATA_PROCESSOR"] = new Data\Processors\AlbumsDelete($exportId);
209 break;
210
211 case 'ALBUMS_DELETE_ALL':
212 $feedParams["DATA_SOURCE"] = false;
213 $feedParams["DATA_CONVERTER"] = false;
214 $feedParams["DATA_PROCESSOR"] = new Data\Processors\AlbumsDeleteAll($exportId);
215 break;
216
217 default:
218 throw new SystemException("Unknown type of feed \"" . $feedType . "\". " . __METHOD__);
219 break;
220 }
221
222 $feed = new Feed($feedParams, $startPosition);
223
224 return $feed;
225 }
226
227
233 public static function prepareType($type)
234 {
235 return ($type == self::DEFAULT_PROCESS_TYPE) ? self::FIRST_PROCESS_TYPE : $type;
236 }
237}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static runProcess($exportId, $processType)
Definition manager.php:29