message.php 36.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        archiveAuthorizationTrait,
        archiveNotificationTrait,
        archiveRestitutionTrait,
        \presentation\maarchRM\Presenter\exceptions\exceptionTrait;

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

    /**
     * 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,
        \dependency\localisation\TranslatorInterface $translator
    ) {
        $this->view = $view;
        $this->sdoFactory = $sdoFactory;

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

        $this->translator = $translator;
        $this->translator->setCatalog('medona/messages');
    }

    /**
     * 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
105
            $owner = is_array($currentService->orgRoleCodes) && in_array('owner', $currentService->orgRoleCodes);
Prosper De Laure's avatar
Prosper De Laure committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

            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 = [];
174

Prosper De Laure's avatar
Prosper De Laure committed
175
        $messages = array($message);
Alexandre Morin's avatar
Alexandre Morin committed
176
        $baseMessage = reset($messages);
177
178
179
        if (isset($message->acknowledgement)) {
            $messages = array_merge($messages, [$message->acknowledgement]);
        }
Prosper De Laure's avatar
Prosper De Laure committed
180
181
182
183
184
185
186
187
188
189

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

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

        foreach ($messages as $message) {
190
191
192
            if (!isset($message->object)) {
                continue;
            }
Prosper De Laure's avatar
Prosper De Laure committed
193
194
            $messageObject = $message->object;
            $messageObject->type = $message->type;
195
            $messageObject->date = $message->date;
196
            $messageObject->schema = $message->schema;
Prosper De Laure's avatar
Prosper De Laure committed
197
198
199
            $messageObject->messageId = $message->messageId;
            $messageObject->xml = isset($message->xml);
            $messageObject->archived = $message->archived;
200
201
202
203
204
            $messageObject->status = $message->status;
            $messageObject->archivalAgreement = $message->archivalAgreementReference;
            $messageObject->reference = $message->reference;
            $messageObject->size = number_format($message->size, 0, '', ' ');

205
206
207
            $user = \laabs::callService('auth/userAccount/read_userAccountId_', $message->accountId);
            $messageObject->accountDisplayName = $user->displayName;
            $messageObject->accountName = $user->accountName;
208

209
210
211
            if (isset($message->lifeCycleEvent)) {
                $messageObject->lifeCycleEvent = $message->lifeCycleEvent;
            }
212

Prosper De Laure's avatar
Prosper De Laure committed
213
214
            // Load the organizations names
            if (isset($messageObject->archivalAgency)) {
Alexandre Morin's avatar
Alexandre Morin committed
215
                $messageObject->archivalAgency->name = $this->loadOrganizationName($messageObject->archivalAgency, $message);
Prosper De Laure's avatar
Prosper De Laure committed
216
217
            }
            if (isset($messageObject->transferringAgency)) {
Alexandre Morin's avatar
Alexandre Morin committed
218
                $messageObject->transferringAgency->name = $this->loadOrganizationName($messageObject->transferringAgency, $message);
Prosper De Laure's avatar
Prosper De Laure committed
219
220
            }
            if (isset($messageObject->controlAuthority)) {
Alexandre Morin's avatar
Alexandre Morin committed
221
                $messageObject->controlAuthority->name = $this->loadOrganizationName($messageObject->controlAuthority, $message);
Prosper De Laure's avatar
Prosper De Laure committed
222
223
            }
            if (isset($messageObject->originatingAgency)) {
Alexandre Morin's avatar
Alexandre Morin committed
224
                $messageObject->originatingAgency->name = $this->loadOrganizationName($messageObject->originatingAgency, $message);
Prosper De Laure's avatar
Prosper De Laure committed
225
226
            }
            if (isset($messageObject->requester)) {
Alexandre Morin's avatar
Alexandre Morin committed
227
                $messageObject->requester->name = $this->loadOrganizationName($messageObject->requester, $message);
Prosper De Laure's avatar
Prosper De Laure committed
228
            }
229
            if (isset($messageObject->sender)) {
Alexandre Morin's avatar
Alexandre Morin committed
230
                $messageObject->sender->name = $this->loadOrganizationName($messageObject->sender, $message);
231
232
            }
            if (isset($messageObject->receiver)) {
Alexandre Morin's avatar
Alexandre Morin committed
233
                $messageObject->receiver->name = $this->loadOrganizationName($messageObject->receiver, $message);
234
            }
Prosper De Laure's avatar
Prosper De Laure committed
235
236
237
238
239
240

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

241
242
243
244
            if ($message->status === 'error') {
                $messageObject->retryButton = "/medona/message/". $message->messageId . "/retry";
            }

245
            if ($baseMessage->schema == 'seda2' && isset($messageObject->dataObjectPackage)) {
Alexandre Morin's avatar
Alexandre Morin committed
246
247
248
249
                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
250
251
252
253
254
255
256
257

                    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
258
                }
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
            } elseif ($baseMessage->schema == 'recordsManagement') {
                $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;
                                $object->value = $value;

                                $descriptiveMetadata->descriptionObject[] = $object;
                            }
                        }
                    }
                }

Alexandre Morin's avatar
Alexandre Morin committed
286
            }
287

Prosper De Laure's avatar
Prosper De Laure committed
288
289
290
291
            $messageObjects[] = $messageObject;
        }

        end($messageObjects)->last = true;
292

293
        $this->view->addContentFile($baseMessage->schema."/message/messageModal.html");
294
295

        $messageDiv = $this->view->getElementById('[?merge .messageId ?]');
Prosper De Laure's avatar
Prosper De Laure committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
        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);
                        }
                    }
                }
            }
311
        }
312

313
314
        if ($secondTable = $this->view->getElementsByClass("dataTable")->item(2)) {
            $dataTable = $secondTable->plugin['dataTable'];
Alexandre Morin's avatar
Alexandre Morin committed
315
316
317
            $dataTable->setPaginationType("full_numbers");
            $dataTable->setSorting(array(array(0, 'desc')));
        }
Prosper De Laure's avatar
Prosper De Laure committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489

        $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();
    }
490

Prosper De Laure's avatar
Prosper De Laure committed
491
492
493
494
495
496
497
498
499
500
501
502
503
    /**
     * 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
504
     * Serializer JSON for derogationDeliveryRequest method
Prosper De Laure's avatar
Prosper De Laure committed
505
506
507
     *
     * @return type
     */
Alexandre Morin's avatar
Alexandre Morin committed
508
    public function derogationDeliveryRequest()
Prosper De Laure's avatar
Prosper De Laure committed
509
    {
Alexandre Morin's avatar
Alexandre Morin committed
510
        $this->json->message = $this->translator->getText("Message sent to derogation");
Prosper De Laure's avatar
Prosper De Laure committed
511
512
513
514
515
516
517
518
519

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

    /**
     * Serializer JSON for rejectArchiveTransfer method
     *
     * @return type
     */
Alexandre Morin's avatar
Alexandre Morin committed
520
    public function rejectDeliveryRequest()
Prosper De Laure's avatar
Prosper De Laure committed
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
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
    {
        $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();
    }

624
625
626
627
628
629
630
631
632
633
634
635
    /**
     * 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
    /**
     * 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
650
651
652
653
                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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
            }
        }
        $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
677
     * @param medona/message $ack The ack message object
Prosper De Laure's avatar
Prosper De Laure committed
678
679
680
     *
     * @return object JSON object with a status
     */
681
    public function receive($ack)
Prosper De Laure's avatar
Prosper De Laure committed
682
683
684
    {
        $this->json->status = true;
        $this->json->message = $this->translator->getText("Message received");
685
686
687
688
        $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
689
690
691
692

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

693
    /**
Prosper De Laure's avatar
Prosper De Laure committed
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
     * 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)
    {
715

Prosper De Laure's avatar
Prosper De Laure committed
716
717
718
719
720
721
        if (in_array($message->recipientOrgRegNumber, $registrationNumber)) {
            $messageId = (string) $messageObject->messageId;

            switch ($messageObject->type) {
                case 'ArchiveTransfer':
                    if ($message->status == "received") {
722
                        if (!$message->isIncoming) {
723
                            $messageObject->acknowledgeButton = "/restitution/".$messageId."/Acknowledge";
724
725
                        }
                        $messageObject->validateButton = "/transferValidate/" . $messageId;
Prosper De Laure's avatar
Prosper De Laure committed
726
727
728
729
730
731
732
733
                    } elseif ($message->status == "valid") {
                        $messageObject->acceptButton = "/transferAcceptance/".$messageId;
                        $messageObject->rejectButton = "/transferRejection/".$messageId;
                    } elseif ($message->status == "invalid") {
                        //$message->modifyButton = "".(string) $message->messageId;
                        //$message->rejectButton = "".(string) $message->messageId;
                    } elseif ($message->status == "accepted") {
                        $messageObject->processButton = "/transferProcess/".$messageId;
734
735
736
737
738
                    } elseif ($message->status == "sent") {
                        $messageObject->exportButton = "/outgoingTransfer/".$messageId."/Export";
                    } elseif ($message->status == "downloaded") {
                        $messageObject->acknowledgeButton = "/outgoingTransfer/".$messageId."/Acknowledge";
                        $messageObject->rejectButton = "/outgoingTransfer/".$messageId."/Reject";
739
740
                    } elseif ($message->status == "processing") {
                        $messageObject->retryButton = "/medona/message/". $message->messageId . "/retry";
Prosper De Laure's avatar
Prosper De Laure committed
741
742
743
                    }
                    break;

744
745
746
747
                case 'Acknowledgement':
                    $messageObject->exportButton = "/medona/message/".$messageId."/Export";
                    break;

Prosper De Laure's avatar
Prosper De Laure committed
748
749
                case 'ArchiveDeliveryRequest':
                    if (count($messages) == 1) {
Alexandre Morin's avatar
Alexandre Morin committed
750
751
752
                        if ($message->status === "sent") {
                            $messageObject->rejectButton = "/deliveryt/".$messageId."/Reject";
                            $messageObject->derogationButton = "/delivery/".$messageId."/Derogation";
Prosper De Laure's avatar
Prosper De Laure committed
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
                        }
                    }
                    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";
769
                            $messageObject->acceptWarningButton = "/restitutionRequest/".$messageId."/Accept";
Prosper De Laure's avatar
Prosper De Laure committed
770
771
772
773
774
775
776
777
778
779
                        }
                    } 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") {
780
                        $messageObject->rejectButton = "/restitution/".$messageId."/Reject";
Alexandre Morin's avatar
Alexandre Morin committed
781
                        $messageObject->acknowledgeButton = "/restitution/".$messageId."/Acknowledge";
782
783
                    } elseif ($message->status == "acknowledge") {
                        $messageObject->validationButton = "/restitution/".$messageId."/process";
Prosper De Laure's avatar
Prosper De Laure committed
784
785
786
787
788
                    }
                    break;

                case 'ArchiveDestructionRequest':
                    if ($message->status == "accepted") {
789
790
                        $messageObject->validationButton = "/destructionRequest/".$messageId."/Accept";
                        //$messageObject->rejectButton = "/destructionRequest/".(string) $message->messageId."/Reject";
Prosper De Laure's avatar
Prosper De Laure committed
791
792
793
794
795
796
797
798
799
                    }
                    break;

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

                case 'AuthorizationOriginatingAgencyRequestReply':
800
                    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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
                        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;
841
842
                /*
                */
Prosper De Laure's avatar
Prosper De Laure committed
843
844
845
846
847
848
849
850
851
852
            }
        } 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;
853
854
855
856
857
                case 'ArchiveTransfer' :
                    if ($message->status == "acknowledge") {
                        $messageObject->validationButton = "/outgoingTransfer/".$messageId."/process";
                    }
                    break;
Prosper De Laure's avatar
Prosper De Laure committed
858
859
860
861
            }
        }
    }

862
    /**
Prosper De Laure's avatar
Prosper De Laure committed
863
864
865
866
867
868
869
870
     * Load organization name
     * @param object         $object
     * @param medona/message $message
     */
    protected function loadOrganizationName($object, $message)
    {
        $organization = null;

Alexandre Morin's avatar
Alexandre Morin committed
871
        if (!isset($object->identifier) && !isset($object->identification->value)) {
Prosper De Laure's avatar
Prosper De Laure committed
872
873
            return;
        }
874

875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
        if (isset($object->identifier)) {
            $name = 'identifier';
        } else {
            $name = 'identification';
        }

        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 . ")";
            }
        }
892

Alexandre Morin's avatar
Alexandre Morin committed
893
        return $organization;
Prosper De Laure's avatar
Prosper De Laure committed
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
921
922
923
    }

    /**
     * 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
924
925
     * @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
926
927
928
     *
     * @return string The view
     */
929
    protected function prepareMesageList($messages, $history = null)
Prosper De Laure's avatar
Prosper De Laure committed
930
931
932
933
    {

        $dataTable = $this->view->getElementsByClass("dataTable")->item(0)->plugin['dataTable'];
        $dataTable->setPaginationType("full_numbers");
934
935
936
937
938
939
940
941

        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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
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
996
997
998
999
1000

        $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;
For faster browsing, not all history is shown. View entire blame