From af3a5deab618b2112620cf00f7a3455f56eeccdd Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Thu, 9 May 2019 22:28:54 +0100
Subject: [PATCH] FEAT #10529 TIME 2 start print entity separator

---
 rest/index.php                                |   1 +
 .../controllers/EntitySeparatorController.php | 111 ++++++++++++++++++
 src/core/lang/lang-en.php                     |   3 +
 src/core/lang/lang-fr.php                     |   2 +
 src/core/lang/lang-nl.php                     |   3 +
 5 files changed, 120 insertions(+)
 create mode 100755 src/app/entity/controllers/EntitySeparatorController.php

diff --git a/rest/index.php b/rest/index.php
index f65169963d3..e761b5f7b19 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -169,6 +169,7 @@ $app->get('/entities/{id}/users', \Entity\controllers\EntityController::class .
 $app->put('/entities/{id}/reassign/{newEntityId}', \Entity\controllers\EntityController::class . ':reassignEntity');
 $app->put('/entities/{id}/status', \Entity\controllers\EntityController::class . ':updateStatus');
 $app->get('/entityTypes', \Entity\controllers\EntityController::class . ':getTypes');
+$app->post('/entitySeparators', \Entity\controllers\EntitySeparatorController::class . ':create');
 
 //Groups
 $app->get('/groups', \Group\controllers\GroupController::class . ':get');
diff --git a/src/app/entity/controllers/EntitySeparatorController.php b/src/app/entity/controllers/EntitySeparatorController.php
new file mode 100755
index 00000000000..b7542e87e29
--- /dev/null
+++ b/src/app/entity/controllers/EntitySeparatorController.php
@@ -0,0 +1,111 @@
+<?php
+
+/**
+* Copyright Maarch since 2008 under licence GPLv3.
+* See LICENCE.txt file at the root folder for more details.
+* This file is part of Maarch software.
+*
+*/
+
+/**
+* @brief Print Separator Controller
+* @author dev@maarch.org
+*/
+
+namespace Entity\controllers;
+
+use Endroid\QrCode\QrCode;
+use Entity\models\EntityModel;
+use Group\models\ServiceModel;
+use Parameter\models\ParameterModel;
+use Respect\Validation\Validator;
+use setasign\Fpdi\Tcpdf\Fpdi;
+use Slim\Http\Request;
+use Slim\Http\Response;
+
+class EntitySeparatorController
+{
+    public function create(Request $request, Response $response)
+    {
+        if (!ServiceModel::hasService(['id' => 'entities_print_sep_mlb', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) {
+            return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
+        }
+
+        $bodyData = $request->getParsedBody();
+
+        if (!Validator::stringType()->notEmpty()->validate($bodyData['type'])) {
+            return $response->withStatus(403)->withJson(['errors' => 'type is not set or empty']);
+        }
+
+        if (!in_array($bodyData['type'], ['barcode', 'qrcode'])) {
+            return $response->withStatus(403)->withJson(['errors' => 'type value must be qrcode or barcode']);
+        }
+
+        $entitiesList = [];
+        if ($bodyData['target'] == 'generic') {
+            $entitiesList['COURRIER'] = 'Maarch Courrier';
+        } else {
+            if (!Validator::arrayType()->notEmpty()->validate($bodyData['entities'])) {
+                return $response->withStatus(403)->withJson(['errors' => 'entities is not set or empty']);
+            }
+
+            $entitiesInfo = EntityModel::get([
+                'select'   => ['entity_label', 'entity_id'],
+                'where'    => ['entity_id in (?)'],
+                'data'     => [$bodyData['entities']],
+                'order_by' => ['entity_label asc']
+            ]);
+
+            foreach ($entitiesInfo as $value) {
+                $entitiesList[$value['entity_id']] = $value['entity_label'];
+            }
+        }
+
+        $pdf = new Fpdi('P', 'pt');
+        $pdf->setPrintHeader(false);
+
+        foreach ($entitiesList as $entityId => $entityLabel) {
+            $pdf->AddPage();
+            $pdf->SetFont('', 'B', 20);
+            $pdf->Cell(250, 20, _PRINT_SEP_TITLE, 0, 1, 'C');
+            $pdf->Cell(250, 20, $entityId, 0, 1, 'C');
+            $pdf->Cell(180, 10, '', 0, 1, 'C');
+
+            if ($bodyData['type'] == 'qrcode') {
+                $parameter = ParameterModel::getById(['select' => ['param_value_int'], 'id' => 'QrCodePrefix']);
+                $prefix = '';
+                if ($parameter['param_value_int'] == 1) {
+                    $prefix = 'Maarch_';
+                }
+                $qrCode = new QrCode($prefix . $entityId);
+                // $qrCode->setSize(110);
+                // $qrCode->setMargin(25);
+                $pdf->Image('@'.$qrCode->writeString(), 0, 0, 80);
+            } else {
+                // $p_cab = $cab_pdf->generateBarCode($type, $code, 40, '', '', '');
+                // $pdf->Image($_SESSION['config']['tmppath'].DIRECTORY_SEPARATOR.$p_cab, 40, 50, 120);
+            }
+            
+            $pdf->Cell(180, 10, '', 0, 1, 'C');
+            $pdf->Cell(180, 10, '', 0, 1, 'C');
+            $pdf->Cell(180, 10, '', 0, 1, 'C');
+            $pdf->Cell(180, 10, '', 0, 1, 'C');
+            $pdf->Cell(180, 10, '', 0, 1, 'C');
+            $pdf->Cell(180, 10, '', 0, 1, 'C');
+            $pdf->Cell(180, 10, '', 0, 1, 'C');
+            $pdf->Cell(180, 10, '', 0, 1, 'C');
+            $pdf->Cell(180, 10, utf8_decode(_ENTITY), 1, 1, 'C');
+            $pdf->SetFont('', 'B', 12);
+            $pdf->Cell(180, 10, utf8_decode($entityLabel), 1, 1, 'C');
+        }
+
+        $fileContent = $pdf->Output('', 'S');
+        $finfo    = new \finfo(FILEINFO_MIME_TYPE);
+        $mimeType = $finfo->buffer($fileContent);
+
+        $response->write($fileContent);
+        $response = $response->withAddedHeader('Content-Disposition', "inline; filename=entitySeparator.pdf");
+
+        return $response->withHeader('Content-Type', $mimeType);
+    }
+}
diff --git a/src/core/lang/lang-en.php b/src/core/lang/lang-en.php
index b8ada9a1c00..ab5fdf6ef1b 100755
--- a/src/core/lang/lang-en.php
+++ b/src/core/lang/lang-en.php
@@ -495,3 +495,6 @@ define("_XPARAPH_ACCOUNT_CREATED", "xParaph account created");
 define("_FILE_NOT_ALLOWED_INFO_1", "This extension");
 define("_FILE_NOT_ALLOWED_INFO_2", "with MIME-type");
 define("_FILE_NOT_ALLOWED_INFO_3", "is not allowed");
+
+define("_ENTITY", "Entity");
+define("_PRINT_SEP_TITLE", "Scan separator");
diff --git a/src/core/lang/lang-fr.php b/src/core/lang/lang-fr.php
index c541012b1da..b37733902af 100755
--- a/src/core/lang/lang-fr.php
+++ b/src/core/lang/lang-fr.php
@@ -495,3 +495,5 @@ define("_XPARAPH_ACCOUNT_CREATED", "Compte xParaph créé");
 define("_FILE_NOT_ALLOWED_INFO_1", "L'extension");
 define("_FILE_NOT_ALLOWED_INFO_2", "avec le type MIME");
 define("_FILE_NOT_ALLOWED_INFO_3", "n'est pas autorisée");
+define("_ENTITY", "Entité");
+define("_PRINT_SEP_TITLE", "Séparateur de scan");
diff --git a/src/core/lang/lang-nl.php b/src/core/lang/lang-nl.php
index a1baa20b791..247795abf33 100755
--- a/src/core/lang/lang-nl.php
+++ b/src/core/lang/lang-nl.php
@@ -497,3 +497,6 @@ define("_XPARAPH_ACCOUNT_CREATED", "xParaph account created_TO_TRANSLATE");
 define("_FILE_NOT_ALLOWED_INFO_1", "This extension_TO_TRANSLATE");
 define("_FILE_NOT_ALLOWED_INFO_2", "with MIME-type_TO_TRANSLATE");
 define("_FILE_NOT_ALLOWED_INFO_3", "is not allowed_TO_TRANSLATE");
+
+define("_ENTITY", "Entity_TO_TRANSLATE");
+define("_PRINT_SEP_TITLE", "Scan separator_TO_TRANSLATE");
-- 
GitLab