content.php 28.6 KB
Newer Older
cyril-vazquez's avatar
cyril-vazquez 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
36
37
38
39
40
<?php

/*
 * Copyright (C) 2015 Maarch
 *
 * This file is part of bundle archivesPubliques
 *
 * Bundle archivesPubliques 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 archivesPubliques 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 archivesPubliques.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace ext\archivesPubliques\presentation\maarchRM\Presenter\archivesPubliques;

/**
 * Serializer html of contentDescription
 *
 * @author Alexis Ragot <alexis.ragot@maarch.org>
 */
class content
{
    use \presentation\maarchRM\Presenter\exceptions\exceptionTrait;

    public $view;
    protected $json;

    /**
     * Constuctor of invoice html serializer
     * @param \dependency\html\Document $view The view
     * @param \dependency\html\Document $json The json object
     */
41
    public function __construct(\dependency\html\Document $view, \dependency\json\JsonObject $json)
42
    {
cyril-vazquez's avatar
cyril-vazquez committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        $this->view = $view;

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

    /**
     * Formulaire de recherche d'un contenu d'archive
     *
     * @return string
     */
    public function search()
    {
        $currentService = \laabs::getToken("ORGANIZATION");
        if (!$currentService) {
            $this->view->addContentFile("recordsManagement/welcome/noWorkingOrg.html");
Elodie Somé-Blad's avatar
Elodie Somé-Blad committed
59
60
            $this->view->translate();

cyril-vazquez's avatar
cyril-vazquez committed
61
62
63
64
65
66
67
68

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

        $archivalProfiles = \laabs::callService('recordsManagement/archivalProfile/readIndex');
        $archivalAgreements = \laabs::callService('medona/archivalAgreement/readIndex');

        $this->view->addContentFile("archivesPubliques/contentDescription/search.html");
69

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        // $ownerOriginatorOrgs = $this->getOwnerOrgs($currentService, 'originator');
        $orgController = \laabs::newController('organization/organization');

        $ownerOriginatorOrgs = [];

        $originators = $orgController->getOriginator();
        foreach ($originators as $originator) {
            if (!isset($ownerOriginatorOrgs[(string) $originator->ownerOrgId])) {
                $ownerOriginatorOrgs[(string) $originator->ownerOrgId] = new \stdClass();
                $ownerOriginatorOrgs[(string) $originator->ownerOrgId]->displayName = $originator->ownerOrgName;
                $ownerOriginatorOrgs[(string) $originator->ownerOrgId]->originators = [];
            }
            if (!in_array($originator, $ownerOriginatorOrgs[(string) $originator->ownerOrgId]->originators)) {
                $ownerOriginatorOrgs[(string)$originator->ownerOrgId]->originators[] = $originator;
            }
        }
cyril-vazquez's avatar
cyril-vazquez committed
86

87
        $ownerDepositorOrgs = $this->getOwnerOrgs($currentService, 'depositor');
cyril-vazquez's avatar
cyril-vazquez committed
88

89
90
91
        /*foreach ($ownerOriginatorOrgs as $ownerOriginatorOrgs) {
            if (!isset($ownerOriginatorOrgs[(string) $ownerOriginatorOrgs->ownerOrgId])) {
                $orgObject = \laabs::callService('organization/organization/read_orgId_', (string) $ownerOriginatorOrgs->ownerOrgId);
92
93
94
95

                $ownerOriginatorOrgs[(string) $orgObject->orgId] = new \stdClass();
                $ownerOriginatorOrgs[(string) $orgObject->orgId]->displayName = $orgObject->displayName;
                $ownerOriginatorOrgs[(string) $orgObject->orgId]->originators = [];
cyril-vazquez's avatar
cyril-vazquez committed
96
            }
97
            $ownerOriginatorOrgs[(string) $orgObject->orgId]->originators[] = $originator;
98
        }*/
99

100
        /*foreach ($depositors as $depositor) {
101
102
103
104
105
106
            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 = [];
cyril-vazquez's avatar
cyril-vazquez committed
107
            }
108
109

            $ownerDepositorOrgs[(string) $orgObject->orgId]->depositors[] = $depositor ;
110
        }*/
Dylan's avatar
Dylan committed
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
        $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) {
                if (is_file($conf['refDirectory'].'/'.$thesaurusName.'.csv')) {
                    $thesaurusList->{$thesaurusName} = $thesaurusName;
                }
            }
        }

141
142
143
144
145
        $maxResults = null;
        if (isset(\laabs::configuration('presentation.maarchRM')['maxResults'])) {
            $maxResults = \laabs::configuration('presentation.maarchRM')['maxResults'];
        }

146
147
148
149
150
151
152
153
154
155
156
        $retentionRules = \laabs::callService('recordsManagement/retentionRule/readIndex');
        // sort retention rules alphabetically and add Capitalized letter on first character
        usort($retentionRules, function ($a, $b) {
            $a->label = ucfirst($a->label);
            $b->label = ucfirst($b->label);
            return $a->label <=> $b->label;
        });
        for ($i = 0, $count = count($retentionRules); $i < $count; $i++) {
            $retentionRules[$i]->durationText = (string) $retentionRules[$i]->duration;
        }

cyril-vazquez's avatar
cyril-vazquez committed
157
        $this->view->translate();
158
        $this->view->setSource('maxResults', $maxResults);
159
        $this->view->setSource('retentionRules', $retentionRules);
160
        $this->view->setSource('profiles', $archivalProfiles);
161
        $this->view->setSource('thesaurus', $thesaurusList);
cyril-vazquez's avatar
cyril-vazquez committed
162
163
        $this->view->setSource('archivalAgreements', $archivalAgreements);
        $this->view->setSource('ownerOriginatorOrgs', $ownerOriginatorOrgs);
Dylan's avatar
#6295    
Dylan committed
164
        $this->view->setSource("organizationsOriginator", $ownerOriginatorOrgs);
cyril-vazquez's avatar
cyril-vazquez committed
165
        $this->view->setSource('ownerDepositorOrgs', $ownerDepositorOrgs);
166
        $this->view->setSource('isPublicArchive', true);
cyril-vazquez's avatar
cyril-vazquez committed
167
168
169
170
171
        $this->view->merge();

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

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
198
199
200
201
202
203
204
205
206
207
208
209
210
    /**
     * Get the list of user accessible oranizations
     * @param object $currentService The user's current service
     * @param string $role           The org unit role to select
     *
     * @return The list of user accessible  orgs
     */
    protected function getOwnerOrgs($currentService, $role)
    {
        $orgUnits = \laabs::callService('organization/organization/readByrole_role_', $role);

        $userPositionController = \laabs::newController('organization/userPosition');
        $orgController = \laabs::newController('organization/organization');

        $owner = false;
        $archiver = false;
        $userOrgUnits = [];
        $userOrgs = [];

        $userOrgUnitOrgRegNumbers = array_merge(array($currentService->registrationNumber), $userPositionController->readDescandantService((string) $currentService->orgId));
        foreach ($userOrgUnitOrgRegNumbers as $userOrgUnitOrgRegNumber) {
            $userOrgUnit = $orgController->getOrgByRegNumber($userOrgUnitOrgRegNumber);
            $userOrgUnits[] = $userOrgUnit;
            if (isset($userOrgUnit->orgRoleCodes)) {
                foreach ($userOrgUnit->orgRoleCodes as $orgRoleCode) {
                    if ($orgRoleCode == 'owner') {
                        $owner = true;
                    }
                    if ($orgRoleCode == 'archiver') {
                        $archiver = true;
                    }
                }
            }
        }

        foreach ($userOrgUnits as $userOrgUnit) {
            foreach ($orgUnits as $orgUnit) {
                if (
                    // Owner = all originators
211
                    $owner
212
213
214
215
216
217
218
219
220
221
222
223
                    // Archiver = all originators fo the same org
                    || ($archiver && $orgUnit->ownerOrgId == $userOrgUnit->ownerOrgId)
                    // Originator = all originators at position and sub-services
                    || ($role == 'originator' && $orgUnit->registrationNumber == $userOrgUnit->registrationNumber)
                    // Depositor = all
                    || $role == 'depositor'
                    ) {
                    if (!isset($userOrgs[(string) $orgUnit->ownerOrgId])) {
                        $orgObject = \laabs::callService('organization/organization/read_orgId_', (string) $orgUnit->ownerOrgId);

                        $userOrgs[(string) $orgObject->orgId] = new \stdClass();
                        $userOrgs[(string) $orgObject->orgId]->displayName = $orgObject->displayName;
224
                        $userOrgs[(string) $orgObject->orgId]->originators = [];
225
                    }
226
227
                    if (!in_array($orgUnit, $userOrgs[(string) $orgObject->orgId]->originators)) {
                        $userOrgs[(string)$orgObject->orgId]->originators[] = $orgUnit;
Alexandre Morin's avatar
Alexandre Morin committed
228
                    }
229
230
231
232
233
234
235
                }
            }
        }

        return $userOrgs;
    }

cyril-vazquez's avatar
cyril-vazquez committed
236
237
    /**
     * Resultat de recherche d'un contenu d'archive
238
239
     * @param array   $archives Array of archive object
     * @param integer $count    Max count of archives from search without limit
cyril-vazquez's avatar
cyril-vazquez committed
240
241
242
     *
     * @return string
     */
243
    public function find(array $archives, $count)
cyril-vazquez's avatar
cyril-vazquez committed
244
245
246
247
    {
        $this->view->addContentFile("archivesPubliques/contentDescription/list.html");
        $this->view->translate();

248
        $transaction = isset(\laabs::configuration("medona")["transaction"]);
Alexandre Morin's avatar
Alexandre Morin committed
249

cyril-vazquez's avatar
cyril-vazquez committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
        $originators = array();
        $this->organizationController = \laabs::newController('organization/organization');
        foreach ((array) $this->organizationController->getOrgsByRole('originator') as $originator) {
            $originators[(string) $originator->registrationNumber] = $originator;
        }

        //access code selector
        $accessRuleController = \laabs::newController('recordsManagement/accessRule');
        $accessRules = $accessRuleController->index();
        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);
            }
        }

Alexandre Morin's avatar
Alexandre Morin committed
267
268
269
270
271
272
273
274
275
276
277
        //retention code selector
        $retentionRuleController = \laabs::newController('recordsManagement/retentionRule');
        $retentionRules = $retentionRuleController->index();
        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);
            }
        }

Alexis Ragot's avatar
Alexis Ragot committed
278
279
280
        $positionController = \laabs::newController("organization/userPosition");
        $descendantServices = $positionController->listMyCurrentDescendantServices(\laabs::getToken("ORGANIZATION")->orgId);

cyril-vazquez's avatar
cyril-vazquez committed
281
        $currentDate = \laabs::newDate();
282
283
        $archiveController = \laabs::newController('recordsManagement/archive');

Cyril Vazquez's avatar
Cyril Vazquez committed
284
285
        $collection = \laabs::callService('Collection/Collection/readByUser');

cyril-vazquez's avatar
cyril-vazquez committed
286
287
288
289
        foreach ($archives as $archive) {
            $archive->finalDispositionDesc = $this->view->translator->getText($archive->finalDisposition, false, "recordsManagement/messages");
            $archive->statusDesc = $this->view->translator->getText($archive->status, false, "recordsManagement/messages");

Alexandre Morin's avatar
Alexandre Morin committed
290
            if ($archive->status == 'disposed' || $archive->status == 'error' || $archive->status == 'restituted' || $archive->status == 'transfered') {
cyril-vazquez's avatar
cyril-vazquez committed
291
292
293
294
295
296
297
298
299
300
301
                $archive->statusModif = true;
            }
            if (!empty($archive->disposalDate) && $archive->disposalDate <= $currentDate) {
                $archive->disposable = true;
            }
            if (isset($originators[$archive->originatorOrgRegNumber])) {
                $archive->originator = $originators[$archive->originatorOrgRegNumber];
            } else {
                $archive->originator = $archive->originatorOrgRegNumber;
            }

Alexis Ragot's avatar
Alexis Ragot committed
302
303
304
305
306
            $archive->restituable = false;
            if ($archive->status == 'preserved' && in_array($archive->originatorOrgRegNumber, $descendantServices)) {
                $archive->restituable = true;
            }

307
308
309
310
311
312
313
314
315
316
            if (isset($archive->description) && is_object($archive->description)) {
                if (isset($archive->description->startDate)) {
                    $archive->startDate = \laabs::newDate($archive->description->startDate);
                }

                if (isset($archive->description->endDate)) {
                    $archive->endDate = \laabs::newDate($archive->description->endDate);
                }
            }

Cyril Vazquez's avatar
Cyril Vazquez committed
317
318
319
320
321
322
323
324
325
            $archive->isInUserCollection = false;
            if (!is_null($collection->archiveIds) && !empty($collection->archiveIds)) {
                foreach ($collection->archiveIds as $collectedArchiveId) {
                    if ($collectedArchiveId == $archive->archiveId) {
                        $archive->isInUserCollection = true;
                    }
                }
            }

326
            $archive->isCommunicable = '2';
Alexandre Morin's avatar
Alexandre Morin committed
327
328
329
            if ($archive->accessRuleComDate) {
                $communicationDelay = $archive->accessRuleComDate->diff(\laabs::newTimestamp());
                if ($communicationDelay->invert != 0) {
330
                    $archive->isCommunicable = '1';
Alexandre Morin's avatar
Alexandre Morin committed
331
                }
332
333
334
335
            } elseif (
                isset(\laabs::configuration("recordsManagement")['actionWithoutCommunicationRule'])
                && \laabs::configuration("recordsManagement")['actionWithoutCommunicationRule'] == 'deny'
            ) {
336
                $archive->isCommunicable = '1';
Alexandre Morin's avatar
Alexandre Morin committed
337
            }
cyril-vazquez's avatar
cyril-vazquez committed
338
            $archive->actions = array();
339
340
341
342
343
344
345
346
347

            try {
                if (
                    $archive->status == 'disposed'
                    || $archive->status == 'error'
                    || $archive->status == 'restituted'
                    || $archive->status == 'transfered'
                ) {
                    $archive->hasRights = false;
348
349
                } elseif ($archive->isCommunicable == '2') {
                    $archive->hasRights = true;
350
351
352
353
354
355
                } else {
                    $archive->hasRights = $archiveController->checkRights($archive);
                }
            } catch (\Exception $e) {
                $archive->hasRights = false;
            }
cyril-vazquez's avatar
cyril-vazquez committed
356
357
        }

358
        $hasReachMaxResults = false;
359
360
361
362
        if (
            isset(\laabs::configuration('presentation.maarchRM')['maxResults'])
            && count($archives) >= \laabs::configuration('presentation.maarchRM')['maxResults']
        ) {
363
364
365
            $hasReachMaxResults = true;
        }

cyril-vazquez's avatar
cyril-vazquez committed
366
367
        $this->readPrivilegesOnArchives();

368
369
370
        $this->view->setSource('hasReachMaxResults', $hasReachMaxResults);
        $this->view->setSource('totalResultsWithoutLimit', $count);
        $this->view->setSource('maxResults', \laabs::configuration('presentation.maarchRM')['maxResults']);
cyril-vazquez's avatar
cyril-vazquez committed
371
372
        $this->view->setSource('content', $archives);
        $this->view->setSource("accessRules", $accessRules);
Alexandre Morin's avatar
Alexandre Morin committed
373
        $this->view->setSource("retentionRules", $retentionRules);
Alexandre Morin's avatar
Alexandre Morin committed
374
        $this->view->setSource('transaction', $transaction);
375
376
        $packageSchemas = \laabs::configuration("medona")["packageSchemas"];
        $this->view->setSource('packageSchemas', $packageSchemas);
Cyril Vazquez's avatar
Cyril Vazquez committed
377
        $this->view->setSource('collection', $collection);
Alexandre Morin's avatar
Alexandre Morin committed
378

cyril-vazquez's avatar
cyril-vazquez committed
379
380
381
382
383
        $this->view->merge();

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

        $dataTable->setPaginationType("full_numbers");
Dylan's avatar
#6994    
Dylan committed
384
        $dataTable->setUnsortableColumns(0, 6);
385
        $dataTable->setSorting(array(array(3, 'desc')));
cyril-vazquez's avatar
cyril-vazquez committed
386

Benjamin Rousselière's avatar
Benjamin Rousselière committed
387
        $dataTable->setColumnsToExport([1, 2, 3, 4, 5]);
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
        $titleExport = $this->view->translator->getText(
            "Export to ",
            false,
            "recordsManagement/messages"
        );

        $dataTable->setExport(
            [
                [
                    "exportType" => "csv",
                    "text" => "<i class='fa fa-download'></i> CSV",
                    "titleAttr" => $titleExport . "CSV"
                ],
                [
                    "exportType" => "pdf",
                    "text" => "<i class='fa fa-download'></i> PDF",
                    "titleAttr" => $titleExport . "PDF"
                ]
            ],
            false
        );
409

cyril-vazquez's avatar
cyril-vazquez committed
410
411
412
413
414
415
416
417
418
419
420
        return $this->view->saveHtml();
    }

    /**
     * Read users privileges on archives
     */
    protected function readPrivilegesOnArchives()
    {
        $hasModificationPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveManagement/modify");
        $hasIntegrityCheckPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveManagement/checkIntegrity");
        $hasDestructionPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "destruction/destructionRequest");
421
        $hasDestructionCancelPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "destruction/destructionCancel");
cyril-vazquez's avatar
cyril-vazquez committed
422
423
        $hasRestitutionPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "restitution/restitutionRequest");
        $hasDeliveryPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "delivery/deliveryRequest");
424
        $hasTransferPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "definitiveTransfer/outgoingTransferSend");
425
        $hasModificationRequestPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveManagement/modificationRequestSend");
426
        
cyril-vazquez's avatar
cyril-vazquez committed
427
428
429
        $this->view->setSource('hasModificationPrivilege', $hasModificationPrivilege);
        $this->view->setSource('hasIntegrityCheckPrivilege', $hasIntegrityCheckPrivilege);
        $this->view->setSource('hasDestructionPrivilege', $hasDestructionPrivilege);
430
        $this->view->setSource('hasDestructionCancelPrivilege', $hasDestructionCancelPrivilege);
cyril-vazquez's avatar
cyril-vazquez committed
431
432
        $this->view->setSource('hasRestitutionPrivilege', $hasRestitutionPrivilege);
        $this->view->setSource('hasDeliveryPrivilege', $hasDeliveryPrivilege);
433
        $this->view->setSource('hasTransferPrivilege', $hasTransferPrivilege);
434
        $this->view->setSource('hasModificationRequestPrivilege', $hasModificationRequestPrivilege);
cyril-vazquez's avatar
cyril-vazquez committed
435
436
437
438
439
440
441
442
443
444
    }

    /**
     * Visualisation d'une archive publique
     * @param archivesPubliques/content[] $contents The archive description
     *
     * @return string
     */
    public function read($contents)
    {
Alexandre Morin's avatar
Alexandre Morin committed
445
        $languageCodes = \laabs::callService("seda/archiveTransferComposition/readLanguageCodes");
cyril-vazquez's avatar
cyril-vazquez committed
446
447
448
449
450
451
452
453
        $this->view->addContentFile("archivesPubliques/contentDescription/contentDescription.table.html");
        $this->view->translate();

        /*$keywordTypesDoc = \laabs::newService('dependency/xml/Document');
        $keywordTypesDoc->load('bundle/seda/Resources/xsd/seda/v10/codes/seda_v1-0_keywordtype_code.xsd');
        $keywordTypesXpath = \laabs::newService('dependency/xml/XPath', $keywordTypesDoc);
        $keywordTypesXpath->registerNamespace('xsd', "http://www.w3.org/2001/XMLSchema");
        $keywordTypesXpath->registerNamespace('ccts', "urn:un:unece:uncefact:documentation:standard:CoreComponentsTechnicalSpecification:2");
454
        */
455

cyril-vazquez's avatar
cyril-vazquez committed
456
457
458
459
460
461
462
463
464
465
466
467
468
        $keywordTypes = array(
            'corpname' => 'Collectivité',
            'famname' => 'Nom de famille',
            'geogname' => 'Nom géographique',
            'name' => 'Nom',
            'occupation' => 'Fonction',
            'persname' => 'Nom de personne',
            'subject' => 'Mot-matière',
            'genreform' => 'Type de document',
            'function' => 'Activité',
        );

        $descriptionLevels = array(
Alexandre Morin's avatar
Alexandre Morin committed
469
470
471
472
473
474
475
476
477
478
            'class',
            'collection',
            'file',
            'fonds',
            'item',
            'recordgrp',
            'series',
            'subfonds',
            'subgrp',
            'subseries'
cyril-vazquez's avatar
cyril-vazquez committed
479
        );
Cyril Vazquez's avatar
Cyril Vazquez committed
480
        if (empty($contents)) {
481
482
483
484
485
            $this->view->merge();

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

486

Cyril Vazquez's avatar
Cyril Vazquez committed
487

Cyril Vazquez's avatar
Cyril Vazquez committed
488
489
490
491
492
493
494
        if (is_array($contents)) {
            $hasContent = false;
            foreach ($contents as $content) {
                if (empty($content->resId)) {
                    $hasContent = true;
                    break;
                }
495
            }
Cyril Vazquez's avatar
Cyril Vazquez committed
496
497
            if (!$hasContent) {
                $this->view->merge();
498

Cyril Vazquez's avatar
Cyril Vazquez committed
499
500
                return $this->view->saveHtml();
            }
501
502
        } else {
            $content = $contents;
503
        }
504

505
506
507
508
        if (!empty($content->startDate) || !empty($content->endDate)) {
            $content->hasDateRange = true;
        }

509
510
        // Backward compat with bug #13540
        $content->descriptionLevel = strtolower($content->descriptionLevel);
511
512

        if (isset($content->language) && is_string($content->language)) {
513
514
            $content->language=[$content->language];
        }
515

516
        if (!empty($content->language) && count($content->language)) {
Alexandre Morin's avatar
Alexandre Morin committed
517
518
519
520
521
522
523
524
525
526
527
            $languageList = [];
            foreach ($content->language as $language) {
                foreach ($languageCodes as $languageCode) {
                    if ($language === $languageCode->alpha3b || $language === $languageCode->alpha3t) {
                        $languageList[] = ucfirst($languageCode->French);
                        break;
                    }
                }
            }

            $content->language = $languageList;
cyril-vazquez's avatar
cyril-vazquez committed
528
529
        }

530
        if (!empty($content->keyword) && count($content->keyword)) {
cyril-vazquez's avatar
cyril-vazquez committed
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
            foreach ($content->keyword as $keyword) {
                if (isset($keywordTypes[$keyword->type])) {
                    $keyword->type = $keywordTypes[$keyword->type];
                }
            }
        }

        $this->view->setSource("content", $content);
        $this->view->merge();

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

    //JSON
    /**
     * Exception
     * @param archivesPubliques/Exception/contentDescriptionException $contentDescriptionException
     *
     * @return string
     */
    public function contentDescriptionException($contentDescriptionException)
    {
        $this->json->load($contentDescriptionException);
        $this->json->status = false;

        return $this->json->save();
    }
Dylan's avatar
#6295    
Dylan committed
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

    /**
     * Get the list of owner originators oranizations
     * @param object $currentService The user's current service
     *
     * @return The list of owner originators orgs
     */
    protected function getOwnerOriginatorsOrgs($currentService)
    {
        $originators = \laabs::callService('organization/organization/readIndex', 'isOrgUnit=true');

        $userPositionController = \laabs::newController('organization/userPosition');
        $orgController = \laabs::newController('organization/organization');

        $owner = false;
        $userServices = [];
        $ownerOriginatorOrgs = [];

        // Get all user services,  and check OWNER role on one of them
        $userServiceOrgRegNumbers = array_merge(array($currentService->registrationNumber), $userPositionController->readDescandantService((string) $currentService->orgId));
        foreach ($userServiceOrgRegNumbers as $userServiceOrgRegNumber) {
            $userService = $orgController->getOrgByRegNumber($userServiceOrgRegNumber);
            $userServices[] = $userService;
            if (isset($userService->orgRoleCodes)) {
                foreach ($userService->orgRoleCodes as $orgRoleCode) {
                    if ($orgRoleCode == 'owner') {
                        $owner = true;

                        break;
                    }
                }
            }
        }
        foreach ($userServices as $userService) {
            foreach ($originators as $originator) {
                if ($owner || $originator->registrationNumber == $userService->registrationNumber) {
                    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]->orgId = $orgObject->orgId;
                        $ownerOriginatorOrgs[(string) $orgObject->orgId]->originators = [];
                    }
                    $ownerOriginatorOrgs[$originator->ownerOrgId]->originators[] = $originator;
                }
            }
        }

        return $ownerOriginatorOrgs;
    }
Alexandre Morin's avatar
Alexandre Morin committed
608
609
610
611
612
613
614
615

    /**
     * Get metadata to edit
     * @param archive           $archive
     * @param language code     $languageCodes
     *
     * @return string
     */
616
    public function edit($archive)
Alexandre Morin's avatar
Alexandre Morin committed
617
    {
618
619
620
621
622
623
624
625
626
627
628
629
630
631
        $this->view->addContentFile("archivesPubliques/contentDescription/metadata.html");

        $languageCodes = \laabs::callService("seda/archiveTransferComposition/readLanguageCodes");

        foreach ($languageCodes as $languageCode) {
            $languageCode->title =  ucfirst($languageCode->French);

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

Alexandre Morin's avatar
Alexandre Morin committed
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
        foreach ($archive->descriptionObject as $descriptionObject) {
            $descriptionObject->json = json_encode($descriptionObject);
        }

        if (isset($archive->descriptionObject[0]->language)) {
            $archive->descriptionObject[0]->lang = [];
            for ($i = 0; $i < count($archive->descriptionObject[0]->language); $i++) {
                $language = new \stdClass();
                $language->value = $archive->descriptionObject[0]->language[$i];

                foreach ($languageCodes as $languageCode) {
                    if ($languageCode->value === $archive->descriptionObject[0]->language[$i]) {
                        $language->name = $languageCode->title;
                    }
                }
                $archive->descriptionObject[0]->lang[] = $language;
            }
        }

        if (isset($archive->descriptionObject[0]->custodialHistory)) {
            foreach ($archive->descriptionObject[0]->custodialHistory as $custodialHistory) {
                $custodialHistory->json = json_encode($custodialHistory);
            }
        }

        if (isset($archive->descriptionObject[0]->keyword)) {
            foreach ($archive->descriptionObject[0]->keyword as $keyword) {
659
                $keyword->typeText = $this->view->translator->getText($keyword->type, false, "archivesPubliques/messages");
Alexandre Morin's avatar
Alexandre Morin committed
660
661
662
663
                $keyword->json = json_encode($keyword);
            }
        }

664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
        if ($archive->descriptionObject) {
            $archive->descriptionObject = $archive->descriptionObject[0];
        }

        $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'];
687

688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
        if (isset($conf['refDirectory']) || is_dir($conf['refDirectory'])) {
            $refDirectory = $conf['refDirectory'];
            foreach ($thesaurusNames as $thesaurusName) {
                if (glob($conf['refDirectory'].'/'.$thesaurusName.'.*')) {
                    $thesaurusList->$thesaurusName = $thesaurusName;
                }
            }
        }

        $this->view->setSource('thesaurus', $thesaurusList);
        $this->view->setSource('languageCodes', $languageCodes);
        $this->view->setSource("archive", $archive);

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

        return $this->view->saveHtml();
Alexandre Morin's avatar
Alexandre Morin committed
705
    }
cyril-vazquez's avatar
cyril-vazquez committed
706
}