From 5a5b37aabed5698c913dca8ab9432d12005f2c0b Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Thu, 13 Feb 2020 11:28:43 +0100 Subject: [PATCH] FEAT #12072 TIME 2:10 Get emailsInitialization --- rest/index.php | 1 + src/app/convert/models/AdrModel.php | 19 +++ src/app/email/controllers/EmailController.php | 124 ++++++++++++++++++ .../resource/controllers/StoreController.php | 11 ++ 4 files changed, 155 insertions(+) diff --git a/rest/index.php b/rest/index.php index 36dae4ced8e..7f2933b5651 100755 --- a/rest/index.php +++ b/rest/index.php @@ -373,6 +373,7 @@ $app->put('/resources/{resId}/unsign', \SignatureBook\controllers\SignatureBookC $app->get('/resources/{resId}/acknowledgementReceipts', \AcknowledgementReceipt\controllers\AcknowledgementReceiptController::class . ':getByResId'); $app->get('/resources/{resId}/shippings', \Shipping\controllers\ShippingController::class . ':getByResId'); $app->get('/resources/{resId}/messageExchanges', \MessageExchange\controllers\MessageExchangeController::class . ':getByResId'); +$app->get('/resources/{resId}/emailsInitialization', \Email\controllers\EmailController::class . ':getInitializationByResId'); $app->get('/messageExchanges/{id}', \MessageExchange\controllers\MessageExchangeController::class . ':getById'); $app->put('/res/resource/status', \Resource\controllers\ResController::class . ':updateStatus'); diff --git a/src/app/convert/models/AdrModel.php b/src/app/convert/models/AdrModel.php index 782bb03f69e..90bd802b298 100755 --- a/src/app/convert/models/AdrModel.php +++ b/src/app/convert/models/AdrModel.php @@ -38,6 +38,25 @@ class AdrModel return $documents; } + public static function getAttachments(array $args) + { + ValidatorModel::notEmpty($args, ['select']); + ValidatorModel::arrayType($args, ['select', 'where', 'data', 'orderBy']); + ValidatorModel::intType($args, ['offset', 'limit']); + + $attachments = DatabaseModel::select([ + 'select' => empty($args['select']) ? ['*'] : $args['select'], + 'table' => ['adr_attachments'], + 'where' => empty($args['where']) ? [] : $args['where'], + 'data' => empty($args['data']) ? [] : $args['data'], + 'order_by' => empty($args['orderBy']) ? [] : $args['orderBy'], + 'offset' => empty($args['offset']) ? 0 : $args['offset'], + 'limit' => empty($args['limit']) ? 0 : $args['limit'] + ]); + + return $attachments; + } + public static function getConvertedDocumentById(array $aArgs) { ValidatorModel::notEmpty($aArgs, ['resId', 'type', 'collId']); diff --git a/src/app/email/controllers/EmailController.php b/src/app/email/controllers/EmailController.php index c93a62164d3..12545987a6a 100644 --- a/src/app/email/controllers/EmailController.php +++ b/src/app/email/controllers/EmailController.php @@ -17,6 +17,7 @@ namespace Email\controllers; use Attachment\controllers\AttachmentController; use Attachment\models\AttachmentModel; use Configuration\models\ConfigurationModel; +use Convert\models\AdrModel; use Docserver\models\DocserverModel; use Docserver\models\DocserverTypeModel; use Email\models\EmailModel; @@ -31,6 +32,7 @@ use Note\models\NoteModel; use PHPMailer\PHPMailer\PHPMailer; use Resource\controllers\ResController; use Resource\controllers\StoreController; +use Resource\models\ResModel; use Respect\Validation\Validator; use Slim\Http\Request; use Slim\Http\Response; @@ -414,6 +416,128 @@ class EmailController return $response->withJson(['emails' => $emails]); } + public static function getInitializationByResId(Request $request, Response $response, array $args) + { + if (!Validator::intVal()->validate($args['resId']) || !ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) { + return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']); + } + + $resource = ResModel::getById(['select' => ['filename', 'version', 'alt_identifier', 'subject', 'typist', 'format', 'filesize'], 'resId' => $args['resId']]); + if (empty($resource)) { + return $response->withStatus(400)->withJson(['errors' => 'Document does not exist']); + } + + $document = []; + if (!empty($resource['filename'])) { + $convertedResource = AdrModel::getDocuments([ + 'select' => ['docserver_id', 'path', 'filename'], + 'where' => ['res_id = ?', 'type in (?)', 'version = ?'], + 'data' => [$args['resId'], ['PDF', 'SIGN'], $resource['version']], + 'orderBy' => ["type='SIGN' DESC"], + 'limit' => 1 + ]); + $convertedDocument = null; + if (!empty($convertedResource[0])) { + $docserver = DocserverModel::getByDocserverId(['docserverId' => $convertedResource[0]['docserver_id'], 'select' => ['path_template']]); + $pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $convertedResource[0]['path']) . $convertedResource[0]['filename']; + if (file_exists($pathToDocument)) { + $convertedDocument = [ + 'size' => StoreController::getFormattedSizeFromBytes(['size' => filesize($pathToDocument)]) + ]; + } + } + + $document = [ + 'id' => $args['resId'], + 'chrono' => $resource['alt_identifier'], + 'label' => $resource['subject'], + 'convertedDocument' => $convertedDocument, + 'creator' => UserModel::getLabelledUserById(['id' => $resource['typist']]), + 'format' => $resource['format'], + 'size' => StoreController::getFormattedSizeFromBytes(['size' => $resource['filesize']]) + ]; + } + + $attachments = []; + $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML(); + $rawAttachments = AttachmentModel::get([ + 'select' => ['res_id', 'title', 'identifier', 'attachment_type', 'typist', 'format', 'filesize'], + 'where' => ['res_id_master = ?', 'attachment_type not in (?)', 'status not in (?)'], + 'data' => [$args['resId'], ['signed_response'], ['DEL', 'OBS']] + ]); + foreach ($rawAttachments as $attachment) { + $attachmentId = $attachment['res_id']; + $signedAttachment = AttachmentModel::get([ + 'select' => ['res_id'], + 'where' => ['origin = ?', 'status != ?', 'attachment_type = ?'], + 'data' => ["{$attachment['resId']},res_attachments", 'DEL', 'signed_response'] + ]); + if (!empty($signedAttachment[0])) { + $attachmentId = $signedAttachment[0]['res_id']; + } + + $convertedAttachment = AdrModel::getAttachments([ + 'select' => ['docserver_id', 'path', 'filename'], + 'where' => ['res_id = ?', 'type = ?'], + 'data' => [$attachmentId, 'PDF'], + ]); + $convertedDocument = null; + if (!empty($convertedAttachment[0])) { + $docserver = DocserverModel::getByDocserverId(['docserverId' => $convertedAttachment[0]['docserver_id'], 'select' => ['path_template']]); + $pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $convertedAttachment[0]['path']) . $convertedAttachment[0]['filename']; + if (file_exists($pathToDocument)) { + $convertedDocument = [ + 'size' => StoreController::getFormattedSizeFromBytes(['size' => filesize($pathToDocument)]) + ]; + } + } + + $attachments[] = [ + 'id' => $attachment['res_id'], + 'chrono' => $attachment['identifier'], + 'label' => $attachment['title'], + 'typeLabel' => $attachmentTypes[$attachment['attachment_type']]['label'], + 'convertedDocument' => $convertedDocument, + 'creator' => UserModel::getLabelledUserById(['login' => $attachment['typist']]), + 'format' => $attachment['format'], + 'size' => StoreController::getFormattedSizeFromBytes(['size' => $attachment['filesize']]) + ]; + } + + $notes = []; + $userEntities = EntityModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['entity_id']]); + $userEntities = array_column($userEntities, 'entity_id'); + $rawNotes = NoteModel::get(['select' => ['id', 'note_text', 'user_id'], 'where' => ['identifier = ?'], 'data' => [$args['resId']]]); + foreach ($rawNotes as $rawNote) { + $allowed = false; + if ($rawNote['user_id'] == $GLOBALS['id']) { + $allowed = true; + } else { + $noteEntities = NoteEntityModel::get(['select' => ['item_id'], 'where' => ['note_id = ?'], 'data' => [$rawNote['id']]]); + if (!empty($noteEntities)) { + foreach ($noteEntities as $noteEntity) { + if (in_array($noteEntity['item_id'], $userEntities)) { + $allowed = true; + break; + } + } + } else { + $allowed = true; + } + } + if ($allowed) { + $notes[] = [ + 'id' => $rawNote['id'], + 'label' => $rawNote['note_text'], + 'creator' => UserModel::getLabelledUserById(['id' => $rawNote['user_id']]), + 'format' => 'html', + 'size' => null + ]; + } + } + + return $response->withJson(['resource' => $document, 'attachments' => $attachments, 'notes' => $notes]); + } public static function sendEmail(array $args) { diff --git a/src/app/resource/controllers/StoreController.php b/src/app/resource/controllers/StoreController.php index b52c2b77c99..767d456e441 100755 --- a/src/app/resource/controllers/StoreController.php +++ b/src/app/resource/controllers/StoreController.php @@ -417,4 +417,15 @@ class StoreController return (int)$args['size']; } + + public static function getFormattedSizeFromBytes(array $args) + { + if ($args['size'] / 1073741824 > 1) { + return round($args['size'] / 1073741824, 2) . ' Go'; + } elseif ($args['size'] / 1048576 > 1) { + return round($args['size'] / 1048576, 2) . ' Mo'; + } + + return round($args['size'] / 1024, 2) . ' Ko'; + } } -- GitLab