message.php 40.8 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
271
272
273
274
275
276
277

                    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
278
                }
Alexandre Morin's avatar
Alexandre Morin committed
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
            } 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;
                            }
                        }
                    }
                }
316
317
318
319
320
321
322
323
324
325
            } elseif($baseMessage->schema == 'seda') {
                if (isset($messageObject->archive)) {
                    foreach ($messageObject->archive as $archive) {
                        if (isset($archive->document)) {
                            foreach ($archive->document as $document) {
                                $document->integrity->hashAlgorithm = substr($document->integrity->algorithme, strrpos($document->integrity->algorithme, "#") + 1);
                            }
                        }
                    }
                }
Alexandre Morin's avatar
Alexandre Morin committed
326
            }
327
328
329
330
331
332
            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
333
                        
334
                        if ($dateInter->y != 0) {
Arnaud Pauget's avatar
Arnaud Pauget committed
335
336
337
338
339
340
                            if ($dateInter->y == 999999999) {
                                $toDisplay = "Unlimited";
                            } else {
                                $numberDuration = $dateInter->y;
                                $toDisplay = "Year(s)";
                            }
341
342
343
344
345
346
347
348
349
                        } 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;
350
351
352
                    }
                }
            }
Prosper De Laure's avatar
Prosper De Laure committed
353
354
355
356
            $messageObjects[] = $messageObject;
        }

        end($messageObjects)->last = true;
357
        $this->view->addContentFile($baseMessage->schema."/message/messageModal.html");
358
359

        $messageDiv = $this->view->getElementById('[?merge .messageId ?]');
Prosper De Laure's avatar
Prosper De Laure committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
        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);
                        }
                    }
                }
            }
375
376
377
        } /**else {
    $this->view->addContentFile($baseMessage->schema.'/message/message.html', $messageDiv);
    }*/
378

379
380
        if ($this->view->getElementsByClass("dataTable")->item(2)) {
            $dataTable = $this->view->getElementsByClass("dataTable")->item(2)->plugin['dataTable'];
Alexandre Morin's avatar
Alexandre Morin committed
381
382
383
            $dataTable->setPaginationType("full_numbers");
            $dataTable->setSorting(array(array(0, 'desc')));
        }
384
        
Prosper De Laure's avatar
Prosper De Laure committed
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
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
        $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();
    }
556

Prosper De Laure's avatar
Prosper De Laure committed
557
558
559
560
561
562
563
564
565
566
567
568
569
    /**
     * 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
570
     * Serializer JSON for derogationDeliveryRequest method
Prosper De Laure's avatar
Prosper De Laure committed
571
572
573
     *
     * @return type
     */
Alexandre Morin's avatar
Alexandre Morin committed
574
    public function derogationDeliveryRequest()
Prosper De Laure's avatar
Prosper De Laure committed
575
    {
Alexandre Morin's avatar
Alexandre Morin committed
576
        $this->json->message = $this->translator->getText("Message sent to derogation");
Prosper De Laure's avatar
Prosper De Laure committed
577
578
579
580
581
582
583
584
585

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

    /**
     * Serializer JSON for rejectArchiveTransfer method
     *
     * @return type
     */
Alexandre Morin's avatar
Alexandre Morin committed
586
    public function rejectDeliveryRequest()
Prosper De Laure's avatar
Prosper De Laure committed
587
588
589
590
591
592
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
    {
        $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();
    }

690
691
692
693
694
695
696
697
698
699
700
701
    /**
     * 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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
    /**
     * 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
716
717
718
719
                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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
            }
        }
        $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
743
     * @param medona/message $ack The ack message object
Prosper De Laure's avatar
Prosper De Laure committed
744
745
746
     *
     * @return object JSON object with a status
     */
747
    public function receive($ack)
Prosper De Laure's avatar
Prosper De Laure committed
748
749
750
    {
        $this->json->status = true;
        $this->json->message = $this->translator->getText("Message received");
751
752
753
754
        $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
755
756
757
758

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

759
    /**
Prosper De Laure's avatar
Prosper De Laure committed
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
     * 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)
    {
781

Prosper De Laure's avatar
Prosper De Laure committed
782
783
784
785
786
        if (in_array($message->recipientOrgRegNumber, $registrationNumber)) {
            $messageId = (string) $messageObject->messageId;

            switch ($messageObject->type) {
                case 'ArchiveTransfer':
787
                    if ($message->status == "received" || $message->status == "modified") {
788
                        if (!$message->isIncoming) {
789
                            $messageObject->acknowledgeButton = "/restitution/".$messageId."/Acknowledge";
790
791
                        }
                        $messageObject->validateButton = "/transferValidate/" . $messageId;
Alexandre Morin's avatar
Alexandre Morin committed
792
                        $messageObject->exportButton = "/medona/message/".$messageId."/Export";
Prosper De Laure's avatar
Prosper De Laure committed
793
794
795
                    } elseif ($message->status == "valid") {
                        $messageObject->acceptButton = "/transferAcceptance/".$messageId;
                        $messageObject->rejectButton = "/transferRejection/".$messageId;
Alexandre Morin's avatar
Alexandre Morin committed
796
                        $messageObject->exportButton = "/medona/message/".$messageId."/Export";
797
798
799
800
                    } elseif ($message->status == "toBeModified") {
                        $messageObject->rejectButton = "/transferRejection/".$messageId;
                        $messageObject->retryButton = "/medona/message/". $message->messageId . "/retry";
                        $messageObject->modifyButton = "/transferModify/". $message->messageId;
Prosper De Laure's avatar
Prosper De Laure committed
801
802
                    } elseif ($message->status == "accepted") {
                        $messageObject->processButton = "/transferProcess/".$messageId;
Alexandre Morin's avatar
Alexandre Morin committed
803
                        $messageObject->exportButton = "/medona/message/".$messageId."/Export";
804
805
806
807
808
                    } 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
809
                        $messageObject->exportButton = "/medona/message/".$messageId."/Export";
810
811
                    } elseif ($message->status == "processing") {
                        $messageObject->retryButton = "/medona/message/". $message->messageId . "/retry";
Prosper De Laure's avatar
Prosper De Laure committed
812
813
814
                    }
                    break;

815
816
817
818
                case 'Acknowledgement':
                    $messageObject->exportButton = "/medona/message/".$messageId."/Export";
                    break;

Prosper De Laure's avatar
Prosper De Laure committed
819
820
                case 'ArchiveDeliveryRequest':
                    if (count($messages) == 1) {
Alexandre Morin's avatar
Alexandre Morin committed
821
                        if ($message->status === "sent") {
822
                            $messageObject->rejectButton = "/delivery/".$messageId."/Reject";
Alexandre Morin's avatar
Alexandre Morin committed
823
                            $messageObject->derogationButton = "/delivery/".$messageId."/Derogation";
Prosper De Laure's avatar
Prosper De Laure committed
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
                        }
                    }
                    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";
840
                            $messageObject->acceptWarningButton = "/restitutionRequest/".$messageId."/Accept";
Prosper De Laure's avatar
Prosper De Laure committed
841
842
843
844
845
846
847
848
849
850
                        }
                    } 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") {
851
                        $messageObject->rejectButton = "/restitution/".$messageId."/Reject";
Alexandre Morin's avatar
Alexandre Morin committed
852
                        $messageObject->acknowledgeButton = "/restitution/".$messageId."/Acknowledge";
853
854
                    } elseif ($message->status == "acknowledge") {
                        $messageObject->validationButton = "/restitution/".$messageId."/process";
Alexandre Morin's avatar
Alexandre Morin committed
855
                        $messageObject->exportButton = "/medona/message/".$messageId."/Export";
Prosper De Laure's avatar
Prosper De Laure committed
856
857
858
859
860
                    }
                    break;

                case 'ArchiveDestructionRequest':
                    if ($message->status == "accepted") {
861
862
                        $messageObject->validationButton = "/destructionRequest/".$messageId."/Accept";
                        //$messageObject->rejectButton = "/destructionRequest/".(string) $message->messageId."/Reject";
Prosper De Laure's avatar
Prosper De Laure committed
863
864
865
866
867
868
869
870
871
                    }
                    break;

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

                case 'AuthorizationOriginatingAgencyRequestReply':
872
                    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
873
874
875
876
877
878
879
880
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
                        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;
913
914
                /*
                */
Prosper De Laure's avatar
Prosper De Laure committed
915
916
917
918
919
920
921
922
923
924
            }
        } 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;
925
926
927
928
929
                case 'ArchiveTransfer' :
                    if ($message->status == "acknowledge") {
                        $messageObject->validationButton = "/outgoingTransfer/".$messageId."/process";
                    }
                    break;
Prosper De Laure's avatar
Prosper De Laure committed
930
931
932
933
            }
        }
    }

934
    /**
Prosper De Laure's avatar
Prosper De Laure committed
935
936
937
938
939
940
941
942
     * Load organization name
     * @param object         $object
     * @param medona/message $message
     */
    protected function loadOrganizationName($object, $message)
    {
        $organization = null;

Alexandre Morin's avatar
Alexandre Morin committed
943
        if (!isset($object->identifier) && !isset($object->identification->value)) {
Prosper De Laure's avatar
Prosper De Laure committed
944
945
            return;
        }
946

947
948
949
950
        if (isset($object->identifier)) {
            $name = 'identifier';
        } else {
            $name = 'identification';
951
952
        }

953
954
955
956
957
958
959
960
961
962
        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 . ")";
            }
963
        }
964

Alexandre Morin's avatar
Alexandre Morin committed
965
        return $organization;
Prosper De Laure's avatar
Prosper De Laure committed
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
    }

    /**
     * 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;
    }

    /**
     * Show message list
996
997
     * @param array     $messages Array of message object
     * @param boolean   $history  Type of table (true => history and false => other)
Prosper De Laure's avatar
Prosper De Laure committed
998
999
1000
     *
     * @return string The view
     */
1001
    protected function prepareMesageList($messages, $history = null)
Prosper De Laure's avatar
Prosper De Laure committed
1002
1003
1004
1005
    {

        $dataTable = $this->view->getElementsByClass("dataTable")->item(0)->plugin['dataTable'];
        $dataTable->setPaginationType("full_numbers");
1006
1007
1008
1009
1010
1011
1012
1013

        if ($history) {
            $dataTable->setUnsortableColumns(6);
            $dataTable->setSorting(array(array(4, 'desc')));
        } else {
            $dataTable->setUnsortableColumns(5);
            $dataTable->setSorting(array(array(3, 'desc')));
        }
Prosper De Laure's avatar
Prosper De Laure committed
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073

        $this->view->translate();

        foreach ($messages as $message) {
            $message->status = $this->view->translator->getText($message->status, false, "medona/messages");
            $message->typeTranslate = $this->view->translator->getText($message->type, false, "medona/messages");
        }

        $this->loadSendingModal(false, false);
        $this->view->setSource("messages", $messages);
    }

    /**
     * load sending modal
     * @param type $showOrganization
     * @param type $showReference
     */
    protected function loadSendingModal($showOrganization = true, $showReference = true)
    {
        if ($showOrganization) {
            $orgController = \laabs::newController('organization/organization');

            $organizations = $orgController->index();
            $userOrg = $orgController->listMyOrg();
            $currentOrg = \laabs::getToken("ORGANIZATION");
            if ($currentOrg) {
                foreach ($userOrg as $key => $org) {
                    if ($currentOrg->orgId == $org->orgId) {
                        unset($userOrg[$key]);
                        break;
                    }
                }
            }
            if (count($userOrg) < 2) {
                $userOrg = null;
            }
            $this->view->setSource('organizations', $organizations);
            $this->view->setSource('currentOrg', $currentOrg);
            $this->view->setSource('userOrg', $userOrg);
        }

        $this->view->setSource('reference', $showReference);
    }

    /**
     * Get numbers of messages
     */
    protected function count($messagesCount)
    {
        foreach ($messagesCount as $key => $tab) {
            foreach ($tab as $list => $number) {
                if ($number == "0") {
                    $number = "";
                    $messagesCount[$key][$list] = "";
                }
            }
        }

        return $messagesCount;
    }
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083


    /**
     * Wrong archive infos exceptions
     * @param object $exception The exception
     *
     * @return string String serialized in JSON
     */
    public function notDisposableArchiveException($exception)
    {
1084
        $this->translator->setCatalog('recordsManagement/messages');
Cyril Vazquez's avatar
Cyril Vazquez committed
1085

1086
1087
1088
1089
1090
1091
        // Manage errors
        $this->json->status = false;
        $this->json->message = $this->translator->getText($exception->getMessage());

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