1 <?php
2
3 4 5 6 7
8 class BaseMediawebImportParser {
9
10 static $dtd = 'http://import.mediaweb.ru/import.dtd';
11 private $filename;
12 private $ImageFolder;
13 private $encoding;
14 private $disableValidation = false;
15 private $message = '';
16 protected $categories = array();
17 protected $offers = array();
18 protected $currency = array();
19
20 public function __construct($filename, $imagefolder = null, $encoding='UTF-8') {
21 $this->encoding = $encoding;
22 $this->filename = $filename;
23 $this->ImageFolder = $imagefolder;
24 }
25
26 public function setDisableValidation() {
27 $this->disableValidation = true;
28 }
29
30 public function parse() {
31 $valid = $this->validate();
32
33 if (!$valid) {
34 $this->setDisableValidation();
35 $valid = true;
36 }
37
38 if (!$valid) return false;
39
40 $xml = simplexml_load_file($this->filename);
41 foreach ($xml as $element => $value) {
42 switch ($element) {
43 case 'currencies':
44 $result = $this->parse_currency($value);
45 break;
46
47 case 'categories':
48 $result = $this->parse_categories($value);
49 break;
50
51 case 'offers':
52 $result = $this->parse_offers($value);
53 break;
54
55 default:
56 $this->setMessage('Invalid XML element: ' . $element);
57 $result = false;
58 break;
59 }
60 if (!$result) {
61 return false;
62 }
63 }
64 if (!$this->saveImages()) {
65
66 return false;
67 }
68 return true;
69 }
70
71 public function saveImages() {
72 if ($this->ImageFolder) {
73 $i = 0;
74
75 foreach ($this->offers as $offer) {
76 if (isset($offer->photo)) {
77 if (isset($offer->image)) {
78 $img = base64_decode($offer->image);
79 $filename = $this->ImageFolder . '/' . $offer->photo;
80 } else {
81 $img = file_get_contents($offer->photo);
82 $filename = $this->ImageFolder . '/' . time() . '.' . substr(strrchr($offer->photo, '.'), 1);
83 }
84 if (!isset($filename)) {
85 return false;
86 }
87 $f = fopen($filename, 'w+');
88 if (!fwrite($f, $img))
89 return false;
90 else {
91 $this->offers[$i]->photo = $filename;
92 }
93 fclose($f);
94 }
95 }
96 }
97 return true;
98 }
99
100 public function setImageFolder($folder) {
101 $this->ImageFolder = $folder;
102 }
103
104 public function getImageFolder() {
105 return $this->ImageFolder;
106 }
107
108 public function setMessage($val) {
109 $this->message = $val;
110 }
111
112 public function getMessage() {
113 return $this->message;
114 }
115
116 public function validate() {
117 if ($this->disableValidation) {
118 return true;
119 }
120 if (!file_exists($this->filename)) {
121 $this->setMessage('XML file not found');
122 return false;
123 }
124 $xml = file_get_contents($this->filename);
125
126 $dom = new DOMDocument;
127 $dom->loadXML($xml, LIBXML_PARSEHUGE);
128
129 if ($dom->validate())
130 return true;
131 $this->setMessage('DOM Validation failed');
132 return false;
133 }
134
135 private function parse_currency($elements) {
136 foreach ($elements as $currency) {
137 if ($currency->getName() != 'currency') {
138 $this->setMessage('Invalid currency element: ' . $currency->getName());
139 return false;
140 }
141
142
143 $rate = 1;
144 if ($currency->attributes()->rate)
145 $rate = (float) $currency->attributes ()->rate;
146
147 $this->currency[(string) $currency->attributes()->id] = array(
148 'rate' => $rate
149 );
150 }
151
152 return true;
153 }
154
155 private function parse_categories($elements) {
156 foreach ($elements as $category) {
157 if ($category->getName() != 'category') {
158 $this->setMessage('Invalid category element: ' . $category->getName());
159 return false;
160 }
161 $parentid = (isset($category->attributes()->parentId)) ? (string) $category->attributes()->parentId : NULL;
162 array_push($this->categories, array(
163 'id' => (string) $category->attributes()->id,
164 'parentid' => $parentid,
165 'title' => $this->convert((string) $category)
166 ));
167 }
168
169 return true;
170 }
171
172 private function parse_offers($elements) {
173 foreach ($elements as $offer) {
174 if ($offer->getName() != 'offer') {
175 $this->setMessage('Invalid offer element: ' . $offer->getName());
176 return false;
177 }
178
179 $url = (isset($offer->url)) ? $this->convert((string) $offer->url) : NULL;
180 $description = (isset($offer->description)) ? $this->convert((string) $offer->description) : NULL;
181 $fulldesc = (isset($offer->fulldesc)) ? $this->convert((string) $offer->fulldesc) : NULL;
182 $picture = (isset($offer->picture)) ? $this->convert((string) $offer->picture) : NULL;
183 $image = (isset($offer->image)) ? $this->convert((string) $offer->image) : NULL;
184 $typePrefix = (isset($offer->typePrefix)) ? $this->convert((string) $offer->typePrefix) : NULL;
185 $xCategory = (isset($offer->xCategory)) ? $this->convert((string) $offer->xCategory) : NULL;
186 $vendor = (isset($offer->vendor)) ? $this->convert((string) $offer->vendor) : NULL;
187 $vendorCode = (isset($offer->vendorCode)) ? $this->convert((string) $offer->vendorCode) : NULL;
188 $model = (isset($offer->model)) ? $this->convert((string) $offer->model) : NULL;
189
190 $categories = NULL;
191 if ($offer->categoryId) {
192 $categories = array();
193 foreach ($offer->categoryId as $categoryId) {
194 array_push($categories, array(
195 'id' => (string) $this->convert($categoryId),
196 'type' => (isset($categoryId->attributes()->type)) ? (string) $categoryId->attributes()->type : 'Own'
197 ));
198 }
199 }
200
201 $special = NULL;
202 if ($offer->special) {
203 $special = array();
204 foreach ($offer->special as $obj) {
205 array_push($special, array(
206 'id' => (string) $obj->attributes()->id,
207 'url' => (string) $obj->attributes()->url,
208 'siteid' => (string) $obj->attributes()->siteid,
209 ));
210 }
211 }
212
213 $params = array();
214 if ($offer->param) {
215 foreach ($offer->param as $param) {
216 array_push($params, array(
217 'name' => (string) $param->attributes()->name,
218 'value' => (string) $param
219 ));
220 }
221 }
222
223 array_push($this->offers, array(
224
225 'id' => (string) $offer->attributes()->id,
226 'available' => ($offer->attributes()->available == 'true') ? 1 : 0,
227 'title' => $this->convert((string) $offer->title),
228 'price' => (float) str_replace(',', '.', $this->convert((string) $offer->price)),
229 'currencyId' => $this->convert((string) $offer->currencyId),
230
231 'description' => $description,
232 'url' => $url,
233 'picture' => $picture,
234 'image' => $image,
235 'fulldesc' => $fulldesc,
236 'typePrefix' => $typePrefix,
237 'xCategory' => $xCategory,
238 'vendor' => $vendor,
239 'vendorCode' => $vendorCode,
240 'model' => $model,
241
242 'categories' => $categories,
243 'special' => $special,
244 'params' => $params
245 ));
246 }
247
248 return true;
249 }
250
251 private function convert($string) {
252 $string = htmlspecialchars_decode($string);
253 return $string;
254 }
255
256 }
257
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-