Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
converter.php
1<?php
3
4
11
17abstract class Converter implements IConverter
18{
20 const KOEF_DEFAULT = 1;
22
24 protected $settings = null;
25
26 protected $entityTypeId;
27 protected $documentTypeId;
28
30 private static $instances = null;
31
37 public static function getInstance($typeId)
38 {
39 if(!is_int($typeId))
40 {
41 $typeId = (int)$typeId;
42 }
43
44 if(!DocumentType::IsDefined($typeId))
45 {
46 throw new ArgumentOutOfRangeException('Is not defined', DocumentType::FIRST, DocumentType::LAST);
47 }
48
49 if(self::$instances === null || !isset(self::$instances[$typeId]))
50 {
51 if(self::$instances === null)
52 {
53 self::$instances = array();
54 }
55
56 if(!isset(self::$instances[$typeId]))
57 {
58 self::$instances[$typeId] = ConverterFactory::create($typeId);
59 }
60 }
61 return self::$instances[$typeId];
62 }
63
67 abstract protected function getFieldsInfo();
68
72 public function getSettings()
73 {
74 return $this->settings;
75 }
76
77 public function getEntityTypeId()
78 {
79 return $this->entityTypeId;
80 }
81
82 public function getDocmentTypeId()
83 {
84 return $this->documentTypeId;
85 }
86
87 public function init(ISettings $settings, $entityTypeId = EntityType::UNDEFINED, $documentTypeId = DocumentType::UNDEFINED)
88 {
89 $this->settings = $settings;
90 $this->entityTypeId = EntityType::isDefined($entityTypeId) ? $entityTypeId:EntityType::UNDEFINED;
91 $this->documentTypeId = DocumentType::isDefined($documentTypeId) ? $documentTypeId:DocumentType::UNDEFINED;
92 }
93
98 protected function externalizeField(&$value, $fieldInfo=null)
99 {
100 if($value<>'')
101 {
102 switch($fieldInfo['TYPE'])
103 {
104 case 'text':
105 $value = self::toText($value);
106 break;
107 case 'time':
108 if($value instanceof DateTime)
109 $value = \CDatabase::FormatDate($value->toString(), \CSite::GetDateFormat("FULL", LANG), "HH:MI:SS");
110 break;
111 case 'date':
112 if($value instanceof DateTime)
113 $value = \CDatabase::FormatDate($value->toString(), \CSite::GetDateFormat("FULL", LANG), "YYYY-MM-DD");
114 break;
115 case 'datetime':
116 if($value instanceof DateTime)
117 $value = \CDatabase::FormatDate($value->toString(), \CSite::GetDateFormat("FULL", LANG), "YYYY-MM-DD HH:MI:SS");
118 break;
119 case 'bool':
120 $value = $value == 'Y'? 'true':'false';
121 break;
122 case 'int':
123 $value = intval($value);
124 break;
125 }
126 }
127 }
128
133 protected static function toText($value)
134 {
135 $value = preg_replace('/<style[^>]*>.*?<\/style>/is', '', $value);
136 $value = preg_replace('/<blockquote[^>]*>.*?<\/blockquote>/is', '', $value);
137 $value = preg_replace('/<script[^>]*>.*?<\/script>/is', '', $value);
138
139 return html_entity_decode(
140 strip_tags(
141 preg_replace('/(<br[^>]*>)+/is'.BX_UTF_PCRE_MODIFIER, "\n", $value)
142 )
143 );
144 }
145
150 public static function normalizeExternalCode($code)
151 {
152 $xml_id = $code;
153 list($productXmlId, $offerXmlId) = explode("#", $xml_id, 2);
154 if ($productXmlId === $offerXmlId)
155 $xml_id = $productXmlId;
156
157 return $xml_id;
158 }
159
165 protected function externalizeRekv(array $rekv, array $info)
166 {
167 $result = array();
168 $rowId=0;
169 foreach($rekv as $kRekv=>$vRekv)
170 {
171 foreach($info['FIELDS'] as $name=>$fieldInfo)
172 {
173 $value='';
174 switch($name)
175 {
176 case 'NAME':
177 $value = $kRekv;
178 break;
179 case 'VALUE':
180 $value = $vRekv;
181 if($value instanceof DateTime)
182 $fieldInfo['TYPE'] = 'datetime';
183 break;
184 }
185 $this->externalizeField($value, $fieldInfo);
186 $result[$rowId][$name] = $value;
187 }
188 $rowId++;
189 }
190 return $result;
191 }
192
198 protected function externalizeRekvValue($kRekv, $vRekv, array $info)
199 {
200 $result = array();
201 foreach($info['FIELDS'] as $name=>$fieldInfo)
202 {
203 $value='';
204 switch($name)
205 {
206 case 'NAME':
207 $value = DocumentBase::getLangByCodeField($kRekv);
208 break;
209 case 'VALUE':
210 $value = $vRekv;
211 break;
212 }
213 $this->externalizeField($value, $fieldInfo);
214 $result[$name] = $value;
215 }
216 return $result;
217 }
218
223 protected function modifyTrim(array $fields)
224 {
225
226 $result = array();
227 foreach ($fields as $key=>$value)
228 {
229 if(is_array($value))
230 {
231 if(count($value)>0)
232 {
233 if($key === 'REK_VALUES' || $key === 'ADDRESS_FIELD' || $key === 'CONTACT' || $key === 'REPRESENTATIVE')
234 {
235 $groupFieldValues = array();
236 foreach ($value as $k=>$v)
237 {
238 if($v['VALUE']=='')
239 {
240 unset($fields[$key][$k]);
241 }
242 $groupFieldValues = $fields[$key];
243 }
244 if(count($groupFieldValues)>0)
245 $result[$key] = $groupFieldValues;
246 else
247 unset($fields[$key]);
248 }
249 else
250 {
251 $value = $this->modifyTrim($value);
252 if(count($value)>0)
253 $result[$key]=$value;
254 }
255 }
256 }
257 else
258 {
259 if($value<>'')
260 $result[$key]=$value;
261 }
262 }
263
264 return $result;
265 }
266
271 static protected function getSiteNameByLid($lid)
272 {
273 static $sites;
274
275 if($sites === null)
276 {
277 $res = \CSite::getList("sort", "desc");
278 while ($site = $res->fetch())
279 {
280 $sites[$site['LID']]=$site['NAME'];
281 }
282
283 if(!is_array($sites))
284 {
285 $sites = array();
286 }
287 }
288 return isset($sites[$lid]) ? $sites[$lid]:'';
289 }
290
291 public function modifyItemIdByItemName($fields)
292 {
293 $result = $fields;
294
295 if(is_array($fields))
296 {
297 foreach($fields as $k=>$items)
298 {
299 foreach($items as $productXML_ID => $item)
300 {
301 if($item['NAME'] == DocumentBase::getLangByCodeField(ImportOneCBase::DELIVERY_SERVICE_XMLID))
302 {
303 unset($result[$k][$productXML_ID]);
304
305 $item['ID'] = ImportOneCBase::DELIVERY_SERVICE_XMLID;
306 $result[$k][ImportOneCBase::DELIVERY_SERVICE_XMLID] = $item;
307 }
308 }
309 }
310 }
311 return $result;
312 }
313}
externalizeField(&$value, $fieldInfo=null)
Definition converter.php:98
externalizeRekv(array $rekv, array $info)
externalizeRekvValue($kRekv, $vRekv, array $info)
init(ISettings $settings, $entityTypeId=EntityType::UNDEFINED, $documentTypeId=DocumentType::UNDEFINED)
Definition converter.php:87