archive.php 47 KB
Newer Older
Prosper De Laure's avatar
Prosper De Laure committed
1
2
3
<?php

/*
4
 * Copyright (C) 2018 Maarch
Prosper De Laure's avatar
Prosper De Laure committed
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
 *
 * This file is part of bundle recordsManagement.
 *
 * Bundle recordsManagement 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 recordsManagement 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 recordsManagement.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace presentation\maarchRM\Presenter\recordsManagement;

/**
 * archive html serializer
 *
 * @package RecordsManagement
 * @author  Alexis Ragot <alexis.ragot@maarch.org>
 */
class archive
{
32
    use \presentation\maarchRM\Presenter\exceptions\exceptionTrait;
Prosper De Laure's avatar
Prosper De Laure committed
33
34
35
36

    public $view;
    protected $json;
    protected $translator;
Alexandre Morin's avatar
Alexandre Morin committed
37
    protected $archivalProfileController;
Prosper De Laure's avatar
Prosper De Laure committed
38
39
40
41
42
43
44
45
46
47

    /**
     * Constuctor
     * @param \dependency\html\Document                    $view
     * @param \dependency\json\JsonObject                  $json
     * @param \dependency\localisation\TranslatorInterface $translator
     */
    public function __construct(
        \dependency\html\Document $view,
        \dependency\json\JsonObject $json,
48
        \dependency\localisation\TranslatorInterface $translator
49
50
    )
    {
Prosper De Laure's avatar
Prosper De Laure committed
51
52
53
54
55
56
57
        $this->view = $view;

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

        $this->translator = $translator;
        $this->translator->setCatalog('recordsManagement/messages');
Alexandre Morin's avatar
Alexandre Morin committed
58
59

        $this->archivalProfileController = \laabs::newController("recordsManagement/archivalProfile");
60
61

        $this->transaction = isset(\laabs::configuration("medona")["transaction"]) && \laabs::configuration("medona")["transaction"];
Prosper De Laure's avatar
Prosper De Laure committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    }

    /**
     * get a form to search resource
     * @param array $profiles Array of profile
     *
     * @return string
     */
    public function searchForm($profiles)
    {
        $currentService = \laabs::getToken("ORGANIZATION");

        $ownerOriginatorOrgs = [];

        if (!$currentService) {
            $emptyRole = true;
        } else {
            $emptyRole = false;
            $ownerOriginatorOrgs = $this->getOwnerOriginatorsOrgs($currentService);
        }

Alexandre Morin's avatar
Alexandre Morin committed
83
84
        $retentionRuleController = \laabs::newController('recordsManagement/retentionRule');
        $retentionRules = $retentionRuleController->index();
85

Prosper De Laure's avatar
Prosper De Laure committed
86
87
88
        $this->view->addContentFile("recordsManagement/archive/search.html");

        $this->view->translate();
jerome's avatar
jerome committed
89

90
        usort($profiles, array($this, "compareProfiles"));
91
92
93
94
95

        $deleteDescription = true;
        if (isset(\laabs::configuration("recordsManagement")['deleteDescription'])) {
            $deleteDescription = (bool) \laabs::configuration("recordsManagement")['deleteDescription'];
        }
Prosper De Laure's avatar
Prosper De Laure committed
96

97
        $this->view->setSource("retentionRules", $retentionRules);
Prosper De Laure's avatar
Prosper De Laure committed
98
99
100
        $this->view->setSource("emptyRole", $emptyRole);
        $this->view->setSource("profiles", $profiles);
        $this->view->setSource("organizationsOriginator", $ownerOriginatorOrgs);
101
        $this->view->setSource("deleteDescription", $deleteDescription);
Prosper De Laure's avatar
Prosper De Laure committed
102
103
104
105
106
107

        $this->view->merge();

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

108
109
110
111
112
    private function compareProfiles($a, $b)
    {
        return \laabs::alphabeticalSort($a, $b, "name");
    }

Prosper De Laure's avatar
Prosper De Laure committed
113
114
115
116
117
118
119
120
121
122
123
124
125
    /**
     * get archives with information
     * @param array $archives Array of archive object
     *
     * @return string
     */
    public function search($archives)
    {
        $this->view->addContentFile("recordsManagement/archive/resultList.html");

        $this->view->translate();

        //access code selector
Alexandre Morin's avatar
Alexandre Morin committed
126
127
        $accessRuleController = \laabs::newController('recordsManagement/accessRule');
        $accessRules = $accessRuleController->index();
Prosper De Laure's avatar
Prosper De Laure committed
128
129
130
131
132
133
134
135
        foreach ($accessRules as $accessRule) {
            $accessRule->json = json_encode($accessRule);
            if ($accessRule->duration != null) {
                $accessRule->accessRuleDurationUnit = substr($accessRule->duration, -1);
                $accessRule->accessRuleDuration = substr($accessRule->duration, 1, -1);
            }
        }

Dylan's avatar
Dylan committed
136
         //retention code selector
Alexandre Morin's avatar
Alexandre Morin committed
137
138
        $retentionRuleController = \laabs::newController('recordsManagement/retentionRule');
        $retentionRules = $retentionRuleController->index();
Dylan's avatar
Dylan committed
139
140
141
142
143
144
145
146
        foreach ($retentionRules as $retentionRule) {
            $retentionRule->json = json_encode($retentionRule);
            if ($retentionRule->duration != null) {
                $retentionRule->retentionRuleDurationUnit = substr($retentionRule->duration, -1);
                $retentionRule->retentionRuleDuration = substr($retentionRule->duration, 1, -1);
            }
        }

Alexandre Morin's avatar
Alexandre Morin committed
147
        $orgController = \laabs::newController('organization/organization');
148
        $archiveController = \laabs::newController('recordsManagement/archive');
Alexandre Morin's avatar
Alexandre Morin committed
149
        $orgsByRegNumber = $orgController->orgList();
Prosper De Laure's avatar
Prosper De Laure committed
150
151
152

        $currentDate = \laabs::newDate();
        foreach ($archives as $archive) {
Alexandre Morin's avatar
Alexandre Morin committed
153
154
155
156
157
158
159
160
161
162
            $archive->finalDispositionDesc = $this->view->translator->getText(
                $archive->finalDisposition,
                false,
                "recordsManagement/messages"
            );
            $archive->statusDesc = $this->view->translator->getText(
                $archive->status,
                false,
                "recordsManagement/messages"
            );
Prosper De Laure's avatar
Prosper De Laure committed
163
164
165
166
167

            if (!empty($archive->disposalDate) && $archive->disposalDate <= $currentDate) {
                $archive->disposable = true;
            }

Alexandre Morin's avatar
Alexandre Morin committed
168
169
170
            if (empty($archive->disposalDate)
                && (empty($archive->retentionRuleCode) || empty($archive->retentionDuration))
            ) {
171
172
173
                $archive->noRetention = true;
            }

Prosper De Laure's avatar
Prosper De Laure committed
174
175
            if (isset($orgsByRegNumber[$archive->originatorOrgRegNumber])) {
                $archive->originatorOrgName = $orgsByRegNumber[$archive->originatorOrgRegNumber]->displayName;
176
177

                try {
Alexandre Morin's avatar
Alexandre Morin committed
178
179
180
181
182
                    if ($archive->status == 'disposed'
                        || $archive->status == 'error'
                        || $archive->status == 'restituted'
                        || $archive->status == 'transfered'
                    ) {
183
184
185
186
                        $archive->hasRights = false;
                    } else {
                        $archive->hasRights = $archiveController->checkRights($archive);
                    }
Alexandre Morin's avatar
Alexandre Morin committed
187
                } catch (\Exception $e) {
188
189
                    $archive->hasRights = false;
                }
Prosper De Laure's avatar
Prosper De Laure committed
190
            }
191
192
193
194
195
196
197
198

            $archive->isCommunicable = '2';
            if ($archive->accessRuleComDate) {
                $communicationDelay = $archive->accessRuleComDate->diff(\laabs::newTimestamp());
                if ($communicationDelay->invert != 0) {
                    $archive->isCommunicable = '1';
                }
            }
Prosper De Laure's avatar
Prosper De Laure committed
199
        }
Alexandre Morin's avatar
Alexandre Morin committed
200

Prosper De Laure's avatar
Prosper De Laure committed
201
202
203
        $dataTable = $this->view->getElementsByClass("dataTable")->item(0)->plugin['dataTable'];
        $dataTable->setPaginationType("full_numbers");

204
205
        $dataTable->setUnsortableColumns(8);
        $dataTable->setUnsearchableColumns(8);
Prosper De Laure's avatar
Prosper De Laure committed
206
207
208

        $dataTable->setUnsortableColumns(0);
        $dataTable->setUnsearchableColumns(0);
Arnaud Pauget's avatar
Arnaud Pauget committed
209
        $dataTable->setSorting(array(array(5, 'desc')));
Alexandre Morin's avatar
Alexandre Morin committed
210

Prosper De Laure's avatar
Prosper De Laure committed
211
212
213
        $this->readPrivilegesOnArchives();

        $this->view->setSource("accessRules", $accessRules);
Dylan's avatar
Dylan committed
214
        $this->view->setSource("retentionRules", $retentionRules);
Prosper De Laure's avatar
Prosper De Laure committed
215
        $this->view->setSource('archive', $archives);
216
        $this->view->setSource('transaction', $this->transaction);
Prosper De Laure's avatar
Prosper De Laure committed
217
218
219
220
221
222
        $this->view->merge();

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

    /**
223
     * Get resource contents
224
     * @param digitalResource/digitalResource $digitalResource The resource object
Prosper De Laure's avatar
Prosper De Laure committed
225
226
227
     *
     * @return string
     */
228
    public function getContents($digitalResource = null)
Prosper De Laure's avatar
Prosper De Laure committed
229
    {
230
        if (!$digitalResource) {
Prosper De Laure's avatar
Prosper De Laure committed
231
232
            // @TODO : throw exception
            $contents = "<h4>This archive does not have any document.</h4>";
233
234
235
            \laabs::setResponseType('text/html');

            return $contents;
Prosper De Laure's avatar
Prosper De Laure committed
236
        }
237

238
        $contents = base64_decode($digitalResource->attachment->data);
239
240
241
242
        $mimetype = $digitalResource->mimetype;

        \laabs::setResponseType($mimetype);
        $response = \laabs::kernel()->response;
243
        $response->setHeader("Content-Disposition", "inline; filename=".$digitalResource->attachment->filename."");
244
245

        return $contents;
246
    }
Prosper De Laure's avatar
Prosper De Laure committed
247

248
249
250
251
252
253
254
255
256
257
    /**
     * Get archive description
     * @param archive $archive
     *
     * @return string
     */
    public function getArchiveDetails($archive)
    {
        $this->view->addContentFile("recordsManagement/archive/archiveInfo/archiveInfo.html");

Alexandre Goldstein's avatar
Alexandre Goldstein committed
258
        // Management metadata
259
260
261
        $this->setManagementMetadatas($archive);

        // Descriptive metadata
262
        $this->setDescriptiveMetadatas($archive);
263
264
265
266

        // Relationships
        $this->setArchiveRelationships($archive);

Cyril Vazquez's avatar
Cyril Vazquez committed
267
268
269
        // Message
        $this->checkMessage($archive);

270
271
        $this->view->setSource("archive", $archive);

272
273
274
275
        $messageDataTable = $this->view->getElementById('messageTable')->plugin['dataTable'];
        $messageDataTable->setPaginationType("full_numbers");
        $messageDataTable->setSorting(array(array(0, 'desc')));

Arnaud Pauget's avatar
Arnaud Pauget committed
276
277
278
        $lifeCycleDataTable = $this->view->getElementById('lifeCycleTable')->plugin['dataTable'];
        $lifeCycleDataTable->setPaginationType("full_numbers");
        $lifeCycleDataTable->setSorting(array(array(0, 'desc')));
279

280
281
        $this->view->translate();
        $this->view->merge();
282

283
284
285
286
        return $this->view->saveHtml();
    }


Alexis Ragot's avatar
Alexis Ragot committed
287
288
289
290
291
292
293
294
    /**
     * Get archive description
     * @param archive $archive
     *
     * @return string
     */
    public function getArchiveInfo($archive)
    {
295
296
297
298
299
        if(\laabs::kernel()->request->getHeader("Accept")=="application/json") {
            \laabs::setResponseHeader('Content-Type', "application/json");
            return json_encode($archive);
        }

Alexis Ragot's avatar
Alexis Ragot committed
300
301
        $this->view->addContentFile('dashboard/mainScreen/archiveInformation.html');

302
303
        // Digital resources
        $this->setDigitalResources($archive);
Alexis Ragot's avatar
Alexis Ragot committed
304

305
        // Contents
Alexis Ragot's avatar
Alexis Ragot committed
306
307
308
309
310
311
        $this->setArchiveTree($archive);

        // Managment metadata
        $this->setManagementMetadatas($archive);

        // Descriptive metadata
312
        $this->setDescriptiveMetadatas($archive);
Alexis Ragot's avatar
Alexis Ragot committed
313
314
315
316
317
318
319
320
321
322
323
324
325

        $this->getChildrenArchivesProfiles($archive);

        $this->view->setSource("archive", $archive);
        $this->view->setSource("archivalProfileList", $archive->archivalProfileList);
        $this->view->setSource("acceptArchiveWithoutProfile", $archive->acceptArchiveWithoutProfile);
        $this->view->setSource("acceptUserIndex", $archive->acceptUserIndex);

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

Prosper De Laure's avatar
Prosper De Laure committed
326
327
328
329
330
331
    /**
     * Get archive description
     * @param archive $archive
     *
     * @return string
     */
Alexandre Morin's avatar
Alexandre Morin committed
332
    public function getDescription($archive)
Prosper De Laure's avatar
Prosper De Laure committed
333
    {
Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
334
        $this->view->addContentFile("recordsManagement/archive/description.html");
335

336
337
338
339
        // Digital resources
        $this->setDigitalResources($archive);

        // Contents
340
        $this->setArchiveTree($archive);
341

342
343
        // Managment metadata
        $this->setManagementMetadatas($archive);
344

345
        // Descriptive metadata
346
        $this->setDescriptiveMetadatas($archive);
Prosper De Laure's avatar
Prosper De Laure committed
347

348
349
        // Relationships
        $this->setArchiveRelationships($archive);
Prosper De Laure's avatar
Prosper De Laure committed
350

351
352
        // Message
        $this->checkMessage($archive);
353
354
355

        $currentService = \laabs::getToken("ORGANIZATION");

356
        $canDeleteResource = $canAddResource = false ;
357

358
        if (($currentService->registrationNumber == $archive->archiverOrgRegNumber || in_array("owner", $currentService->orgRoleCodes))
359
            && \laabs::callService('auth/userAccount/readHasprivilege', "archiveManagement/addResource")
Alexandre Morin's avatar
Alexandre Morin committed
360
            && $archive->status === 'preserved') {
361
            $canDeleteResource = $canAddResource = true ;
362
        }
363

364
365
        $this->view->setSource("canDeleteResource", $canDeleteResource);
        $this->view->setSource("canAddResource", $canAddResource);
Alexandre Morin's avatar
Alexandre Morin committed
366
        $this->view->setSource("archive", $archive);
Prosper De Laure's avatar
Prosper De Laure committed
367

Alexandre Morin's avatar
Alexandre Morin committed
368
369
        $this->view->translate();
        $this->view->merge();
370

Alexandre Morin's avatar
Alexandre Morin committed
371
        return $this->view->saveHtml();
Prosper De Laure's avatar
Prosper De Laure committed
372
    }
373

Alexandre Morin's avatar
Alexandre Morin committed
374
375
    /**
     * Get metadata to edit
Alexandre Morin's avatar
Alexandre Morin committed
376
     * @param archive $archive
Alexandre Morin's avatar
Alexandre Morin committed
377
378
379
380
     *
     * @return string
     */
    public function getEditMetadata($archive)
Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
381
    {
Alexandre Morin's avatar
Alexandre Morin committed
382
        $languageCodes = \laabs::callService("seda/archiveTransferComposition/readLanguageCodes");
383

Alexandre Morin's avatar
Alexandre Morin committed
384
385
386
387
388
389
390
        foreach ($languageCodes as $languageCode) {
            $languageCode->title =  ucfirst($languageCode->French);

            if ($languageCode->alpha3t) {
                $languageCode->value = $languageCode->alpha3t;
            } else {
                $languageCode->value = $languageCode->alpha3b;
391
392
393
            }
        }

394
395
        if (!empty($archive->descriptionClass) && $presenter = $this->getPresenter($archive->descriptionClass)) {
            $archive = $presenter->getEditMetadata($archive, $languageCodes);
396
397
        }

Alexandre Morin's avatar
Alexandre Morin committed
398
399
400
401
        if ($archive->descriptionClass != "archivesPubliques/content") {
            $this->view->addContentFile($archive->descriptionClass . "/metadata.html");
        } else {
            $this->view->addContentFile("archivesPubliques/contentDescription/metadata.html");
402
403
        }

Alexandre Morin's avatar
Alexandre Morin committed
404
405
        if ($archive->descriptionObject) {
            $archive->descriptionObject = $archive->descriptionObject[0];
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
        $thesaurusNames = [
            "corpname",
            "famname",
            "geogname",
            "name",
            "occupation",
            "persname",
            "subject",
            "genreform",
            "function"
            ];

        $thesaurusList = new \stdClass();
        // Set default thesaurus
        $thesaurusList->subject = "T1";
        $thesaurusList->genreform = "T3";
        $thesaurusList->function = "T2";

        $conf = \laabs::Configuration()['recordsManagement'];
        
        if (isset($conf['refDirectory']) || is_dir($conf['refDirectory'])) {
            $refDirectory = $conf['refDirectory'];
            foreach ($thesaurusNames as $thesaurusName) {
431
                if (glob($conf['refDirectory'].'/'.$thesaurusName.'.*')) {
432
433
434
435
436
437
                    $thesaurusList->$thesaurusName = $thesaurusName;
                }
            }
        }

        $this->view->setSource('thesaurus', $thesaurusList);
Alexandre Morin's avatar
Alexandre Morin committed
438
439
440
441
442
443
444
        $this->view->setSource('languageCodes', $languageCodes);
        $this->view->setSource("archive", $archive);

        $this->view->merge();
        $this->view->translate();

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

Prosper De Laure's avatar
Prosper De Laure committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
    /**
     * Serializer html of verifyIntegrity method
     * @param recordsManagement/archive[] $archives
     *
     * @return type
     */
    public function verifyIntegrity($archives)
    {
        $this->view->addContentFile("recordsManagement/archive/modalIntegrity.html");
        $archives['count'] = count($archives['success']) + count($archives['error']);

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

        $this->view->translate();
        $this->view->merge();

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

    /**
     * Access denied exception
     * @param Exception $exception The exception
     *
     * @return string
     */
    public function accessDeniedException($exception)
    {
        $this->view->addContentFile("recordsManagement/archive/accessDenied.html");

        $this->view->translate();

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

    /**
     * Access denied exception
     * @param Exception $exception The exception
     *
     * @return string
     */
    public function clusterException($exception)
    {
        $this->view->addContentFile("recordsManagement/archive/clusterException.html");

        $this->view->translate();

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

    /**
     * No orgUnit exception
     *
     * @return string
     */
    public function noOrgUnit()
    {
        //$this->view->addHeaders();
        $this->view->addContentFile("recordsManagement/archive/noOrgUnit.html");

        $this->view->translate();

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

    /**
     * resourceUnavailableException
     * @param object $exception
     *
     * @return string
     */
    public function resourceUnavailableException($exception)
    {
        $this->view->addContentFile("recordsManagement/archive/resourceUnavailable.html");

        $this->view->translate();


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

    //JSON

    /**
     * Return archive with his retention rule
     * @param recordsManagement/archiveRetentionRule $retentionRule
     *
     * @return string
     */
    public function editArchiveRetentionRule($retentionRule)
    {
        $this->json->retentionRule = $retentionRule;
        $this->json->retentionRule->startDate = (string) $this->json->retentionRule->retentionStartDate;
        unset($this->json->retentionRule->retentionStartDate);

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

    /**
     * Return archive with his access rule
     * @param recordsManagement/archiveAccessRule $accessRule
     *
     * @return string
     */
    public function editArchiveAccessRule($accessRule)
    {
        $this->json->accessRule = $accessRule;
        $this->json->accessRule->startDate = (string) $this->json->accessRule->accessRuleStartDate;
        unset($this->json->accessRule->accessRuleStartDate);

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

    /**
     * Serializer JSON for modification method
     * @param recordsManagement/archiveRetentionRule $result The new retention rule
     *
     * @return object JSON object with a status and message parameters
     */
    public function modifyRetentionRule($result)
    {
        $success = count($result['success']);
        $echec = count($result['error']);

        $this->json->message = '%1$s archive(s) modified.';
        $this->json->message = $this->translator->getText($this->json->message);
        $this->json->message = sprintf($this->json->message, $success);

        if ($echec > 0) {
            $message = '%1$s archive(s) can not be modified.';
            $message = $this->translator->getText($message);
            $message = sprintf($message, $echec);

            $this->json->message .= ' '.$message;
        }

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

    /**
     * Serializer JSON for modification method
     * @param recordsManagement/archiveAccessRule $result The new retention rule
     *
     * @return object JSON object with a status and message parameters
     */
    public function modifyAccessRule($result)
    {
        $success = count($result['success']);
        $echec = count($result['error']);

        $this->json->message = '%1$s archive(s) modified.';
        $this->json->message = $this->translator->getText($this->json->message);
        $this->json->message = sprintf($this->json->message, $success);

        if ($echec > 0) {
            $message = '%1$s archive(s) can not be modified.';
            $message = $this->translator->getText($message);
            $message = sprintf($message, $echec);

            $this->json->message .= ' '.$message;
        }

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

    /**
     * Serializer JSON for freeze method
     * @param array $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function freeze($result)
    {
        $success = count($result['success']);
        $echec = count($result['error']);

        $this->json->message = '%1$s archive(s) freezed.';
        $this->json->message = $this->translator->getText($this->json->message);
        $this->json->message = sprintf($this->json->message, $success);

        if ($success == 0) {
            $this->json->status = false;
        }

        if ($echec > 0) {
            $message = '%1$s archive(s) can not be freezed.';
            $message = $this->translator->getText($message);
            $message = sprintf($message, $echec);

            $this->json->message .= ' '.$message;
        }

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

    /**
     * Serializer JSON for unfreeze method
     * @param array $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function unfreeze($result)
    {
        $success = count($result['success']);
        $echec = count($result['error']);

        $this->json->message = '%1$s archive(s) unfreezed.';
        $this->json->message = $this->translator->getText($this->json->message);
        $this->json->message = sprintf($this->json->message, $success);

        if ($success == 0) {
            $this->json->status = false;
        }

        if ($echec > 0) {
            $message = '%1$s archive(s) can not be unfreezed.';
            $message = $this->translator->getText($message);
            $message = sprintf($message, $echec);

            $this->json->message .= ' '.$message;
        }

        return $this->json->save();
    }
jerome's avatar
jerome committed
670

671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
    /**
     * Serializer JSON for metadata method
     * @param array $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function metadata($result)
    {
        if ($result) {
             $this->json->message = 'Archive updated';
        } else {
             $this->json->message = 'Archive not updated';
        }

        $this->json->message = $this->translator->getText($this->json->message);
Prosper De Laure's avatar
Prosper De Laure committed
686

687
688
        return $this->json->save();
    }
jerome's avatar
jerome committed
689

Prosper De Laure's avatar
Prosper De Laure committed
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
    /**
     * Return new digital resource for an archive
     * @param digitalResource/digitalResource $digitalResource
     *
     * @return string
     */
    public function newDigitalResource($digitalResource)
    {
        $this->json->digitalResource = $digitalResource;

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

    /**
     * Serializer JSON for conversion method
     * @param array $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function convert($result)
    {
711
712
713
        if ($result == false) {
            $count = 0;
        } else {
Alexandre Goldstein's avatar
Alexandre Goldstein committed
714
            $count = 1;
715
        }
Prosper De Laure's avatar
Prosper De Laure committed
716
717
        $this->json->message = '%1$s document(s) converted.';
        $this->json->message = $this->translator->getText($this->json->message);
718
719
        $this->json->message = sprintf($this->json->message, $count);
        $this->json->result = $result;
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784

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

    /**
     * Serializer JSON for validateRestitution method
     * @param array $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function validateRestitution($result)
    {
        $success = count($result['success']);
        $echec = count($result['error']);

        $this->json->message = '%1$s restitution(s) validated.';
        $this->json->message = $this->translator->getText($this->json->message);
        $this->json->message = sprintf($this->json->message, $success);

        if ($echec > 0) {
            $message = '%1$s restitution(s) can not be validate(s).';
            $message = $this->translator->getText($message);
            $message = sprintf($message, $echec);

            $this->json->message .= ' '.$message;
        }

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

    /**
     * Serializer JSON for cancelRestitution method
     * @param array $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function cancelRestitution($result)
    {
        $success = count($result['success']);
        $echec = count($result['error']);

        $this->json->message = '%1$s restitution(s) canceled.';
        $this->json->message = $this->translator->getText($this->json->message);
        $this->json->message = sprintf($this->json->message, $success);

        if ($echec > 0) {
            $message = ' %1$s restitution(s) can not be canceled.';
            $message = $this->translator->getText($message);
            $message = sprintf($message, $echec);

            $this->json->message .= ' '.$message;
        }

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

    /**
     * Serializer JSON for delete method
     * @param array $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function dispose($result)
    {
        $echec = 0;
785
786
        $success = 0;

Prosper De Laure's avatar
Prosper De Laure committed
787
788
789
790
        $success = count($result['success']);
        if (array_key_exists('error', $result)) {
            $echec = count($result['error']);
        }
jerome's avatar
jerome committed
791

792
        $archivesNumber = $echec+$success;
Dylan's avatar
#6832    
Dylan committed
793
        $this->translator->setCatalog('recordsManagement/messages');
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818

        $this->json->message = $message = "";

        if ($archivesNumber > 1) {
            if ($success) {
                $message = '%1$s / %3$s archive(s) flagged for destruction.';
                $message = $this->translator->getText($message);
                $this->json->message .= $message;
                $this->json->message .= "</br>";
            }
            if ($echec) {
                (!empty($this->json->message)) ? $this->json->message .= "</br>" : "";
                $message = '%2$s / %3$s archive(s) not eliminable.';
                $message = $this->translator->getText($message);
                $this->json->message .= $message;
            }
        } else {
            if ($success) {
                $this->json->message = 'The archive is flagged for destruction.';
                $this->json->message = $this->translator->getText($this->json->message);
            } else {
                $this->json->message = 'The archive cannot be eliminated.';
                $this->json->message = $this->translator->getText($this->json->message);
            }
        }
819

820
        $this->json->message = sprintf($this->json->message, $success, $echec, $archivesNumber);
Prosper De Laure's avatar
Prosper De Laure committed
821
822
823
824
825
826
827
828
829
830
831
832
833
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
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

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

    /**
     * Serializer JSON for cancelDestruction method
     * @param array $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function cancelDestruction($result)
    {
        $success = count($result['success']);
        $echec = count($result['error']);

        $this->json->message = '%1$s destruction(s) canceled.';
        $this->json->message = $this->translator->getText($this->json->message);
        $this->json->message = sprintf($this->json->message, $success);

        if ($echec > 0) {
            $message = '%1$s destruction(s) can not be canceled.';
            $message = $this->translator->getText($message);
            $message = sprintf($message, $echec);

            $this->json->message .= ' '.$message;
        }

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

    /**
     * No organization unit exceptions
     * @param string $exception The exception
     *
     * @return string String serialized in JSON
     */
    public function noOrgUnitException($exception)
    {
        // Manage errors
        $this->json->status = false;
        $this->json->message = $this->translator->getText($exception->getMessage());

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

    /**
     * No depositor organization exceptions
     * @param string $exception The exception
     *
     * @return string String serialized in JSON
     */
    public function noDepositorOrgException($exception)
    {
        // Manage errors
        $this->json->status = false;
        $this->translator->setCatalog('recordsManagement/exception');
        $this->json->message = $this->translator->getText($exception->getMessage());

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

    /**
     * DigitalResource exceptions
     * @param string $exception The exception
     *
     * @return string String serialized in JSON
     */
    public function formatIdentificationException($exception)
    {
        // Manage errors
        $this->json->status = false;
        $this->json->message = $this->translator->getText($exception->getMessage());

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

    /**
     * DigitalResource exceptions
     * @param string $exception The exception
     *
     * @return string String serialized in JSON
     */
    public function formatValidationException($exception)
    {
        // Manage errors
        $this->json->status = false;
        $this->json->message = $this->translator->getText($exception->getMessage());

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

912
913
914
915
916
917
918
919
920
921
922
    /**
     * Wrong archive infos exceptions
     * @param object $exception The exception
     *
     * @return string String serialized in JSON
     */
    public function notDisposableArchiveException($exception)
    {
        // Manage errors
        $this->json->status = false;
        $this->json->message = $this->translator->getText($exception->getMessage());
Prosper De Laure's avatar
Prosper De Laure committed
923

924
925
        return $this->json->save();
    }
926

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
957
958
959
960
961
962
963
964
    /**
     * DigitalResource exceptions
     * @param string $exception The exception
     *
     * @return string String serialized in JSON
     */
    public function unauthorizedDigitalResourceFormatException($exception)
    {
        // Manage errors
        $this->json->status = false;
        $this->json->message = $this->translator->getText($exception->getMessage());

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

    /**
     * Archive doesn't match with profile exceptions
     * @param string $exception The exception
     *
     * @return string String serialized in JSON
     */
    public function archiveDoesNotMatchProfileException($exception)
    {
        // Manage errors
        $this->json->status = false;
        $this->json->message = $this->translator->getText($exception->getMessage());

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

    /**
     * Get digitalResource
     * @param digitalResource/digitalResource $digitalResource
     *
     * @return string
     */
    public function view($digitalResource)
    {
965
        $this->json->url = \laabs::createPublicResource($digitalResource->getContents());
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

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

    /**
     * Serializer JSON for check if archive exists
     * @param object $result Object with archiveId and a boolean 'exist'
     *
     * @return object JSON object with a status and certificate of deposit
     */
    public function exists($result)
    {
        if (!$result->exist) {
            $this->json->status = $result->exist;
            $this->translator->setCatalog('recordsManagement/exception');
            $this->json->message = $this->translator->getText("Archive with identifier '%s' doesn't exists");
            $this->json->message = sprintf($this->json->message, $result->archiveId);
        }

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

Alexis Ragot's avatar
Alexis Ragot committed
988
989
990
991
992
993
994
995
996
997
998
999
    /**
     * Show an archive tree content
     * @param object $archive
     *
     * @return string
     */
    public function showArchiveTree($archive)
    {
        if (isset($archive->digitalResources)) {
            $this->json->digitalResources = $archive->digitalResources;
        }

Cyril Vazquez's avatar
Cyril Vazquez committed
1000
        $profiles = \laabs::callService('recordsManagement/archivalProfile/readIndex');
For faster browsing, not all history is shown. View entire blame