Пример #1
0
 public static function saveRawFilesToUF($arAttachedFilesRaw, $ufCode, &$arFields)
 {
     static $isDiskEnabled = false;
     static $isWebDavEnabled = false;
     if ($isDiskEnabled === false) {
         $isDiskEnabled = \Bitrix\Main\Config\Option::get('disk', 'successfully_converted', false) && CModule::includeModule('disk') && ($storage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId($GLOBALS["USER"]->GetID())) && ($folder = $storage->getFolderForUploadedFiles($GLOBALS["USER"]->GetID())) ? "Y" : "N";
     }
     if ($isWebDavEnabled === false) {
         $isWebDavEnabled = IsModuleInstalled('webdav') ? "Y" : "N";
     }
     if (empty($arFields[$ufCode])) {
         $arFields[$ufCode] = array();
     }
     $arRelation = array();
     foreach ($arAttachedFilesRaw as $attachedFileRow) {
         if (!empty($attachedFileRow["base64"]) && !empty($attachedFileRow["url"])) {
             $fileContent = base64_decode($attachedFileRow["base64"]);
             $arUri = parse_url($attachedFileRow["url"]);
             if (!empty($arUri) && !empty($arUri["path"])) {
                 $fileName = $arUri["path"];
             }
             if (!empty($fileContent) && !empty($fileName)) {
                 $fileName = CTempFile::GetFileName($fileName);
                 if (CheckDirPath($fileName)) {
                     file_put_contents($fileName, $fileContent);
                     $arFile = CFile::MakeFileArray($fileName);
                     if (is_array($arFile)) {
                         $resultId = false;
                         if ($isDiskEnabled == "Y") {
                             $file = $folder->uploadFile($arFile, array('NAME' => $arFile["name"], 'CREATED_BY' => $GLOBALS["USER"]->GetID()), array(), true);
                             if ($file) {
                                 $resultId = \Bitrix\Disk\Uf\FileUserType::NEW_FILE_PREFIX . $file->getId();
                             }
                         } elseif ($isWebDavEnabled == "Y") {
                             $webDavData = CWebDavIblock::getRootSectionDataForUser($GLOBALS["USER"]->GetID());
                             if (is_array($webDavData)) {
                                 $webDavObject = new CWebDavIblock($webDavData["IBLOCK_ID"], "", array("ROOT_SECTION_ID" => $webDavData["SECTION_ID"], "DOCUMENT_TYPE" => array("webdav", 'CIBlockDocumentWebdavSocnet', 'iblock_' . $webDavData['SECTION_ID'] . '_user_' . intval($GLOBALS["USER"]->GetID()))));
                                 if ($webDavObject) {
                                     $arParent = $webDavObject->GetObject(array("section_id" => $webDavObject->GetMetaID("DROPPED")));
                                     if (!$arParent["not_found"]) {
                                         $path = $webDavObject->_get_path($arParent["item_id"], false);
                                         $tmpName = str_replace(array(":", ".", "/", "\\"), "_", ConvertTimeStamp(time(), "FULL"));
                                         $tmpOptions = array("path" => str_replace("//", "/", $path . "/" . $tmpName));
                                         $arParent = $webDavObject->GetObject($tmpOptions);
                                         if ($arParent["not_found"]) {
                                             $rMKCOL = $webDavObject->MKCOL($tmpOptions);
                                             if (intval($rMKCOL) == 201) {
                                                 $webDavData["SECTION_ID"] = $webDavObject->arParams["changed_element_id"];
                                             }
                                         } else {
                                             $webDavData["SECTION_ID"] = $arParent['item_id'];
                                             if (!$webDavObject->CheckUniqueName($tmpName, $webDavData["SECTION_ID"], $tmpRes)) {
                                                 $path = $webDavObject->_get_path($webDavData["SECTION_ID"], false);
                                                 $tmpName = randString(6);
                                                 $tmpOptions = array("path" => str_replace("//", "/", $path . "/" . $tmpName));
                                                 $rMKCOL = $webDavObject->MKCOL($tmpOptions);
                                                 if (intval($rMKCOL) == 201) {
                                                     $webDavData["SECTION_ID"] = $webDavData->arParams["changed_element_id"];
                                                 }
                                             }
                                         }
                                     }
                                     $options = array("new" => true, 'dropped' => true, "arFile" => $arFile, "arDocumentStates" => false, "arUserGroups" => array_merge($webDavObject->USER["GROUPS"], array("Author")), "FILE_NAME" => $arFile["name"], "IBLOCK_ID" => $webDavData["IBLOCK_ID"], "IBLOCK_SECTION_ID" => $webDavData["SECTION_ID"], "USER_FIELDS" => array());
                                     $GLOBALS['USER_FIELD_MANAGER']->EditFormAddFields($webDavObject->GetUfEntity(), $options['USER_FIELDS']);
                                     $GLOBALS["DB"]->StartTransaction();
                                     if (!$webDavObject->put_commit($options)) {
                                         $GLOBALS["DB"]->Rollback();
                                     } else {
                                         $GLOBALS["DB"]->Commit();
                                         $resultId = $options['ELEMENT_ID'];
                                     }
                                 }
                             }
                         } else {
                             $resultId = CFile::SaveFile($arFile, $arFile["MODULE_ID"]);
                         }
                         if ($resultId) {
                             $arFields[$ufCode][] = $resultId;
                         }
                         if (!empty($attachedFileRow["id"])) {
                             $arRelation[$attachedFileRow["id"]] = $resultId;
                         }
                     }
                 }
             }
         }
     }
     if (!empty($arRelation)) {
         $arFields["DETAIL_TEXT"] = preg_replace_callback("/\\[DISK\\s+FILE\\s+ID\\s*=\\s*pseudo@([\\d]+)\\]/is" . BX_UTF_PCRE_MODIFIER, function ($matches) use($arRelation, $isDiskEnabled, $isWebDavEnabled) {
             if (isset($arRelation[intval($matches[1])])) {
                 if ($isDiskEnabled == "Y") {
                     return "[DISK FILE ID=" . $arRelation[intval($matches[1])] . "]";
                 } elseif ($isWebDavEnabled == "Y") {
                     return "[DOCUMENT ID=" . intval($arRelation[intval($matches[1])]) . "]";
                 } else {
                     return "[DISK FILE ID=pseudo@" . $matches[1] . "]";
                 }
             } else {
                 return "[DISK FILE ID=pseudo@" . $matches[1] . "]";
             }
         }, $arFields["DETAIL_TEXT"]);
     }
 }