Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
results.php
1<?php
2
4
5use \Bitrix\Main\ArgumentNullException;
7use \Bitrix\Sale\TradingPlatform\Logger;
8use \Bitrix\Sale\TradingPlatform\Ebay\Ebay;
9use \Bitrix\Sale\TradingPlatform\Ebay\Feed\ResultsTable;
10
11class Results extends DataSource implements \Iterator
12{
13 protected $siteId;
14 protected $feedsToCheck = array();
15 protected $resultFileContent = "";
16 protected $remotePathTmpl = "";
17 protected $filter = array();
18
19 public function __construct($params)
20 {
21 if(!isset($params["SITE_ID"]) || $params["SITE_ID"] == '')
22 throw new ArgumentNullException("SITE_ID");
23
24 if(!isset($params["REMOTE_PATH_TMPL"]) || $params["REMOTE_PATH_TMPL"] == '')
25 throw new ArgumentNullException("REMOTE_PATH_TMPL");
26
27 if(!isset($params["FILTER"]))
28 throw new ArgumentNullException("FILTER");
29
30 $this->siteId = $params["SITE_ID"];
31 $this->remotePathTmpl = $params["REMOTE_PATH_TMPL"];
32 $this->filter = $params["FILTER"];
33 }
34
35 public function current()
36 {
37 return array(
38 "RESULT_ID" => $this->key(),
39 "CONTENT" => $this->resultFileContent
40 );
41 }
42
43 public function key()
44 {
45 return key($this->feedsToCheck);
46 }
47
48 public function next()
49 {
50 $feedData = next($this->feedsToCheck);
51
52 if($feedData !== false)
53 $this->resultFileContent = $this->getFileContent($feedData);
54 }
55
56 public function rewind()
57 {
58 $this->feedsToCheck = array();
59
60 $res = ResultsTable::getList(array(
61 'filter' => $this->filter
62 ));
63
64 while($feed = $res->fetch())
65 $this->feedsToCheck[$feed["ID"]] = $feed;
66
67 $feedData = reset($this->feedsToCheck);
68
69 if($feedData !== false)
70 $this->resultFileContent = $this->getFileContent($feedData);
71 }
72
73 public function valid()
74 {
75 return current($this->feedsToCheck) !== false;
76 }
77
78 protected function createRemotePath($feedData)
79 {
80 return str_replace(
81 array(
82 "##FEED_TYPE##",
83 "##UPLOAD_DATE##"
84 ),
85 array(
86 $feedData["FEED_TYPE"],
87 $feedData["UPLOAD_TIME"]->format("M-d-Y")
88 ),
89 $this->remotePathTmpl
90 );
91 }
92
93 protected function getFileContent($feedData)
94 {
95 $result = "";
96 $timeToKeepFiles = 24;
97 $tmpDir = \CTempFile::GetDirectoryName($timeToKeepFiles);
98 CheckDirPath($tmpDir);
99
100 $sftp = \Bitrix\Sale\TradingPlatform\Ebay\Helper::getSftp($this->siteId);
101
102 if(!$sftp)
103 return "";
104
105 $sftp->connect();
106 $remotePath = $this->createRemotePath($feedData);
107
108 try
109 {
110 $files = $sftp->getFilesList($remotePath);
111 }
112 catch(SystemException $e)
113 {
114 $files = array();
115 }
116
117 foreach($files as $file)
118 {
119 if(!mb_strstr($file, $feedData["FILENAME"]))
120 continue;
121
122 if($sftp->downloadFile($remotePath."/".$file, $tmpDir.$file))
123 {
124 $result = file_get_contents($tmpDir.$file);
125 Ebay::log(Logger::LOG_LEVEL_INFO, "EBAY_DATA_SOURCE_RESULTS_RECEIVED", $file, "File received successfully.", $this->siteId);
126 }
127 else
128 {
129 Ebay::log(Logger::LOG_LEVEL_ERROR, "EBAY_DATA_SOURCE_RESULTS_ERROR", $tmpDir.$file, "Can't receive file content.", $this->siteId);
130 }
131 }
132
133 return $result;
134 }
135}
static log($level, $type, $itemId, $description, $siteId)
Definition ebay.php:147