Newer
Older
<?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 User Controller
* @author dev@maarch.org
*/
namespace ExportSeda\controllers;
use SrcCore\models\CoreConfigModel;

Arnaud Pauget
committed
use exportSeda\models\ArchiveTransfer;
class SendMessageController
{
public static function send($messageObject, $messageId, $type)
{
$channel = $messageObject->ArchivalAgency->OrganizationDescriptiveMetadata->Communication[0]->Channel;
$adapter = '';
if ($channel == 'url') {
$res = $adapter->send($messageObject, $messageId, $type);
return $res;
}
public static function generateMessageFile($aArgs = [])
{

Arnaud Pauget
committed
$tmpPath = CoreConfigModel::getTmpPath();
$messageObject = $aArgs['messageObject'];
$type = $aArgs['type'];

Arnaud Pauget
committed
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
92
93
94
95
96
97
98
99
100
101
102
103
$seda2Message = SendMessageController::initMessage(new \stdClass);
$seda2Message->MessageIdentifier->value = $messageObject->messageIdentifier;
$seda2Message->ArchivalAgreement->value = $messageObject->archivalAgreement;
$seda2Message->ArchivalAgency->Identifier->value = $messageObject->archivalAgency;
$seda2Message->TransferringAgency->Identifier->value = $messageObject->transferringAgency;
$seda2Message->DataObjectPackage->DescriptiveMetadata->ArchiveUnit[] = self::getArchiveUnit(
"RecordGrp",
null,
null,
'group_1',
null,
null
);
foreach($messageObject->dataObjectPackage->attachments as $attachment) {
$seda2Message->DataObjectPackage->BinaryDataObject[] = self::getBinaryDataObject(
$attachment->filePath,
$attachment->id
);
$pathInfo = pathinfo($attachment->filePath);
copy($attachment->filePath, $tmpPath . $pathInfo["basename"]);
if ($attachment->type == "mainDocument") {
$messageObject->dataObjectPackage->label = $attachment->label;
$messageObject->dataObjectPackage->originatingSystemId = $attachment->id;
$seda2Message->DataObjectPackage->DescriptiveMetadata->ArchiveUnit[0] = self::getArchiveUnit(
"File",
$messageObject->dataObjectPackage,
null,
$attachment->id,
"res_" . $attachment->id,
null
);
} else {
if (!isset($attachment->retentionRule)) {
$attachment->retentionRule = $messageObject->dataObjectPackage->retentionRule;
$attachment->retentionFinalDisposition = $messageObject->dataObjectPackage->retentionFinalDisposition;
}
$seda2Message->DataObjectPackage->DescriptiveMetadata->ArchiveUnit[0]->ArchiveUnit[] = self::getArchiveUnit(
$attachment->type,
$attachment,
null,
$attachment->id,
"res_" . $attachment->id,
null
);
}
}
// TODO : Externaliser la fonction de création du xml final
$DOMTemplate = new \DOMDocument();
$DOMTemplate->load('modules/export_seda/resources/'.$type.'.xml');
$DOMTemplateProcessor = new DOMTemplateProcessorController($DOMTemplate);

Arnaud Pauget
committed
$DOMTemplateProcessor->setSource($type, $seda2Message);
$DOMTemplateProcessor->merge();
$DOMTemplateProcessor->removeEmptyNodes();

Arnaud Pauget
committed
file_put_contents($tmpPath . $seda2Message->MessageIdentifier->value . ".xml", $DOMTemplate->saveXML());
$filename = self::generateZip($seda2Message, $tmpPath);

Arnaud Pauget
committed
$arrayReturn = [
"messageObject" => $seda2Message,
"encodedFilePath" => $filename,
"messageFilename" => $seda2Message->MessageIdentifier->value
];
return $arrayReturn;

Arnaud Pauget
committed
private static function generateZip($seda2Message, $tmpPath)

Arnaud Pauget
committed
$filename = $tmpPath.$seda2Message->MessageIdentifier->value. ".zip";
$zip->open($filename, \ZipArchive::CREATE);

Arnaud Pauget
committed
$zip->addFile($tmpPath . $seda2Message->MessageIdentifier->value . ".xml", $seda2Message->MessageIdentifier->value . ".xml");

Arnaud Pauget
committed
if ($seda2Message->DataObjectPackage) {
foreach ($seda2Message->DataObjectPackage->BinaryDataObject as $binaryDataObject) {
$zip->addFile($tmpPath . $binaryDataObject->Attachment->filename, $binaryDataObject->Attachment->filename);
}
}
return $filename;
}

Arnaud Pauget
committed
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
private static function initMessage($messageObject)
{
$date = new \DateTime;
$messageObject->Date = $date->format(\DateTime::ATOM);
$messageObject->MessageIdentifier = new \stdClass();
$messageObject->MessageIdentifier->value = "";
$messageObject->TransferringAgency = new \stdClass();
$messageObject->TransferringAgency->Identifier = new \stdClass();
$messageObject->ArchivalAgency = new \stdClass();
$messageObject->ArchivalAgency->Identifier = new \stdClass();
$messageObject->ArchivalAgreement = new \stdClass();
$messageObject->DataObjectPackage = new \stdClass();
$messageObject->DataObjectPackage->BinaryDataObject = [];
$messageObject->DataObjectPackage->DescriptiveMetadata = new \stdClass();
$messageObject->DataObjectPackage->ManagementMetadata = new \stdClass();
return $messageObject;
}
private function getBinaryDataObject($filePath, $id)
{
$binaryDataObject = new \stdClass();
$pathInfo = pathinfo($filePath);
if ($filePath) {
$filename = $pathInfo["basename"];
}
$binaryDataObject->id = "res_" . $id;
$binaryDataObject->MessageDigest = new \stdClass();
$binaryDataObject->MessageDigest->value = hash_file('sha256', $filePath);
$binaryDataObject->MessageDigest->algorithm = "sha256";
$binaryDataObject->Size = filesize($filePath);
$binaryDataObject->Attachment = new \stdClass();
$binaryDataObject->Attachment->filename = $filename;
$binaryDataObject->FileInfo = new \stdClass();
$binaryDataObject->FileInfo->Filename = $filename;
$binaryDataObject->FormatIdentification = new \stdClass();
$binaryDataObject->FormatIdentification->MimeType = mime_content_type($filePath);
return $binaryDataObject;
}
private function getArchiveUnit(
$type,
$object = null,
$attachments = null,
$archiveUnitId = null,
$dataObjectReferenceId = null,
$relatedObjectReference = null
) {
$archiveUnit = new \stdClass();
if ($archiveUnitId) {
$archiveUnit->id = $archiveUnitId;
} else {
$archiveUnit->id = uniqid();
}
if (isset($object)) {
if ($relatedObjectReference) {
$archiveUnit->Content = self::getContent($type, $object, $relatedObjectReference);
} else {
$archiveUnit->Content = self::getContent($type, $object);
}
$archiveUnit->Management = self::getManagement($object);
} else {
$archiveUnit->Content = self::getContent($type);
$archiveUnit->Management = self::getManagement();
}
if ($dataObjectReferenceId) {
$archiveUnit->DataObjectReference = new \stdClass();
if ($type == 'File') {
$archiveUnit->DataObjectReference->DataObjectReferenceId = $dataObjectReferenceId;
} elseif ($type == 'Note') {
$archiveUnit->DataObjectReference->DataObjectReferenceId = $dataObjectReferenceId;
} elseif ($type == 'Email') {
$archiveUnit->DataObjectReference->DataObjectReferenceId = $dataObjectReferenceId;
} else {
$archiveUnit->DataObjectReference->DataObjectReferenceId = $dataObjectReferenceId;
}
}
$archiveUnit->ArchiveUnit = [];
if ($attachments) {
$i = 1;
foreach ($attachments as $attachment) {
if ($attachment->res_id_master == $object->res_id) {
if ($attachment->attachment_type != "signed_response") {
$archiveUnit->ArchiveUnit[] = self::getArchiveUnit(
"Item",
$attachment,
null,
$archiveUnitId. '_attachment_' . $i,
$attachment->res_id
);
}
}
$i++;
}
}
if (count($archiveUnit->ArchiveUnit) == 0) {
unset($archiveUnit->ArchiveUnit);
}
return $archiveUnit;
}
private function getContent($type, $object = null, $relatedObjectReference = null)
{
$content = new \stdClass();
switch ($type) {
case 'RecordGrp':
$content->DescriptionLevel = $type;
$content->Title = [];
if ($object) {
$content->Title[] = $object->label;
$content->DocumentType = 'Document Principal';
} else {
$content->DocumentType = 'Dossier';
}
break;
case 'File':
$content->DescriptionLevel = $type;
$sentDate = new \DateTime($object->modificationDate);
$acquiredDate = new \DateTime($object->creationDate);
if ($object->documentDate) {
$receivedDate = new \DateTime($object->documentDate);
} else {
$receivedDate = new \DateTime($object->receivedDate);
}
$content->SentDate = $sentDate->format(\DateTime::ATOM);
$content->ReceivedDate = $receivedDate->format(\DateTime::ATOM);
$content->AcquiredDate = $acquiredDate->format(\DateTime::ATOM);
$content->Addressee = [];
$content->Sender = [];
$content->Keyword = [];
if ($object->contacts) {
foreach($object->contacts as $contactType => $contacts) {
foreach($contacts as $contact) {
if ($contactType == "senders") {
$content->Sender[] = self::getAddresse($contact, $contactType);
} else if ($contactType == "recipients") {
$content->Addressee[] = self::getAddresse($contact, $contactType);
}
}
}
}
if ($object->folders ) {
$content->FilePlanPosition = [];
$content->FilePlanPosition[] = new \stdClass;
$content->FilePlanPosition[0]->value="";
foreach($object->folders as $folder) {
$content->FilePlanPosition[0]->value .= "/".$folder;
}
}
if (!empty($keyword)) {
$content->Keyword[] = $keyword;
}
if (!empty($addressee)) {
$content->Addressee[] = $addressee;
}
$content->DocumentType = 'Document Principal';
$content->OriginatingAgencyArchiveUnitIdentifier = $object->chrono;
$content->OriginatingSystemId = $object->originatingSystemId;
$content->Title = [];
$content->Title[] = $object->label;
break;
case 'Item':
case 'attachment':
case 'response':
case 'note':
case 'email':
case 'summarySheet':
$content->DescriptionLevel = "Item";
$content->Title = [];
$content->Title[] = $object->label;
if ($type == "attachment") {
$content->DocumentType = "Pièce jointe";
$date = new \DateTime($object->creation_date);
$content->CreatedDate = $date->format('Y-m-d');
} elseif ($type == "note") {
$content->DocumentType = "Note";
$date = new \DateTime($object->creation_date);
$content->CreatedDate = $date->format('Y-m-d');
} elseif ($type == "email") {
$content->DocumentType = "Courriel";
$date = new \DateTime($object->creation_date);
$content->CreatedDate = $date->format('Y-m-d');
} elseif ($type == "response") {
$content->DocumentType = "Réponse";
$date = new \DateTime($object->creation_date);
$content->CreatedDate = $date->format('Y-m-d');
} elseif ($type == "summarySheet") {
$content->DocumentType = "Fiche de liaison";
$date = new \DateTime($object->creation_date);
$content->CreatedDate = $date->format('Y-m-d');
}
break;
}
if (isset($relatedObjectReference)) {
$content->RelatedObjectReference = new \stdClass();
$content->RelatedObjectReference->References = [];
foreach ($relatedObjectReference as $key => $value) {
$reference = new \stdClass();
if ($value) {
$reference->ArchiveUnitRefId = 'letterbox_' . $key;
$content->RelatedObjectReference->References[] = $reference;
} else {
if (isset($destination)) {
$res = array_key_exists($destination, self::entities);
$reference->RepositoryArchiveUnitPID = 'originator:' . $entity->business_id . ':' . $key;
$content->RelatedObjectReference->References[] = $reference;
}
}
}
}
if (isset($object->originatorAgency)) {
$content->OriginatingAgency = new \stdClass();
$content->OriginatingAgency->Identifier = new \stdClass();
$content->OriginatingAgency->Identifier->value = $object->originatorAgency->id;
if (empty($content->OriginatingAgency->Identifier->value)) {
unset($content->OriginatingAgency);
}
}
if (isset($object->history)) {
$content->CustodialHistory = new \stdClass();
$content->CustodialHistory->CustodialHistoryItem = [];
foreach($object->history as $history) {
$content->CustodialHistory->CustodialHistoryItem[] = self::getCustodialHistoryItem($history);
}
if (count($content->CustodialHistory->CustodialHistoryItem) == 0) {
unset($content->CustodialHistory);
}
}
return $content;
}
private function getManagement($valueInData = null)
{
$management = new \stdClass();
$management->AppraisalRule = new \stdClass();
$management->AppraisalRule->Rule = new \stdClass();
if ($valueInData->retentionRule) {
$management->AppraisalRule->Rule->value = $valueInData->retentionRule;
$management->AppraisalRule->StartDate = date("Y-m-d");
if (isset($valueInData->retentionFinalDisposition) && $valueInData->retentionFinalDisposition == "Conservation") {
$management->AppraisalRule->FinalAction = "Keep";
} else {
$management->AppraisalRule->FinalAction = "Destroy";
}
}
if ($valueInData->accessRuleCode) {
$management->AccessRule = new \stdClass();
$management->AccessRule->Rule = new \stdClass();
$management->AccessRule->Rule->value = $valueInData->accessRuleCode;
$management->AccessRule->StartDate = date("Y-m-d");
}
return $management;
}
private function getCustodialHistoryItem($history)
{
$date = new \DateTime($history->event_date);
$custodialHistoryItem = new \stdClass();
$custodialHistoryItem->value = $history->info;
$custodialHistoryItem->when = $date->format('Y-m-d');
return $custodialHistoryItem;
}
private function getAddresse($informations, $type = null)
{
$addressee = new \stdClass();
if ($informations->civility) {
$addressee->Gender = $informations->civility->label;
}
if ($informations->firstname) {
$addressee->FirstName = $informations->firstname;
}
if ($informations->lastname) {
$addressee->BirthName = $informations->lastname;
}
return $addressee;
}