Newer
Older

Florian Azizian
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?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 PreProcessActionSEDATrait
* @author dev <dev@maarch.org>
* @ingroup core
*/
namespace ExportSeda\controllers;
use Respect\Validation\Validator;
use Action\controllers\PreProcessActionController;
use Attachment\models\AttachmentModel;
use Docserver\models\DocserverModel;
use Docserver\models\DocserverTypeModel;
use MessageExchange\models\MessageExchangeModel;
use Resource\controllers\ResController;
use Resource\controllers\ResourceListController;
use Resource\controllers\StoreController;
use Resource\models\ResModel;
use Slim\Http\Request;
use Slim\Http\Response;
trait PreProcessActionSEDATrait
{
public function checkAcknowledgementRecordManagement(Request $request, Response $response, array $args)
{
$body = $request->getParsedBody();
if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
}
$errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
if (!empty($errors['errors'])) {
return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
}
$body['resources'] = array_slice($body['resources'], 0, 500);
if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
}
$resourcesInformations = ['success' => [], 'errors' => []];
$body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
$attachments = AttachmentModel::get([
'select' => ['res_id_master', 'path', 'filename', 'docserver_id', 'fingerprint'],
'where' => ['res_id_master in (?)', 'attachment_type = ?', 'status = ?'],
'data' => [$body['resources'], 'acknowledgement_record_management', 'TRA']
]);
$resourcesAcknowledgement = array_column($attachments, null, 'res_id_master');
$resIdAcknowledgement = array_column($attachments, 'res_id_master');
$resourceAltIdentifier = ResModel::get(['select' => ['alt_identifier', 'res_id'], 'where' => ['res_id in (?)'], 'data' => [$body['resources']]]);
$altIdentifiers = array_column($resourceAltIdentifier, 'alt_identifier', 'res_id');
foreach ($body['resources'] as $resId) {
if (!in_array($resId, $resIdAcknowledgement)) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'recordManagement_noAcknowledgement'];
continue;
}
$acknowledgement = $resourcesAcknowledgement[$resId];
$docserver = DocserverModel::getByDocserverId(['docserverId' => $acknowledgement['docserver_id'], 'select' => ['path_template', 'docserver_type_id']]);
if (empty($docserver['path_template']) || !file_exists($docserver['path_template'])) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'docserverDoesNotExists'];
continue;
}
$pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $acknowledgement['path']) . $acknowledgement['filename'];
if (!file_exists($pathToDocument)) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'fileDoesNotExists'];
continue;
}
$docserverType = DocserverTypeModel::getById(['id' => $docserver['docserver_type_id'], 'select' => ['fingerprint_mode']]);
$fingerprint = StoreController::getFingerPrint(['filePath' => $pathToDocument, 'mode' => $docserverType['fingerprint_mode']]);
if (empty($acknowledgement['fingerprint'])) {
AttachmentModel::update(['set' => ['fingerprint' => $fingerprint], 'where' => ['res_id = ?'], 'data' => [$args['resId']]]);
$acknowledgement['fingerprint'] = $fingerprint;
}
if (!empty($acknowledgement['fingerprint']) && $acknowledgement['fingerprint'] != $fingerprint) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'fingerprintsDoNotMatch'];
continue;
}

Florian Azizian
committed
$acknowledgementXml = @simplexml_load_file($pathToDocument);

Florian Azizian
committed
if (empty($acknowledgementXml)) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'recordManagement_acknowledgementNotReadable'];
continue;
}
$messageExchange = MessageExchangeModel::getMessageByReference(['select' => ['message_id'], 'reference' => (string)$acknowledgementXml->MessageReceivedIdentifier]);
if (empty($messageExchange)) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'recordManagement_noAcknowledgementReference'];
continue;
}
$unitIdentifier = MessageExchangeModel::getUnitIdentifierByResId(['select' => ['message_id'], 'resId' => $resId]);
if ($unitIdentifier['message_id'] != $messageExchange['message_id']) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'recordManagement_wrongAcknowledgement'];
continue;
}

Florian Azizian
committed
$resourcesInformations['success'][] = $resId;

Florian Azizian
committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
}
return $response->withJson(['resourcesInformations' => $resourcesInformations]);
}
public function checkReplyRecordManagement(Request $request, Response $response, array $args)
{
$body = $request->getParsedBody();
if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
}
$errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
if (!empty($errors['errors'])) {
return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
}
$body['resources'] = array_slice($body['resources'], 0, 500);
if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
}
$resourcesInformations = ['success' => [], 'errors' => []];
$body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
$attachments = AttachmentModel::get([
'select' => ['res_id_master', 'path', 'filename', 'docserver_id', 'fingerprint'],
'where' => ['res_id_master in (?)', 'attachment_type = ?', 'status = ?'],
'data' => [$body['resources'], 'reply_record_management', 'TRA']
]);
$resourcesReply = array_column($attachments, null, 'res_id_master');
$resIdReply = array_column($attachments, 'res_id_master');
$resourceAltIdentifier = ResModel::get(['select' => ['alt_identifier', 'res_id'], 'where' => ['res_id in (?)'], 'data' => [$body['resources']]]);
$altIdentifiers = array_column($resourceAltIdentifier, 'alt_identifier', 'res_id');
foreach ($body['resources'] as $resId) {
if (!in_array($resId, $resIdReply)) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'recordManagement_noReply'];
continue;
}
$reply = $resourcesReply[$resId];
$docserver = DocserverModel::getByDocserverId(['docserverId' => $reply['docserver_id'], 'select' => ['path_template', 'docserver_type_id']]);
if (empty($docserver['path_template']) || !file_exists($docserver['path_template'])) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'docserverDoesNotExists'];
continue;
}
$pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $reply['path']) . $reply['filename'];
if (!file_exists($pathToDocument)) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'fileDoesNotExists'];
continue;
}
$docserverType = DocserverTypeModel::getById(['id' => $docserver['docserver_type_id'], 'select' => ['fingerprint_mode']]);
$fingerprint = StoreController::getFingerPrint(['filePath' => $pathToDocument, 'mode' => $docserverType['fingerprint_mode']]);
if (empty($reply['fingerprint'])) {
AttachmentModel::update(['set' => ['fingerprint' => $fingerprint], 'where' => ['res_id = ?'], 'data' => [$args['resId']]]);
$reply['fingerprint'] = $fingerprint;
}
if (!empty($reply['fingerprint']) && $reply['fingerprint'] != $fingerprint) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'fingerprintsDoNotMatch'];
continue;
}

Florian Azizian
committed
$replyXml = @simplexml_load_file($pathToDocument);

Florian Azizian
committed
if (empty($replyXml)) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'recordManagement_replyNotReadable'];
continue;
}
$messageExchange = MessageExchangeModel::getMessageByReference(['select' => ['message_id'], 'reference' => (string)$replyXml->MessageRequestIdentifier]);
if (empty($messageExchange)) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'recordManagement_noReplyReference'];
continue;
}
$unitIdentifier = MessageExchangeModel::getUnitIdentifierByResId(['select' => ['message_id'], 'resId' => $resId]);
if ($unitIdentifier['message_id'] != $messageExchange['message_id']) {
$resourcesInformations['errors'][] = ['alt_identifier' => $altIdentifiers[$resId], 'res_id' => $resId, 'reason' => 'recordManagement_wrongReply'];
continue;
}

Florian Azizian
committed
$resourcesInformations['success'][] = $resId;

Florian Azizian
committed
}
return $response->withJson(['resourcesInformations' => $resourcesInformations]);
}
}