• Модуль: documentgenerator
  • Путь к файлу: ~/bitrix/modules/documentgenerator/lib/controller/document.php
  • Класс: BitrixDocumentGeneratorControllerDocument
  • Вызов: Document::getRestTemplate
protected function getRestTemplate(CRestServer $restServer, $moduleId, $region)
{
	$appInfo = $this->getRestAppInfo($restServer);
	if(!$appInfo)
	{
		$this->errorCollection[] = new Error('Application not found');
		return false;
	}

	$template = TemplateTable::getList(['select' => ['ID'], 'order' => ['ID' => 'desc',],'filter' => ['MODULE_ID' => $moduleId, 'CODE' => $appInfo['CODE'], 'REGION' => $region]])->fetch();
	if(!$template)
	{
		$fileResult = FileTable::saveFile($this->generateStubFile());
		if(!$fileResult->isSuccess())
		{
			$this->errorCollection[] = new Error('Error generating file for template');
			return false;
		}
		$data = [
			'NAME' => $appInfo['TITLE'],
			'CODE' => $appInfo['CODE'],
			'REGION' => $region,
			'CREATED_BY' => CurrentUser::get()->getId(),
			'UPDATED_BY' => CurrentUser::get()->getId(),
			'MODULE_ID' => $moduleId,
			'FILE_ID' => $fileResult->getId(),
			'BODY_TYPE' => Docx::class,
			'IS_DELETED' => 'Y',
		];
		$addResult = TemplateTable::add($data);
		if($addResult->isSuccess())
		{
			$templateId = $addResult->getId();
		}
		else
		{
			$this->errorCollection->add($addResult->getErrors());
			return false;
		}
	}
	else
	{
		$templateId = $template['ID'];
	}

	return BitrixDocumentGeneratorTemplate::loadById($templateId);
}