From eddd1661368c9fcbb2d93a00f1bba9320125344c Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Fri, 20 Aug 2021 16:41:58 +0200
Subject: [PATCH] FEAT #17999 TIME 1:45 only configure position and text of
 watermark

---
 rest/index.php                                |  1 +
 .../controllers/ConfigurationController.php   | 31 +++++++++++++++----
 .../controllers/WatermarkController.php       |  2 +-
 3 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/rest/index.php b/rest/index.php
index 1f5a9b0efa..63619db57a 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -86,6 +86,7 @@ $app->get('/connectors/{id}/currentVisa', \ExternalSignatoryBook\controllers\Ext
 
 //Customization
 $app->put('/customization', \Configuration\controllers\ConfigurationController::class . ':updateCustomization');
+$app->get('/customization/watermark', \Configuration\controllers\ConfigurationController::class . ':getWatermarkConfiguration');
 
 //Documents
 $app->post('/documents', \Document\controllers\DocumentController::class . ':create');
diff --git a/src/app/configuration/controllers/ConfigurationController.php b/src/app/configuration/controllers/ConfigurationController.php
index 2d468801b5..988cf5d41f 100755
--- a/src/app/configuration/controllers/ConfigurationController.php
+++ b/src/app/configuration/controllers/ConfigurationController.php
@@ -386,21 +386,25 @@ class ConfigurationController
         $data['applicationUrl'] = $body['applicationUrl'] ?: null;
 
         if (!empty($body['watermark'])) {
+            $possibleAlign = ['left' => 'L', 'center' => 'C', 'right' => 'R'];
+
             if (!Validator::notEmpty()->boolType()->validate($body['watermark']['enabled'])) {
                 return $response->withStatus(400)->withJson(['errors' => 'Body watermark[enabled] is empty or not a boolean']);
-            } elseif (!Validator::notEmpty()->intVal()->validate($body['watermark']['posX'])) {
-                return $response->withStatus(400)->withJson(['errors' => 'Body watermark[posX] is empty or not an integer']);
             } elseif (!Validator::notEmpty()->intVal()->validate($body['watermark']['posY'])) {
                 return $response->withStatus(400)->withJson(['errors' => 'Body watermark[posY] is empty or not an integer']);
             } elseif (!Validator::notEmpty()->stringType()->validate($body['watermark']['text'])) {
-                return $response->withStatus(400)->withJson(['errors' => 'Body watermark[label] is empty or not a string']);
+                return $response->withStatus(400)->withJson(['errors' => 'Body watermark[text] is empty or not a string']);
+            } elseif (!Validator::notEmpty()->stringType()->validate($body['watermark']['align'])) {
+                return $response->withStatus(400)->withJson(['errors' => 'Body watermark[align] is empty or not a string']);
+            } elseif (!in_array($body['watermark']['align'], array_keys($possibleAlign))) {
+                return $response->withStatus(400)->withJson(['errors' => 'Body watermark[align] can only be "left", "right" or "center"']);
             }
 
-            $data['watermark'] += [
+            $data['watermark'] = [
                 'enabled' => $body['watermark']['enabled'],
-                'posX'    => $body['watermark']['posX'],
                 'posY'    => $body['watermark']['posY'],
-                'text'    => $body['watermark']['text']
+                'text'    => $body['watermark']['text'],
+                'align'   => $possibleAlign[$body['watermark']['align']]
             ];
         }
 
@@ -420,6 +424,21 @@ class ConfigurationController
         return $response->withStatus(204);
     }
 
+    public function getWatermarkConfiguration(Request $request, Response $response)
+    {
+        if (!PrivilegeController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_customization'])) {
+            return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
+        }
+
+        $configuration = ConfigurationModel::getByIdentifier(['identifier' => 'customization']);
+        if (empty($configuration[0])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Configuration does not exist']);
+        }
+        $configuration = json_decode($configuration[0]['value'], true);
+
+        return $response->withJson(['configuration' => $configuration['watermark'] ?? []]);
+    }
+
     private static function checkMailer(array $args)
     {
         if (!Validator::stringType()->notEmpty()->validate($args['type'])) {
diff --git a/src/app/document/controllers/WatermarkController.php b/src/app/document/controllers/WatermarkController.php
index bdbb049722..8c8de05acc 100644
--- a/src/app/document/controllers/WatermarkController.php
+++ b/src/app/document/controllers/WatermarkController.php
@@ -77,7 +77,7 @@ class WatermarkController
                 $pdf->SetTextColor(0, 0, 0); // Color black
                 $pdf->SetAlpha(0.5); // Opacity
                 $pdf->Rotate(0); // No rotation
-                $pdf->Text($watermark['posX'], $watermark['posY'], $text, false, false, true, 0, 0, 'R');
+                $pdf->Text(8, $watermark['posY'], $text, false, false, true, 0, 0, $watermark['align']); // Position X = 8
             }
             $fileContent = $pdf->Output('', 'S');
         } catch (\Exception $e) {
-- 
GitLab