1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
sftpqueue.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale\TradingPlatform\Ebay\Feed\Data\Processors;
4
5use \Bitrix\Main\Type\DateTime;
6use \Bitrix\Main\SystemException;
7use \Bitrix\Sale\TradingPlatform\Sftp;
8use \Bitrix\Main\ArgumentNullException;
9use \Bitrix\Sale\TradingPlatform\Logger;
10use \Bitrix\Sale\TradingPlatform\Ebay\Ebay;
11use \Bitrix\Sale\TradingPlatform\Ebay\Feed\QueueTable;
12use \Bitrix\Sale\TradingPlatform\Ebay\Feed\ResultsTable;
13
15{
16 // todo: check if the record alredy exist
17
18 protected $feedType;
19 protected $coverTag = null;
20 protected $schemeFileName = null;
21 protected $fileNameSalt;
22 protected $remotePath;
23 protected $siteId;
24 protected $timer = null;
25 protected $path;
26
27 public function __construct(array $params)
28 {
29 if(!isset($params["FEED_TYPE"]) || $params["FEED_TYPE"] == '')
30 throw new ArgumentNullException("FEED_TYPE");
31
32 if($this->feedType == "ORDER_ACK")
33 $this->feedType = "order-ack";
34 else
35 $this->feedType = mb_strtolower($params["FEED_TYPE"]);
36
37 if(!isset($params["SITE_ID"]) || $params["SITE_ID"] == '')
38 throw new ArgumentNullException("SITE_ID");
39
40 $this->siteId = $params["SITE_ID"];
41
42 if(isset($params["COVER_TAG"]) && $params["COVER_TAG"] <> '')
43 $this->coverTag = $params["COVER_TAG"];
44
45 if(isset($params["SCHEMA_FILE_NAME"]))
46 $this->schemeFileName = $params["SCHEMA_FILE_NAME"];
47
48 if(isset($params["TIMER"]))
49 $this->timer = $params["TIMER"];
50
51 $this->fileNameSalt = time();
52 $this->remotePath = "/store/".$this->feedType;
54 }
55
56 protected function prepareFile($file)
57 {
58 $res = file_put_contents($file, '<?xml version="1.0" encoding="UTF-8"?>'."\n");
59
60 if(!$res)
61 throw new SystemException("Can't flush data feed \"".$this->feedType."\" to file ".$file);
62
63 if($this->coverTag !== null)
64 file_put_contents($file, "<".$this->coverTag.">\n", FILE_APPEND);
65 }
66
67 protected function flushData()
68 {
69 $fileXml = "";
70
71 $feedDataRes = QueueTable::getList(array(
72 "filter" => array(
73 "FEED_TYPE" => $this->feedType
74 )
75 ));
76
77 $filePrepared = false;
78
79 while($feedData = $feedDataRes->fetch())
80 {
81 if(!$filePrepared)
82 {
83 $fileXml = $this->path."/xml/".$this->feedType."_".$this->fileNameSalt.".xml";
84 $this->prepareFile($fileXml);
85 $filePrepared = true;
86 }
87
88 Ebay::log(Logger::LOG_LEVEL_DEBUG, "EBAY_DATA_PROCESSOR_SFTPQUEUE_FLUSHING", $this->feedType, print_r($feedData["DATA"],true), $this->siteId);
89
90 $res = file_put_contents($fileXml, $feedData["DATA"], FILE_APPEND);
91
92 if($res !== false)
93 QueueTable::delete($feedData["ID"]);
94 else
95 throw new SystemException("Can't flush data feed \"".$this->feedType."\" to file ".$fileXml);
96 }
97
98 if($this->coverTag !== null && $filePrepared)
99 file_put_contents($fileXml, "</".$this->coverTag.">\n", FILE_APPEND);
100
101 return $fileXml;
102 }
103
104 public function process($data)
105 {
106 return $this->addData($data);
107 }
108
109 public function addData($data)
110 {
111 $result = QueueTable::add(array(
112 "FEED_TYPE" => $this->feedType,
113 "DATA" => $data
114 ));
115
116 return $result->isSuccess();
117 }
118
119 public function sendData()
120 {
121 $xmlFile = $this->flushData();
122
123 if(!$xmlFile)
124 return false;
125
126 $tmpFile = $this->packData($xmlFile);
127 $zipFile = new \Bitrix\Main\IO\File($tmpFile);
128 $zipFile->rename($this->path."/zip/".$this->feedType."_".$this->fileNameSalt.".zip");
129 $this->sendDataSftp();
130
131 $checkResultsInterval = 5; //min.
132 \Bitrix\Sale\TradingPlatform\Ebay\Agent::add('RESULTS', $this->siteId, $checkResultsInterval, true);
133
134 return true;
135 }
136
137 protected function packData($xmlFile)
138 {
139 $tmpDir = $this->path."/tmp";
140 $archiveName = $tmpDir."/".$this->feedType."_".$this->fileNameSalt.".zip";
141 $oArchiver = \CBXArchive::GetArchive($archiveName, "ZIP");
142 $oArchiver->SetOptions(array(
143 "REMOVE_PATH" => $this->path."/xml",
144 "ADD_PATH" => $this->feedType
145 ));
146
147 if($oArchiver->Pack($xmlFile))
149
150 return $archiveName;
151 }
152
153 protected function sendDataSftp()
154 {
155 $directory = new \Bitrix\Main\IO\Directory($this->path."/zip");
156
157 if(!$directory->isExists())
158 throw new SystemException("Directory".$this->path."/zip does not exist! ".__METHOD__);
159
160 $filesToSend = $directory->getChildren();
161
162 if(empty($filesToSend))
163 return false;
164
165 $sftp = \Bitrix\Sale\TradingPlatform\Ebay\Helper::getSftp($this->siteId);
166
167 if(!$sftp)
168 return false;
169
170 $sftp->connect();
171
172 for($i = 0; $i < count($filesToSend); $i++)
173 {
174 $directoryEntry = $filesToSend[$i];
175 $localPath = $directoryEntry->getPath();
176
177 if((!($directoryEntry instanceof \Bitrix\Main\IO\File)) || GetFileExtension($localPath) != "zip")
178 continue;
179
180 $remote = $this->remotePath."/".$directoryEntry->getName();
181
182 while(!$this->checkOuterConditions($sftp))
183 {
184 if($this->timer !== null && !$this->timer->check(15))
185 return false;
186
187 sleep(10);
188 }
189
190 if($sftp->uploadFile($localPath, $remote))
191 {
192 $directoryEntry->delete();
193 ResultsTable::add(array(
194 "FILENAME" => $directoryEntry->getName(),
195 "FEED_TYPE" => $this->feedType,
196 "UPLOAD_TIME" => DateTime::createFromTimestamp(time())
197 ));
198 Ebay::log(Logger::LOG_LEVEL_INFO, "EBAY_DATA_PROCESSOR_SFTPQUEUE_SEND", $remote, "File sent successfully.", $this->siteId);
199 }
200 }
201
202 return true;
203 }
204
205 protected function checkOuterConditions($sftp)
206 {
207 $files = $sftp->getFilesList($this->remotePath);
208
209 if(!empty($files))
210 return false;
211
212 if($this->feedType == "inventory" || $this->feedType == "image")
213 {
214 $filesProd = $sftp->getFilesList("/store/product");
215 $filesProdInProc = $sftp->getFilesList("/store/product/inprocess");
216
217 if(!empty($filesProd) || !empty($filesProdInProc))
218 return false;
219 }
220
221 return true;
222 }
223}
static deleteFile($path)
Определения file.php:267
static add($feedType, $siteId, $interval, $once=false)
Определения agent.php:82
static log($level, $type, $itemId, $description, $siteId)
Определения ebay.php:161
static getSftpPath()
Определения helper.php:45
const LOG_LEVEL_INFO
Определения logger.php:16
const LOG_LEVEL_DEBUG
Определения logger.php:17
static GetArchive($strArcName, $strType="")
Определения archive.php:26
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
GetFileExtension($path)
Определения tools.php:2972
Определения Image.php:9
Определения directory.php:3
$files
Определения mysql_to_pgsql.php:30
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
path
Определения template_copy.php:201
$localPath
Определения template_copy.php:184