Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
importer.php
1<?php
2namespace Bitrix\Lists;
3
6
7Main\Loader::includeModule("iblock");
8Main\Loader::includeModule("bizproc");
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 $iblockUtf8 = Main\Text\Encoding::convertEncodingArray($iblock, LANG_CHARSET, "UTF-8");
94 $iblockUtf8 = serialize($iblockUtf8);
95 $iblockUtf8Length = Main\Text\BinaryString::getLength($iblockUtf8);
96 $datum = str_pad($iblockUtf8Length, 10, "0", STR_PAD_LEFT).$iblockUtf8;
97
98 if (intval($iblock["PICTURE"]) > 0)
99 {
100 $picture = \CFile::MakeFileArray($iblock["PICTURE"]);
101 if (isset($picture["tmp_name"]) && !empty($picture["tmp_name"]))
102 {
103 $f = fopen($picture["tmp_name"], "rb");
104 $pictureData = fread($f, filesize($picture["tmp_name"]));
105 fclose($f);
106
107 $pictureTypeLength = Main\Text\BinaryString::getLength($picture["type"]);
108 $pictureLength = Main\Text\BinaryString::getLength($pictureData);
109 $datum .= "P".str_pad($pictureTypeLength, 10, "0", STR_PAD_LEFT).$picture["type"].str_pad($pictureLength, 10, "0", STR_PAD_LEFT).$pictureData;
110 }
111 }
112
113 $documentType = self::getDocumentType($iblock["IBLOCK_TYPE_ID"], $iblockId);
114
115 $templatesList = \CBPWorkflowTemplateLoader::GetList(
116 array(),
117 array("DOCUMENT_TYPE" => $documentType),
118 false,
119 false,
120 array("ID", "AUTO_EXECUTE", "NAME", "DESCRIPTION", "SYSTEM_CODE")
121 );
122 while ($templatesListItem = $templatesList->Fetch())
123 {
124 $bpDescrUtf8 = Main\Text\Encoding::convertEncodingArray($templatesListItem, LANG_CHARSET, "UTF-8");
125 $bpDescrUtf8 = serialize($bpDescrUtf8);
126 $bpDescrUtf8Length = Main\Text\BinaryString::getLength($bpDescrUtf8);
127 $datum .= "B".str_pad($bpDescrUtf8Length, 10, "0", STR_PAD_LEFT).$bpDescrUtf8;
128
129 $bp = \CBPWorkflowTemplateLoader::ExportTemplate($templatesListItem["ID"], false);
130 $bpLength = Main\Text\BinaryString::getLength($bp);
131 $datum .= str_pad($bpLength, 10, "0", STR_PAD_LEFT).$bp;
132 }
133
134 if (function_exists("gzcompress"))
135 $datum = "compressed".gzcompress($datum, 9);
136
137 return $datum;
138 }
139
144 public static function getDataProcess($filePath)
145 {
146 $f = fopen($filePath, "rb");
147 $datum = fread($f, filesize($filePath));
148 fclose($f);
149
150 if (mb_substr($datum, 0, 10) === "compressed")
151 $datum = gzuncompress(Main\Text\BinaryString::getSubstring($datum, 10));
152
153 $len = intval(Main\Text\BinaryString::getSubstring($datum, 0, 10));
154 $dataSerialized = Main\Text\BinaryString::getSubstring($datum, 10, $len);
155
156 $data = CheckSerializedData($dataSerialized) ? unserialize($dataSerialized, ["allowed_classes" => false]) : [];
157 $data = Main\Text\Encoding::convertEncodingArray($data, "UTF-8", LANG_CHARSET);
158
159 return $data;
160 }
161
168 public static function import($iblockType, $datum, $siteId = null)
169 {
170 if (empty($datum))
171 throw new Main\ArgumentNullException("datum");
172
173 if (mb_substr($datum, 0, 10) === "compressed")
174 $datum = gzuncompress(Main\Text\BinaryString::getSubstring($datum, 10));
175
176 $len = intval(Main\Text\BinaryString::getSubstring($datum, 0, 10));
177 $iblockSerialized = Main\Text\BinaryString::getSubstring($datum, 10, $len);
178 $datum = Main\Text\BinaryString::getSubstring($datum, $len + 10);
179
180 $marker = Main\Text\BinaryString::getSubstring($datum, 0, 1);
181 $picture = null;
182 $pictureType = null;
183 if ($marker == "P")
184 {
185 $len = intval(Main\Text\BinaryString::getSubstring($datum, 1, 10));
186 $pictureType = Main\Text\BinaryString::getSubstring($datum, 11, $len);
187 $datum = Main\Text\BinaryString::getSubstring($datum, $len + 11);
188
189 $len = intval(Main\Text\BinaryString::getSubstring($datum, 0, 10));
190 $picture = Main\Text\BinaryString::getSubstring($datum, 10, $len);
191 $datum = Main\Text\BinaryString::getSubstring($datum, $len + 10);
192
193 $marker = Main\Text\BinaryString::getSubstring($datum, 0, 1);
194 }
195
196 $iblock = CheckSerializedData($iblockSerialized) ? unserialize($iblockSerialized, ['allowed_classes' => false]) : [];
197 $iblock = Main\Text\Encoding::convertEncodingArray($iblock, "UTF-8", LANG_CHARSET);
198 $iblockId = static::createIBlock($iblockType, $iblock, $pictureType, $picture, $siteId);
199
200 if ($iblockId > 0)
201 {
202 $documentType = self::getDocumentType($iblockType, $iblockId);
203
204 while (!empty($datum))
205 {
206 if ($marker == "B")
207 {
208 $len = intval(Main\Text\BinaryString::getSubstring($datum, 1, 10));
209 $bpDescr = Main\Text\BinaryString::getSubstring($datum, 11, $len);
210 $datum = Main\Text\BinaryString::getSubstring($datum, $len + 11);
211
212 $bpDescr = CheckSerializedData($bpDescr) ? unserialize($bpDescr, ["allowed_classes" => false]) : [];
213 $bpDescr = Main\Text\Encoding::convertEncodingArray($bpDescr, "UTF-8", LANG_CHARSET);
214
215 $len = intval(Main\Text\BinaryString::getSubstring($datum, 0, 10));
216 $bp = Main\Text\BinaryString::getSubstring($datum, 10, $len);
217 $datum = Main\Text\BinaryString::getSubstring($datum, $len + 10);
218
219 static::importTemplate($documentType, $bpDescr, $bp);
220 }
221 else
222 {
223
224 }
225
226 if (empty($datum))
227 break;
228
229 $marker = Main\Text\BinaryString::getSubstring($datum, 0, 1);
230 }
231 }
232 }
233
234 private static function importTemplate($documentType, $bpDescr, $bp)
235 {
236 $id = 0;
237
238 $db = \CBPWorkflowTemplateLoader::GetList(
239 array(),
240 array("DOCUMENT_TYPE" => $documentType, "SYSTEM_CODE" => $bpDescr["SYSTEM_CODE"]),
241 false,
242 false,
243 array("ID", "IS_MODIFIED")
244 );
245 if ($res = $db->Fetch())
246 {
247 if ($res["IS_MODIFIED"] == "Y")
248 return;
249
250 $id = $res["ID"];
251 }
252
253 try
254 {
255 \CBPWorkflowTemplateLoader::ImportTemplate(
256 $id,
257 $documentType,
258 $bpDescr["AUTO_EXECUTE"],
259 $bpDescr["NAME"],
260 $bpDescr["DESCRIPTION"],
261 $bp,
262 $bpDescr["SYSTEM_CODE"],
263 true
264 );
265 }
266 catch (\Exception $e)
267 {
268 }
269 }
270
271 private static function createIBlock($iblockType, $iblock, $pictureType, $picture, $siteId = null)
272 {
273 if (is_null($siteId))
274 $siteId = \CSite::GetDefSite();
275
276 $db = \CIBlock::GetList(
277 array(),
278 array("IBLOCK_TYPE_ID" => $iblockType, "CODE" => $iblock["CODE"], "CHECK_PERMISSIONS" => "N", "SITE_ID" => $siteId)
279 );
280 if ($res = $db->Fetch())
281 return $res["ID"];
282
283 $fields = array(
284 "NAME" => $iblock["NAME"],
285 "DESCRIPTION" => $iblock["DESCRIPTION"],
286 "IBLOCK_TYPE_ID" => $iblockType,
287 "SORT" => $iblock["SORT"],
288 "CODE" => $iblock["CODE"],
289 "WORKFLOW" => "N",
290 "ELEMENTS_NAME" => $iblock["ELEMENTS_NAME"],
291 "ELEMENT_NAME" => $iblock["ELEMENT_NAME"],
292 "ELEMENT_ADD" => $iblock["ELEMENT_ADD"] ?? null,
293 "ELEMENT_EDIT" => $iblock["ELEMENT_EDIT"] ?? null,
294 "ELEMENT_DELETE" => $iblock["ELEMENT_DELETE"] ?? null,
295 "SECTIONS_NAME" => $iblock["SECTIONS_NAME"] ?? null,
296 "SECTION_NAME" => $iblock["SECTION_NAME"] ?? null,
297 "SECTION_ADD" => $iblock["SECTION_ADD"] ?? null,
298 "SECTION_EDIT" => $iblock["SECTION_EDIT"] ?? null,
299 "SECTION_DELETE" => $iblock["SECTION_DELETE"] ?? null,
300 "BIZPROC" => "Y",
301 "SITE_ID" => array($siteId),
302 "RIGHTS_MODE" => "E",
303 );
304
305 if ($iblock["SOCNET_GROUP_ID"])
306 {
307 $fields["SOCNET_GROUP_ID"] = $iblock["SOCNET_GROUP_ID"];
308 }
309
310 static $exts = array(
311 "image/jpeg" => "jpg",
312 "image/png" => "png",
313 "image/gif" => "gif",
314 );
315 if (!empty($picture) && isset($exts[$pictureType]))
316 {
317 $fn = \CTempFile::GetFileName();
318 Main\IO\Directory::createDirectory($fn);
319
320 $fn .= md5(mt_rand()).".".$exts[$pictureType];
321
322 $f = fopen($fn, "wb");
323 fwrite($f, $picture);
324 fclose($f);
325
326 $fields["PICTURE"] = \CFile::MakeFileArray($fn/*, $pictureType*/);
327 }
328
329 $ob = new \CIBlock;
330 $res = $ob->Add($fields);
331 if ($res)
332 {
333 self::createIBlockRights($res);
334
335 $list = new \CList($res);
336
337 if (isset($iblock["~NAME_FIELD"]))
338 $list->UpdateField("NAME", $iblock["~NAME_FIELD"]);
339
340 $list->Save();
341
342 \CLists::setLiveFeed(1, $res);
343
344 return $res;
345 }
346
347 return 0;
348 }
349
350 protected static function getIBlockType()
351 {
352 $iblockType = Main\Config\Option::get("lists", "livefeed_iblock_type_id", "bitrix_processes");
353 if (empty($iblockType))
354 $iblockType = "bitrix_processes";
355
356 return $iblockType;
357 }
358
359 protected static function getDocumentType($iblockType, $iblockId)
360 {
361 if ($iblockType == static::getIBlockType())
362 $documentType = array('lists', 'BizprocDocument', 'iblock_'.$iblockId);
363 else
364 $documentType = array('lists', 'Bitrix\Lists\BizprocDocumentLists', 'iblock_'.$iblockId);
365
366 return $documentType;
367 }
368
372 private static function createIBlockRights($iblockId)
373 {
374 $rightObject = new \CIBlockRights($iblockId);
375 $rights = $rightObject->getRights();
376 $rightsList = $rightObject->getRightsList(false);
377
378 $rightId = array_search('iblock_full', $rightsList);
379 $rights['n0'] = array('GROUP_CODE' => "G1", 'TASK_ID' => $rightId);
380 $rights['n1'] = array('GROUP_CODE' => "U1", 'TASK_ID' => $rightId);
381
382 $rightId = array_search('iblock_element_add', $rightsList);
383 $rights['n2'] = array('GROUP_CODE' => "G2", 'TASK_ID' => $rightId);
384
385 $rightObject->setRights($rights);
386 }
387
388 const PATH = "/bitrix/modules/lists/install/bizproc/process/";
389 const PATH_USER_PROCESSES = "/bitrix/lists/processes/";
390
397 public static function installProcesses($lang, $siteId = null)
398 {
399 if (empty($lang))
400 throw new Main\ArgumentNullException("lang");
401
402 if (! Main\Loader::includeModule("bizproc"))
403 return;
404
405 $iblockType = static::getIBlockType();
406
407 $db = \CIBlockType::GetList(array(), array("=ID" => $iblockType));
408 $res = $db->Fetch();
409 if (!$res)
410 static::createIBlockType();
411
412 if(in_array($lang, self::$listRuLanguage))
413 $lang = 'ru';
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
447 $path = Main\Loader::getDocumentRoot() . $path;
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
558 $con = Main\Application::getConnection();
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}
static migrateList($id)
Definition importer.php:596
static installProcess($path, $siteId=null)
Definition importer.php:439
static createIBlockType()
Definition importer.php:538
static onAgent($lang)
Definition importer.php:583
static getDocumentType($iblockType, $iblockId)
Definition importer.php:359
static export($iblockId)
Definition importer.php:60
static getDataProcess($filePath)
Definition importer.php:144
static getSubstring($str, $start,... $args)