1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
importer.php
См. документацию.
1<?php
2namespace Bitrix\Lists;
3
4use Bitrix\Main;
5use Bitrix\Main\ModuleManager;
6
9
47{
50
51 private static $listRuLanguage = array('ua', 'by', 'kz');
52
60 public static function export($iblockId)
61 {
62 $iblockId = intval($iblockId);
63 if ($iblockId <= 0)
64 throw new Main\ArgumentNullException("iblockId");
65
66 $db = \CIBlock::GetList(Array(), Array("ID" => $iblockId, "CHECK_PERMISSIONS" => "N"));
67 $iblock = $db->Fetch();
68 if (!$iblock)
69 throw new Main\ArgumentOutOfRangeException("iblockId");
70
71 if(!$iblock["CODE"])
72 throw new Main\ArgumentException("Parameter 'CODE' is required.", "matches");
73
74 foreach(\CIBlock::getMessages($iblockId) as $messageKey => $message)
75 $iblock[$messageKey] = $message;
76
77 $list = new \CList($iblockId);
78 $fields = $list->getFields();
79 foreach($fields as $fieldId => $field)
80 {
81 if ($field["TYPE"] == "NAME")
82 {
83 $iblock["~NAME_FIELD"] = array(
84 "NAME" => $field["NAME"],
85 "SETTINGS" => $field["SETTlINGS"],
86 "DEFAULT_VALUE" => $field["DEFAULT_VALUE"],
87 "SORT" => $field["SORT"],
88 );
89 break;
90 }
91 }
92
93 $iblock = serialize($iblock);
94 $iblockLength = strlen($iblock);
95 $datum = str_pad($iblockLength, 10, "0", STR_PAD_LEFT).$iblock;
96
97 if (intval($iblock["PICTURE"]) > 0)
98 {
99 $picture = \CFile::MakeFileArray($iblock["PICTURE"]);
100 if (isset($picture["tmp_name"]) && !empty($picture["tmp_name"]))
101 {
102 $f = fopen($picture["tmp_name"], "rb");
103 $pictureData = fread($f, filesize($picture["tmp_name"]));
104 fclose($f);
105
106 $pictureTypeLength = strlen($picture["type"]);
107 $pictureLength = strlen($pictureData);
108 $datum .= "P".str_pad($pictureTypeLength, 10, "0", STR_PAD_LEFT).$picture["type"].str_pad($pictureLength, 10, "0", STR_PAD_LEFT).$pictureData;
109 }
110 }
111
112 $documentType = self::getDocumentType($iblock["IBLOCK_TYPE_ID"], $iblockId);
113
114 $templatesList = \CBPWorkflowTemplateLoader::GetList(
115 array(),
116 array("DOCUMENT_TYPE" => $documentType),
117 false,
118 false,
119 array("ID", "AUTO_EXECUTE", "NAME", "DESCRIPTION", "SYSTEM_CODE")
120 );
121 while ($templatesListItem = $templatesList->Fetch())
122 {
123 $templatesListItem = serialize($templatesListItem);
124 $bpDescrLength = strlen($templatesListItem);
125 $datum .= "B".str_pad($bpDescrLength, 10, "0", STR_PAD_LEFT).$templatesListItem;
126
127 $bp = \CBPWorkflowTemplateLoader::ExportTemplate($templatesListItem["ID"], false);
128 $bpLength = strlen($bp);
129 $datum .= str_pad($bpLength, 10, "0", STR_PAD_LEFT).$bp;
130 }
131
132 if (function_exists("gzcompress"))
133 $datum = "compressed".gzcompress($datum, 9);
134
135 return $datum;
136 }
137
142 public static function getDataProcess($filePath)
143 {
144 $f = fopen($filePath, "rb");
145 $datum = fread($f, filesize($filePath));
146 fclose($f);
147
148 if (mb_substr($datum, 0, 10) === "compressed")
149 $datum = gzuncompress(substr($datum, 10));
150
151 $len = intval(substr($datum, 0, 10));
152 $dataSerialized = substr($datum, 10, $len);
153
154 $data = CheckSerializedData($dataSerialized) ? unserialize($dataSerialized, ["allowed_classes" => false]) : [];
155
156 return $data;
157 }
158
165 public static function import($iblockType, $datum, $siteId = null)
166 {
167 if (empty($datum))
168 throw new Main\ArgumentNullException("datum");
169
170 if (mb_substr($datum, 0, 10) === "compressed")
171 $datum = gzuncompress(substr($datum, 10));
172
173 $len = intval(substr($datum, 0, 10));
174 $iblockSerialized = substr($datum, 10, $len);
175 $datum = substr($datum, $len + 10);
176
177 $marker = substr($datum, 0, 1);
178 $picture = null;
179 $pictureType = null;
180 if ($marker == "P")
181 {
182 $len = intval(substr($datum, 1, 10));
183 $pictureType = substr($datum, 11, $len);
184 $datum = substr($datum, $len + 11);
185
186 $len = intval(substr($datum, 0, 10));
187 $picture = substr($datum, 10, $len);
188 $datum = substr($datum, $len + 10);
189
190 $marker = substr($datum, 0, 1);
191 }
192
193 $iblock = CheckSerializedData($iblockSerialized) ? unserialize($iblockSerialized, ['allowed_classes' => false]) : [];
194 $iblockId = static::createIBlock($iblockType, $iblock, $pictureType, $picture, $siteId);
195
196 if ($iblockId > 0)
197 {
198 $documentType = self::getDocumentType($iblockType, $iblockId);
199
200 while (!empty($datum))
201 {
202 if ($marker == "B")
203 {
204 $len = intval(substr($datum, 1, 10));
205 $bpDescr = substr($datum, 11, $len);
206 $datum = substr($datum, $len + 11);
207
208 $bpDescr = CheckSerializedData($bpDescr) ? unserialize($bpDescr, ["allowed_classes" => false]) : [];
209
210 $len = intval(substr($datum, 0, 10));
211 $bp = substr($datum, 10, $len);
212 $datum = substr($datum, $len + 10);
213
214 static::importTemplate($documentType, $bpDescr, $bp);
215 }
216 else
217 {
218
219 }
220
221 if (empty($datum))
222 break;
223
224 $marker = substr($datum, 0, 1);
225 }
226 }
227 }
228
229 private static function importTemplate($documentType, $bpDescr, $bp)
230 {
231 $id = 0;
232
233 $db = \CBPWorkflowTemplateLoader::GetList(
234 array(),
235 array("DOCUMENT_TYPE" => $documentType, "SYSTEM_CODE" => $bpDescr["SYSTEM_CODE"]),
236 false,
237 false,
238 array("ID", "IS_MODIFIED")
239 );
240 if ($res = $db->Fetch())
241 {
242 if ($res["IS_MODIFIED"] == "Y")
243 return;
244
245 $id = $res["ID"];
246 }
247
248 try
249 {
250 \CBPWorkflowTemplateLoader::ImportTemplate(
251 $id,
252 $documentType,
253 $bpDescr["AUTO_EXECUTE"],
254 $bpDescr["NAME"],
255 $bpDescr["DESCRIPTION"],
256 $bp,
257 $bpDescr["SYSTEM_CODE"],
258 true
259 );
260 }
261 catch (\Exception $e)
262 {
263 }
264 }
265
266 private static function createIBlock($iblockType, $iblock, $pictureType, $picture, $siteId = null)
267 {
268 if (is_null($siteId))
269 $siteId = \CSite::GetDefSite();
270
271 $db = \CIBlock::GetList(
272 array(),
273 array("IBLOCK_TYPE_ID" => $iblockType, "CODE" => $iblock["CODE"], "CHECK_PERMISSIONS" => "N", "SITE_ID" => $siteId)
274 );
275 if ($res = $db->Fetch())
276 return $res["ID"];
277
278 $fields = array(
279 "NAME" => $iblock["NAME"],
280 "DESCRIPTION" => $iblock["DESCRIPTION"],
281 "IBLOCK_TYPE_ID" => $iblockType,
282 "SORT" => $iblock["SORT"],
283 "CODE" => $iblock["CODE"],
284 "WORKFLOW" => "N",
285 "ELEMENTS_NAME" => $iblock["ELEMENTS_NAME"],
286 "ELEMENT_NAME" => $iblock["ELEMENT_NAME"],
287 "ELEMENT_ADD" => $iblock["ELEMENT_ADD"] ?? null,
288 "ELEMENT_EDIT" => $iblock["ELEMENT_EDIT"] ?? null,
289 "ELEMENT_DELETE" => $iblock["ELEMENT_DELETE"] ?? null,
290 "SECTIONS_NAME" => $iblock["SECTIONS_NAME"] ?? null,
291 "SECTION_NAME" => $iblock["SECTION_NAME"] ?? null,
292 "SECTION_ADD" => $iblock["SECTION_ADD"] ?? null,
293 "SECTION_EDIT" => $iblock["SECTION_EDIT"] ?? null,
294 "SECTION_DELETE" => $iblock["SECTION_DELETE"] ?? null,
295 "BIZPROC" => "Y",
296 "SITE_ID" => array($siteId),
297 "RIGHTS_MODE" => "E",
298 );
299
300 if ($iblock["SOCNET_GROUP_ID"])
301 {
302 $fields["SOCNET_GROUP_ID"] = $iblock["SOCNET_GROUP_ID"];
303 }
304
305 static $exts = array(
306 "image/jpeg" => "jpg",
307 "image/png" => "png",
308 "image/gif" => "gif",
309 );
310 if (!empty($picture) && isset($exts[$pictureType]))
311 {
312 $fn = \CTempFile::GetFileName();
313 Main\IO\Directory::createDirectory($fn);
314
315 $fn .= md5(mt_rand()).".".$exts[$pictureType];
316
317 $f = fopen($fn, "wb");
318 fwrite($f, $picture);
319 fclose($f);
320
321 $fields["PICTURE"] = \CFile::MakeFileArray($fn/*, $pictureType*/);
322 }
323
324 $ob = new \CIBlock;
325 $res = $ob->Add($fields);
326 if ($res)
327 {
328 self::createIBlockRights($res);
329
330 $list = new \CList($res);
331
332 if (isset($iblock["~NAME_FIELD"]))
333 $list->UpdateField("NAME", $iblock["~NAME_FIELD"]);
334
335 $list->Save();
336
337 \CLists::setLiveFeed(1, $res);
338
339 return $res;
340 }
341
342 return 0;
343 }
344
345 protected static function getIBlockType()
346 {
347 $iblockType = Main\Config\Option::get("lists", "livefeed_iblock_type_id", "bitrix_processes");
348 if (empty($iblockType))
349 $iblockType = "bitrix_processes";
350
351 return $iblockType;
352 }
353
354 protected static function getDocumentType($iblockType, $iblockId)
355 {
356 if ($iblockType == static::getIBlockType())
357 $documentType = array('lists', 'BizprocDocument', 'iblock_'.$iblockId);
358 else
359 $documentType = array('lists', 'Bitrix\Lists\BizprocDocumentLists', 'iblock_'.$iblockId);
360
361 return $documentType;
362 }
363
367 private static function createIBlockRights($iblockId)
368 {
369 $rightObject = new \CIBlockRights($iblockId);
370 $rights = $rightObject->getRights();
371 $rightsList = $rightObject->getRightsList(false);
372
373 $rightId = array_search('iblock_full', $rightsList);
374 $rights['n0'] = array('GROUP_CODE' => "G1", 'TASK_ID' => $rightId);
375 $rights['n1'] = array('GROUP_CODE' => "U1", 'TASK_ID' => $rightId);
376
377 $rightId = array_search('iblock_element_add', $rightsList);
378 $rights['n2'] = array('GROUP_CODE' => "G2", 'TASK_ID' => $rightId);
379
380 $rightObject->setRights($rights);
381 }
382
383 const PATH = "/bitrix/modules/lists/install/bizproc/process/";
384 const PATH_USER_PROCESSES = "/bitrix/lists/processes/";
385
392 public static function installProcesses($lang, $siteId = null)
393 {
394 if (empty($lang))
395 throw new Main\ArgumentNullException("lang");
396
397 if (! Main\Loader::includeModule("bizproc"))
398 return;
399
400 $iblockType = static::getIBlockType();
401
402 $db = \CIBlockType::GetList(array(), array("=ID" => $iblockType));
403 $res = $db->Fetch();
404 if (!$res)
405 static::createIBlockType();
406
407 if(in_array($lang, self::$listRuLanguage))
408 $lang = 'ru';
409
410 if ($lang === 'co')
411 {
412 $lang = 'la';
413 }
414
415 $dir = new Main\IO\Directory(Main\Loader::getDocumentRoot() . static::PATH . $lang . "/");
416 if(!$dir->isExists())
417 $dir = new Main\IO\Directory(Main\Loader::getDocumentRoot() . static::PATH . "en/");
418
419 if ($dir->isExists())
420 {
421 $children = $dir->getChildren();
422 foreach ($children as $child)
423 {
425 if ($child->isFile() && ($child->getExtension() == "prc"))
426 {
427 static::import($iblockType, $child->getContents(), $siteId);
428 }
429 }
430 }
431 }
432
439 public static function installProcess($path, $siteId = null)
440 {
441 if (empty($path))
442 throw new Main\ArgumentNullException("path");
443
444 if (!Main\Loader::includeModule("bizproc"))
445 return;
446
448 $iblockType = static::getIBlockType();
449
450 $db = \CIBlockType::GetList(array(), array("=ID" => $iblockType));
451 $res = $db->Fetch();
452 if (!$res)
453 static::createIBlockType();
454
455 $file = new Main\IO\File($path);
456 if($file->isExists() && $file->getExtension() == "prc")
457 {
458 static::import($iblockType, $file->getContents(), $siteId);
459 }
460 }
461
470 public static function loadDataProcesses($lang, $systemProcesses = true, &$fileData, $path = null)
471 {
472 if (empty($lang))
473 throw new Main\ArgumentNullException("lang");
474
475 if(in_array($lang, self::$listRuLanguage))
476 $lang = 'ru';
477
478 if(!empty($path))
479 {
480 $path = rtrim($path, "/");
481 $path = $path."/";
482 }
483 else
484 {
485 $path = self::getPathToProcesses($lang, $systemProcesses);
486 }
487
488 $dir = new Main\IO\Directory($path);
489 if (!$dir->isExists() && $lang === 'en')
490 {
491 return;
492 }
493
494 if ($dir->isExists())
495 {
496 $children = $dir->getChildren();
497 foreach ($children as $key => $child)
498 {
500 if ($child->isFile() && ($child->getExtension() == "prc"))
501 {
502 $data = self::getDataProcess($path.$child->getName());
503 $fileData[$data['CODE']]['FILE_NAME'] = $child->getName();
504 $fileData[$data['CODE']]['FILE_PATH'] = str_replace(Main\Loader::getDocumentRoot(), '', $child->getPath());
505 $fileData[$data['CODE']]['NAME'] = $data['NAME'];
506 $fileData[$data['CODE']]['DESCRIPTION'] = $data['DESCRIPTION'];
507 $fileData[$data['CODE']]['CODE'] = $data['CODE'];
508 $fileData[$data['CODE']]['IBLOCK_TYPE_ID'] = $data['IBLOCK_TYPE_ID'];
509 $fileData[$data['CODE']]['DIRECTORY_NAME'] = $child->getDirectory()->getName();
510 }
511 elseif($child->isDirectory())
512 {
513 self::loadDataProcesses($lang, $systemProcesses, $fileData, $child->getPath());
514 }
515 }
516 }
517 else
518 {
519 $path = self::getPathToProcesses("en", $systemProcesses);
520 self::loadDataProcesses('en', $systemProcesses, $fileData, $path);
521 }
522 }
523
524 private static function getPathToProcesses($lang, $systemProcesses = true)
525 {
526 if($systemProcesses)
527 {
528 $path = Main\Loader::getDocumentRoot() . static::PATH . $lang . "/";
529 }
530 else
531 {
532 $path = Main\Loader::getDocumentRoot() . static::PATH_USER_PROCESSES . $lang . "/";
533 }
534
535 return $path;
536 }
537
538 protected static function createIBlockType()
539 {
540 $iblockType = array(
541 'ID' => 'bitrix_processes',
542 'SECTIONS' => 'Y',
543 'SORT' => 500,
544 'LANG' => array(),
545 );
546
547 $langList = \CLanguage::GetList('lid', 'asc', array("ACTIVE" => "Y"));
548 while ($lang = $langList->Fetch())
549 $iblockType['LANG'][$lang['LID']]['NAME'] = "Processes";
550
551 $iblockTypeList = \CIBlockType::GetList(array(), array('=ID' => $iblockType['ID']));
552 $res = $iblockTypeList->fetch();
553 if (!$res)
554 {
555 $iblockTypeObject = new \CIBlockType;
556 $iblockTypeObject->add($iblockType);
557
559 $con->queryExecute("
560 insert into b_lists_permission (IBLOCK_TYPE_ID, GROUP_ID)
561 select 'bitrix_processes', p.GROUP_ID
562 from
563 b_lists_permission p
564 left join b_lists_permission p2 on p2.GROUP_ID = p.GROUP_ID and p2.IBLOCK_TYPE_ID = 'bitrix_processes'
565 where
566 p.IBLOCK_TYPE_ID = 'lists'
567 and p2.IBLOCK_TYPE_ID is null
568 ");
569
570 global $CACHE_MANAGER;
571 $CACHE_MANAGER->Clean("b_lists_permission");
572 }
573
574 Main\Config\Option::set("lists", "livefeed_iblock_type_id", "bitrix_processes");
575 }
576
583 public static function onAgent($lang)
584 {
585 if (ModuleManager::isModuleInstalled("bizproc"))
586 {
587 self::installProcesses($lang);
588 return "";
589 }
590 else
591 {
592 return '\Bitrix\Lists\Importer::onAgent("'.$lang.'");';
593 }
594 }
595
596 public static function migrateList($id)
597 {
598 $id = intval($id);
599 if ($id <= 0)
600 throw new Main\ArgumentNullException("id");
601
602 $db = \CIBlock::GetList(
603 array(),
604 array("ID" => $id, "IBLOCK_TYPE_ID" => "lists", "CHECK_PERMISSIONS" => "N")
605 );
606 $iblock = $db->Fetch();
607 if (!$iblock)
608 throw new Main\ArgumentOutOfRangeException("id");
609
610 $iblockType = static::getIBlockType();
611
612 $ob = new \CIBlock;
613 $res = $ob->Update($id, array("IBLOCK_TYPE_ID" => $iblockType));
614 if ($res)
615 {
616 \CLists::setLiveFeed(1, $id);
617 }
618
619 \CBPDocument::MigrateDocumentType(
620 array("lists", 'Bitrix\Lists\BizprocDocumentLists', "iblock_".$id),
621 array("lists", "BizprocDocument", "iblock_".$id)
622 );
623 }
624}
$path
Определения access_edit.php:21
$con
Определения admin_tab.php:7
const PATH_USER_PROCESSES
Определения importer.php:384
const PATH
Определения importer.php:383
static migrateList($id)
Определения importer.php:596
static installProcess($path, $siteId=null)
Определения importer.php:439
static createIBlockType()
Определения importer.php:538
static getIBlockType()
Определения importer.php:345
const DIRECTION_EXPORT
Определения importer.php:48
static onAgent($lang)
Определения importer.php:583
static getDocumentType($iblockType, $iblockId)
Определения importer.php:354
static export($iblockId)
Определения importer.php:60
static getDataProcess($filePath)
Определения importer.php:142
const DIRECTION_IMPORT
Определения importer.php:49
static getConnection($name="")
Определения application.php:638
static get($moduleId, $name, $default="", $siteId=false)
Определения option.php:30
static set($moduleId, $name, $value="", $siteId="")
Определения option.php:261
static getDocumentRoot()
Определения loader.php:254
static includeModule($moduleName)
Определения loader.php:67
static GetList($by="sort", $order="asc", $arFilter=[])
Определения language.php:12
static GetList($arOrder=array("SORT"=> "ASC"), $arFilter=array())
Определения iblocktype.php:53
global $CACHE_MANAGER
Определения clear_component_cache.php:7
$children
Определения sync.php:12
$f
Определения component_props.php:52
$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
$res
Определения filter_act.php:7
$iblockId
Определения iblock_catalog_edit.php:30
if(! $catalogEdit->isSuccess()) $iblock
Определения iblock_catalog_edit.php:38
if(!defined('SITE_ID')) $lang
Определения include.php:91
$siteId
Определения ajax.php:8
CheckSerializedData($str, $max_depth=200)
Определения tools.php:4949
Определения Image.php:9
$message
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$dir
Определения quickway.php:303
if(empty($signedUserToken)) $key
Определения quickway.php:257
$rights
Определения options.php:4
$fields
Определения yandex_run.php:501