message.php 38.4 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,
Cyril Vazquez's avatar
Cyril Vazquez committed
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
82
83
84
85
86
87
88
89
90
91

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

    /**
     * Dashobord for messages
     */
    public function index()
    {
        $this->view->addContentFile("medona/message/menu.html");
        
        $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;
                            }
                        }
                    }
                }
Alexandre Morin's avatar
Alexandre Morin committed
316
            }
317

Prosper De Laure's avatar
Prosper De Laure committed
318
319
320
321
            $messageObjects[] = $messageObject;
        }

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

323
        $this->view->addContentFile($baseMessage->schema."/message/messageModal.html");
324
325

        $messageDiv = $this->view->getElementById('[?merge .messageId ?]');
Prosper De Laure's avatar
Prosper De Laure committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
        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);
                        }
                    }
                }
            }
341
342
343
        } /**else {
    $this->view->addContentFile($baseMessage->schema.'/message/message.html', $messageDiv);
    }*/
344

345
346
        if ($this->view->getElementsByClass("dataTable")->item(2)) {
            $dataTable = $this->view->getElementsByClass("dataTable")->item(2)->plugin['dataTable'];
Alexandre Morin's avatar
Alexandre Morin committed
347
348
349
            $dataTable->setPaginationType("full_numbers");
            $dataTable->setSorting(array(array(0, 'desc')));
        }
Prosper De Laure's avatar
Prosper De Laure committed
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
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

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

Prosper De Laure's avatar
Prosper De Laure committed
523
524
525
526
527
528
529
530
531
532
533
534
535
    /**
     * 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
536
     * Serializer JSON for derogationDeliveryRequest method
Prosper De Laure's avatar
Prosper De Laure committed
537
538
539
     *
     * @return type
     */
Alexandre Morin's avatar
Alexandre Morin committed
540
    public function derogationDeliveryRequest()
Prosper De Laure's avatar
Prosper De Laure committed
541
    {
Alexandre Morin's avatar
Alexandre Morin committed
542
        $this->json->message = $this->translator->getText("Message sent to derogation");
Prosper De Laure's avatar
Prosper De Laure committed
543
544
545
546
547
548
549
550
551

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

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

656
657
658
659
660
661
662
663
664
665
666
667
    /**
     * 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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
    /**
     * 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
682
683
684
685
                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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
            }
        }
        $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
709
     * @param medona/message $ack The ack message object
Prosper De Laure's avatar
Prosper De Laure committed
710
711
712
     *
     * @return object JSON object with a status
     */
713
    public function receive($ack)
Prosper De Laure's avatar
Prosper De Laure committed
714
715
716
    {
        $this->json->status = true;
        $this->json->message = $this->translator->getText("Message received");
717
718
719
720
        $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
721
722
723
724

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

725
    /**
Prosper De Laure's avatar
Prosper De Laure committed
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
     * 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)
    {
747

Prosper De Laure's avatar
Prosper De Laure committed
748
749
750
751
752
        if (in_array($message->recipientOrgRegNumber, $registrationNumber)) {
            $messageId = (string) $messageObject->messageId;

            switch ($messageObject->type) {
                case 'ArchiveTransfer':
753
                    if ($message->status == "received" || $message->status == "modified") {
754
                        if (!$message->isIncoming) {
755
                            $messageObject->acknowledgeButton = "/restitution/".$messageId."/Acknowledge";
756
757
                        }
                        $messageObject->validateButton = "/transferValidate/" . $messageId;
Prosper De Laure's avatar
Prosper De Laure committed
758
759
760
                    } elseif ($message->status == "valid") {
                        $messageObject->acceptButton = "/transferAcceptance/".$messageId;
                        $messageObject->rejectButton = "/transferRejection/".$messageId;
761
762
763
764
                    } 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
765
766
                    } elseif ($message->status == "accepted") {
                        $messageObject->processButton = "/transferProcess/".$messageId;
767
768
769
770
771
                    } elseif ($message->status == "sent") {
                        $messageObject->exportButton = "/outgoingTransfer/".$messageId."/Export";
                    } elseif ($message->status == "downloaded") {
                        $messageObject->acknowledgeButton = "/outgoingTransfer/".$messageId."/Acknowledge";
                        $messageObject->rejectButton = "/outgoingTransfer/".$messageId."/Reject";
772
773
                    } elseif ($message->status == "processing") {
                        $messageObject->retryButton = "/medona/message/". $message->messageId . "/retry";
Prosper De Laure's avatar
Prosper De Laure committed
774
775
776
                    }
                    break;

777
778
779
780
                case 'Acknowledgement':
                    $messageObject->exportButton = "/medona/message/".$messageId."/Export";
                    break;

Prosper De Laure's avatar
Prosper De Laure committed
781
782
                case 'ArchiveDeliveryRequest':
                    if (count($messages) == 1) {
Alexandre Morin's avatar
Alexandre Morin committed
783
                        if ($message->status === "sent") {
784
                            $messageObject->rejectButton = "/delivery/".$messageId."/Reject";
Alexandre Morin's avatar
Alexandre Morin committed
785
                            $messageObject->derogationButton = "/delivery/".$messageId."/Derogation";
Prosper De Laure's avatar
Prosper De Laure committed
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
                        }
                    }
                    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";
802
                            $messageObject->acceptWarningButton = "/restitutionRequest/".$messageId."/Accept";
Prosper De Laure's avatar
Prosper De Laure committed
803
804
805
806
807
808
809
810
811
812
                        }
                    } 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") {
813
                        $messageObject->rejectButton = "/restitution/".$messageId."/Reject";
Alexandre Morin's avatar
Alexandre Morin committed
814
                        $messageObject->acknowledgeButton = "/restitution/".$messageId."/Acknowledge";
815
816
                    } elseif ($message->status == "acknowledge") {
                        $messageObject->validationButton = "/restitution/".$messageId."/process";
Prosper De Laure's avatar
Prosper De Laure committed
817
818
819
820
821
                    }
                    break;

                case 'ArchiveDestructionRequest':
                    if ($message->status == "accepted") {
822
823
                        $messageObject->validationButton = "/destructionRequest/".$messageId."/Accept";
                        //$messageObject->rejectButton = "/destructionRequest/".(string) $message->messageId."/Reject";
Prosper De Laure's avatar
Prosper De Laure committed
824
825
826
827
828
829
830
831
832
                    }
                    break;

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

                case 'AuthorizationOriginatingAgencyRequestReply':
833
                    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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
                        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;
874
875
                /*
                */
Prosper De Laure's avatar
Prosper De Laure committed
876
877
878
879
880
881
882
883
884
885
            }
        } 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;
886
887
888
889
890
                case 'ArchiveTransfer' :
                    if ($message->status == "acknowledge") {
                        $messageObject->validationButton = "/outgoingTransfer/".$messageId."/process";
                    }
                    break;
Prosper De Laure's avatar
Prosper De Laure committed
891
892
893
894
            }
        }
    }

895
    /**
Prosper De Laure's avatar
Prosper De Laure committed
896
897
898
899
900
901
902
903
     * Load organization name
     * @param object         $object
     * @param medona/message $message
     */
    protected function loadOrganizationName($object, $message)
    {
        $organization = null;

Alexandre Morin's avatar
Alexandre Morin committed
904
        if (!isset($object->identifier) && !isset($object->identification->value)) {
Prosper De Laure's avatar
Prosper De Laure committed
905
906
            return;
        }
907

908
909
910
911
        if (isset($object->identifier)) {
            $name = 'identifier';
        } else {
            $name = 'identification';
912
913
        }

914
915
916
917
918
919
920
921
922
923
        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 . ")";
            }
924
        }
925

Alexandre Morin's avatar
Alexandre Morin committed
926
        return $organization;
Prosper De Laure's avatar
Prosper De Laure committed
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
    }

    /**
     * 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
957
958
     * @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
959
960
961
     *
     * @return string The view
     */
962
    protected function prepareMesageList($messages, $history = null)
Prosper De Laure's avatar
Prosper De Laure committed
963
964
965
966
    {

        $dataTable = $this->view->getElementsByClass("dataTable")->item(0)->plugin['dataTable'];
        $dataTable->setPaginationType("full_numbers");
967
968
969
970
971
972
973
974

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