Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userimportbase.php
1<?php
3
4
10
11abstract class UserImportBase extends ImportBase
12{
13 const EXTERNAL_AUTH_ID = 'sale';
14
16 protected $entity;
17
21 public function setEntity(ImportBase $entity)
22 {
23 $this->entity = $entity;
24 }
25
29 public function getEntity()
30 {
31 return $this->entity;
32 }
33
40 public function getPropertyOrdersByConfig($personalTypeId, $profile, $property)
41 {
42 $result = array();
43
44 if($fieldsConfig = $this->getFieldsConfig($personalTypeId, $profile))
45 {
46 if(is_array($fieldsConfig))
47 {
48 foreach($fieldsConfig as $k => $v)
49 {
50 if(!isset($v['VALUE']))
51 continue;
52
53 if(!empty($property[$k]))
54 {
55 $result[$v["VALUE"]] = $property[$k];
56 }
57
58 if(empty($result[$v["VALUE"]]) && !empty($profile[$v["VALUE"]]))
59 {
60 $result[$v["VALUE"]] = $profile[$v["VALUE"]];
61 }
62 }
63 }
64 }
65 return $result;
66 }
67
73 public function getFieldsConfig($orgFormId, $userProps=array())
74 {
75 if(intval($orgFormId)<=0)
76 return false;
77
78 $config = $this->getConfig();
79
80 if(empty($config[$orgFormId]))
81 return false;
82
83 $fields = $config[$orgFormId];
84 foreach($fields as $k => $v)
85 {
86 if(empty($v) ||
87 ((empty($v["VALUE"]) || $v["TYPE"] != "PROPERTY") &&
88 (empty($userProps) || (is_array($v) && is_string($v["VALUE"]) && empty($userProps[$v["VALUE"]])))
89 )
90 )
91 {
92 unset($fields[$k]);
93 }
94
95 }
96 return $fields;
97 }
98
102 public function getConfig()
103 {
104 static $config = null;
105
106 if($config === null)
107 {
108 if($personTypes = $this->getListPersonType($this->settings->getSiteId()))
109 {
110 $r = \CSaleExport::GetList(array(), array("PERSON_TYPE_ID" => $personTypes));
111 while($ar = $r->Fetch())
112 {
113 $config[$ar["PERSON_TYPE_ID"]] = unserialize($ar["VARS"], ['allowed_classes' => false]);
114 }
115 }
116 }
117 return $config;
118 }
119
124 public function getListPersonType($siteId)
125 {
126 static $personType = null;
127
128 if($personType === null)
129 {
130 $registry = \Bitrix\Sale\Registry::getInstance(\Bitrix\Sale\Registry::REGISTRY_TYPE_ORDER);
131 $class = $registry->getPersonTypeClassName();
132 $r = $class::getlist(['filter' => ["=ACTIVE" => "Y", "=PERSON_TYPE_SITE.SITE_ID" => $siteId]]);
133 while($ar = $r->fetch())
134 {
135 $personType[] = $ar["ID"];
136 }
137 }
138 return $personType;
139 }
140
144 public function isFiz()
145 {
146 $fields = $this->getFieldValues();
147 return ($fields["TRAITS"]["TYPE"] == "FIZ");
148 }
149
155 {
156 $config = $this->getConfig();
157
158 if(is_array($config))
159 {
160 foreach($config as $id => $value)
161 {
162 if(($value["IS_FIZ"] == "Y" && $this->isFiz()) ||
163 ($value["IS_FIZ"] == "N" && !$this->isFiz()))
164 {
165 return $id;
166 }
167 }
168 }
169
170 return null;
171 }
172
177 public static function getPropertyOrdersByPersonalTypeId($personTypeId)
178 {
179 static $result = null;
180
181 if($result[$personTypeId] === null)
182 {
183 $dbOrderProperties = \CSaleOrderProps::GetList(
184 array("SORT" => "ASC"),
185 array(
186 "PERSON_TYPE_ID" => $personTypeId,
187 "ACTIVE" => "Y",
188 "UTIL" => "N",
189 "USER_PROPS" => "Y",
190 ),
191 false,
192 false,
193 array("ID", "TYPE", "NAME", "CODE", "USER_PROPS", "SORT", "MULTIPLE")
194 );
195 while ($arOrderProperties = $dbOrderProperties->Fetch())
196 $result[$personTypeId][] = $arOrderProperties;
197 }
198
199 return $result[$personTypeId];
200 }
201
207 public function registerUser($fields, &$arErrors)
208 {
209 $userFields = array(
210 "NAME" => $fields["ITEM_NAME"],
211 "EMAIL" => $fields["CONTACT"]["MAIL_NEW"],
212 );
213
214 if ($userFields["NAME"] == '')
215 $userFields["NAME"] = $fields["CONTACT"]["CONTACT_PERSON"];
216
217 $userFields["NAME"] = ($this->isFiz() ? $userFields["NAME"]:array("NAME"=>$userFields["NAME"]));
218
219 $emServer = $_SERVER["SERVER_NAME"];
220 if(mb_strpos($_SERVER["SERVER_NAME"], ".") === false)
221 $emServer .= ".bx";
222
223 if ($userFields["EMAIL"] == '')
224 $userFields["EMAIL"] = "buyer" . time() . GetRandomCode(2) . "@" . $emServer;
225
226 $id = \CSaleUser::DoAutoRegisterUser($userFields["EMAIL"], $userFields["NAME"], $this->settings->getSiteId(), $arErrors, array("XML_ID"=>$fields["XML_ID"], "EXTERNAL_AUTH_ID"=>self::EXTERNAL_AUTH_ID));
227
228 $obUser = new \CUser;
229 if($fields["CONTACT"]["PHONE"] <> '')
230 $obUser->Update($id, array('WORK_PHONE'=>$fields["CONTACT"]["PHONE"]), true);
231
232 return $id;
233 }
234
242 static public function updateEmptyXmlId($id, $xmlId)
243 {
244 $result = false;
245
246 if(intval($id)>0)
247 {
248 $user = UserTable::getById($id);
249 if($fields = $user->fetch())
250 {
251 if($fields['XML_ID'] == '' && $fields['XML_ID'] <> $xmlId)
252 {
253 $user = new \CUser;
254 $result = $user->Update($id, array('XML_ID'=>$xmlId), true);
255 }
256 }
257 }
258
259 return $result;
260 }
261
262 public function initFields()
263 {
264 $this->setFields(array(
265 'TRAITS'=> $this->getFieldsTraits(),
266 ));
267 }
268
274 {
275 if(!($entity instanceof Order))
276 throw new ArgumentException("entity must be instanceof Order");
277
278 return $entity;
279 }
280}
getPropertyOrdersByConfig($personalTypeId, $profile, $property)
static getPropertyOrdersByPersonalTypeId($personTypeId)
getFieldsConfig($orgFormId, $userProps=array())
static getBusinessValueOrderProvider(IBusinessValueProvider $entity)