Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
profileimport.php
1<?php
3
8
16{
17
21 public function __construct()
22 {
23 $this->fields = new Sale\Internals\Fields();
24 }
25
29 public function getOwnerTypeId()
30 {
32 }
33
39 public function load(array $fields)
40 {
41 $result = new Sale\Result();
42
43 $r = $this->checkFields($fields);
44 if(!$r->isSuccess())
45 {
46 throw new Main\ArgumentException('XML_ID is not defined');
47 }
48
49 $profileFields = $this->initFieldsProfile($fields, $arErrors);
50
51 if(count($arErrors)>0)
52 {
53 foreach($arErrors as $error)
54 {
55 $result->addError(new Main\Error(str_replace('<br>','', $error['TEXT'])));
56 }
57 }
58
59 if(count($profileFields)>0)
60 {
61 $profile = new static();
62
63 $profile->setFields($profileFields);
64
65 $this->setEntity($profile);
66 }
67
68 return $result;
69 }
70
74 public function getPersonalTypeId()
75 {
76 return $this->getField('PERSON_TYPE_ID');
77 }
78
82 public function getUserId()
83 {
84 return $this->getField('USER_ID');
85 }
86
90 public function getId()
91 {
93 $profile = $this->getEntity();
94 return $profile->getField('USER_PROFILE_ID');
95 }
96
100 public function profileGetId()
101 {
102 return $this->getField('USER_PROFILE_ID');
103 }
104
108 public function isImportable()
109 {
110 return $this->settings->isImportableFor($this->getOwnerTypeId());
111 }
112
117 public function add(array $params)
118 {
119 $result = new Sale\Result();
120
122 $profile = $this->getEntity();
123
124 $fields = $params["TRAITS"];
125 $property = $params["ORDER_PROP"];
126
127 $propertyOrders = static::getPropertyOrdersByPersonalTypeId($profile->getPersonalTypeId());
128
129 if(is_array($propertyOrders))
130 {
131 foreach($propertyOrders as $filedsProperty)
132 {
133 $propertyId = $filedsProperty["ID"];
134 if(array_key_exists($propertyId, $property))
135 {
136 $propertyByConfigValue = $property[$propertyId];
137 if($profile->profileGetId()<=0)
138 {
139 if(!empty($propertyByConfigValue))
140 {
141 $profileId = \CSaleOrderUserProps::Add(array(
142 "NAME" => $fields["AGENT_NAME"],
143 "USER_ID" => $profile->getUserId(),
144 "PERSON_TYPE_ID" => $profile->getPersonalTypeId(),
145 "XML_ID" => $fields["XML_ID"],
146 "VERSION_1C" => $fields["VERSION_1C"]
147 ));
148
149 $profile->setField("USER_PROFILE_ID", $profileId);
150 }
151 }
152
153 \CSaleOrderUserPropsValue::Add(array(
154 "USER_PROPS_ID" => $profile->profileGetId(),
155 "ORDER_PROPS_ID" => $propertyId,
156 "NAME" => $filedsProperty["NAME"],
157 "VALUE" => $propertyByConfigValue
158 ));
159 }
160 }
161 }
162
163 return $result;
164 }
165
170 public function update(array $params)
171 {
172 $result = new Sale\Result();
173
175 $profile = $this->getEntity();
176
177 $criterion = $this->getCurrentCriterion($profile);
178
179 $fields = $params["TRAITS"];
180 $property = $params["ORDER_PROP"];
181
182 if($criterion->equals($fields))
183 {
184 $fieldsProfileUpdate = array(
185 "VERSION_1C" => $fields["VERSION_1C"],
186 "NAME" => $fields["AGENT_NAME"],
187 "USER_ID" => $profile->getUserId()
188 );
189
190 \CSaleOrderUserProps::Update($profile->profileGetId(), $fieldsProfileUpdate);
191
192
193 $profileFields = static::getFieldsUserProfile($profile->profileGetId());
194 foreach($profileFields as $fieldsProfile)
195 {
196 $fieldsProfileByProperty[$fieldsProfile["ORDER_PROPS_ID"]] = array("ID" => $fieldsProfile["ID"], "VALUE" => $fieldsProfile["VALUE"]);
197 }
198
199 $propertyOrders = static::getPropertyOrdersByPersonalTypeId($profile->getPersonalTypeId());
200 foreach($propertyOrders as $filedsProperty)
201 {
202 $propertyId = $filedsProperty["ID"];
203 if(array_key_exists($propertyId, $property))
204 {
205 $propertyByConfigValue = $property[$propertyId];
206 if(!empty($propertyByConfigValue))
207 {
208 $fields = array(
209 "USER_PROPS_ID" => $profile->getField("USER_PROFILE_ID"),
210 "ORDER_PROPS_ID" => $propertyId,
211 "NAME" => $filedsProperty["NAME"],
212 "VALUE" => $propertyByConfigValue
213 );
214
215 if(empty($fieldsProfileByProperty[$propertyId]))
216 {
217 \CSaleOrderUserPropsValue::Add($fields);
218 }
219 elseif($fieldsProfileByProperty[$propertyId]["VALUE"] != $propertyByConfigValue)
220 {
221 \CSaleOrderUserPropsValue::Update($fieldsProfileByProperty[$propertyId]["ID"], $fields);
222 }
223 }
224 }
225 }
226 }
227 return $result;
228 }
229
235 public function delete(array $params = null)
236 {
237 return new Sale\Result();
238 }
239
244 protected function checkFields(array $fields)
245 {
246 $result = new Sale\Result();
247
248 if(empty($fields['XML_ID']))
249 {
250 $result->addError(new Main\Error('XML_ID is not defined',''));
251 }
252
253 return $result;
254 }
255
260 public static function getUserProfile(array $fields)
261 {
262 $result = array();
263
264 $r = \CSaleOrderUserProps::GetList(array(),
265 array("XML_ID" => $fields["XML_ID"]),
266 false,
267 false,
268 array("ID", "NAME", "USER_ID", "PERSON_TYPE_ID", "XML_ID", "VERSION_1C")
269 );
270 if ($ar = $r->Fetch())
271 $result = $ar;
272
273 return $result;
274 }
275
280 public static function getFieldsUserProfile($profileId)
281 {
282 $result = array();
283
284 if(intval($profileId) <= 0)
285 return false;
286
287 $r = \CSaleOrderUserPropsValue::GetList(array(), array("USER_PROPS_ID" => $profileId));
288 while ($ar = $r->Fetch())
289 {
290 //$result[$ar["ORDER_PROPS_ID"]] = $ar["VALUE"];
291 $result[] = $ar;
292 }
293
294 return $result;
295 }
296
301 public function initFieldsProfile(array $fields, &$arErrors)
302 {
303 static $profiles = null;
304
305 if($profiles[$fields['XML_ID']] === null)
306 {
307 $result = array();
308 $profile = static::getUserProfile(array('XML_ID'=>$fields['XML_ID']));
309
310 if(!empty($profile))
311 {
312 $result['USER_ID'] = $profile['USER_ID'];
313 $result['PERSON_TYPE_ID'] = $profile['PERSON_TYPE_ID'];
314
315 $result['USER_PROFILE_ID'] = $profile['ID'];
316 $result['USER_PROFILE_VERSION'] = $profile['VERSION_1C'];
317
318 $profileFields = static::getFieldsUserProfile($profile['ID']);
319 if(!empty($profileFields))
320 {
321 foreach($profileFields as $profileField)
322 {
323 $result['USER_PROPS'][$profileField["ORDER_PROPS_ID"]] = $profileField["VALUE"];
324 }
325 }
326 }
327 elseif($this->isImportable())
328 {
329 $result['PERSON_TYPE_ID'] = $this->resolvePersonTypeId($fields);
330
331 $user = static::getUserByCode($fields['XML_ID']);
332 if(!empty($user))
333 $result['USER_ID'] = $user['ID'];
334 else
335 $result['USER_ID'] = $this->registerUser($fields, $arErrors);
336
337 $result['USER_PROFILE_ID'] = null;
338 $result['USER_PROFILE_VERSION'] = null;
339 $result['USER_PROPS'] = null;
340 }
341
342 if(count($arErrors)>0)
343 return $result;
344 else
345 $profiles[$fields['XML_ID']] = $result;
346 }
347
348 return $profiles[$fields['XML_ID']];
349 }
350
356 public static function getUserByCode($code)
357 {
358 return Exchange\Entity\UserProfileImportLoader::getUserByCode($code);
359 }
360
364 public static function getFieldExternalId()
365 {
366 return 'XML_ID';
367 }
368
372 public function refreshData(array $fields)
373 {
374 }
375}
initFieldsProfile(array $fields, &$arErrors)
static getUserProfile(array $fields)