- Модуль: main
- Путь к файлу: ~/bitrix/modules/main/classes/general/component_util.php
- Класс: CComponentUtil
- Вызов: CComponentUtil::CopyTemplate
static function CopyTemplate($componentName, $templateName, $siteTemplate, $newSiteTemplate, $newName = false, $bRewrite = false)
{
global $APPLICATION;
$componentName = trim($componentName);
if ($componentName == '')
{
$APPLICATION->ThrowException(GetMessage("comp_util_err1"), "EMPTY_COMPONENT_NAME");
return false;
}
$path2Comp = CComponentEngine::MakeComponentPath($componentName);
if ($path2Comp == '')
{
$APPLICATION->ThrowException(str_replace("#NAME#", $componentName, GetMessage("comp_util_err2")), "ERROR_NOT_COMPONENT");
return false;
}
$componentPath = getLocalPath("components".$path2Comp);
if (!CComponentUtil::isComponent($componentPath))
{
$APPLICATION->ThrowException(str_replace("#NAME#", $componentName, GetMessage("comp_util_err2")), "ERROR_NOT_COMPONENT");
return false;
}
if ($templateName == '')
$templateName = ".default";
if (preg_match("#[^a-z0-9_.-]#i", $templateName))
{
$APPLICATION->ThrowException(str_replace("#NAME#", $templateName, GetMessage("comp_util_err7")), "ERROR_BAD_TEMPLATE_NAME");
return false;
}
if ($siteTemplate == '')
$siteTemplate = false;
if ($siteTemplate != false)
{
$siteTemplateDir = getLocalPath("templates/".$siteTemplate, BX_PERSONAL_ROOT);
if ($siteTemplateDir === false || !is_dir($_SERVER["DOCUMENT_ROOT"].$siteTemplateDir))
{
$APPLICATION->ThrowException(str_replace("#NAME#", $siteTemplate, GetMessage("comp_util_err8")), "ERROR_NO_SITE_TEMPL");
return false;
}
}
if ($siteTemplate != false)
$path = getLocalPath("templates/".$siteTemplate."/components".$path2Comp."/".$templateName, BX_PERSONAL_ROOT);
else
$path = getLocalPath("components".$path2Comp."/templates/".$templateName);
if ($path === false || !file_exists($_SERVER["DOCUMENT_ROOT"].$path))
{
$APPLICATION->ThrowException(str_replace("#C_NAME#", $componentName, str_replace("#T_NAME#", $templateName, GetMessage("comp_util_err9"))), "ERROR_NO_TEMPL");
return false;
}
if ($newSiteTemplate == '')
{
$APPLICATION->ThrowException(GetMessage("comp_util_err10"), "ERROR_EMPTY_SITE_TEMPL");
return false;
}
$newSiteTemplateDir = getLocalPath("templates/".$newSiteTemplate, BX_PERSONAL_ROOT);
if ($newSiteTemplateDir === false || !is_dir($_SERVER["DOCUMENT_ROOT"].$newSiteTemplateDir))
{
$APPLICATION->ThrowException(str_replace("#NAME#", $newSiteTemplate, GetMessage("comp_util_err8")), "ERROR_NO_SITE_TEMPL");
return false;
}
if ($siteTemplate !== false
&& $siteTemplate == $newSiteTemplate
&& ($newName === false || $templateName == $newName))
{
$APPLICATION->ThrowException(GetMessage("comp_util_err11"), "ERROR_DUPL1");
return false;
}
if ($newName !== false)
$templateNameNew = $newName;
else
$templateNameNew = $templateName;
if (preg_match("#[^a-z0-9_.-]#i", $templateNameNew))
{
$APPLICATION->ThrowException(str_replace("#NAME#", $templateNameNew, GetMessage("comp_util_err7")), "ERROR_BAD_TEMPLATE_NAME");
return false;
}
$pathNew = $newSiteTemplateDir."/components".$path2Comp."/".$templateNameNew;
if (file_exists($_SERVER["DOCUMENT_ROOT"].$pathNew))
{
if (!$bRewrite)
{
$APPLICATION->ThrowException(str_replace("#NAME#", $templateNameNew, GetMessage("comp_util_err12")), "ERROR_EXISTS");
return false;
}
else
{
DeleteDirFilesEx($pathNew);
}
}
CopyDirFiles($_SERVER["DOCUMENT_ROOT"].$path, $_SERVER["DOCUMENT_ROOT"].$pathNew, true, true, false);
return true;
}