message.php 41.2 KB
Newer Older
Prosper De Laure's avatar
Prosper De Laure 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
<?php

/*
 * Copyright (C) 2015 Maarch
 *
 * This file is part of bundle medona.
 *
 * Bundle medona is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bundle medona is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with bundle medona.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace ext\thirdPartyArchiving\presentation\maarchRM\Presenter\medona;

/**
 * Serializer html retentionRule
 *
 * @package RecordsManagement
 * @author  Maarch Prosper DE LAURE <prosper.delaure@maarch.com>
 */
class message
{

    use archiveDeliveryTrait,
        archiveDestructionTrait,
        archiveTransferTrait,
36
        archiveOutgoingTransferTrait,
Prosper De Laure's avatar
Prosper De Laure committed
37
38
39
40
41
42
43
44
45
46
        archiveAuthorizationTrait,
        archiveNotificationTrait,
        archiveRestitutionTrait,
        \presentation\maarchRM\Presenter\exceptions\exceptionTrait;

    public $view;
    public $sdoFactory;
    protected $json;
    protected $translator;

47
    protected $dashboardPresenter;
Cyril Vazquez's avatar
Cyril Vazquez committed
48
    protected $menu;
49

Prosper De Laure's avatar
Prosper De Laure committed
50
51
52
53
54
55
56
57
58
59
60
    /**
     * Constuctor of message html serializer
     * @param \dependency\html\Document                    $view       The view
     * @param \dependency\sdo\Factory                      $sdoFactory The SDO dependency
     * @param \dependency\json\JsonObject                  $json       The JSON dependency
     * @param \dependency\localisation\TranslatorInterface $translator The localisation dependency
     */
    public function __construct(
        \dependency\html\Document $view,
        \dependency\sdo\Factory $sdoFactory,
        \dependency\json\JsonObject $json,
61
        \dependency\localisation\TranslatorInterface $translator
Prosper De Laure's avatar
Prosper De Laure committed
62
63
64
65
66
67
68
69
70
    ) {
        $this->view = $view;
        $this->sdoFactory = $sdoFactory;

        $this->json = $json;
        $this->json->status = true;

        $this->translator = $translator;
        $this->translator->setCatalog('medona/messages');
Cyril Vazquez's avatar
Cyril Vazquez committed
71
        $this->menu = \laabs::configuration()['medona']['menu'];
72
73
74
75
76
77
78
79
80
81

        $this->dashboardPresenter = \laabs::newService('presentation/dashboard');
    }

    /**
     * Dashobord for messages
     */
    public function index()
    {
        $this->view->addContentFile("medona/message/menu.html");
82

83
84
85
86
87
88
89
90
91
        $menu = $this->dashboardPresenter->filterMenuAuth($this->menu);

        $this->view->translate();

        $this->view->setSource('medonaMenu', $menu);

        $this->view->merge();

        return $this->view->saveHtml();
Prosper De Laure's avatar
Prosper De Laure committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    }

    /**
     * View message description
     * @param thirdPartyArchiving/message $message The message object
     *
     * @return string
     */
    public function read($message)
    {
        $this->view->addContentFile("medona/message/message.table.html");
        $this->view->translate();

        $this->view->setSource("message", $message);

        $this->view->merge();

        return $this->view->saveHtml();
    }

    /**
     * Initialise the history form
     */
    public function initHistoryForm()
    {
        $archivalAgreements = \laabs::callService('medona/archivalAgreement/readIndex');
        $currentService = \laabs::getToken("ORGANIZATION");
        $ownerOriginatorOrgs = [];
        $ownerDepositorOrgs = [];
        $ownerArchiverOrgs = [];

        if ($currentService) {
            $originators = \laabs::callService('organization/organization/readByrole_role_', 'originator');
            $depositors = \laabs::callService('organization/organization/readByrole_role_', 'depositor');
            $archivers = \laabs::callService('organization/organization/readByrole_role_', 'archiver');


Cyril Vazquez's avatar
Cyril Vazquez committed
129
            $owner = is_array($currentService->orgRoleCodes) && in_array('owner', $currentService->orgRoleCodes);
Prosper De Laure's avatar
Prosper De Laure committed
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

            foreach ($originators as $originator) {
                if (!isset($ownerOriginatorOrgs[(string) $originator->ownerOrgId])) {
                    $orgObject = \laabs::callService('organization/organization/read_orgId_', (string) $originator->ownerOrgId);

                    $ownerOriginatorOrgs[(string) $orgObject->orgId] = new \stdClass();
                    $ownerOriginatorOrgs[(string) $orgObject->orgId]->displayName = $orgObject->displayName;
                    $ownerOriginatorOrgs[(string) $orgObject->orgId]->originators = [];
                }

                $ownerOriginatorOrgs[(string) $orgObject->orgId]->originators[] = $originator;
            }
            foreach ($depositors as $depositor) {
                if (!isset($ownerDepositorOrgs[(string) $depositor->ownerOrgId])) {
                    $orgObject = \laabs::callService('organization/organization/read_orgId_', (string) $depositor->ownerOrgId);
                    $ownerDepositorOrgs[(string) $orgObject->orgId] = new \stdClass();
                    $ownerDepositorOrgs[(string) $orgObject->orgId]->displayName = $orgObject->displayName;
                    $ownerDepositorOrgs[(string) $orgObject->orgId]->depositors = [];
                }

                $ownerDepositorOrgs[(string) $orgObject->orgId]->depositors[] = $depositor ;
            }
            foreach ($archivers as $archiver) {
                if (!isset($ownerArchiverOrgs[(string) $archiver->ownerOrgId])) {
                    $orgObject = \laabs::callService('organization/organization/read_orgId_', (string) $archiver->ownerOrgId);
                    $ownerArchiverOrgs[(string) $orgObject->orgId] = new \stdClass();
                    $ownerArchiverOrgs[(string) $orgObject->orgId]->displayName = $orgObject->displayName;
                    $ownerArchiverOrgs[(string) $orgObject->orgId]->archivers = [];
                }

                $ownerArchiverOrgs[(string) $orgObject->orgId]->archivers[] = $archiver ;
            }
        }

        $this->view->setSource('archivalAgreements', $archivalAgreements);
        $this->view->setSource('ownerOriginatorOrgs', $ownerOriginatorOrgs);
        $this->view->setSource('ownerDepositorOrgs', $ownerDepositorOrgs);
        $this->view->setSource('ownerArchiverOrgs', $ownerArchiverOrgs);
    }

    /**
     * get a message's information for history view
     * @param medona/message $message The message objects
     *
     * @return string
     */
    public function displayInHistory($message)
    {
        return $this->display($message, $withActionsButtons = false);
    }

    /**
     * get a message's information
     * @param medona/message $message            The message objects
     * @param boolean        $withActionsButtons Set the action buttons up on screen
     *
     * @return string
     */
    public function display($message, $withActionsButtons = true)
    {
        if (isset(\laabs::configuration('medona')['parentsDerogation']) && \laabs::configuration('medona')['parentsDerogation']) {
            $registrationNumber = \laabs::callService('organization/userPosition/readDescendantservices');
        } else {
            $organization = \laabs::getToken("ORGANIZATION");
            $registrationNumber = array($organization->registrationNumber);
        }

        $messageObjects = [];
198

Prosper De Laure's avatar
Prosper De Laure committed
199
        $messages = array($message);
Alexandre Morin's avatar
Alexandre Morin committed
200
        $baseMessage = reset($messages);
201
202
203
        if (isset($message->acknowledgement)) {
            $messages = array_merge($messages, [$message->acknowledgement]);
        }
Prosper De Laure's avatar
Prosper De Laure committed
204
205
206
207
208
209
210
211
212
213
214
215

        if (isset($message->authorizationMessages)) {
            $messages = array_merge($messages, $message->authorizationMessages);
        }

        if (isset($message->replyMessage)) {
            $messages[] = $message->replyMessage;
        }

        foreach ($messages as $message) {
            $messageObject = $message->object;
            $messageObject->type = $message->type;
216
            $messageObject->schema = $message->schema;
Prosper De Laure's avatar
Prosper De Laure committed
217
218
219
            $messageObject->messageId = $message->messageId;
            $messageObject->xml = isset($message->xml);
            $messageObject->archived = $message->archived;
220
221
222
223
224
            $messageObject->status = $message->status;
            $messageObject->archivalAgreement = $message->archivalAgreementReference;
            $messageObject->reference = $message->reference;
            $messageObject->size = number_format($message->size, 0, '', ' ');

225
226
227
            $user = \laabs::callService('auth/userAccount/read_userAccountId_', $message->accountId);
            $messageObject->accountDisplayName = $user->displayName;
            $messageObject->accountName = $user->accountName;
228

229
230
231
            if (isset($message->lifeCycleEvent)) {
                $messageObject->lifeCycleEvent = $message->lifeCycleEvent;
            }
232

Prosper De Laure's avatar
Prosper De Laure committed
233
234
            // Load the organizations names
            if (isset($messageObject->archivalAgency)) {
Alexandre Morin's avatar
Alexandre Morin committed
235
                $messageObject->archivalAgency->name = $this->loadOrganizationName($messageObject->archivalAgency, $message);
Prosper De Laure's avatar
Prosper De Laure committed
236
237
            }
            if (isset($messageObject->transferringAgency)) {
Alexandre Morin's avatar
Alexandre Morin committed
238
                $messageObject->transferringAgency->name = $this->loadOrganizationName($messageObject->transferringAgency, $message);
Prosper De Laure's avatar
Prosper De Laure committed
239
240
            }
            if (isset($messageObject->controlAuthority)) {
Alexandre Morin's avatar
Alexandre Morin committed
241
                $messageObject->controlAuthority->name = $this->loadOrganizationName($messageObject->controlAuthority, $message);
Prosper De Laure's avatar
Prosper De Laure committed
242
243
            }
            if (isset($messageObject->originatingAgency)) {
Alexandre Morin's avatar
Alexandre Morin committed
244
                $messageObject->originatingAgency->name = $this->loadOrganizationName($messageObject->originatingAgency, $message);
Prosper De Laure's avatar
Prosper De Laure committed
245
246
            }
            if (isset($messageObject->requester)) {
Alexandre Morin's avatar
Alexandre Morin committed
247
                $messageObject->requester->name = $this->loadOrganizationName($messageObject->requester, $message);
Prosper De Laure's avatar
Prosper De Laure committed
248
            }
249
            if (isset($messageObject->sender)) {
Alexandre Morin's avatar
Alexandre Morin committed
250
                $messageObject->sender->name = $this->loadOrganizationName($messageObject->sender, $message);
251
252
            }
            if (isset($messageObject->receiver)) {
Alexandre Morin's avatar
Alexandre Morin committed
253
                $messageObject->receiver->name = $this->loadOrganizationName($messageObject->receiver, $message);
254
            }
Prosper De Laure's avatar
Prosper De Laure committed
255
256
257
258
259
260

            // Set messages action buttons
            if ($withActionsButtons) {
                $this->setMessageActions($message, $messages, $messageObject, $registrationNumber);
            }

261
262
263
264
            if ($message->status === 'error') {
                $messageObject->retryButton = "/medona/message/". $message->messageId . "/retry";
            }

265
            if ($baseMessage->schema == 'seda2' && isset($messageObject->dataObjectPackage)) {
Alexandre Morin's avatar
Alexandre Morin committed
266
267
268
269
                foreach ($messageObject->dataObjectPackage->descriptiveMetadata->archiveUnit as $archive) {
                    if ($archive->content->originatingAgency) {
                        $archive->content->originatingAgency->name = $this->loadOrganizationName($archive->content->originatingAgency, $message);
                    }
Alexandre Morin's avatar
Alexandre Morin committed
270

Alexandre Morin's avatar
Alexandre Morin committed
271
272
273
274
                    if ($archive->content->submissionAgency) {
                        $archive->content->submissionAgency->name = $this->loadOrganizationName($archive->content->submissionAgency, $message);
                    }

Alexandre Morin's avatar
Alexandre Morin committed
275
276
277
278
279
280
281
                    if ($archive->archiveUnit) {
                        foreach ($archive->archiveUnit as $archiveChildren) {
                            if ($archiveChildren->content->originatingAgency) {
                                $archiveChildren->content->originatingAgency->name = $this->loadOrganizationName($archiveChildren->content->originatingAgency, $message);
                            }
                        }
                    }
Alexandre Morin's avatar
Alexandre Morin committed
282
                }
Alexandre Morin's avatar
Alexandre Morin committed
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
            } elseif ($baseMessage->schema == 'recordsManagement') {
                if (!isset($descriptionField)) {
                    $descriptionFields = \laabs::callService('recordsManagement/descriptionField/readIndex');
                }

                $messageObject->sender = $message->senderOrg;
                $messageObject->receiver = $message->recipientOrg;

                if (isset($messageObject->binaryDataObject)) {
                    $messageObject->binaryDataObject = (array) $messageObject->binaryDataObject;
                }

                if (isset($messageObject->descriptiveMetadata)) {
                    $messageObject->descriptiveMetadata = (array) $messageObject->descriptiveMetadata;

                    foreach ($messageObject->descriptiveMetadata as $descriptiveMetadata) {
                        if (isset($descriptiveMetadata->descriptionObject)) {
                            $descriptionObjects = $descriptiveMetadata->descriptionObject;
                            $descriptiveMetadata->descriptionObject = [];

                            foreach($descriptionObjects as $key => $value) {
                                $object = new \stdClass();
                                $object->key = $key;

                                foreach ($descriptionFields as $descriptionField) {
                                    if ($descriptionField->name === $key) {
                                        $object->label = $descriptionField->label;
                                    }
                                }

                                $object->value = $value;

                                $descriptiveMetadata->descriptionObject[] = $object;
                            }
                        }
                    }
                }
320
321
322
323
324
            } elseif($baseMessage->schema == 'seda') {
                if (isset($messageObject->archive)) {
                    foreach ($messageObject->archive as $archive) {
                        if (isset($archive->document)) {
                            foreach ($archive->document as $document) {
325
326
327
                                if (isset($document->integrity)) {
                                    $document->integrity->hashAlgorithm = substr($document->integrity->algorithme, strrpos($document->integrity->algorithme, "#") + 1);
                                }
328
329
330
331
                            }
                        }
                    }
                }
Alexandre Morin's avatar
Alexandre Morin committed
332
            }
333
334
335
336
337
338
            if (isset($messageObject->archive)) {
                foreach ($messageObject->archive as $archive) {
                    if (isset($archive->appraisalRule)) {
                        $dateInter = new \DateInterval($archive->appraisalRule->duration);
                        $numberDuration = 0;
                        $toDisplay = '';
Arnaud Pauget's avatar
Arnaud Pauget committed
339
                        
340
                        if ($dateInter->y != 0) {
Arnaud Pauget's avatar
Arnaud Pauget committed
341
342
343
344
345
346
                            if ($dateInter->y == 999999999) {
                                $toDisplay = "Unlimited";
                            } else {
                                $numberDuration = $dateInter->y;
                                $toDisplay = "Year(s)";
                            }
347
348
349
350
351
352
353
354
355
                        } elseif ($dateInter->m != 0) {
                            $numberDuration = $dateInter->m;
                            $toDisplay = "Month(s)";
                        } elseif ($dateInter->d != 0) {
                            $numberDuration = $dateInter->d;
                            $toDisplay = "Day(s)";
                        }
                        $archive->appraisalRule->durationNumber = $numberDuration;
                        $archive->appraisalRule->durationToDisplay = $toDisplay;
356
357
358
                    }
                }
            }
Prosper De Laure's avatar
Prosper De Laure committed
359
360
361
362
            $messageObjects[] = $messageObject;
        }

        end($messageObjects)->last = true;
363
        $this->view->addContentFile($baseMessage->schema."/message/messageModal.html");
364
365

        $messageDiv = $this->view->getElementById('[?merge .messageId ?]');
Prosper De Laure's avatar
Prosper De Laure committed
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
        if ($baseMessage->schema == 'medona') {
            $this->view->addContentFile('medona/message/message.html', $messageDiv);

            // Append description specific form
            $descriptionMetadataDiv = $this->view->getElementById('descriptiveMetadata');
            if (isset($messageObjects[0]->dataObjectPackage)) {
                $this->view->addContentFile($messageObjects[0]->dataObjectPackage->descriptiveMetadataClass.'.html', $descriptionMetadataDiv);
                if ($messageObjects[0]->dataObjectPackage->descriptiveMetadataClass == 'medona/archivePackage') {
                    foreach ($messageObjects[0]->dataObjectPackage->descriptiveMetadata->archive as $archive) {
                        if (isset($archive->descriptionObject)) {
                            $archive->descriptionObject->json = json_encode($archive->descriptionObject);
                        }
                    }
                }
            }
381
382
383
        } /**else {
    $this->view->addContentFile($baseMessage->schema.'/message/message.html', $messageDiv);
    }*/
384

385
386
        if ($this->view->getElementsByClass("dataTable")->item(2)) {
            $dataTable = $this->view->getElementsByClass("dataTable")->item(2)->plugin['dataTable'];
Alexandre Morin's avatar
Alexandre Morin committed
387
388
389
            $dataTable->setPaginationType("full_numbers");
            $dataTable->setSorting(array(array(0, 'desc')));
        }
390
        
Prosper De Laure's avatar
Prosper De Laure committed
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
        $this->view->setSource('messages', $messageObjects);
        $this->view->merge();

        $this->view->translate();

        return $this->view->saveHtml();
    }

    /**
     * Get a message as a certificate
     * @param medona/message $message The message objects
     *
     * @return string
     */
    public function getCertificate($message)
    {
        $message->last = true;
        $messages = array($message);

        $this->view->addContentFile("medona/message/messageModal.html");
        $this->view->setSource('messages', $messages);
        $this->view->merge();

        $messageSerializer = $this->getMessageTypeSerializer($message);
        $html = $messageSerializer->display($message);
        $html = utf8_decode($html);
        if ($html) {
            $messageContent = $this->view->createDocumentFragment();
            $messageContent->appendHTML($html);

            $messageDiv = $this->view->getElementById($message->messageId);
            $messageDiv->appendChild($messageContent);
        }
        $this->view->translate();

        return $this->view->saveHtml();
    }

    /**
     * Get a form to search resource
     * @param recordsManagement/digitalResource $digitalResource The digital resouce
     *
     * @return string
     */
    public function getDataObjectAttachment($digitalResource)
    {
        $contents = $digitalResource->getContents();
        $mimetype = $digitalResource->mimetype;
        \laabs::setResponseType($mimetype);

        switch ($mimetype) {
            case 'application/xml':
            case 'text/xml':
                $dom = new \DOMDocument();
                $dom->formatOutput = true;

                $dom->loadXml($contents);

                $contents = "<pre>".htmlentities(preg_replace('#\>\n(\s*\n)*#', ">\n", $dom->saveXml()))."</pre>";
                \laabs::setResponseType("text/html");
                break;

            default:
                break;
        }

        return $contents;
    }

    /**
     * Get a form to search resource
     * @param string $mimetype The mime type
     *
     * @return string
     */
    public function showDataObjectAttachmentsContent($mimetype)
    {
        $this->view->addContentFile("recordsManagement/archive/modalViewDocument.html");
        $this->view->translate();

        //$this->view->setSource('url', $url);
        $this->view->setSource('mimetype', $mimetype);
        $this->view->merge();

        return $this->view->saveHtml();
    }

    /**
     * List archive conversion request
     *
     * @param array $archivesConversionRequest Array of medona/message object
     *
     * @return string
     */
    public function listConversionRequest($archivesConversionRequest)
    {
        $this->view->addContentFile("medona/message/conversionRequestList.html");

        $dataTable = $this->view->getElementsByClass("dataTable")->item(0)->plugin['dataTable'];
        $dataTable->setPaginationType("full_numbers");
        $dataTable->setUnsortableColumns(3);

        $this->view->translate();

        $this->view->setSource('archivesConversionRequest', $archivesConversionRequest);
        $this->view->merge();

        return $this->view->saveHtml();
    }

    /**
     * Get the message import form
     * @param string $messageZip The message zip
     *
     * @return string
     */
    public function messageExport($messageZip)
    {
        \laabs::setResponseType("application/zip");

        return $messageZip;
    }

    //JSON

    /**
     * Get messages numbers
     * @param array $messagesCount Array of number for each message type
     *
     * @return array
     */
    public function countMessages($messagesCount)
    {
        return json_encode($this->count($messagesCount));
    }

    /**
     * Serializer JSON for acceptArchiveTransfer method
     *
     * @return type
     */
    public function acceptArchiveTransfer()
    {
        $this->json->message = $this->translator->getText("Message accepted");

        return $this->json->save();
    }

    /**
     * Serializer JSON for rejectArchiveTransfer method
     *
     * @return type
     */
    public function rejectArchiveTransfer()
    {
        $this->json->message = $this->translator->getText("Message rejected");

        return $this->json->save();
    }

    /**
     * Serializer JSON for validateArchiveTransfer method
     *
     * @return type
     */
    public function validateArchiveTransfer()
    {
        $this->json->message = $this->translator->getText("Message validated");

        return $this->json->save();
    }
562

Prosper De Laure's avatar
Prosper De Laure committed
563
564
565
566
567
568
569
570
571
572
573
574
575
    /**
     * Serializer JSON for processBash method
     *
     * @return type
     */
    public function processArchiveTransfer()
    {
        $this->json->message = $this->translator->getText("Message processed");

        return $this->json->save();
    }

    /**
Alexandre Morin's avatar
Alexandre Morin committed
576
     * Serializer JSON for derogationDeliveryRequest method
Prosper De Laure's avatar
Prosper De Laure committed
577
578
579
     *
     * @return type
     */
Alexandre Morin's avatar
Alexandre Morin committed
580
    public function derogationDeliveryRequest()
Prosper De Laure's avatar
Prosper De Laure committed
581
    {
Alexandre Morin's avatar
Alexandre Morin committed
582
        $this->json->message = $this->translator->getText("Message sent to derogation");
Prosper De Laure's avatar
Prosper De Laure committed
583
584
585
586
587
588
589
590
591

        return $this->json->save();
    }

    /**
     * Serializer JSON for rejectArchiveTransfer method
     *
     * @return type
     */
Alexandre Morin's avatar
Alexandre Morin committed
592
    public function rejectDeliveryRequest()
Prosper De Laure's avatar
Prosper De Laure committed
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
    {
        $this->json->message = $this->translator->getText("Message rejected");

        return $this->json->save();
    }

    /**
     * Serializer JSON for rejectArchiveTransfer method
     *
     * @return type
     */
    public function rejectArchiveDestructionRequest()
    {
        $this->json->message = $this->translator->getText("Message rejected");

        return $this->json->save();
    }

    /**
     * Serializer JSON for rejectArchiveTransfer method
     *
     * @return type
     */
    public function validateArchiveDestructionRequest()
    {
        $this->json->message = $this->translator->getText("Message accepted");

        return $this->json->save();
    }


    /**
     * Serializer JSON for sendAuthorizationOriginatingAgencyRequest method
     *
     * @return type
     */
    public function sendAuthorizationOriginatingAgencyRequest()
    {
        $this->json->message = $this->translator->getText("Authorization request send to originator agency");

        return $this->json->save();
    }

    /**
     * Serializer JSON for sendAuthorizationControlAuthorityRequest method
     *
     * @return type
     */
    public function sendAuthorizationControlAuthorityRequest()
    {
        $this->json->message = $this->translator->getText("Authorization request send to control authority");

        return $this->json->save();
    }

    /**
     * Serializer JSON for acceptAuthorizationOriginatingAgencyRequest method
     *
     * @return type
     */
    public function acceptAuthorizationOriginatingAgencyRequest()
    {
        $this->json->message = $this->translator->getText("Authorization accepted");

        return $this->json->save();
    }

    /**
     * Serializer JSON for rejectAuthorizationOriginatingAgencyRequest method
     *
     * @return type
     */
    public function rejectAuthorizationOriginatingAgencyRequest()
    {
        $this->json->message = $this->translator->getText("Authorization rejected");

        return $this->json->save();
    }

    /**
     * Serializer JSON for acceptAuthorizationControlAuthorityRequest method
     *
     * @return type
     */
    public function acceptAuthorizationControlAuthorityRequest()
    {
        $this->json->message = $this->translator->getText("Authorization accepted");

        return $this->json->save();
    }

    /**
     * Serializer JSON for rejectAuthorizationControlAuthorityRequest method
     *
     * @return type
     */
    public function rejectAuthorizationControlAuthorityRequest()
    {
        $this->json->message = $this->translator->getText("Authorization rejected");

        return $this->json->save();
    }

696
697
698
699
700
701
702
703
704
705
706
707
    /**
     * Serializer JSON for acceptArchiveTransfer method
     *
     * @return type
     */
    public function retry()
    {
        $this->json->message = $this->translator->getText("Message restarted");

        return $this->json->save();
    }

Prosper De Laure's avatar
Prosper De Laure committed
708
709
710
711
712
713
714
715
716
717
718
719
720
721
    /**
     * Serializer JSON for invalid message exception
     * @param Exception $exception The exception
     *
     * @return object JSON object with a status
     */
    public function invalidMessageException($exception)
    {
        $this->json->status = false;
        $this->json->message = $this->translator->getText($exception->getFormat());
        $this->json->message = vsprintf($this->json->message, $exception->getVariables());

        if (count($exception->errors)) {
            foreach ($exception->errors as $error) {
Alexandre Morin's avatar
Alexandre Morin committed
722
723
724
725
                if (method_exists($error,'getFormat')) {
                    $format = $this->translator->getText($error->getFormat());
                    $error->setMessage(vsprintf($format, $error->getVariables()));
                }
Prosper De Laure's avatar
Prosper De Laure committed
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
            }
        }
        $this->json->errors = $exception->errors;

        return $this->json->save();
    }

    /**
     * Serializer JSON for invalid status exception
     * @param Exception $exception The exception
     *
     * @return object JSON object with a status
     */
    public function invalidStatusException(\Exception $exception)
    {
        $this->json->status = false;
        $this->json->message = $this->translator->getText($exception->getMessage());

        return $this->json->save();
    }

    /**
     * Serializer JSON for receive method
749
     * @param medona/message $ack The ack message object
Prosper De Laure's avatar
Prosper De Laure committed
750
751
752
     *
     * @return object JSON object with a status
     */
753
    public function receive($ack)
Prosper De Laure's avatar
Prosper De Laure committed
754
755
756
    {
        $this->json->status = true;
        $this->json->message = $this->translator->getText("Message received");
757
758
759
760
        $this->json->messageId = (string) $ack->receivedMessageId;
        if (isset($ack->xml)) {
            $this->json->acknowledgement = $ack->xml->saveXML();
        }
Prosper De Laure's avatar
Prosper De Laure committed
761
762
763
764

        return $this->json->save();
    }

765
    /**
Prosper De Laure's avatar
Prosper De Laure committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
     * Serializer JSON for validate method
     *
     * @return object JSON object with a status
     */
    public function validate()
    {
        $this->json->status = true;
        $this->json->message = $this->translator->getText("Message received");

        return $this->json->save();
    }

    /**
     * Load organization name
     * @param object $message            The message
     * @param object $messages           The message list
     * @param object $messageObject      The message object
     * @param string $registrationNumber The current user registration number
     */
    protected function setMessageActions($message, $messages, $messageObject, $registrationNumber)
    {
787

Prosper De Laure's avatar
Prosper De Laure committed
788
789
790
791
792
        if (in_array($message->recipientOrgRegNumber, $registrationNumber)) {
            $messageId = (string) $messageObject->messageId;

            switch ($messageObject->type) {
                case 'ArchiveTransfer':
793
                    if ($message->status == "received" || $message->status == "modified") {
794
                        if (!$message->isIncoming) {
795
                            $messageObject->acknowledgeButton = "/restitution/".$messageId."/Acknowledge";
796
797
                        }
                        $messageObject->validateButton = "/transferValidate/" . $messageId;
Alexandre Morin's avatar
Alexandre Morin committed
798
                        $messageObject->exportButton = "/medona/message/".$messageId."/Export";
Prosper De Laure's avatar
Prosper De Laure committed
799
800
801
                    } elseif ($message->status == "valid") {
                        $messageObject->acceptButton = "/transferAcceptance/".$messageId;
                        $messageObject->rejectButton = "/transferRejection/".$messageId;
Alexandre Morin's avatar
Alexandre Morin committed
802
                        $messageObject->exportButton = "/medona/message/".$messageId."/Export";
803
804
805
                    } elseif ($message->status == "toBeModified") {
                        $messageObject->rejectButton = "/transferRejection/".$messageId;
                        $messageObject->retryButton = "/medona/message/". $message->messageId . "/retry";
806
807
808
                        if ($message->schema === 'seda') {
                            $messageObject->modifyButton = "/transferModify/" . $message->messageId;
                        }
Prosper De Laure's avatar
Prosper De Laure committed
809
810
                    } elseif ($message->status == "accepted") {
                        $messageObject->processButton = "/transferProcess/".$messageId;
Alexandre Morin's avatar
Alexandre Morin committed
811
                        $messageObject->exportButton = "/medona/message/".$messageId."/Export";
812
813
814
815
816
                    } elseif ($message->status == "sent") {
                        $messageObject->exportButton = "/outgoingTransfer/".$messageId."/Export";
                    } elseif ($message->status == "downloaded") {
                        $messageObject->acknowledgeButton = "/outgoingTransfer/".$messageId."/Acknowledge";
                        $messageObject->rejectButton = "/outgoingTransfer/".$messageId."/Reject";
Alexandre Morin's avatar
Alexandre Morin committed
817
                        $messageObject->exportButton = "/medona/message/".$messageId."/Export";
818
819
                    } elseif ($message->status == "processing") {
                        $messageObject->retryButton = "/medona/message/". $message->messageId . "/retry";
Prosper De Laure's avatar
Prosper De Laure committed
820
821
822
                    }
                    break;

823
824
825
826
                case 'Acknowledgement':
                    $messageObject->exportButton = "/medona/message/".$messageId."/Export";
                    break;

Prosper De Laure's avatar
Prosper De Laure committed
827
828
                case 'ArchiveDeliveryRequest':
                    if (count($messages) == 1) {
Alexandre Morin's avatar
Alexandre Morin committed
829
                        if ($message->status === "sent") {
830
                            $messageObject->rejectButton = "/delivery/".$messageId."/Reject";
Alexandre Morin's avatar
Alexandre Morin committed
831
                            $messageObject->derogationButton = "/delivery/".$messageId."/Derogation";
Prosper De Laure's avatar
Prosper De Laure committed
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
                        }
                    }
                    break;

                case 'ArchiveDeliveryRequestReply':
                    if (count($messages) == 1) {
                        if ($message->status == "sent") {
                            $messageObject->exportButton = "/delivery/".$messageId."/Export";
                        }
                    }
                    break;

                case 'ArchiveRestitutionRequest':
                    if (count($messages) == 1) {
                        if ($message->status == "sent") {
                            $messageObject->rejectButton = "/restitutionRequest/".$messageId."/Reject";
848
                            $messageObject->acceptWarningButton = "/restitutionRequest/".$messageId."/Accept";
Prosper De Laure's avatar
Prosper De Laure committed
849
850
851
852
853
854
855
856
857
858
                        }
                    } elseif ($message->status == "accepted") {
                        $messageObject->processButton = "/restitutionRequest/".$messageId."/process";
                    }
                    break;

                case 'ArchiveRestitution':
                    if ($message->status == "sent") {
                        $messageObject->exportButton = "/restitution/".$messageId."/Export";
                    } elseif ($message->status == "received") {
859
                        $messageObject->rejectButton = "/restitution/".$messageId."/Reject";
Alexandre Morin's avatar
Alexandre Morin committed
860
                        $messageObject->acknowledgeButton = "/restitution/".$messageId."/Acknowledge";
861
862
                    } elseif ($message->status == "acknowledge") {
                        $messageObject->validationButton = "/restitution/".$messageId."/process";
Alexandre Morin's avatar
Alexandre Morin committed
863
                        $messageObject->exportButton = "/medona/message/".$messageId."/Export";
Prosper De Laure's avatar
Prosper De Laure committed
864
865
866
867
868
                    }
                    break;

                case 'ArchiveDestructionRequest':
                    if ($message->status == "accepted") {
869
870
                        $messageObject->validationButton = "/destructionRequest/".$messageId."/Accept";
                        //$messageObject->rejectButton = "/destructionRequest/".(string) $message->messageId."/Reject";
Prosper De Laure's avatar
Prosper De Laure committed
871
872
873
874
875
876
877
878
879
                    }
                    break;

                case 'AuthorizationOriginatingAgencyRequest':
                    $messageObject->acceptButton = "/authorizationOriginatingAgencyRequest/".(string) $message->messageId."/Accept";
                    $messageObject->rejectButton = "/authorizationOriginatingAgencyRequest/".(string) $message->messageId."/Reject";
                    break;

                case 'AuthorizationOriginatingAgencyRequestReply':
880
                    if (($messages[0]->status != "originator_authorization_wait" && $messages[0]->status != "control_authorization_wait") || ($message != end($messages))) {
Prosper De Laure's avatar
Prosper De Laure committed
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
                        break;
                    }

                    if (strtoupper($message->replyCode) == "000") {
                        if ($messages[0]->type == "ArchiveDeliveryRequest") {
                            $messageObject->acceptButton = "/deliveryRequest/".(string) $messages[0]->messageId."/Accept";
                            $messageObject->sendRequestAuth = "/authorizationControlAuthorityRequest/".(string) $messages[0]->messageId;
                        }

                        if ($messageObject[0]->type == "ArchiveDestructionRequest") {
                            $messageObject->acceptButton = "/destructionRequest/".(string) $messages[0]->messageId."/Accept";
                            $messageObject->sendRequestAuth = "/authorizationControlAuthorityRequest/".(string) $messages[0]->messageId;
                        }
                    } else {
                        if ($messages[0]->type == "ArchiveDeliveryRequest") {
                            $messageObject->rejectButton = "/deliveryRequest/".$messageId."/Reject";
                        }

                        if ($messages[0]->type == "ArchiveDestructionRequest") {
                            $messageObject->rejectButton = "/destructionRequest/".(string) $messages[0]->messageId."/Reject";
                        }
                    }
                    break;

                case 'AuthorizationControlAuthorityRequest':
                    $messageObject->acceptButton = "/authorizationControlAuthorityRequest/".(string) $message->messageId."/Accept";
                    $messageObject->rejectButton = "/authorizationControlAuthorityRequest/".(string) $message->messageId."/Reject";
                    break;

                case 'AuthorizationControlAuthorityRequestReply':
                    if (strtoupper($message->replyCode) == "000") {
                        if ($messages[0]->type == "ArchiveDeliveryRequest") {
                            $messageObject->acceptButton = "/deliveryRequest/".(string) $messages[0]->messageId."/Accept";
                        }
                    } else {
                        if ($messages[0]->type == "ArchiveDeliveryRequest") {
                            $messageObject->rejectButton = "/deliveryRequest/".(string) $messages[0]->messageId."/Reject";
                        }
                    }
                    break;
921
922
                /*
                */
Prosper De Laure's avatar
Prosper De Laure committed
923
924
925
926
927
928
929
930
931
932
            }
        } elseif (in_array($message->senderOrgRegNumber, $registrationNumber)) {
            $messageId = (string) $messageObject->messageId;

            switch ($messageObject->type) {
                case 'ArchiveRestitution':
                    if ($message->status == "acknowledge") {
                        $messageObject->validationButton = "/restitution/".$messageId."/process";
                    }
                    break;
933
934
935
936
937
                case 'ArchiveTransfer' :
                    if ($message->status == "acknowledge") {
                        $messageObject->validationButton = "/outgoingTransfer/".$messageId."/process";
                    }
                    break;
Prosper De Laure's avatar
Prosper De Laure committed
938
939
940
941
            }
        }
    }

942
    /**
Prosper De Laure's avatar
Prosper De Laure committed
943
944
945
946
947
948
949
950
     * Load organization name
     * @param object         $object
     * @param medona/message $message
     */
    protected function loadOrganizationName($object, $message)
    {
        $organization = null;

Alexandre Morin's avatar
Alexandre Morin committed
951
        if (!isset($object->identifier) && !isset($object->identification->value)) {
Prosper De Laure's avatar
Prosper De Laure committed
952
953
            return;
        }
954

955
956
957
958
        if (isset($object->identifier)) {
            $name = 'identifier';
        } else {
            $name = 'identification';
959
960
        }

961
962
963
964
965
966
967
968
969
970
        if ($object->{$name}->value == $message->recipientOrg->registrationNumber) {
            $organization = $message->recipientOrg;
        } elseif ($object->{$name}->value == $message->senderOrg->registrationNumber) {
            $organization = $message->senderOrg;
        } else {
            try {
                $organization = \laabs::callService('organization/organization/readByregnumber', $object->{$name}->value)->orgName;
            } catch (\Exception $e) {
                $organization = "Organisation inconnue (" . $object->{$name}->value . ")";
            }
971
        }
972

Alexandre Morin's avatar
Alexandre Morin committed
973
        return $organization;
Prosper De Laure's avatar
Prosper De Laure committed
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
    }

    /**
     * Get the message type and schema specific serializer
     * @param medona/message $message The message object
     * @param string         $format  The implementation format
     *
     * @return The parser
     */
    protected function getMessageTypeSerializer($message, $format = "html")
    {
        if (!isset($message->type)) {
            $message->type = $message->xml->documentElement->nodeName;
        }

        if (!isset($message->schema)) {
            $messageNamespace = $message->xml->documentElement->namespaceURI;
            if (!$messageSchema = \laabs::resolveXmlNamespace($messageNamespace)) {
                throw new \Exception('Unknown message namespace'.$messageNamespace);
            }
            $message->schema = $messageSchema;
        }

        $this->messageTypeSerializer = \laabs::newSerializer($message->schema.LAABS_URI_SEPARATOR.$message->type, $format);

        return $this->messageTypeSerializer;
    }
For faster browsing, not all history is shown. View entire blame