2025-02-15 10:59:54 +01:00
|
|
|
import TemplateService from "../service/configuration/templateService";
|
2024-12-23 14:00:50 +01:00
|
|
|
import { PermissionModule } from "../type/permissionTypes";
|
2025-02-15 10:59:54 +01:00
|
|
|
import TemplateUsageService from "../service/configuration/templateUsageService";
|
2025-01-01 13:21:28 +01:00
|
|
|
import Handlebars, { template } from "handlebars";
|
2024-12-25 12:22:28 +01:00
|
|
|
import { FileSystemHelper } from "./fileSystemHelper";
|
2025-03-28 22:28:07 +01:00
|
|
|
import { TemplateFormat } from "../type/templateTypes";
|
2024-12-23 14:00:50 +01:00
|
|
|
|
|
|
|
export abstract class TemplateHelper {
|
|
|
|
static getTemplateFromFile(template: string) {
|
2025-03-18 16:54:53 +01:00
|
|
|
let tmpFile;
|
|
|
|
try {
|
|
|
|
tmpFile = FileSystemHelper.readTemplateFile(`/src/templates/${template}.template.html`);
|
|
|
|
} catch (err) {
|
|
|
|
tmpFile = FileSystemHelper.readTemplateFile(`/src/templates/${template.split(".")[1]}.template.html`);
|
|
|
|
}
|
|
|
|
return tmpFile;
|
2024-12-23 14:00:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static async getTemplateFromStore(templateId: number): Promise<string> {
|
|
|
|
return (await TemplateService.getById(templateId)).html;
|
|
|
|
}
|
|
|
|
|
|
|
|
static applyDataToTemplate(template: string, data: any): string {
|
|
|
|
const normalizedTemplate = this.normalizeTemplate(template);
|
|
|
|
const templateCompiled = Handlebars.compile(normalizedTemplate);
|
|
|
|
return templateCompiled(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static normalizeTemplate(template: string): string {
|
|
|
|
template = template.replace(/<listend>.*?<\/listend>/g, "{{/each}}");
|
|
|
|
template = template.replace(/<liststart\b[^>]*>(WDH Start: )?/g, "{{#each ");
|
|
|
|
template = template.replace(/<\/liststart>/g, "}}");
|
|
|
|
|
|
|
|
return template;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async renderFileForModule({
|
|
|
|
module,
|
2025-02-16 10:21:10 +01:00
|
|
|
title = "pdf-export FF Admin",
|
2024-12-23 14:00:50 +01:00
|
|
|
headerData = {},
|
|
|
|
bodyData = {},
|
|
|
|
footerData = {},
|
|
|
|
}: {
|
2025-03-28 22:28:07 +01:00
|
|
|
module: TemplateFormat;
|
2024-12-23 14:00:50 +01:00
|
|
|
title?: string;
|
|
|
|
headerData?: any;
|
|
|
|
bodyData?: any;
|
|
|
|
footerData?: any;
|
2025-01-01 13:21:28 +01:00
|
|
|
}): Promise<{ header: string; body: string; footer: string; headerMargin?: number; footerMargin?: number }> {
|
|
|
|
const moduleTemplate = await TemplateUsageService.getByScope(module);
|
2024-12-23 14:00:50 +01:00
|
|
|
|
|
|
|
let header = `<h1 style="font-size:10px; text-align:center; width:100%;">${title}</h1>`;
|
2024-12-24 13:52:56 +01:00
|
|
|
let footer = "";
|
2024-12-23 14:00:50 +01:00
|
|
|
let body = "";
|
|
|
|
|
2025-01-01 13:21:28 +01:00
|
|
|
if (moduleTemplate.headerId) {
|
|
|
|
header = await this.getTemplateFromStore(moduleTemplate.headerId);
|
2024-12-28 18:03:33 +01:00
|
|
|
header = this.applyDataToTemplate(header, { title, ...headerData });
|
2024-12-23 14:00:50 +01:00
|
|
|
}
|
|
|
|
|
2025-01-01 13:21:28 +01:00
|
|
|
if (moduleTemplate.footerId) {
|
|
|
|
footer = await this.getTemplateFromStore(moduleTemplate.footerId);
|
2024-12-24 13:52:56 +01:00
|
|
|
} else {
|
|
|
|
footer = this.getTemplateFromFile(module + ".footer");
|
2024-12-23 14:00:50 +01:00
|
|
|
}
|
2025-03-18 16:54:53 +01:00
|
|
|
footer = this.applyDataToTemplate(footer, { title, ...footerData });
|
2024-12-23 14:00:50 +01:00
|
|
|
|
2025-01-01 13:21:28 +01:00
|
|
|
if (moduleTemplate.bodyId) {
|
|
|
|
body = await this.getTemplateFromStore(moduleTemplate.bodyId);
|
2024-12-23 14:00:50 +01:00
|
|
|
} else {
|
2024-12-24 13:52:56 +01:00
|
|
|
body = this.getTemplateFromFile(module + ".body");
|
2024-12-23 14:00:50 +01:00
|
|
|
}
|
2025-03-18 16:54:53 +01:00
|
|
|
body = this.applyDataToTemplate(body, { title, ...bodyData });
|
2024-12-23 14:00:50 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
header,
|
|
|
|
footer,
|
|
|
|
body,
|
2025-01-01 13:21:28 +01:00
|
|
|
headerMargin: moduleTemplate.headerHeight,
|
|
|
|
footerMargin: moduleTemplate.footerHeight,
|
2024-12-23 14:00:50 +01:00
|
|
|
};
|
|
|
|
}
|
2025-03-18 16:54:53 +01:00
|
|
|
|
|
|
|
static async renderFileForCustom({
|
2025-03-19 15:19:03 +01:00
|
|
|
module,
|
2025-03-18 16:54:53 +01:00
|
|
|
title = "pdf-export FF Admin",
|
|
|
|
headerData = {},
|
|
|
|
bodyData = {},
|
|
|
|
footerData = {},
|
|
|
|
customTemplate,
|
|
|
|
}: {
|
2025-03-28 22:28:07 +01:00
|
|
|
module: TemplateFormat;
|
2025-03-18 16:54:53 +01:00
|
|
|
title?: string;
|
|
|
|
headerData?: any;
|
|
|
|
bodyData?: any;
|
|
|
|
footerData?: any;
|
|
|
|
customTemplate: {
|
|
|
|
headerId?: number;
|
|
|
|
footerId?: number;
|
|
|
|
bodyId?: string | number;
|
|
|
|
headerHeight: number;
|
|
|
|
footerHeight: number;
|
|
|
|
};
|
|
|
|
}): Promise<{ header: string; body: string; footer: string; headerMargin?: number; footerMargin?: number }> {
|
|
|
|
let header = `<h1 style="font-size:10px; text-align:center; width:100%;">${title}</h1>`;
|
|
|
|
let footer = "";
|
|
|
|
let body = "";
|
|
|
|
|
|
|
|
if (customTemplate.headerId) {
|
|
|
|
header = await this.getTemplateFromStore(customTemplate.headerId);
|
|
|
|
header = this.applyDataToTemplate(header, { title, ...headerData });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (customTemplate.footerId) {
|
|
|
|
footer = await this.getTemplateFromStore(customTemplate.footerId);
|
|
|
|
} else {
|
|
|
|
footer = this.getTemplateFromFile(module + ".footer");
|
|
|
|
}
|
|
|
|
footer = this.applyDataToTemplate(footer, { title, ...footerData });
|
|
|
|
|
|
|
|
if (customTemplate.bodyId && typeof customTemplate.bodyId == "number") {
|
|
|
|
body = await this.getTemplateFromStore(customTemplate.bodyId);
|
|
|
|
} else {
|
2025-03-19 15:19:03 +01:00
|
|
|
body = this.getTemplateFromFile((customTemplate.bodyId || module) + ".body");
|
2025-03-18 16:54:53 +01:00
|
|
|
}
|
|
|
|
body = this.applyDataToTemplate(body, { title, ...bodyData });
|
|
|
|
|
|
|
|
return {
|
|
|
|
header,
|
|
|
|
footer,
|
|
|
|
body,
|
|
|
|
headerMargin: customTemplate.headerHeight,
|
|
|
|
footerMargin: customTemplate.footerHeight,
|
|
|
|
};
|
|
|
|
}
|
2024-12-23 14:00:50 +01:00
|
|
|
}
|