getSystemValue("instanceid", null) . "/" . self::$appName . "/" . self::$templateFolderName; $rootFolder = Server::get(IRootFolder::class); $templateDir = null; try { $templateDir = $rootFolder->get($dirPath); } catch (NotFoundException) { $templateDir = $rootFolder->newFolder($dirPath); } return $templateDir; } /** * Get global templates * * @param string $mimetype - mimetype of the template * * @return array */ public static function getGlobalTemplates(?string $mimetype = null): array { $templateDir = self::getGlobalTemplateDir(); $templatesList = $templateDir->getDirectoryListing(); if (!empty($mimetype) && is_array($templatesList) && count($templatesList) > 0) { $templatesList = $templateDir->searchByMime($mimetype); } return $templatesList; } /** * Get template file */ public static function getTemplate(int $templateId): ?File { $logger = \OCP\Log\logger('onlyoffice'); if (empty($templateId)) { $logger->info("templateId is empty", ["app" => self::$appName]); return null; } $templateDir = self::getGlobalTemplateDir(); try { $templates = $templateDir->getById($templateId); } catch (\Exception $e) { $logger->error("getTemplate: $templateId", ['exception' => $e]); return null; } if (empty($templates)) { return null; } return $templates[0]; } /** * Get type template from mimetype */ public static function getTypeTemplate(string $mime): string { return match ($mime) { "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "document", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "spreadsheet", "application/vnd.openxmlformats-officedocument.presentationml.presentation" => "presentation", default => "", }; } /** * Get mimetype template from format type */ public static function getMimeTemplate(string $type): string { return match ($type) { "document" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "spreadsheet" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "presentation" => "application/vnd.openxmlformats-officedocument.presentationml.presentation", default => "", }; } /** * Check template type */ public static function isTemplateType(string $name): bool { $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION)); return match ($ext) { "docx", "xlsx", "pptx" => true, default => false, }; } /** * Check file if it's template */ public static function isTemplate(int $fileId): bool { $template = self::getTemplate($fileId); return !empty($template); } /** * Get template by file name */ public static function getEmptyTemplate(string $name): false|string { $ext = strtolower("." . pathinfo($name, PATHINFO_EXTENSION)); $lang = Server::get(IFactory::class)->get("")->getLanguageCode(); $templatePath = self::getEmptyTemplatePath($lang, $ext); if (!file_exists($templatePath)) { return false; } return file_get_contents($templatePath); } /** * Get template path * * @param string $lang - language * @param string $ext - file extension */ public static function getEmptyTemplatePath(string $lang, string $ext): string { if (!array_key_exists($lang, self::$localPath)) { $lang = "default"; } return dirname(__DIR__) . DIRECTORY_SEPARATOR . "assets" . DIRECTORY_SEPARATOR . "document-templates" . DIRECTORY_SEPARATOR . self::$localPath[$lang] . DIRECTORY_SEPARATOR . "new" . $ext; } /** * Mapping local path to templates */ private static array $localPath = [ "ar" => "ar-SA", "az" => "az-Latn-AZ", "bg" => "bg-BG", "ca" => "ca-ES", "cs" => "cs-CZ", "da" => "da-DK", "de" => "de-DE", "de_DE" => "de-DE", "default" => "default", "el" => "el-GR", "en" => "en-US", "en_GB" => "en-GB", "es" => "es-ES", "eu" => "eu-ES", "fi" => "fi-FI", "fr" => "fr-FR", "gl" => "gl-ES", "he" => "he-IL", "hu_HU" => "hu-HU", "id_ID" => "id-ID", "it" => "it-IT", "ja" => "ja-JP", "ko" => "ko-KR", "lv" => "lv-LV", "nb" => "nb-NO", "nl" => "nl-NL", "pl" => "pl-PL", "pt_BR" => "pt-BR", "pt_PT" => "pt-PT", "ro" => "ro-RO", "ru" => "ru-RU", "sq_AL" => "sq-AL", "si" => "si-LK", "sk" => "sk-SK", "sl" => "sl-SI", "sr" => "sr-Latn-RS", "sv" => "sv-SE", "tr" => "tr-TR", "uk" => "uk-UA", "ur_PK" => "ur-PK", "vi" => "vi-VN", "zh_CN" => "zh-CN", "zh_TW" => "zh-TW" ]; }