1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
location_import.php
См. документацию.
1<?php
2
5
7
8if (!Loader::includeModule('sale'))
9{
10 $arReturn['ERROR'] = GetMessage('SL_MODULE_SALE_NOT_INSTALLED');
11
12 return $arReturn;
13}
14
16{
17 $region = \Bitrix\Main\Application::getInstance()->getLicense()->getRegion();
21 ;
22
23 if ($region === 'ru' || $region === 'by' || $region === 'kz' || $isBitrixSiteManagementOnly)
24 {
25 return [
26 'loc_ussr.csv',
27 'loc_kz.csv',
28 'loc_usa.csv',
29 'loc_cntr.csv',
30 'locations.csv',
31 ];
32 }
33
34 return [
35 'locations.csv',
36 ];
37}
38
40{
41 $arReturn = [
42 'STEP' => false,
43 'ERROR' => '',
44 'MESSAGE' => '',
45 ];
46
47 if (!defined('DLSERVER'))
48 {
49 define('DLSERVER', $arParams['DLSERVER']);
50 }
51 if (!defined('DLPORT'))
52 {
53 define('DLPORT', $arParams['DLPORT']);
54 }
55 if (!defined('DLPATH'))
56 {
57 define('DLPATH', $arParams['DLPATH']);
58 }
59 if (!defined('DLMETHOD'))
60 {
61 define('DLMETHOD', $arParams['DLMETHOD']);
62 }
63 if (!defined('DLZIPFILE'))
64 {
65 define('DLZIPFILE', $arParams['DLZIPFILE']);
66 }
67
68 if(isset($arParams['TMP_PATH']))
69 $sTmpFilePath = $arParams['TMP_PATH'];
70 else
71 $sTmpFilePath = CTempFile::GetDirectoryName(12, 'sale');
72
73 set_time_limit(600);
74
75 $STEP = (int)($arParams['STEP'] ?? 0);
76 $CSVFILE = (string)($arParams['CSVFILE'] ?? '');
77 $LOADZIP = $arParams["LOADZIP"];
78
79 if ($CSVFILE !== '' && !in_array($CSVFILE, getAllowedFiles(), true))
80 {
81 $arReturn['ERROR'] = GetMessage('SL_IMPORT_ERROR_FILES');
82 }
83 else
84 {
85 if ($STEP == 1 && ($CSVFILE == '' || $CSVFILE == 'locations.csv'))
86 {
87 if ($LOADZIP == 'Y') $STEP = 2;
88 else $STEP = 3;
89 }
90
91 switch($STEP)
92 {
93 case 0:
94 $arReturn['MESSAGE'] = GetMessage('SL_LOADER_LOADING');
95 $arReturn['STEP'] = 1;
96 break;
97
98 case 1:
99 $url = $arParams['DLSERVER']
100 . (isset($arParams['DLPORT']) ? ':' . $arParams['DLPORT'] : '')
101 . $arParams['DLPATH']
102 . $CSVFILE
103 ;
104 $http = new Main\Web\HttpClient();
105 $http->setRedirect(true);
106 $data =
107 DLMETHOD === 'POST'
108 ? $http->post($url)
109 : $http->get($url)
110 ;
111 $data = (string)$data;
112
113 if ($data !== '')
114 {
115 CheckDirPath($sTmpFilePath);
116 $fp = fopen($sTmpFilePath.$CSVFILE, 'w');
117 fwrite($fp, Main\Text\Encoding::convertEncoding($data, 'windows-1251', LANG_CHARSET));
118 fclose($fp);
119
120 $arReturn['MESSAGE'] = GetMessage('SL_LOADER_FILE_LOADED').' '.$CSVFILE;
121 $arReturn['STEP'] = $LOADZIP == "Y" ? 2 : 3;
122 }
123 else
124 {
125 $arReturn['ERROR'] = GetMessage('SL_LOADER_FILE_ERROR').' '.$CSVFILE;
126 $arReturn['RUN_ERROR'] = true;
127 }
128
129 break;
130
131 case 2:
132 $url = $arParams['DLSERVER']
133 . (isset($arParams['DLPORT']) ? ':' . $arParams['DLPORT'] : '')
134 . $arParams['DLPATH']
135 . $CSVFILE
136 ;
137 $http = new Main\Web\HttpClient();
138 $http->setRedirect(true);
139 $data =
140 DLMETHOD === 'POST'
141 ? $http->post($url)
142 : $http->get($url)
143 ;
144 $data = (string)$data;
145
146 if ($data !== '')
147 {
148 CheckDirPath($sTmpFilePath);
149 $fp = fopen($sTmpFilePath.DLZIPFILE, 'w');
150 fwrite($fp, Main\Text\Encoding::convertEncoding($data, 'windows-1251', LANG_CHARSET));
151 fclose($fp);
152
153 $arReturn['MESSAGE'] = GetMessage('SL_LOADER_FILE_LOADED').' '.DLZIPFILE;
154 $arReturn['STEP'] = 3;
155 }
156 else
157 {
158 $arReturn['ERROR'] = GetMessage('SL_LOADER_FILE_ERROR').' '.DLZIPFILE;
159 $arReturn['RUN_ERROR'] = true;
160 }
161
162 break;
163
164 case 3:
165 $arReturn['COMPLETE'] = true;
166 break;
167 }
168 }
169
170 return $arReturn;
171}
172
174{
175 global $DB;
176
177 $arReturn = [
178 'STEP' => false,
179 'ERROR' => '',
180 'AMOUNT' => 0,
181 'POS' => 0,
182 'MESSAGE' => '',
183 ];
184
185 $step_length = (int)($arParams['STEP_LENGTH'] ?? 0);
186
187 if ($step_length <= 0)
188 $step_length = 10;
189
190 define('ZIP_STEP_LENGTH', $step_length);
191 define('LOC_STEP_LENGTH', $step_length);
192 define('DLZIPFILE', $arParams["DLZIPFILE"]);
193
194 $STEP = (int)($arParams['STEP'] ?? 0);
195 $CSVFILE = (string)($arParams['CSVFILE'] ?? '');
196 $LOADZIP = $arParams["LOADZIP"];
197 $bSync = $arParams["SYNC"] == "Y";
198
199 if(isset($arParams['TMP_PATH']))
200 $sTmpFilePath = $arParams['TMP_PATH'];
201 else
202 $sTmpFilePath = CTempFile::GetDirectoryName(12, 'sale');
203
204
205 if ($CSVFILE !== '' && !in_array($CSVFILE, getAllowedFiles(), true))
206 {
207 //echo GetMessage('SL_IMPORT_ERROR_FILES');
208 $arReturn['ERROR'] = GetMessage('SL_IMPORT_ERROR_FILES');
209 }
210 else
211 {
212 if ($STEP == 1 && $CSVFILE == '')
213 {
214 if ($LOADZIP == 'Y') $STEP = 2;
215 else $STEP = 3;
216 }
217
218 switch($STEP)
219 {
220 case 0:
221 $arReturn['MESSAGE'] = GetMessage('WSL_IMPORT_FILES_LOADING');
222 $arReturn['STEP'] = 1;
223 break;
224
225 case 1:
226
227 $time_limit = ini_get('max_execution_time');
228 if ($time_limit < LOC_STEP_LENGTH) set_time_limit(LOC_STEP_LENGTH + 5);
229
230 $start_time = time();
231 $finish_time = $start_time + LOC_STEP_LENGTH;
232
233 $file_url = $sTmpFilePath.$CSVFILE;
234
235 if (!file_exists($file_url))
236 {
237 $arReturn['ERROR'] = GetMessage('SL_IMPORT_ERROR_NO_LOC_FILE');
238 break;
239 }
240
241 $bFinish = true;
242
243 $arSysLangs = Array();
244 $db_lang = CLangAdmin::GetList("sort", "asc", array("ACTIVE" => "Y"));
245 while ($arLang = $db_lang->Fetch())
246 {
247 $arSysLangs[$arLang["LID"]] = $arLang["LID"];
248 }
249
250 $arLocations = array();
251
252 if (!$bSync)
253 {
254 if (!is_set($_SESSION["LOC_POS"]))
255 {
257 }
258 }
259 else
260 {
261 $dbLocations = CSaleLocation::GetList(
262 array(),
263 array(),
264 false,
265 false,
266 array("ID", "COUNTRY_ID", "REGION_ID", "CITY_ID"));
267
268 while ($arLoc = $dbLocations->Fetch())
269 {
270 $arLocations[$arLoc["ID"]] = $arLoc;
271 }
272 }
273
274 if (empty($arLocations))
275 {
276 $bSync = false;
277 }
278
279 include_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/classes/general/csv_data.php");
280
281 $strWarning = '';
282
283 $csvFile = new CCSVData();
284 $csvFile->LoadFile($file_url);
285 $csvFile->SetFieldsType("R");
286 $csvFile->SetFirstHeader(false);
287 $csvFile->SetDelimiter(",");
288
289 $arRes = $csvFile->Fetch();
290 if (!is_array($arRes) || count($arRes)<=0 || mb_strlen($arRes[0]) != 2)
291 {
292 $strWarning .= GetMessage('SL_IMPORT_ERROR_WRONG_LOC_FILE')."<br />";
293 }
294
295 if ($strWarning == '')
296 {
297 $DefLang = $arRes[0];
298 if (!in_array($DefLang, $arSysLangs))
299 {
300 $strWarning .= GetMessage('SL_IMPORT_ERROR_NO_LANG')."<br />";
301 }
302 }
303
304 if ($strWarning <> '')
305 {
306 $arReturn['ERROR'] = $strWarning."<br />";
307 break;
308 }
309
310
311 if (is_set($_SESSION["LOC_POS"]))
312 {
313 $csvFile->SetPos($_SESSION["LOC_POS"]);
314
315 $CurCountryID = $_SESSION["CUR_COUNTRY_ID"];
316 $CurRegionID = $_SESSION["CUR_REGION_ID"];
317 $numCountries = $_SESSION["NUM_COUNTRIES"];
318 $numRegiones = $_SESSION["NUM_REGIONES"];
319 $numCities = $_SESSION["NUM_CITIES"];
320 $numLocations = $_SESSION["NUM_LOCATIONS"];
321 }
322 else
323 {
324 $CurCountryID = 0;
325 $CurRegionID = 0;
326 $numCountries = 0;
327 $numRegiones = 0;
328 $numCities = 0;
329 $numLocations = 0;
330 }
331
332 $tt = 0;
333 while ($arRes = $csvFile->Fetch())
334 {
335 $type = mb_strtoupper($arRes[0]);
336 $tt++;
337 $arArrayTmp = array();
338 foreach($arRes as $ind => $value)
339 {
340 if ($ind%2 && isset($arSysLangs[$value]))
341 {
342 $arArrayTmp[$value] = array(
343 "LID" => $value,
344 "NAME" => $arRes[$ind + 1]
345 );
346
347 if ($value == $DefLang)
348 {
349 $arArrayTmp["NAME"] = $arRes[$ind + 1];
350 }
351 }
352 }
353
354 //country
355 if (is_array($arArrayTmp) && $arArrayTmp["NAME"] <> '')
356 {
357 if ($type == "S")
358 {
359 $CurRegionID = null;
360 $arRegionList = Array();
361 $CurCountryID = null;
362 $arContList = array();
363 $LLL = 0;
364 if ($bSync)
365 {
366 $db_contList = CSaleLocation::GetList(
367 Array(),
368 Array(
369 "COUNTRY_NAME" => $arArrayTmp["NAME"],
370 "LID" => $DefLang
371 )
372 );
373 if ($arContList = $db_contList->Fetch())
374 {
375 $LLL = intval($arContList["ID"]);
376 $CurCountryID = intval($arContList["COUNTRY_ID"]);
377 }
378 }
379
380 if (intval($CurCountryID) <= 0)
381 {
382 $CurCountryID = CSaleLocation::AddCountry($arArrayTmp);
383 $CurCountryID = intval($CurCountryID);
384 if ($CurCountryID>0)
385 {
386 $numCountries++;
387 if(intval($LLL) <= 0)
388 {
389 $LLL = CSaleLocation::AddLocation(array("COUNTRY_ID" => $CurCountryID));
390 if (intval($LLL)>0) $numLocations++;
391 }
392 }
393 }
394 }
395 elseif ($type == "R") //region
396 {
397 $CurRegionID = null;
398 $arRegionList = Array();
399 $LLL = 0;
400 if ($bSync)
401 {
402 $db_rengList = CSaleLocation::GetList(
403 Array(),
404 Array(
405 "COUNTRY_ID" => $CurCountryID,
406 "REGION_NAME"=>$arArrayTmp["NAME"],
407 "LID" => $DefLang
408 )
409 );
410 if ($arRegionList = $db_rengList->Fetch())
411 {
412 $LLL = $arRegionList["ID"];
413 $CurRegionID = intval($arRegionList["REGION_ID"]);
414 }
415 }
416
417 if (intval($CurRegionID) <= 0)
418 {
419 $CurRegionID = CSaleLocation::AddRegion($arArrayTmp);
420 $CurRegionID = intval($CurRegionID);
421 if ($CurRegionID > 0)
422 {
423 $numRegiones++;
424 if (intval($LLL) <= 0)
425 {
426 $LLL = CSaleLocation::AddLocation(array("COUNTRY_ID" => $CurCountryID, "REGION_ID" => $CurRegionID));
427 if (intval($LLL)>0) $numLocations++;
428 }
429 }
430 }
431 }
432 elseif ($type == "T" && intval($CurCountryID)>0) //city
433 {
434 $city_id = 0;
435 $LLL = 0;
436 $arCityList = Array();
437
438 if ($bSync)
439 {
440 $arFilter = Array(
441 "COUNTRY_ID" => $CurCountryID,
442 "CITY_NAME" => $arArrayTmp["NAME"],
443 "LID" => $DefLang
444 );
445 if(intval($CurRegionID) > 0)
446 $arFilter["REGION_ID"] = $CurRegionID;
447
448 $db_cityList = CSaleLocation::GetList(
449 Array(),
451 );
452 if ($arCityList = $db_cityList->Fetch())
453 {
454 $LLL = $arCityList["ID"];
455 $city_id = intval($arCityList["CITY_ID"]);
456 }
457 }
458
459 if ($city_id <= 0)
460 {
461 $city_id = CSaleLocation::AddCity($arArrayTmp);
462 $city_id = intval($city_id);
463 if ($city_id > 0)
464 $numCities++;
465 }
466
467 if ($city_id > 0)
468 {
469 if (intval($LLL) <= 0)
470 {
472 array(
473 "COUNTRY_ID" => $CurCountryID,
474 "REGION_ID" => $CurRegionID,
475 "CITY_ID" => $city_id
476 ));
477
478 if (intval($LLL) > 0) $numLocations++;
479 }
480 }
481 }
482 }
483
484 if($tt == 10)
485 {
486 $tt = 0;
487 $cur_time = time();
488
489 if ($cur_time >= $finish_time)
490 {
491 $cur_step = $csvFile->GetPos();
492 $amount = $csvFile->iFileLength;
493
494 $_SESSION["LOC_POS"] = $cur_step;
495 $_SESSION["CUR_COUNTRY_ID"] = $CurCountryID;
496 $_SESSION["CUR_REGION_ID"] = $CurRegionID;
497 $_SESSION["NUM_COUNTRIES"] = $numCountries;
498 $_SESSION["NUM_REGIONES"] = $numRegiones;
499 $_SESSION["NUM_CITIES"] = $numCities;
500 $_SESSION["NUM_LOCATIONS"] = $numLocations;
501
502 $bFinish = false;
503
504 //echo "<script>Import(1, {AMOUNT:".CUtil::JSEscape($amount).",POS:".CUtil::JSEscape($cur_step)."})</script>";
505
506 $arReturn['STEP'] = 1;
507 $arReturn['AMOUNT'] = $amount;
508 $arReturn['POS'] = $cur_step;
509 break;
510 }
511 }
512 }
513
514
515 if ($bFinish)
516 {
517 unset($_SESSION["LOC_POS"]);
518
519 $strOK = GetMessage('SL_IMPORT_LOC_STATS').'<br />';
520 $strOK = str_replace('#NUMCOUNTRIES#', intval($numCountries), $strOK);
521 $strOK = str_replace('#NUMREGIONES#', intval($numRegiones), $strOK);
522 $strOK = str_replace('#NUMCITIES#', intval($numCities), $strOK);
523 $strOK = str_replace('#NUMLOCATIONS#', intval($numLocations), $strOK);
524
525 $arReturn['MESSAGE'] = $strOK;
526 $arReturn['STEP'] = $LOADZIP == "Y" ? 2 : 3;
527 //echo '<script>Import('.($LOADZIP == "Y" ? 2 : 3).')</script>';
528 }
529
530 break;
531
532 case 2:
533 $time_limit = ini_get('max_execution_time');
534 if ($time_limit < ZIP_STEP_LENGTH) set_time_limit(ZIP_STEP_LENGTH + 5);
535
536 $start_time = time();
537 $finish_time = $start_time + ZIP_STEP_LENGTH;
538
539 if ($LOADZIP == "Y" && file_exists($sTmpFilePath.DLZIPFILE))
540 {
541 $rsLocations = CSaleLocation::GetList(
542 array(),
543 array("LID" => 'ru'),
544 false,
545 false,
546 array("ID", "CITY_NAME_LANG", "REGION_NAME_LANG"));
547 $arLocationMap = array();
548 while ($arLocation = $rsLocations->Fetch())
549 {
550 if($arLocation["REGION_NAME_LANG"] <> '')
551 {
552 if ($arLocation["CITY_NAME_LANG"] <> '')
553 $arLocationMap[$arLocation["CITY_NAME_LANG"]][$arLocation["REGION_NAME_LANG"]] = $arLocation["ID"];
554 }
555 else
556 $arLocationMap[$arLocation["CITY_NAME_LANG"]] = $arLocation["ID"];
557 }
558
559 $DB->StartTransaction();
560
561 include_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/classes/general/csv_data.php");
562
563 $csvFile = new CCSVData();
564 $csvFile->LoadFile($sTmpFilePath.DLZIPFILE);
565 $csvFile->SetFieldsType("R");
566 $csvFile->SetFirstHeader(false);
567 $csvFile->SetDelimiter(";");
568
569 if (is_set($_SESSION, 'ZIP_POS'))
570 {
571 $numZIP = $_SESSION["NUM_ZIP"];
572 $csvFile->SetPos($_SESSION["ZIP_POS"]);
573 }
574 else
575 {
577
578 unset($_SESSION["NUM_ZIP"]);
579 $numZIP = 0;
580 }
581
582 $bFinish = true;
583 $tt = 0;
584 $REGION = "";
585 while ($arRes = $csvFile->Fetch())
586 {
587 $tt++;
588 $CITY = $arRes[1];
589 if($arRes[3] <> '')
590 $REGION = $arRes[3];
591
592 if (array_key_exists($CITY, $arLocationMap))
593 {
594 if($REGION <> '')
595 $ID = $arLocationMap[$CITY][$REGION];
596 else
597 $ID = $arLocationMap[$CITY];
598 }
599 else
600 {
601 $ID = 0;
602 }
603
604 if ($ID)
605 {
607
608 $numZIP++;
609 }
610
611 if($tt == 10)
612 {
613 $tt = 0;
614
615 $cur_time = time();
616 if ($cur_time >= $finish_time)
617 {
618 $cur_step = $csvFile->GetPos();
619 $amount = $csvFile->iFileLength;
620
621 $_SESSION["ZIP_POS"] = $cur_step;
622 $_SESSION["NUM_ZIP"] = $numZIP;
623
624 $bFinish = false;
625
626 $arReturn['STEP'] = 2;
627 $arReturn['AMOUNT'] = $amount;
628 $arReturn['POS'] = $cur_step;
629 break;
630 }
631 }
632 }
633
634 $DB->Commit();
635
636 if ($bFinish)
637 {
638 unset($_SESSION["ZIP_POS"]);
639
641
642 $strOK = GetMessage('SL_IMPORT_ZIP_STATS');
643 $strOK = str_replace('#NUMZIP#', intval($numZIP), $strOK);
644 $strOK = str_replace('#NUMCITIES#', intval($numCity["CITY_CNT"]), $strOK);
645
646 $arReturn['MESSAGE'] = $strOK;
647 $arReturn['STEP'] = 3;
648 $arReturn['PB_REMOVE'] = true;
649 break;
650 }
651 }
652 else
653 {
654 $arReturn['ERROR'] = GetMessage('SL_IMPORT_ERROR_NO_ZIP_FILE').'<br>';
655 $arReturn['STEP'] = 3;
656 break;
657 }
658
659 break;
660
661 case 3:
662 $arReturn['COMPLETE'] = true;
663 break;
664 }
665 }
666 return $arReturn;
667}
$arParams
Определения access_dialog.php:21
$type
Определения options.php:106
$strOK
Определения options.php:75
if($canUseYandexMarket) $strWarning
Определения options.php:74
static getInstance()
Определения application.php:98
Определения loader.php:13
static includeModule($moduleName)
Определения loader.php:67
static GetList($by="sort", $order="asc", $arFilter=[])
Определения language.php:12
static ClearAllLocationZIP()
Определения location.php:2853
static DeleteAll()
Определения location.php:2707
static AddLocationZIP($location, $ZIP, $bSync=false)
Определения location.php:2867
static _GetZIPImportStats()
Определения location.php:3184
Определения csv_data.php:10
static GetList($arOrder=array("SORT"=>"ASC", "COUNTRY_NAME_LANG"=>"ASC", "CITY_NAME_LANG"=>"ASC"), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения location.php:9
static AddRegion($arFields)
Определения location.php:554
static AddCountry($arFields)
Определения location.php:462
static AddCity($arFields)
Определения location.php:508
static AddLocation($arFields)
Определения location.php:599
$start_time
Определения clock_selector.php:9
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
if($ajaxMode) $ID
Определения get_user.php:27
$region
Определения .description.php:13
$isBitrixSiteManagementOnly
Определения .description.php:12
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
global $DB
Определения cron_frame.php:29
$STEP
Определения csv_new_setup.php:23
saleLocationLoadFile($arParams)
Определения location_import.php:39
saleLocationImport($arParams)
Определения location_import.php:173
if(!Loader::includeModule( 'sale')) getAllowedFiles()
Определения location_import.php:15
const LANG_CHARSET
Определения include.php:65
CheckDirPath($path)
Определения tools.php:2707
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
is_set($a, $k=false)
Определения tools.php:2133
GetMessage($name, $aReplace=null)
Определения tools.php:3397
Определения base32.php:2
if(!function_exists("bx_hmac")) $amount
Определения payment.php:30
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
</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
$arLocation['REGION_NAME']
Определения options.php:2800
$arRes
Определения options.php:104
$arFilter
Определения user_search.php:106
$url
Определения iframe.php:7