Skip to content
Snippets Groups Projects
Verified Commit 5a5b37aa authored by Damien's avatar Damien
Browse files

FEAT #12072 TIME 2:10 Get emailsInitialization

parent c96f4d6b
No related branches found
No related tags found
No related merge requests found
...@@ -373,6 +373,7 @@ $app->put('/resources/{resId}/unsign', \SignatureBook\controllers\SignatureBookC ...@@ -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}/acknowledgementReceipts', \AcknowledgementReceipt\controllers\AcknowledgementReceiptController::class . ':getByResId');
$app->get('/resources/{resId}/shippings', \Shipping\controllers\ShippingController::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}/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->get('/messageExchanges/{id}', \MessageExchange\controllers\MessageExchangeController::class . ':getById');
$app->put('/res/resource/status', \Resource\controllers\ResController::class . ':updateStatus'); $app->put('/res/resource/status', \Resource\controllers\ResController::class . ':updateStatus');
......
...@@ -38,6 +38,25 @@ class AdrModel ...@@ -38,6 +38,25 @@ class AdrModel
return $documents; 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) public static function getConvertedDocumentById(array $aArgs)
{ {
ValidatorModel::notEmpty($aArgs, ['resId', 'type', 'collId']); ValidatorModel::notEmpty($aArgs, ['resId', 'type', 'collId']);
......
...@@ -17,6 +17,7 @@ namespace Email\controllers; ...@@ -17,6 +17,7 @@ namespace Email\controllers;
use Attachment\controllers\AttachmentController; use Attachment\controllers\AttachmentController;
use Attachment\models\AttachmentModel; use Attachment\models\AttachmentModel;
use Configuration\models\ConfigurationModel; use Configuration\models\ConfigurationModel;
use Convert\models\AdrModel;
use Docserver\models\DocserverModel; use Docserver\models\DocserverModel;
use Docserver\models\DocserverTypeModel; use Docserver\models\DocserverTypeModel;
use Email\models\EmailModel; use Email\models\EmailModel;
...@@ -31,6 +32,7 @@ use Note\models\NoteModel; ...@@ -31,6 +32,7 @@ use Note\models\NoteModel;
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\PHPMailer;
use Resource\controllers\ResController; use Resource\controllers\ResController;
use Resource\controllers\StoreController; use Resource\controllers\StoreController;
use Resource\models\ResModel;
use Respect\Validation\Validator; use Respect\Validation\Validator;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
...@@ -414,6 +416,128 @@ class EmailController ...@@ -414,6 +416,128 @@ class EmailController
return $response->withJson(['emails' => $emails]); 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) public static function sendEmail(array $args)
{ {
......
...@@ -417,4 +417,15 @@ class StoreController ...@@ -417,4 +417,15 @@ class StoreController
return (int)$args['size']; 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';
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment