Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
importonecbase.php
1<?php
2
4
5
18
19abstract class ImportOneCBase extends ImportPattern
20{
21 use LoggerTrait;
22 use BaseTrait;
23
24 const EVENT_ON_EXCHANGE_CONFIGURE_IMPORTER = 'OnExchangeConfigureImporter';
25 const DELIVERY_SERVICE_XMLID = 'ORDER_DELIVERY';
26
28 protected $fields;
29 static protected $config;
30
35 public function setFields(array $values)
36 {
37 foreach ($values as $key=>$value)
38 {
39 $this->setField($key, $value);
40 }
41 }
42
47 public function setField($name, $value)
48 {
49 $this->fields->set($name, $value);
50 }
51
56 public function getField($name)
57 {
58 return $this->fields->get($name);
59 }
60
65 protected function checkFields(array $items)
66 {
67 $result = new Result();
68
69 foreach($items as $item)
70 {
71 $params = $item->getFieldValues();
72 $fields = $params['TRAITS'];
73
74 if($fields[$item::getFieldExternalId()] == '')
75 $result->addErrors(array(new Error(" ".EntityType::getDescription($item->getOwnerTypeId()).": ".GetMessage("SALE_EXCHANGE_EXTERNAL_ID_NOT_FOUND"), 'SALE_EXCHANGE_EXTERNAL_ID_NOT_FOUND')));
76 }
77
78 return $result;
79 }
80
84 static public function checkSettings()
85 {
86 return new Result();
87 }
88
89 static public function configuration()
90 {
91 $event = new Event('sale', static::EVENT_ON_EXCHANGE_CONFIGURE_IMPORTER);
92 $event->send();
93 }
94
95 static protected function setConfig($option='', $value=true)
96 {
97 if($value)
98 {
99 static::$config |= $option;
100 }
101 else
102 {
103 static::$config &= ~$option;
104 }
105 }
106
113 protected function modifyEntity($item)
114 {
115 $result = new Result();
116
117 if(!($item instanceof EntityImport) && !($item instanceof UserProfileImport))
118 throw new ArgumentException("Item must be instanceof EntityImport or UserProfileImport");
119
120 $params = $item->getFieldValues();
121
122 $fieldsCriterion = $fields = &$params['TRAITS'];
123
124 $converter = ConverterFactory::create($item->getOwnerTypeId());
125 $converter::sanitizeFields($item->getEntity(), $fields, $item->getSettings());
126
127 $item->refreshData($fields);
128
129 $criterion = $item->getCurrentCriterion($item->getEntity());
130 $collision = $item->getCurrentCollision($item->getOwnerTypeId());
131
132 if($item instanceof ShipmentImport)
133 $fieldsCriterion['ITEMS'] = $params['ITEMS'];
134
135 if($criterion->equals($fieldsCriterion))
136 {
137 $collision->resolve($item);
138 }
139
140 if(!$criterion->equals($fieldsCriterion) ||
141 ($criterion->equals($fieldsCriterion) && !$item->hasCollisionErrors()))
142 {
143 $result = $item->import($params);
144 }
145
146 return $result;
147 }
148
155 public function parse(array $rawFields)
156 {
157 $result = new Result();
158 $list = array();
159
160 foreach($rawFields as $raw)
161 {
162 $documentTypeId = $this->resolveDocumentTypeId($raw);
163
164 $document = $this->documentFactoryCreate($documentTypeId);
165
166 $fields = $document::prepareFieldsData($raw);
167
168 $document->setFields($fields);
169
170 $list[] = $document;
171 }
172
173 $result->setData($list);
174
175 return $result;
176 }
177
182 protected function convertDocument(DocumentBase $document)
183 {
184 $entityTypeId = $this->resolveOwnerEntityTypeId($document->getTypeId());
185 $settings = ManagerImport::getSettingsByType($entityTypeId);
186
187 $convertor = $this->converterFactoryCreate($document->getTypeId());
188 $convertor->init(
189 $settings,
190 $entityTypeId,
191 $document->getTypeId()
192 );
193
194 $fields = $convertor->resolveParams($document);
195
196 $loader = Entity\EntityImportLoaderFactory::create($entityTypeId);
197 $loader->loadSettings($settings);
198
199 if($document->getId() <> '')
200 $fieldsEntity = $loader->getByNumber($document->getId());
201 else
202 $fieldsEntity = $loader->getByExternalId($document->getExternalId());
203
204 if(!empty($fieldsEntity['ID']))
205 $fields['TRAITS']['ID'] = $fieldsEntity['ID'];
206
207 $entityImport = $this->entityFactoryCreate($entityTypeId);
208 ManagerImport::configure($entityImport);
209
210 $entityImport->setFields($fields);
211
212 return $entityImport;
213 }
214
215 abstract protected function resolveOwnerEntityTypeId($typeId);
216
221 protected function resolveDocumentTypeId(array $fields)
222 {
223 return OneC\DocumentBase::resolveRawDocumentTypeId($fields);
224 }
225
229 protected static function getMessage()
230 {
231 return Loc::loadLanguageFile($_SERVER["DOCUMENT_ROOT"].'/bitrix/components/bitrix/sale.export.1c/component.php');
232 }
233
237 public function getDirectionType()
238 {
240 }
241
246 protected function logger(array $items)
247 {
248 return $this->loggerEntities($items);
249 }
250}
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static setConfig($option='', $value=true)
convertDocument(DocumentBase $document)
static configure(ImportBase $entity)
loggerEntities(array $items)