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

FEAT #11691 TIME 2:00 Create attachments route

parent 8390c9fe
No related branches found
No related tags found
No related merge requests found
...@@ -44,84 +44,35 @@ class AttachmentController ...@@ -44,84 +44,35 @@ class AttachmentController
{ {
$body = $request->getParsedBody(); $body = $request->getParsedBody();
if (empty($body)) { $control = AttachmentController::controlAttachment(['body' => $body]);
return $response->withStatus(400)->withJson(['errors' => 'Body is not set or empty']); if (!empty($control['errors'])) {
} elseif (!Validator::notEmpty()->validate($body['encodedFile'])) { return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
return $response->withStatus(400)->withJson(['errors' => 'Body encodedFile is empty']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['format'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body format is empty or not a string']);
}
$attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
$generateChrono = false;
$mandatoryColumns = ['res_id_master', 'attachment_type'];
foreach ($body['data'] as $key => $value) {
foreach ($mandatoryColumns as $columnKey => $column) {
if ($column == $value['column'] && !empty($value['value'])) {
if ($column == 'res_id_master') {
if (!ResController::hasRightByResId(['resId' => [$value['value']], 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'ResId master out of perimeter']);
}
$resId = $value['value'];
} elseif ($column == 'attachment_type') {
if (empty($attachmentsTypes[$value['value']])) {
return $response->withStatus(400)->withJson(['errors' => 'Attachment Type does not exist']);
} elseif ($attachmentsTypes[$value['value']]['chrono']) {
$generateChrono = true;
}
}
unset($mandatoryColumns[$columnKey]);
}
}
if (in_array($value['column'], ['identifier'])) {
unset($body['data'][$key]);
}
}
if (!empty($mandatoryColumns)) {
return $response->withStatus(400)->withJson(['errors' => 'Body data array needs column(s) [' . implode(', ', $mandatoryColumns) . ']']);
} elseif (empty($resId)) {
return $response->withStatus(400)->withJson(['errors' => 'ResId master is missing']);
}
if ($generateChrono) {
$resource = ResModel::getById(['select' => ['destination', 'type_id'], 'resId' => $resId]);
$chrono = ChronoModel::getChrono(['id' => 'outgoing', 'entityId' => $resource['destination'], 'typeId' => $resource['type_id'], 'resId' => $resId]);
$body['data'][] = ['column' => 'identifier', 'value' => $chrono];
} }
$body['status'] = 'A_TRA'; $id = StoreController::storeAttachment($body);
$body['collId'] = 'letterbox_coll'; if (empty($id) || !empty($id['errors'])) {
$body['data'][] = ['column' => 'coll_id', 'value' => 'letterbox_coll']; return $response->withStatus(500)->withJson(['errors' => '[AttachmentController create] ' . $id['errors']]);
$body['data'][] = ['column' => 'type_id', 'value' => '0'];
$body['data'][] = ['column' => 'relation', 'value' => '1'];
$body['fileFormat'] = $body['format'];
$resId = StoreController::storeAttachment($body);
if (empty($resId) || !empty($resId['errors'])) {
return $response->withStatus(500)->withJson(['errors' => '[AttachmentController create] ' . $resId['errors']]);
} }
$collId = empty($body['version']) ? 'attachments_coll' : 'attachments_version_coll';
ConvertPdfController::convert([ ConvertPdfController::convert([
'resId' => $resId, 'resId' => $id,
'collId' => $collId 'collId' => 'attachments_coll'
]); ]);
$customId = CoreConfigModel::getCustomId(); $customId = CoreConfigModel::getCustomId();
$customId = empty($customId) ? 'null' : $customId; $customId = empty($customId) ? 'null' : $customId;
$user = UserModel::getByLogin(['select' => ['id'], 'login' => $GLOBALS['userId']]); exec("php src/app/convert/scripts/FullTextScript.php --customId {$customId} --resId {$id} --collId attachments_coll --userId {$GLOBALS['id']} > /dev/null &");
exec("php src/app/convert/scripts/FullTextScript.php --customId {$customId} --resId {$resId} --collId {$collId} --userId {$user['id']} > /dev/null &");
HistoryController::add([ HistoryController::add([
'tableName' => 'res_attachments', 'tableName' => 'res_attachments',
'recordId' => $resId, 'recordId' => $id,
'eventType' => 'ADD', 'eventType' => 'ADD',
'info' => _DOC_ADDED, 'info' => _DOC_ADDED,
'moduleId' => 'attachment', 'moduleId' => 'attachment',
'eventId' => 'attachmentAdd', 'eventId' => 'attachmentAdd',
]); ]);
return $response->withJson(['resId' => $resId]); return $response->withJson(['id' => $id]);
} }
public function getByResId(Request $request, Response $response, array $aArgs) public function getByResId(Request $request, Response $response, array $aArgs)
...@@ -568,69 +519,6 @@ class AttachmentController ...@@ -568,69 +519,6 @@ class AttachmentController
foreach ($contactsForMailing as $keyContact => $contactForMailing) { foreach ($contactsForMailing as $keyContact => $contactForMailing) {
$chronoPubli = $attachment['identifier'].'-'.($keyContact+1); $chronoPubli = $attachment['identifier'].'-'.($keyContact+1);
$dataValue = [];
$dataValue[] = [
'column' => 'coll_id',
'value' => 'letterbox_coll',
'type' => 'string'
];
array_push($dataValue, [
'column' => 'res_id_master',
'value' => $aArgs['resIdMaster'],
'type' => 'integer'
]);
array_push($dataValue, [
'column' => 'attachment_type',
'value' => $attachment['attachment_type'],
'type' => 'string'
]);
array_push($dataValue, [
'column' => 'identifier',
'value' => $chronoPubli,
'type' => 'string'
]);
array_push($dataValue, [
'column' => 'title',
'value' => $attachment['title'],
'type' => 'string'
]);
array_push($dataValue, [
'column' => 'type_id',
'value' => $attachment['type_id'],
'type' => 'integer'
]);
array_push($dataValue, [
'column' => 'format',
'value' => $attachment['format'],
'type' => 'string'
]);
array_push($dataValue, [
'column' => 'typist',
'value' => $attachment['typist'],
'type' => 'string'
]);
array_push($dataValue, [
'column' => 'relation',
'value' => $attachment['relation'],
'type' => 'integer'
]);
array_push($dataValue, [
'column' => 'dest_contact_id',
'value' => $contactForMailing['contact_id'],
'type' => 'integer'
]);
array_push($dataValue, [
'column' => 'dest_address_id',
'value' => $contactForMailing['address_id'],
'type' => 'integer'
]);
array_push($dataValue, [
'column' => 'in_signature_book',
'value' => 'true',
]);
$params = [ $params = [
'userId' => $aArgs['userId'], 'userId' => $aArgs['userId'],
'res_id' => $aArgs['resIdMaster'], 'res_id' => $aArgs['resIdMaster'],
...@@ -646,12 +534,13 @@ class AttachmentController ...@@ -646,12 +534,13 @@ class AttachmentController
$filePathOnTmp = TemplateController::mergeDatasource($params); $filePathOnTmp = TemplateController::mergeDatasource($params);
$allDatas = [ $allDatas = [
"encodedFile" => base64_encode(file_get_contents($filePathOnTmp)), "encodedFile" => base64_encode(file_get_contents($filePathOnTmp)),
"data" => $dataValue, "format" => $attachment['format'],
"collId" => "letterbox_coll", 'resIdMaster' => $aArgs['resIdMaster'],
"table" => "res_attachments", 'type' => $attachment['attachment_type'],
"fileFormat" => $attachment['format'], 'chrono' => $chronoPubli,
"status" => 'A_TRA' 'title' => $attachment['title'],
'inSignatureBook' => true,
]; ];
StoreController::storeAttachment($allDatas); StoreController::storeAttachment($allDatas);
...@@ -694,4 +583,93 @@ class AttachmentController ...@@ -694,4 +583,93 @@ class AttachmentController
return $return; return $return;
} }
private static function controlAttachment(array $args)
{
$body = $args['body'];
if (empty($body)) {
return ['errors' => 'Body is not set or empty'];
} elseif (!Validator::notEmpty()->validate($body['encodedFile'])) {
return ['errors' => 'Body encodedFile is empty'];
} elseif (!Validator::stringType()->notEmpty()->validate($body['format'])) {
return ['errors' => 'Body format is empty or not a string'];
} elseif (!Validator::intVal()->notEmpty()->validate($body['resIdMaster'])) {
return ['errors' => 'Body resIdMaster is empty or not an integer'];
} elseif (!Validator::stringType()->notEmpty()->validate($body['type'])) {
return ['errors' => 'Body type is empty or not a string'];
}
if (!ResController::hasRightByResId(['resId' => [$body['resIdMaster']], 'userId' => $GLOBALS['id']])) {
return ['errors' => 'Body resIdMaster is out of perimeter'];
}
$attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
if (empty($attachmentsTypes[$body['type']])) {
return ['errors' => 'Body type does not exist'];
}
$control = AttachmentController::controlFileData(['body' => $body]);
if (!empty($control['errors'])) {
return ['errors' => $control['errors']];
}
$control = AttachmentController::controlOrigin(['body' => $body]);
if (!empty($control['errors'])) {
return ['errors' => $control['errors']];
}
$control = AttachmentController::controlDates(['body' => $body]);
if (!empty($control['errors'])) {
return ['errors' => $control['errors']];
}
return true;
}
private static function controlFileData(array $args)
{
$body = $args['body'];
if (!empty($body['encodedFile'])) {
if (!Validator::stringType()->notEmpty()->validate($body['format'])) {
return ['errors' => 'Body format is empty or not a string'];
}
$file = base64_decode($body['encodedFile']);
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($file);
if (!StoreController::isFileAllowed(['extension' => $body['format'], 'type' => $mimeType])) {
return ['errors' => "Format with this mimeType is not allowed : {$body['format']} {$mimeType}"];
}
}
return true;
}
private static function controlOrigin(array $args)
{
$body = $args['body'];
if (!empty($body['originId'])) {
if (!Validator::intVal()->notEmpty()->validate($body['originId'])) {
return ['errors' => 'Body originId is not an integer'];
}
$origin = AttachmentModel::getById(['id' => $body['originId'], 'select' => ['res_id_master']]);
if (empty($origin)) {
return ['errors' => 'Body originId does not exist'];
} elseif ($origin['res_id_master'] != $body['resIdMaster']) {
return ['errors' => 'Body resIdMaster is different from origin'];
}
}
return true;
}
private static function controlDates(array $args)
{
$body = $args['body'];
return true;
}
} }
...@@ -104,19 +104,16 @@ abstract class AttachmentModelAbstract ...@@ -104,19 +104,16 @@ abstract class AttachmentModelAbstract
public static function create(array $aArgs) public static function create(array $aArgs)
{ {
ValidatorModel::notEmpty($aArgs, ['format', 'typist', 'creation_date', 'docserver_id', 'path', 'filename', 'fingerprint', 'filesize', 'status']); ValidatorModel::notEmpty($aArgs, ['res_id', 'format', 'typist', 'creation_date', 'docserver_id', 'path', 'filename', 'fingerprint', 'filesize', 'status', 'relation']);
ValidatorModel::stringType($aArgs, ['format', 'typist', 'creation_date', 'docserver_id', 'path', 'filename', 'fingerprint', 'status']); ValidatorModel::stringType($aArgs, ['format', 'typist', 'creation_date', 'docserver_id', 'path', 'filename', 'fingerprint', 'status']);
ValidatorModel::intVal($aArgs, ['filesize']); ValidatorModel::intVal($aArgs, ['res_id', 'filesize', 'relation']);
$nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'res_attachment_res_id_seq']);
$aArgs['res_id'] = $nextSequenceId;
DatabaseModel::insert([ DatabaseModel::insert([
'table' => 'res_attachments', 'table' => 'res_attachments',
'columnsValues' => $aArgs 'columnsValues' => $aArgs
]); ]);
return $nextSequenceId; return true;
} }
public static function update(array $aArgs) public static function update(array $aArgs)
...@@ -136,7 +133,13 @@ abstract class AttachmentModelAbstract ...@@ -136,7 +133,13 @@ abstract class AttachmentModelAbstract
public static function getAttachmentsTypesByXML() public static function getAttachmentsTypesByXML()
{ {
$attachmentTypes = []; static $types;
if (!empty($types)) {
return $types;
}
$types = [];
$loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'apps/maarch_entreprise/xml/entreprise.xml']); $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'apps/maarch_entreprise/xml/entreprise.xml']);
if ($loadedXml) { if ($loadedXml) {
...@@ -144,7 +147,7 @@ abstract class AttachmentModelAbstract ...@@ -144,7 +147,7 @@ abstract class AttachmentModelAbstract
if (count($attachmentTypesXML) > 0) { if (count($attachmentTypesXML) > 0) {
foreach ($attachmentTypesXML->type as $value) { foreach ($attachmentTypesXML->type as $value) {
$label = defined((string) $value->label) ? constant((string) $value->label) : (string) $value->label; $label = defined((string) $value->label) ? constant((string) $value->label) : (string) $value->label;
$attachmentTypes[(string) $value->id] = [ $types[(string) $value->id] = [
'label' => $label, 'label' => $label,
'icon' => (string)$value['icon'], 'icon' => (string)$value['icon'],
'sign' => (empty($value['sign']) || (string)$value['sign'] == 'true') ? true : false, 'sign' => (empty($value['sign']) || (string)$value['sign'] == 'true') ? true : false,
...@@ -155,7 +158,7 @@ abstract class AttachmentModelAbstract ...@@ -155,7 +158,7 @@ abstract class AttachmentModelAbstract
} }
} }
return $attachmentTypes; return $types;
} }
public static function unsignAttachment(array $aArgs) public static function unsignAttachment(array $aArgs)
......
...@@ -91,7 +91,7 @@ class ResController ...@@ -91,7 +91,7 @@ class ResController
$customId = CoreConfigModel::getCustomId(); $customId = CoreConfigModel::getCustomId();
$customId = empty($customId) ? 'null' : $customId; $customId = empty($customId) ? 'null' : $customId;
exec("php src/app/convert/scripts/FullTextScript.php --customId {$customId} --resId {$resId} --collId 'letterbox_coll' --userId {$GLOBALS['id']} > /dev/null &"); exec("php src/app/convert/scripts/FullTextScript.php --customId {$customId} --resId {$resId} --collId letterbox_coll --userId {$GLOBALS['id']} > /dev/null &");
} }
HistoryController::add([ HistoryController::add([
......
...@@ -24,6 +24,7 @@ use SrcCore\models\DatabaseModel; ...@@ -24,6 +24,7 @@ use SrcCore\models\DatabaseModel;
use SrcCore\models\ValidatorModel; use SrcCore\models\ValidatorModel;
use Resource\models\ResModel; use Resource\models\ResModel;
use SrcCore\models\CoreConfigModel; use SrcCore\models\CoreConfigModel;
use User\models\UserModel;
class StoreController class StoreController
{ {
...@@ -68,40 +69,43 @@ class StoreController ...@@ -68,40 +69,43 @@ class StoreController
} }
} }
public static function storeAttachment(array $aArgs) public static function storeAttachment(array $args)
{ {
ValidatorModel::notEmpty($aArgs, ['encodedFile', 'data', 'fileFormat', 'status']); ValidatorModel::notEmpty($args, ['encodedFile', 'format']);
ValidatorModel::stringType($aArgs, ['collId', 'fileFormat', 'status']); ValidatorModel::stringType($args, ['format']);
try { try {
$fileContent = base64_decode(str_replace(['-', '_'], ['+', '/'], $aArgs['encodedFile'])); $id = DatabaseModel::getNextSequenceValue(['sequenceId' => 'res_attachment_res_id_seq']);
$fileContent = base64_decode(str_replace(['-', '_'], ['+', '/'], $args['encodedFile']));
$storeResult = DocserverController::storeResourceOnDocServer([ $storeResult = DocserverController::storeResourceOnDocServer([
'collId' => 'attachments_coll', 'collId' => 'attachments_coll',
'docserverTypeId' => 'DOC', 'docserverTypeId' => 'DOC',
'encodedResource' => base64_encode($fileContent), 'encodedResource' => base64_encode($fileContent),
'format' => $aArgs['fileFormat'] 'format' => $args['format']
]); ]);
if (!empty($storeResult['errors'])) { if (!empty($storeResult['errors'])) {
return ['errors' => '[storeResource] ' . $storeResult['errors']]; return ['errors' => '[storeAttachment] ' . $storeResult['errors']];
} }
$data = StoreController::prepareAttachmentStorage([ $data = [
'data' => $aArgs['data'], 'res_id' => $id,
'docserverId' => $storeResult['docserver_id'], 'docserver_id' => $storeResult['docserver_id'],
'status' => $aArgs['status'], 'filename' => $storeResult['file_destination_name'],
'fileName' => $storeResult['file_destination_name'], 'filesize' => $storeResult['fileSize'],
'fileFormat' => $aArgs['fileFormat'], 'path' => $storeResult['directory'],
'fileSize' => $storeResult['fileSize'], 'fingerprint' => $storeResult['fingerPrint']
'path' => $storeResult['destination_dir'], ];
'fingerPrint' => $storeResult['fingerPrint']
]); $data = array_merge($args, $data);
$data = StoreController::prepareAttachmentStorage($data);
$id = AttachmentModel::create($data); AttachmentModel::create($data);
return $id; return $id;
} catch (\Exception $e) { } catch (\Exception $e) {
return ['errors' => '[storeResource] ' . $e->getMessage()]; return ['errors' => '[storeAttachment] ' . $e->getMessage()];
} }
} }
...@@ -171,59 +175,52 @@ class StoreController ...@@ -171,59 +175,52 @@ class StoreController
return $preparedData; return $preparedData;
} }
public static function prepareAttachmentStorage(array $aArgs) public static function prepareAttachmentStorage(array $args)
{ {
ValidatorModel::notEmpty($aArgs, ['data', 'docserverId', 'fileName', 'fileFormat', 'fileSize', 'path', 'fingerPrint']); ValidatorModel::notEmpty($args, ['res_id', 'docserver_id', 'filename', 'format', 'path', 'fingerprint']);
ValidatorModel::stringType($aArgs, ['docserverId', 'status', 'fileName', 'fileFormat', 'path', 'fingerPrint']); ValidatorModel::stringType($args, ['docserver_id', 'filename', 'format', 'path', 'fingerprint']);
ValidatorModel::arrayType($aArgs, ['data']); ValidatorModel::intVal($args, ['res_id', 'filesize']);
ValidatorModel::intVal($aArgs, ['fileSize']);
foreach ($aArgs['data'] as $key => $value) { $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
$aArgs['data'][$key]['column'] = strtolower($value['column']); if ($attachmentsTypes[$args['type']]['chrono'] && empty($args['chrono'])) {
$resource = ResModel::getById(['select' => ['destination', 'type_id'], 'resId' => $args['resIdMaster']]);
$args['chrono'] = ChronoModel::getChrono(['id' => 'outgoing', 'entityId' => $resource['destination'], 'typeId' => $resource['type_id'], 'resId' => $args['resIdMaster']]);
} }
$aArgs['data'][] = [ $relation = 1;
'column' => 'docserver_id', if (!empty($args['originId'])) {
'value' => $aArgs['docserverId'], $relations = AttachmentModel::get(['select' => ['relation'], 'where' => ['origin_id = ?'], 'data' => [$args['originId']], 'orderBy' => ['relation DESC'], 'limit' => 1]);
'type' => 'string' $relation = $relations[0]['relation'] ?? 2;
]; AttachmentModel::update(['set' => ['status' => 'OBS'], 'where' => ['(origin_id = ? OR res_id = ?)'], 'data' => [$args['originId'], $args['originId']]]);
$aArgs['data'][] = [ }
'column' => 'creation_date',
'value' => 'CURRENT_TIMESTAMP',
'type' => 'function'
];
$aArgs['data'][] = [
'column' => 'path',
'value' => $aArgs['path'],
'type' => 'string'
];
$aArgs['data'][] = [
'column' => 'fingerprint',
'value' => $aArgs['fingerPrint'],
'type' => 'string'
];
$aArgs['data'][] = [
'column' => 'filename',
'value' => $aArgs['fileName'],
'type' => 'string'
];
$aArgs['data'][] = [
'column' => 'format',
'value' => $aArgs['fileFormat'],
'type' => 'string'
];
$aArgs['data'][] = [
'column' => 'filesize',
'value' => $aArgs['fileSize'],
'type' => 'int'
];
$formatedData = []; $externalId = '{}';
foreach ($aArgs['data'] as $value) { if (!empty($args['externalId']) && is_array($args['externalId'])) {
$formatedData[$value['column']] = $value['value']; $externalId = json_encode($args['externalId']);
} }
return $formatedData; $preparedData = [
'res_id' => $args['res_id'],
'title' => $args['title'] ?? null,
'identifier' => $args['chrono'] ?? null,
'typist' => $GLOBALS['userId'],
'status' => 'A_TRA',
'relation' => $relation,
'origin_id' => $args['originId'] ?? null,
'res_id_master' => $args['resIdMaster'],
'attachment_type' => $args['type'],
'in_signature_book' => empty($args['inSignatureBook']) ? 'false' : 'true',
'external_id' => $externalId,
'format' => $args['format'],
'docserver_id' => $args['docserver_id'],
'filename' => $args['filename'],
'filesize' => $args['filesize'],
'path' => $args['path'],
'fingerprint' => $args['fingerprint'],
'creation_date' => 'CURRENT_TIMESTAMP'
];
return $preparedData;
} }
public static function getFingerPrint(array $aArgs) public static function getFingerPrint(array $aArgs)
......
...@@ -79,7 +79,7 @@ abstract class ResModelAbstract ...@@ -79,7 +79,7 @@ abstract class ResModelAbstract
public static function create(array $args) public static function create(array $args)
{ {
ValidatorModel::notEmpty($args, ['res_id', 'model_id', 'category_id', 'typist', 'creation_date']); ValidatorModel::notEmpty($args, ['res_id', 'model_id', 'category_id', 'typist', 'creation_date']);
ValidatorModel::stringType($args, ['category_id', 'creation_date', 'format', 'docserver_id', 'path', 'filename', 'fingerprint', ]); ValidatorModel::stringType($args, ['category_id', 'creation_date', 'format', 'docserver_id', 'path', 'filename', 'fingerprint']);
ValidatorModel::intVal($args, ['res_id', 'model_id', 'typist', 'filesize']); ValidatorModel::intVal($args, ['res_id', 'model_id', 'typist', 'filesize']);
DatabaseModel::insert([ DatabaseModel::insert([
......
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