1010
1111use OCA \Libresign \AppInfo \Application ;
1212use OCA \Libresign \Handler \FooterHandler ;
13+ use OCA \Libresign \Service \Policy \Provider \Footer \FooterPolicy ;
14+ use OCA \Libresign \Service \Policy \Provider \Footer \FooterPolicyValue ;
1315use OCP \IAppConfig ;
1416
1517class FooterService {
@@ -20,34 +22,64 @@ public function __construct(
2022 }
2123
2224 public function isDefaultTemplate (): bool {
23- $ customTemplate = $ this ->appConfig -> getValueString (Application:: APP_ID , ' footer_template ' , '' );
24- return empty ( $ customTemplate ) ;
25+ $ footerPolicy = $ this ->getEffectiveFooterPolicy ( );
26+ return ! $ footerPolicy [ ' customizeFooterTemplate ' ] ;
2527 }
2628
2729 public function getTemplate (): string {
2830 return $ this ->footerHandler ->getTemplate ();
2931 }
3032
3133 public function saveTemplate (string $ template = '' ): void {
34+ $ currentPolicy = $ this ->getEffectiveFooterPolicy ();
35+ $ defaultTemplateFromPolicy = $ currentPolicy ['footerTemplate ' ];
36+
3237 if (empty ($ template )) {
3338 $ this ->appConfig ->deleteKey (Application::APP_ID , 'footer_template ' );
39+ $ this ->syncFooterPolicyTemplate ('' , false );
3440 return ;
3541 }
3642
37- if ($ template === $ this ->footerHandler ->getDefaultTemplate ()) {
43+ $ isProvidedTemplateEqualsDefault = $ template === $ defaultTemplateFromPolicy ;
44+
45+ if ($ isProvidedTemplateEqualsDefault ) {
3846 $ this ->appConfig ->deleteKey (Application::APP_ID , 'footer_template ' );
47+ $ this ->syncFooterPolicyTemplate ('' , false );
3948 } else {
4049 $ this ->appConfig ->setValueString (Application::APP_ID , 'footer_template ' , $ template );
50+ $ this ->syncFooterPolicyTemplate ($ template , true );
4151 }
4252 }
4353
54+ private function syncFooterPolicyTemplate (string $ template , bool $ customizeFooterTemplate ): void {
55+ $ currentPolicy = $ this ->getEffectiveFooterPolicy ();
56+ $ defaultTemplate = $ currentPolicy ['footerTemplate ' ];
57+
58+ $ normalizedPolicy = FooterPolicyValue::normalize (
59+ $ this ->appConfig ->getValueString (Application::APP_ID , FooterPolicy::KEY , '' ),
60+ $ defaultTemplate
61+ );
62+
63+ $ normalizedPolicy ['customizeFooterTemplate ' ] = $ customizeFooterTemplate ;
64+ $ normalizedPolicy ['footerTemplate ' ] = $ customizeFooterTemplate ? $ template : '' ;
65+
66+ $ this ->appConfig ->setValueString (
67+ Application::APP_ID ,
68+ FooterPolicy::KEY ,
69+ FooterPolicyValue::encode ($ normalizedPolicy )
70+ );
71+ }
72+
73+ private function getEffectiveFooterPolicy (): array {
74+ $ policyJson = $ this ->footerHandler ->getEffectiveFooterPolicyAsJson ();
75+ return FooterPolicyValue::normalize ($ policyJson , '' );
76+ }
77+
4478 public function renderPreviewPdf (string $ template = '' , int $ width = 595 , int $ height = 50 , ?bool $ writeQrcodeOnFooter = null ): string {
4579 if (!empty ($ template )) {
4680 $ this ->saveTemplate ($ template );
4781 }
4882
49- // Generate a realistic UUID format for preview (36 chars with hyphens, same as real UUIDs)
50- // This ensures QR code size matches the final document
5183 $ previewUuid = sprintf (
5284 'preview-%04x-%04x-%04x-%012x ' ,
5385 random_int (0 , 0xffff ),
0 commit comments