1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
csv_user_import.php
См. документацию.
1<?php
8
10
11IncludeModuleLangFile(__FILE__);
12
14{
15 var $csv;
16 var $arHeader = false;
17 var $isErrorOccured = false;
18 var $errorMessage = "";
19 var $ignoreDuplicate = false;
20 var $userGroups = false;
21 var $callback = null;
22 var $defaultEmail = false;
23 var $imageFilePath = null;
24 var $externalAuthID = null;
25
27 var $userPropertyName = "UF_DEPARTMENT";
28
29 var $arSectionCache = Array();
31
32 public function __construct($csvFilePath, $delimiter)
33 {
34 require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/classes/general/csv_data.php");
35
36 $this->csv = new CCSVData($fields_type = "R");
37 $this->csv->LoadFile($csvFilePath);
38 $this->csv->MoveFirst();
39 $this->csv->SetDelimiter($delimiter);
40 $this->csv->SetFirstHeader(false);
41
42 if (!$this->arHeader = $this->csv->Fetch())
43 {
44 $this->isErrorOccured = true;
45 $this->errorMessage = GetMessage("CSV_IMPORT_HEADER_NOT_FOUND");
46 return;
47 }
48
49 foreach($this->arHeader as $key => $val)
50 $this->arHeader[$key] = mb_strtoupper($val);
51
52 if (!$this->CheckRequiredFields())
53 {
54 $this->isErrorOccured = true;
55 return;
56 }
57 }
58
60 {
61 if ($this->isErrorOccured || !is_array($this->arHeader) || count($this->arHeader) <= 1)
62 {
63 $this->errorMessage = GetMessage("CSV_IMPORT_DELIMETER_NOT_FOUND");
64 return false;
65 }
66
67 $success = array_search("NAME", $this->arHeader);
68 if ($success === false)
69 {
70 $this->errorMessage = GetMessage("CSV_IMPORT_NAME_NOT_FOUND");
71 return false;
72 }
73
74 $success = array_search("LAST_NAME", $this->arHeader);
75 if ($success === false)
76 {
77 $this->errorMessage = GetMessage("CSV_IMPORT_LAST_NAME_NOT_FOUND");
78 return false;
79 }
80
81 return true;
82 }
83
84 function AttachUsersToIBlock($iblockID)
85 {
86 $iblockID = intval($iblockID);
87 if (CModule::IncludeModule("iblock") && $iblockID > 0)
88 {
89 $dbIblock = CIBlock::GetByID($iblockID);
90 if ($dbIblock->Fetch())
91 $this->attachIBlockID = $iblockID;
92 }
93 }
94
96 {
98
100 $this->userPropertyName = $userPropertyName;
101 }
102
103 function GenerateUserPassword($pass_len=10)
104 {
105 static $allchars = "abcdefghijklnmopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ0123456789";
106 $n = 61;
107
108 $string = "";
109 for ($i = 0; $i < $pass_len; $i++)
110 $string .= $allchars[mt_rand(0, $n)];
111
112 return $string;
113 }
114
115 function IsErrorOccured()
116 {
118 }
119
121 {
122 if ($externalAuthID <> '')
123 $this->externalAuthID = $externalAuthID;
124 }
125
127 {
128 return $this->errorMessage;
129 }
130
131 function IgnoreDuplicate($ignore = true)
132 {
133 $this->ignoreDuplicate = (bool)$ignore;
134 }
135
136 function SetCallback($functionName)
137 {
138 if (is_callable($functionName))
139 $this->callback = $functionName;
140 }
141
142 function &GetCsvObject()
143 {
144 return $this->csv;
145 }
146
148 {
149 if (check_email($email))
150 $this->defaultEmail = $email;
151 }
152
154 {
155 if ($this->defaultEmail !== false)
156 return $this->defaultEmail;
157
158 return COption::GetOptionString("main", "email_from", "admin@".$_SERVER["SERVER_NAME"]);
159 }
160
162 {
163 if (!is_array($arGroups))
164 return;
165
166 foreach ($arGroups as $groupID)
167 {
168 $groupID = intval($groupID);
169 $rsGroup = CGroup::GetByID($groupID);
170 if (!$rsGroup->Fetch())
171 continue;
172
173 if (!is_array($this->userGroups))
174 $this->userGroups = Array();
175
176 $this->userGroups[] = $groupID;
177 }
178 }
179
180 function SetImageFilePath($relativePath)
181 {
182 $relativePath = Rel2Abs("/", $relativePath);
183 if (is_dir($_SERVER["DOCUMENT_ROOT"].$relativePath))
184 $this->imageFilePath = rtrim($_SERVER["DOCUMENT_ROOT"].$relativePath, "/");
185 }
186
188 {
189 if ($this->attachIBlockID < 1)
190 return false;
191
192 $success = true;
193 $dbRes = CUserTypeEntity::GetList(Array(), Array("ENTITY_ID" => "USER", "FIELD_NAME" => $this->userPropertyName));
194 if (!$dbRes->Fetch())
195 {
196 $arLabelNames = Array();
197 $rsLanguage = CLanguage::GetList();
198 while($arLanguage = $rsLanguage->Fetch())
199 {
200 IncludeModuleLangFile($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/classes/general/csv_user_import_labels.php", $arLanguage["LID"]);
201 $arLabelNames[$arLanguage["LID"]] = GetMessage("DEPARTMENT_USER_PROPERTY_NAME");
202 }
203
204 $arFields = Array(
205 'ENTITY_ID' => 'USER',
206 'FIELD_NAME' => $this->userPropertyName,
207 'USER_TYPE_ID' => 'iblock_section',
208 'XML_ID' => '',
209 'SORT' => 100,
210 'MULTIPLE' => 'Y',
211 'MANDATORY' => 'N',
212 'SHOW_FILTER' => 'I',
213 'SHOW_IN_LIST' => 'Y',
214 'EDIT_IN_LIST' => 'Y',
215 'IS_SEARCHABLE' => 'Y',
216 'SETTINGS' => array(
217 'DISPLAY' => 'LIST',
218 'LIST_HEIGHT' => '8',
219 'IBLOCK_ID' => $this->attachIBlockID,
220 ),
221
222 "EDIT_FORM_LABEL" => $arLabelNames,
223 "LIST_COLUMN_LABEL" => $arLabelNames,
224 "LIST_FILTER_LABEL" => $arLabelNames,
225 );
226
227 $userType = new CUserTypeEntity();
228 $success = (bool)$userType->Add($arFields);
229 }
230
231 return $success;
232 }
233
235 {
236 $sectionID = 0;
237 $i = 0;
238
239 while(true)
240 {
241 $i++;
242
243 $csvSectionCode = "IBLOCK_SECTION_NAME_".$i;
244 if (!array_key_exists($csvSectionCode, $arFields))
245 break;
246
247 $sectionName = trim($arFields[$csvSectionCode]);
248 if (mb_strlen($sectionName) < 1)
249 break;
250
251 $cacheID = md5($csvSectionCode."_".$sectionName."_".$sectionID);
252 if (array_key_exists($cacheID, $this->arSectionCache))
253 {
254 $sectionID = $this->arSectionCache[$cacheID];
255 continue;
256 }
257
258 $dbSection = CIBlockSection::GetList(Array(), Array("IBLOCK_ID" => $this->attachIBlockID, "NAME" => $sectionName, "SECTION_ID" => $sectionID));
259 if ($arGroup = $dbSection->Fetch())
260 {
261 $sectionID = $arGroup["ID"];
262 $this->arSectionCache[$cacheID] = $sectionID;
263 continue;
264 }
265
266 $iblockSection = new CIBlockSection;
267 $arSectionFields = Array(
268 "ACTIVE" => "Y",
269 "IBLOCK_SECTION_ID" => $sectionID,
270 "IBLOCK_ID" => $this->attachIBlockID,
271 "NAME" => $sectionName,
272 );
273
274 $sectionID = (int)$iblockSection->Add($arSectionFields);
275 if ($sectionID > 1)
276 $this->arSectionCache[$cacheID] = $sectionID;
277 else
278 return 0;
279 }
280
281 return $sectionID;
282 }
283
284 function ImportUser()
285 {
286 if ($this->isErrorOccured)
287 return false;
288
289 $this->errorMessage = "";
290
292
293 if (!$arUser = $this->csv->FetchDelimiter())
294 return false;
295
296 $arFields = Array();
297 foreach($this->arHeader as $index => $key)
298 if(($f = trim($arUser[$index])) <> '')
299 $arFields[$key] = $f;
300
301 if (!array_key_exists("NAME", $arFields) || mb_strlen($arFields["NAME"]) < 1)
302 {
303 $this->errorMessage = GetMessage("CSV_IMPORT_NO_NAME")." (".implode(", ", $arFields).").<br>";
304 return true;
305 }
306
307 if (!array_key_exists("LAST_NAME", $arFields) || mb_strlen($arFields["LAST_NAME"]) < 1)
308 {
309 $this->errorMessage = GetMessage("CSV_IMPORT_NO_LASTNAME")." (".implode(", ", $arFields).").<br>";
310 return true;
311 }
312
313 if (!array_key_exists("PASSWORD", $arFields) || mb_strlen($arFields["PASSWORD"]) < 1)
314 $arFields["PASSWORD"] = $this->GenerateUserPassword(6);
315 $arFields["CONFIRM_PASSWORD"] = $arFields["PASSWORD"];
316
317 if (!array_key_exists("EMAIL", $arFields) || mb_strlen($arFields["EMAIL"]) < 3 || !check_email($arFields["EMAIL"]))
318 $arFields["EMAIL"] = $defaultEmail;
319
320 if (!array_key_exists("LOGIN", $arFields))
321 $arFields["LOGIN"] = mb_strtolower($arFields["NAME"]." ".$arFields["LAST_NAME"]);
322
323 if (array_key_exists("PERSONAL_BIRTHDAY", $arFields) && (mb_strlen($arFields["PERSONAL_BIRTHDAY"]) < 2 || !CheckDateTime($arFields["PERSONAL_BIRTHDAY"])))
324 unset($arFields["PERSONAL_BIRTHDAY"]);
325
326 if (array_key_exists("DATE_REGISTER", $arFields) && (mb_strlen($arFields["DATE_REGISTER"]) < 2 || !CheckDateTime($arFields["DATE_REGISTER"])))
327 unset($arFields["DATE_REGISTER"]);
328
329 if ($this->externalAuthID !== null && !array_key_exists("EXTERNAL_AUTH_ID", $arFields))
330 $arFields["EXTERNAL_AUTH_ID"] = $this->externalAuthID;
331
332 if (!array_key_exists("XML_ID", $arFields))
333 $arFields["XML_ID"] = Random::getString(32);
334
335 if(!array_key_exists("CHECKWORD", $arFields) || $arFields["CHECKWORD"] == '')
336 $arFields["CHECKWORD"] = Random::getString(32);
337
338 if ($this->imageFilePath !== null)
339 {
340 if (array_key_exists("PERSONAL_PHOTO", $arFields) && $arFields["PERSONAL_PHOTO"] <> '')
341 {
342 $arFile = CFile::MakeFileArray($this->imageFilePath."/".$arFields["PERSONAL_PHOTO"]);
343 $arFile["MODULE_ID"] = "main";
344 $arFields["PERSONAL_PHOTO"] = $arFile;
345 }
346
347 if (array_key_exists("WORK_LOGO", $arFields) && $arFields["WORK_LOGO"] <> '')
348 {
349 $arFile = CFile::MakeFileArray($this->imageFilePath."/".$arFields["WORK_LOGO"]);
350 $arFile["MODULE_ID"] = "main";
351 $arFields["WORK_LOGO"] = $arFile;
352 }
353 }
354 else
355 {
356 unset($arFields["PERSONAL_PHOTO"]);
357 unset($arFields["WORK_LOGO"]);
358 }
359
360 $arFields["GROUP_ID"] = $this->userGroups;
361
362 $user = new CUser;
363 $userID = (int)$user->Add($arFields);
364
365 if($userID <= 0)
366 {
367 if($user->LAST_ERROR <> '')
368 $this->errorMessage = $arFields["NAME"]." ".$arFields["LAST_NAME"].": ".$user->LAST_ERROR;
369 }
370
371 if ($userID <= 0 && $this->ignoreDuplicate === false)
372 {
373 $postFix = 2;
374 $login = $arFields["LOGIN"];
375 do
376 {
377 $rsUser = CUser::GetByLogin($arFields["LOGIN"]);
378 if (!$rsUser->Fetch())
379 break;
380
381 $arFields["LOGIN"] = $login.$postFix;
382 $userID = (int)$user->Add($arFields);
383 if ($userID > 1)
384 break;
385
386 $postFix++;
387
388 } while(true);
389 }
390
391 if ($userID > 0)
392 {
393 if ($this->attachIBlockID > 0)
394 {
395 $iblockSectionID = $this->__GetIBlockSectionID($arFields);
396 if ($iblockSectionID > 0)
397 {
398 if (!$this->isUserPropertyCreate)
399 $this->isUserPropertyCreate = $this->__CreateUserProperty();
400
401 $arUpdate = Array();
402 $arUpdate[$this->userPropertyName] = Array($iblockSectionID);
403
404 $user->Update($userID, $arUpdate);
405 }
406 }
407
408 if ($this->callback !== null)
409 call_user_func_array($this->callback, Array(&$arFields, &$userID));
410 }
411
412 return true;
413
414 }
415}
const BX_ROOT
Определения bx_root.php:3
$login
Определения change_password.php:8
static GetByID($ID, $SHOW_USERS_AMOUNT="N")
Определения group.php:853
static GetList($by="sort", $order="asc", $arFilter=[])
Определения language.php:12
Определения csv_data.php:10
Определения iblocksection.php:5
ImportUser()
Определения csv_user_import.php:284
$userPropertyName
Определения csv_user_import.php:27
GetErrorMessage()
Определения csv_user_import.php:126
$errorMessage
Определения csv_user_import.php:18
SetUserGroups($arGroups)
Определения csv_user_import.php:161
$ignoreDuplicate
Определения csv_user_import.php:19
$arHeader
Определения csv_user_import.php:16
SetCallback($functionName)
Определения csv_user_import.php:136
__construct($csvFilePath, $delimiter)
Определения csv_user_import.php:32
IgnoreDuplicate($ignore=true)
Определения csv_user_import.php:131
SetExternalAuthID($externalAuthID)
Определения csv_user_import.php:120
SetImageFilePath($relativePath)
Определения csv_user_import.php:180
CheckRequiredFields()
Определения csv_user_import.php:59
$isUserPropertyCreate
Определения csv_user_import.php:30
SetUserPropertyName($userPropertyName)
Определения csv_user_import.php:95
$attachIBlockID
Определения csv_user_import.php:26
$externalAuthID
Определения csv_user_import.php:24
$defaultEmail
Определения csv_user_import.php:22
$isErrorOccured
Определения csv_user_import.php:17
SetDefaultEmail($email)
Определения csv_user_import.php:147
AttachUsersToIBlock($iblockID)
Определения csv_user_import.php:84
$csv
Определения csv_user_import.php:15
IsErrorOccured()
Определения csv_user_import.php:115
$callback
Определения csv_user_import.php:21
GetDefaultEmail()
Определения csv_user_import.php:153
__GetIBlockSectionID(&$arFields)
Определения csv_user_import.php:234
& GetCsvObject()
Определения csv_user_import.php:142
GenerateUserPassword($pass_len=10)
Определения csv_user_import.php:103
$arSectionCache
Определения csv_user_import.php:29
$imageFilePath
Определения csv_user_import.php:23
__CreateUserProperty()
Определения csv_user_import.php:187
$userGroups
Определения csv_user_import.php:20
Определения user.php:6037
Определения usertype.php:985
$f
Определения component_props.php:52
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$arGroups
Определения options.php:1766
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
$success
Определения mail_entry.php:69
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
Rel2Abs($curdir, $relpath)
Определения tools.php:3297
GetMessage($name, $aReplace=null)
Определения tools.php:3397
check_email($email, $strict=false, $domainCheck=false)
Определения tools.php:4571
CheckDateTime($datetime, $format=false)
Определения tools.php:398
$email
Определения payment.php:49
if(empty($signedUserToken)) $key
Определения quickway.php:257
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$val
Определения options.php:1793
$n
Определения update_log.php:107
case callback
Определения wrapper_popup.php:31
$dbRes
Определения yandex_detail.php:168