3333
3434class FooterHandler {
3535 private QrCode $ qrCode ;
36- private FileEntity $ fileEntity ;
3736 private const MIN_QRCODE_SIZE = 100 ;
3837 private const POINT_TO_MILIMETER = 0.3527777778 ;
39- private array $ templateVars = [];
38+
39+ private TemplateVariables $ templateVars ;
4040
4141 public function __construct (
4242 private IAppConfig $ appConfig ,
@@ -46,10 +46,10 @@ public function __construct(
4646 private IFactory $ l10nFactory ,
4747 private ITempManager $ tempManager ,
4848 ) {
49+ $ this ->templateVars = new TemplateVariables ();
4950 }
5051
51- public function getFooter (array $ dimensions , FileEntity $ fileEntity ): string {
52- $ this ->fileEntity = $ fileEntity ;
52+ public function getFooter (array $ dimensions ): string {
5353 $ add_footer = (bool )$ this ->appConfig ->getValueBool (Application::APP_ID , 'add_footer ' , true );
5454 if (!$ add_footer ) {
5555 return '' ;
@@ -72,7 +72,7 @@ public function getFooter(array $dimensions, FileEntity $fileEntity): string {
7272 $ dimension ['h ' ] * self ::POINT_TO_MILIMETER ,
7373 ],
7474 ]);
75- $ pdf ->SetDirectionality ($ this ->templateVars [ ' direction ' ] );
75+ $ pdf ->SetDirectionality ($ this ->templateVars -> getDirection () );
7676 }
7777 $ pdf ->AddPage (
7878 orientation: 'P ' ,
@@ -105,49 +105,72 @@ private function getRenderedHtmlFooter(): string {
105105 );
106106 return $ twigEnvironment
107107 ->createTemplate ($ this ->getTemplate ())
108- ->render ($ this ->getTemplateVars ());
108+ ->render ($ this ->prepareTemplateVars ());
109109 } catch (SyntaxError $ e ) {
110110 throw new LibresignException ($ e ->getMessage ());
111111 }
112112 }
113113
114114 public function setTemplateVar (string $ name , mixed $ value ): self {
115- $ this ->templateVars [$ name] = $ value ;
115+ $ this ->templateVars -> merge ( [$ name => $ value]) ;
116116 return $ this ;
117117 }
118118
119- private function getTemplateVars (): array {
120- $ this ->templateVars ['signedBy ' ] = $ this ->appConfig ->getValueString (Application::APP_ID , 'footer_signed_by ' , $ this ->l10n ->t ('Digitally signed by LibreSign. ' ));
121-
122- $ this ->templateVars ['direction ' ] = $ this ->l10nFactory ->getLanguageDirection ($ this ->l10n ->getLanguageCode ());
119+ private function prepareTemplateVars (): array {
120+ if (!$ this ->templateVars ->getSignedBy ()) {
121+ $ this ->templateVars ->setSignedBy (
122+ $ this ->appConfig ->getValueString (Application::APP_ID , 'footer_signed_by ' , $ this ->l10n ->t ('Digitally signed by LibreSign. ' ))
123+ );
124+ }
123125
124- $ this ->templateVars ['linkToSite ' ] = $ this ->appConfig ->getValueString (Application::APP_ID , 'footer_link_to_site ' , 'https://libresign.coop ' );
126+ if (!$ this ->templateVars ->getDirection ()) {
127+ $ this ->templateVars ->setDirection (
128+ $ this ->l10nFactory ->getLanguageDirection ($ this ->l10n ->getLanguageCode ())
129+ );
130+ }
125131
126- $ this ->templateVars ['validationSite ' ] = $ this ->appConfig ->getValueString (Application::APP_ID , 'validation_site ' );
127- if ($ this ->templateVars ['validationSite ' ]) {
128- $ this ->templateVars ['validationSite ' ] = rtrim ($ this ->templateVars ['validationSite ' ], '/ ' ) . '/ ' . $ this ->fileEntity ->getUuid ();
129- } else {
130- $ this ->templateVars ['validationSite ' ] = $ this ->urlGenerator ->linkToRouteAbsolute ('libresign.page.validationFileWithShortUrl ' , [
131- 'uuid ' => $ this ->fileEntity ->getUuid (),
132- ]);
132+ if (!$ this ->templateVars ->getLinkToSite ()) {
133+ $ this ->templateVars ->setLinkToSite (
134+ $ this ->appConfig ->getValueString (Application::APP_ID , 'footer_link_to_site ' , 'https://libresign.coop ' )
135+ );
133136 }
134137
135- $ this ->templateVars ['validateIn ' ] = $ this ->appConfig ->getValueString (Application::APP_ID , 'footer_validate_in ' , 'Validate in %s. ' );
136- if ($ this ->templateVars ['validateIn ' ] === 'Validate in %s. ' ) {
137- $ this ->templateVars ['validateIn ' ] = $ this ->l10n ->t ('Validate in %s. ' , ['%s ' ]);
138+ if (!$ this ->templateVars ->getValidationSite () && $ this ->templateVars ->getUuid ()) {
139+ $ validationSite = $ this ->appConfig ->getValueString (Application::APP_ID , 'validation_site ' );
140+ if ($ validationSite ) {
141+ $ this ->templateVars ->setValidationSite (
142+ rtrim ($ validationSite , '/ ' ) . '/ ' . $ this ->templateVars ->getUuid ()
143+ );
144+ } else {
145+ $ this ->templateVars ->setValidationSite (
146+ $ this ->urlGenerator ->linkToRouteAbsolute ('libresign.page.validationFileWithShortUrl ' , [
147+ 'uuid ' => $ this ->templateVars ->getUuid (),
148+ ])
149+ );
150+ }
138151 }
139152
140- foreach ($ this ->templateVars as $ key => $ value ) {
141- if (is_string ($ value )) {
142- $ this ->templateVars [$ key ] = htmlentities ($ value , ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_HTML401 );
153+ if (!$ this ->templateVars ->getValidateIn ()) {
154+ $ validateIn = $ this ->appConfig ->getValueString (Application::APP_ID , 'footer_validate_in ' , 'Validate in %s. ' );
155+ if ($ validateIn === 'Validate in %s. ' ) {
156+ $ this ->templateVars ->setValidateIn ($ this ->l10n ->t ('Validate in %s. ' , ['%s ' ]));
157+ } else {
158+ $ this ->templateVars ->setValidateIn ($ validateIn );
143159 }
144160 }
145161
146- if ($ this ->appConfig ->getValueBool (Application::APP_ID , 'write_qrcode_on_footer ' , true )) {
147- $ this ->templateVars ['qrcode ' ] = $ this ->getQrCodeImageBase64 ($ this ->templateVars ['validationSite ' ]);
162+ if ($ this ->appConfig ->getValueBool (Application::APP_ID , 'write_qrcode_on_footer ' , true ) && $ this ->templateVars ->getValidationSite ()) {
163+ $ this ->templateVars ->setQrcode ($ this ->getQrCodeImageBase64 ($ this ->templateVars ->getValidationSite ()));
164+ }
165+
166+ $ vars = $ this ->templateVars ->toArray ();
167+ foreach ($ vars as $ key => $ value ) {
168+ if (is_string ($ value )) {
169+ $ vars [$ key ] = htmlentities ($ value , ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_HTML401 );
170+ }
148171 }
149172
150- return $ this -> templateVars ;
173+ return $ vars ;
151174 }
152175
153176 private function getTemplate (): string {
@@ -171,7 +194,7 @@ private function getQrCodeImageBase64(string $text): string {
171194 $ result = $ writer ->write ($ this ->qrCode );
172195 $ qrcode = base64_encode ($ result ->getString ());
173196
174- $ this ->templateVars [ ' qrcodeSize ' ] = $ this ->qrCode ->getSize () + $ this ->qrCode ->getMargin () * 2 ;
197+ $ this ->templateVars -> setQrcodeSize ( $ this ->qrCode ->getSize () + $ this ->qrCode ->getMargin () * 2 ) ;
175198
176199 return $ qrcode ;
177200 }
0 commit comments