trans = $trans; $this->logger = $logger; } /** * Get templates * * @param string $type - template format type * * @return array * * @NoAdminRequired */ public function GetTemplates($type = null) { return TemplateManager::GetGlobalTemplates($type); } /** * Add global template * * @return array */ public function AddTemplate() { $file = $this->request->getUploadedFile("file"); if (!is_null($file)) { if (is_uploaded_file($file["tmp_name"]) && $file["error"] === 0) { if (!TemplateManager::IsTemplateType($file["name"])) { return [ "error" => $this->trans->t("Template must be in OOXML format") ]; } $templateDir = TemplateManager::GetGlobalTemplateDir(); if ($templateDir->nodeExists($file["name"])) { return [ "error" => $this->trans->t("Template already exists") ]; } $templateContent = file_get_contents($file["tmp_name"]); $template = $templateDir->newFile($file["name"]); $template->putContent($templateContent); $fileInfo = $template->getFileInfo(); $result = [ "id" => $fileInfo->getId(), "name" => $fileInfo->getName(), "type" => TemplateManager::GetTypeTemplate($fileInfo->getMimeType()) ]; return $result; } } return [ "error" => $this->trans->t("Invalid file provided") ]; } /** * Delete template * * @param string $templateId - file identifier * * @return array */ public function DeleteTemplate($templateId) { $templateDir = TemplateManager::GetGlobalTemplateDir(); try { $templates = $templateDir->getById($templateId); } catch(\Exception $e) { $this->logger->logException($e, ["message" => "DeleteTemplate: $templateId", "app" => $this->AppName]); return [ "error" => $this->trans->t("Failed to delete template") ]; } if (empty($templates)) { $this->logger->info("Template not found: $templateId", ["app" => $this->AppName]); return [ "error" => $this->trans->t("Failed to delete template") ]; } $templates[0]->delete(); $this->logger->debug("Template: deleted " . $templates[0]->getName(), ["app" => $this->appName]); return []; } }