archiveAccessTrait.php 53 KB
Newer Older
1
2
3
4
<?php

/*
 *  Copyright (C) 2017 Maarch
5
 *
6
 *  This file is part of bundle XXXX.
Alexis Ragot's avatar
Alexis Ragot committed
7
 *  Bundle recordsManagement is free software: you can redistribute it and/or modify
8
9
10
 *  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.
11
 *
Alexis Ragot's avatar
Alexis Ragot committed
12
 *  Bundle recordsManagement is distributed in the hope that it will be useful,
13
14
15
 *  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.
16
 *
17
 *  You should have received a copy of the GNU General Public License
Alexis Ragot's avatar
Alexis Ragot committed
18
 *  along with bundle recordsManagement.  If not, see <http://www.gnu.org/licenses/>.
19
20
21
22
23
24
25
26
27
 */

namespace bundle\recordsManagement\Controller;

/**
 * Archive access controller
 *
 * @author Alexis Ragot <alexis.ragot@maarch.org>
 */
28
trait archiveAccessTrait
29
{
30
31
    /**
     * Search archives by profile / dates / agreement
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
     * @param string  $archiveId
     * @param string  $profileReference
     * @param string  $status
     * @param string  $archiveName
     * @param string  $agreementReference
     * @param string  $archiveExpired
     * @param string  $finalDisposition
     * @param string  $originatorOrgRegNumber
     * @param string  $originatorOwnerOrgId
     * @param string  $originatorArchiveId
     * @param array   $originatingDate
     * @param string  $filePlanPosition
     * @param bool    $hasParent
     * @param string  $description
     * @param string  $text
     * @param bool    $partialRetentionRule
     * @param string  $retentionRuleCode
     * @param string  $depositStartDate
     * @param string  $depositEndDate
     * @param string  $originatingStartDate
     * @param string  $originatingEndDate
     * @param string  $archiverArchiveId
     * @param string  $processingStatus
     * @param bool    $checkAccess
     * @param integer $maxResults
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
     *
     * @return recordsManagement/archive[] Array of recordsManagement/archive object
     */
    public function search(
        $archiveId = null,
        $profileReference = null,
        $status = null,
        $archiveName = null,
        $agreementReference = null,
        $archiveExpired = null,
        $finalDisposition = null,
        $originatorOrgRegNumber = null,
        $originatorOwnerOrgId = null,
        $originatorArchiveId = null,
        $originatingDate = null,
        $filePlanPosition = null,
        $hasParent = null,
        $description = null,
        $text = null,
        $partialRetentionRule = null,
        $retentionRuleCode = null,
        $depositStartDate = null,
        $depositEndDate = null,
        $originatingStartDate = null,
81
        $originatingEndDate = null,
82
        $archiverArchiveId = null,
83
        $processingStatus = null,
84
85
        $checkAccess = true,
        $maxResults = null
86
    ) {
87
88
89
        $accountController = \laabs::newController('auth/userAccount');
        $accountController->isAuthorized('user');

90
91
        $archives = [];

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
        list($searchClasses, $archiveArgs) = $this->getClassesAndArchiveArgsForSearch(
            $archiveId,
            $profileReference,
            $status,
            $archiveName,
            $agreementReference,
            $archiveExpired,
            $finalDisposition,
            $originatorOrgRegNumber,
            $originatorOwnerOrgId,
            $originatorArchiveId,
            $originatingDate,
            $filePlanPosition,
            $hasParent,
            $partialRetentionRule,
            $retentionRuleCode,
            $depositStartDate,
            $depositEndDate,
            $originatingStartDate,
            $originatingEndDate,
            $archiverArchiveId,
            $processingStatus
        );

        foreach ($searchClasses as $descriptionClass => $descriptionController) {
            $archives = array_merge($archives, $descriptionController->search($description, $text, $archiveArgs, $checkAccess, $maxResults));
        }

        return $archives;
    }

    /**
     * Count archives by profile / dates / agreement
     *
     * @param string  $archiveId
     * @param string  $profileReference
     * @param string  $status
     * @param string  $archiveName
     * @param string  $agreementReference
     * @param string  $archiveExpired
     * @param string  $finalDisposition
     * @param string  $originatorOrgRegNumber
     * @param string  $originatorOwnerOrgId
     * @param string  $originatorArchiveId
     * @param array   $originatingDate
     * @param string  $filePlanPosition
     * @param bool    $hasParent
     * @param string  $description
     * @param string  $text
     * @param bool    $partialRetentionRule
     * @param string  $retentionRuleCode
     * @param string  $depositStartDate
     * @param string  $depositEndDate
     * @param string  $originatingStartDate
     * @param string  $originatingEndDate
     * @param string  $archiverArchiveId
     * @param string  $processingStatus
     * @param bool    $checkAccess
     * @param integer $maxResults
     *
     * @return integer $count Count of archives from search
     */
    public function count(
        $archiveId = null,
        $profileReference = null,
        $status = null,
        $archiveName = null,
        $agreementReference = null,
        $archiveExpired = null,
        $finalDisposition = null,
        $originatorOrgRegNumber = null,
        $originatorOwnerOrgId = null,
        $originatorArchiveId = null,
        $originatingDate = null,
        $filePlanPosition = null,
        $hasParent = null,
        $description = null,
        $text = null,
        $partialRetentionRule = null,
        $retentionRuleCode = null,
        $depositStartDate = null,
        $depositEndDate = null,
        $originatingStartDate = null,
        $originatingEndDate = null,
        $archiverArchiveId = null,
        $processingStatus = null,
        $checkAccess = true,
        $maxResults = null
    ) {
        $accountController = \laabs::newController('auth/userAccount');
        $accountController->isAuthorized('user');

        $archives = [];

        list($searchClasses, $archiveArgs) = $this->getClassesAndArchiveArgsForSearch(
            $archiveId,
            $profileReference,
            $status,
            $archiveName,
            $agreementReference,
            $archiveExpired,
            $finalDisposition,
            $originatorOrgRegNumber,
            $originatorOwnerOrgId,
            $originatorArchiveId,
            $originatingDate,
            $filePlanPosition,
            $hasParent,
            $partialRetentionRule,
            $retentionRuleCode,
            $depositStartDate,
            $depositEndDate,
            $originatingStartDate,
            $originatingEndDate,
            $archiverArchiveId,
            $processingStatus
        );

        $count = 0;
        foreach ($searchClasses as $descriptionClass => $descriptionController) {
            $count += $descriptionController->count($description, $text, $archiveArgs, $checkAccess, $maxResults);
        }

        return $count;
    }

    protected function getClassesAndArchiveArgsForSearch(
        $archiveId = null,
        $profileReference = null,
        $status = null,
        $archiveName = null,
        $agreementReference = null,
        $archiveExpired = null,
        $finalDisposition = null,
        $originatorOrgRegNumber = null,
        $originatorOwnerOrgId = null,
        $originatorArchiveId = null,
        $originatingDate = null,
        $filePlanPosition = null,
        $hasParent = null,
        $partialRetentionRule = null,
        $retentionRuleCode = null,
        $depositStartDate = null,
        $depositEndDate = null,
        $originatingStartDate = null,
        $originatingEndDate = null,
        $archiverArchiveId = null,
        $processingStatus = null
    ) {
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
        $archiveArgs = [
            'archiveId' => $archiveId,
            'profileReference' => $profileReference,
            'status' => $status,
            'archiveName' => $archiveName,
            'agreementReference' => $agreementReference,
            'archiveExpired' => $archiveExpired,
            'finalDisposition' => $finalDisposition,
            'originatorOrgRegNumber' => $originatorOrgRegNumber,
            'originatorOwnerOrgId' => $originatorOwnerOrgId,
            'originatorArchiveId' => $originatorArchiveId,
            'originatingDate' => $originatingDate,
            'filePlanPosition' => $filePlanPosition,
            'hasParent' => $hasParent,
            'partialRetentionRule' => $partialRetentionRule,
            'retentionRuleCode' => $retentionRuleCode,
            'depositStartDate' => $depositStartDate,
            'depositEndDate' => $depositEndDate,
            'originatingDate' => [$originatingStartDate, $originatingEndDate], // [0] startDate, [1] endDate
260
261
            'archiverArchiveId' => $archiverArchiveId,
            'processingStatus' => $processingStatus
262
263
        ];

264
265
266
267
        if (!$filePlanPosition) {
            unset($archiveArgs['filePlanPosition']);
        }

268
269
270
271
        $searchClasses = [];
        if (!$profileReference) {
            $searchClasses['recordsManagement/description'] = $this->useDescriptionController('recordsManagement/description');

272
            $descriptionSchemeController = \laabs::newController('recordsManagement/descriptionScheme');
273

274
275
276
277
            foreach ($descriptionSchemeController->index() as $name => $descriptionScheme) {
                if (isset($descriptionScheme->search)) {
                    $searchClasses[$name] = $this->useDescriptionController($descriptionScheme->search);
                }
278
279
280
281
282
283
284
285
286
            }
        } else {
            $archivalProfile = $this->archivalProfileController->getByReference($profileReference);
            if ($archivalProfile->descriptionClass != '') {
                $searchClasses[$archivalProfile->descriptionClass] = $this->useDescriptionController($archivalProfile->descriptionClass);
            } else {
                $searchClasses['recordsManagement/description'] = $this->useDescriptionController('recordsManagement/description');
            }
        }
287

288
        return [$searchClasses, $archiveArgs];
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
    }
    /**
     * Search archives by profile / dates / agreement
     * @param string $archiveId
     * @param string $profileReference
     * @param string $status
     * @param string $archiveName
     * @param string $agreementReference
     * @param string $archiveExpired
     * @param string $finalDisposition
     * @param string $originatorOrgRegNumber
     * @param string $originatorOwnerOrgId
     * @param string $originatorArchiveId
     * @param array  $originatingDate
     * @param string $filePlanPosition
     * @param bool   $hasParent
     * @param string $description
     * @param string $text
     * @param bool   $partialRetentionRule
     * @param string $retentionRuleCode
     * @param string $depositStartDate
     * @param string $depositEndDate
     * @param string $originatingStartDate
     * @param string $originatingEndDate
     *
     * @return recordsManagement/archive[] Array of recordsManagement/archive object
     */
Cyril Vazquez's avatar
Typo    
Cyril Vazquez committed
316
    public function searchRegistry(
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
        $archiveId = null,
        $profileReference = null,
        $status = null,
        $archiveName = null,
        $agreementReference = null,
        $archiveExpired = null,
        $finalDisposition = null,
        $originatorOrgRegNumber = null,
        $originatorOwnerOrgId = null,
        $originatorArchiveId = null,
        $originatingDate = null,
        $filePlanPosition = null,
        $hasParent = null,
        $description = null,
        $text = null,
        $partialRetentionRule = null,
        $retentionRuleCode = null,
        $depositStartDate = null,
        $depositEndDate = null,
        $originatingStartDate = null,
        $originatingEndDate = null
    ) {
339
340
341
        $accountController = \laabs::newController('auth/userAccount');
        $accountController->isAuthorized('user');

342
343
344
345
346
347
        $queryParts = array();
        $queryParams = array();

        $currentDate = \laabs::newDate();
        $currentDateString = $currentDate->format('Y-m-d');

348
        if ($archiveId) {
349
350
351
            $queryParts['archiveId'] = "archiveId = :archiveId";
            $queryParams['archiveId'] = $archiveId;
        } else {
352
            if ($profileReference) {
353
354
                $queryParts['archivalProfileReference'] = "archivalProfileReference = :archivalProfileReference";
                $queryParams['archivalProfileReference'] = $profileReference;
355
            }
356

357
            if ($status) {
358
359
                $queryParts['status'] = "status = :status";
                $queryParams['status'] = $status;
360
361
            }

362
            if ($retentionRuleCode) {
363
364
365
                $queryParts['retentionRuleCode'] = "retentionRuleCode = :retentionRuleCode";
                $queryParams['retentionRuleCode'] = $retentionRuleCode;
            }
366

367
            if ($filePlanPosition) {
368
369
370
                $queryParts['filePlanPosition'] = "filePlanPosition = :filePlanPosition";
                $queryParams['filePlanPosition'] = $filePlanPosition;
            }
371

372
            if ($originatorArchiveId) {
373
374
375
                $queryParts['originatorArchiveId'] = "originatorArchiveId = :originatorArchiveId";
                $queryParams['originatorArchiveId'] = $originatorArchiveId;
            }
376

377
            if ($originatorOrgRegNumber) {
378
379
380
                $queryParts['originatorOrgRegNumber'] = "originatorOrgRegNumber = :originatorOrgRegNumber";
                $queryParams['originatorOrgRegNumber'] = $originatorOrgRegNumber;
            }
381

382
            if ($finalDisposition) {
383
384
385
                $queryParts['finalDisposition'] = "finalDisposition = :finalDisposition";
                $queryParams['finalDisposition'] = $finalDisposition;
            }
386

387
388
389
390
391
392
393
            if ($originatingStartDate && $originatingEndDate) {
                $queryParams['originatingStartDate'] = $originatingStartDate;
                $queryParams['originatingEndDate'] = $originatingEndDate;
                $queryParts['originatingDate'] = "originatingDate >= :originatingStartDate AND originatingDate <= :originatingEndDate";
            } elseif ($originatingStartDate) {
                $queryParams['originatingStartDate'] = $originatingStartDate;
                $queryParts['originatingDate'] = "originatingDate >= :originatingStartDate";
394

395
396
397
398
            } elseif ($originatingEndDate) {
                $queryParams['originatingEndDate'] = $originatingEndDate;
                $queryParts['originatingDate'] = "originatingDate <= :originatingEndDate";
            }
399

400
401
402
403
404
405
406
            if ($depositStartDate && $depositEndDate) {
                $queryParams['depositStartDate'] = $depositStartDate;
                $queryParams['depositEndDate'] = $depositEndDate;
                $queryParts['depositDate'] = "depositDate >= :depositStartDate AND depositDate <= :depositEndDate";
            } elseif ($depositStartDate) {
                $queryParams['depositStartDate'] = $depositStartDate;
                $queryParts['depositDate'] = "depositDate >= :depositStartDate";
407

408
409
410
411
            } elseif ($depositEndDate) {
                $queryParams['depositEndDate'] = $depositEndDate;
                $queryParts['depositDate'] = "depositDate <= :depositEndDate";
            }
412
            if ($archiveExpired) {
413
414
415
                if ($archiveExpired == "true") {
                    $queryParams['disposalDate'] = $currentDateString;
                    $queryParts['disposalDate'] = "disposalDate <= :disposalDate";
416
                } elseif ($archiveExpired == "false") {
417
418
                    $queryParams['disposalDate'] = $currentDateString;
                    $queryParts['disposalDate'] = "disposalDate >= :disposalDate";
419
                }
420
            }
421

422
            if ($partialRetentionRule) {
423
424
                $queryParts['partialRetentionRule'] = "(retentionDuration=NULL
                OR retentionStartDate=NULL
425
                OR retentionRuleCode=NULL)";
426
427
            }

428
        }
429

430
431
        $queryParams['descriptionClass'] = 'recordsManagement/log';
        $queryParts['descriptionClass'] = "(descriptionClass != :descriptionClass OR descriptionClass=NULL)";
432

433
        $accessRuleAssert = $this->getAccessRuleAssert($currentDateString);
434

435
436
        if ($accessRuleAssert) {
            $queryParts[] = $accessRuleAssert;
437
438
        }

439
        $queryString = \laabs\implode(' AND ', $queryParts);
Alexandre Morin's avatar
Alexandre Morin committed
440
441
442
443
444
445
446
447
448
        $maxResults = \laabs::configuration('presentation.maarchRM')['maxResults'];
        $archives = $this->sdoFactory->find(
            'recordsManagement/archive',
            $queryString,
            $queryParams,
            false,
            false,
            $maxResults
        );
449

450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
        foreach ($archives as $archive) {
            if (!empty($archive->disposalDate) && $archive->disposalDate <= \laabs::newDate()) {
                $archive->disposable = true;
            }
        }

        return $archives;
    }

    /**
     * Get archives list
     * @param string  $originatorOrgRegNumber The organization registration number
     * @param string  $filePlanPosition       The file plan position
     * @param boolean $archiveUnit            List the archive unit
     *
     * @return array recordsManagement/archive
     */
    public function index($originatorOrgRegNumber, $filePlanPosition = null, $archiveUnit = false)
    {
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
        list($queryString, $queryParams) = $this->getQueryStringAndParams($originatorOrgRegNumber, $filePlanPosition, $archiveUnit);
        $maxResults = \laabs::configuration('presentation.maarchRM')['maxResults'];
        $archives = $this->sdoFactory->find(
            'recordsManagement/archive',
            $queryString,
            $queryParams,
            false,
            false,
            $maxResults
        );

        foreach ($archives as $archive) {
            if (!empty($archive->disposalDate) && $archive->disposalDate <= \laabs::newDate()) {
                $archive->disposable = true;
            }
        }

        return $archives;
    }

    protected function getQueryStringAndParams($originatorOrgRegNumber, $filePlanPosition = null, $archiveUnit = false)
    {
        $queryParts = [];
        $queryParams = [];
493

494
495
496
        $currentDate = \laabs::newDate();
        $currentDateString = $currentDate->format('Y-m-d');

497
498
499
        $queryParts['status'] = "status != :status";
        $queryParams['status'] = 'disposed';

500
        if ($originatorOrgRegNumber) {
501
502
503
504
            $queryParts['originatorOrgRegNumber'] = "originatorOrgRegNumber = :originatorOrgRegNumber";
            $queryParams['originatorOrgRegNumber'] = $originatorOrgRegNumber;
        }

505
        if ($filePlanPosition) {
506
507
            $queryParts['filePlanPosition'] = "filePlanPosition = :filePlanPosition";
            $queryParams['filePlanPosition'] = $filePlanPosition;
Prosper De Laure's avatar
Prosper De Laure committed
508
        } else {
509
            $queryParts['filePlanPosition'] = "filePlanPosition = null";
510
511
        }

512
        if ($archiveUnit == false) {
513
514
515
            $queryParts['parentArchiveId'] = "parentArchiveId = null";
        }

516
        if ($archiveUnit == true) {
517
518
519
520
521
522
523
524
            $queryParts['parentArchiveId'] = "parentArchiveId != null";
        }

        $accessRuleAssert = $this->getAccessRuleAssert($currentDateString);

        if ($accessRuleAssert) {
            $queryParts[] = $accessRuleAssert;
        }
525

526
527
        $queryString = \laabs\implode(' AND ', $queryParts);

528
529
530
531
532
533
534
535
536
537
538
        return [$queryString, $queryParams];
    }
    /**
     * Get archives count
     *
     * @param string  $originatorOrgRegNumber The organization registration number
     * @param string  $filePlanPosition       The file plan position
     * @param boolean $archiveUnit            List the archive unit
     *
     * @return integer $count
     */
539
    public function countList($originatorOrgRegNumber, $filePlanPosition = null, $archiveUnit = false)
540
541
542
    {
        list($queryString, $queryParams) = $this->getQueryStringAndParams($originatorOrgRegNumber, $filePlanPosition, $archiveUnit);
        $count = $this->sdoFactory->count('recordsManagement/archive', $queryString, $queryParams);
543

544
        return $count;
545
546
    }

547
548
    /**
     * Get archive metadata
549
     * @param string $archiveId   The archive identifier
550
     *
551
552
     * @return recordsManagement/archive The archive metadata
     */
553
    public function getMetadata($archiveId, $checkAccess = true)
554
    {
555
        if (is_scalar($archiveId)) {
Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
556
            $archive = $this->sdoFactory->read('recordsManagement/archive', $archiveId);
557
        } else {
Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
558
559
560
            $archive = $archiveId;
        }
        $this->getAccessRule($archive);
561

562
563
        if ($checkAccess) {
            $this->checkRights($archive);
564
565
        }

566
        $descriptionController = $this->useDescriptionController($archive->descriptionClass);
567
568
569
570
571
572
573
574

        $archive->descriptionObject = $descriptionController->read($archive->archiveId);

        return $archive;
    }

    /**
     * Get the related information of an archive
575
576
     * @param string $archiveId   The identifier of the archive or the archive itself
     * @param bool   $checkAccess Check access for originator or archiver. if false, caller MUST control access before or after
577
     *
578
     * @return recordsManagement/archive
579
     */
580
    public function getRelatedInformation($archiveId, $checkAccess = true)
581
582
    {
        if (is_scalar($archiveId)) {
583
            $archive = $this->sdoFactory->read('recordsManagement/archive', $archiveId);
584
        } else {
585
            $archive = $archiveId;
586
        }
587
588
589
590

        if ($checkAccess) {
            $this->checkRights($archive);
        }
591

592
        $archive->lifeCycleEvent = $this->getArchiveLifeCycleEvent($archive->archiveId);
593
        $archive->relationships = $this->getArchiveRelationship($archive->archiveId);
594

595
        return $archive;
596
597
598
599
    }

    /**
     * Get the children of an archive as an index
600
     * @param string $archiveId         The identifier of the archive or the archive itself
601
     * @param bool   $loadResourcesInfo Load the resources info
602
     * @param bool   $loadBinary        Load the resources binary
603
604
605
     *
     * @return array recordsManagement/archive
     */
606
    public function listChildrenArchive($archiveId, $loadResourcesInfo = false, $loadBinary = false, $checkAccess = true)
607
608
    {
        if (is_scalar($archiveId)) {
609
            $archive = $this->sdoFactory->read('recordsManagement/archive', $archiveId);
610
        } else {
611
            $archive = $archiveId;
612
        }
613

614
        $archive->digitalResources = $this->getDigitalResources($archive->archiveId, $checkAccess);
615

Prosper De Laure's avatar
Prosper De Laure committed
616
617
618
619
620
        if ($archive->digitalResources) {
            if ($loadBinary) {
                foreach ($archive->digitalResources as $i => $digitalResource) {
                    $archive->digitalResources[$i] = $this->digitalResourceController->retrieve($digitalResource->resId);
                }
Prosper De Laure's avatar
Prosper De Laure committed
621

Prosper De Laure's avatar
Prosper De Laure committed
622
623
624
625
            } elseif ($loadResourcesInfo) {
                foreach ($archive->digitalResources as $i => $digitalResource) {
                    $archive->digitalResources[$i] = $this->digitalResourceController->info($digitalResource->resId);
                }
626
627
            }
        }
628

629
        $archive->contents = $this->sdoFactory->find(
630
631
632
            "recordsManagement/archive",
            "parentArchiveId='".(string) $archive->archiveId."'"
        );
633

634
635
        if ($archive->contents) {
            foreach ($archive->contents as $child) {
636
                $this->listChildrenArchive($child, $loadResourcesInfo, $loadBinary, $checkAccess);
Prosper De Laure's avatar
Prosper De Laure committed
637
            }
638
        }
639

640
641
642
        return $archive;
    }

643
644
645
646
647
648
649
650
651
652
    public function listChildrenArchiveId($archiveId)
    {
        $archiveIds[] = $archiveId;

        $archives = $this->sdoFactory->find(
            "recordsManagement/archive",
            "parentArchiveId='".(string) $archiveId."'"
        );

        foreach ($archives as $archive) {
653
654
            $archiveId = (string)$archive->archiveId;
            $archiveIds = array_merge($archiveIds, $this->listChildrenArchiveId($archiveId));
655
656
657
658
659
        }

        return $archiveIds;
    }

660
661
    /**
     * Retrieve an archive resource contents
662
663
     * @param string $archiveId   The archive identifier
     * @param bool   $checkAccess Check access for originator or archiver. if false, caller MUST control access before or after
664
     *
665
666
     * @return digitalResource/digitalResource[] Array of digitalResource/digitalResource object
     */
667
    public function getDigitalResources($archiveId, $checkAccess = true)
668
    {
669
670
        $archive = $this->sdoFactory->read('recordsManagement/archive', $archiveId);

671
672
673
        if ($checkAccess) {
            $this->checkRights($archive);
        }
674

675
        return $this->digitalResourceController->getResourcesByArchiveId($archiveId);
676
677
678
679
    }

    /**
     * Retrieve an archive resource contents
680
     *
681
682
683
     * @param string $archiveId   The archive identifier
     * @param string $resId       The resource identifier
     * @param bool   $checkAccess Check access for originator or archiver. if false, caller MUST control access before or after
684
     *
685
686
     * @return digitalResource/digitalResource Archive resource contents
     */
687
    public function consultation($archiveId, $resId, $checkAccess = true, $isCommunication = false)
688
    {
689
690
691
        $accountController = \laabs::newController('auth/userAccount');
        $accountController->isAuthorized('user');

692
        $archive = $this->sdoFactory->read('recordsManagement/archive', $archiveId);
693

694
        if ($checkAccess) {
695
            $this->checkRights($archive, $isCommunication);
696
        }
697

698
699
700
701
702
703
704
705
706
707
708
709
710
711
        try {
            $digitalResource = $this->digitalResourceController->retrieve($resId);

            $resourceIntegrity = true;
            foreach ($digitalResource->address as $address) {
                if (!$address->integrityCheckResult) {
                    $resourceIntegrity = false;
                }
            }

            if (!$resourceIntegrity) {
                $this->logIntegrityCheck($archive, "Invalid resource", $digitalResource, false);
            }

712
            if (($checkAccess && !$this->accessVerification($archive)) || $digitalResource->archiveId != $archiveId) {
713
714
715
716
717
718
719
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
                throw \laabs::newException('recordsManagement/accessDeniedException', "Permission denied");
            }

            $this->logConsultation($archive, $digitalResource);

        } catch (\Exception $e) {
            $this->logConsultation($archive, $digitalResource, false);

            throw $e;
        }

        $binaryDataObject = \laabs::newInstance("recordsManagement/BinaryDataObject");
        $binaryDataObject->attachment = new \stdClass();
        $binaryDataObject->attachment->data = base64_encode($digitalResource->getContents());
        $binaryDataObject->attachment->uri = "";
        $binaryDataObject->attachment->filename = $digitalResource->fileName;

        if (!empty($digitalResource->fileExtension)) {
            $digitalResource->fileName = $digitalResource->fileName . $digitalResource->fileExtension;
        }

        $binaryDataObject->format = $digitalResource->puid;
        $binaryDataObject->mimetype = $digitalResource->mimetype;
        $binaryDataObject->size = $digitalResource->size;

        $binaryDataObject->messageDigest = new \stdClass();
        $binaryDataObject->messageDigest->value = $digitalResource->hash;
        $binaryDataObject->messageDigest->algorithm = $digitalResource->hashAlgorithm;

        return $binaryDataObject;
    }

    /**
     * Retrieve an archive by its id
747
     *
748
749
750
     * @param string $archiveId   The archive identifier
     * @param bool   $withBinary  Retrieve contents or only metadata
     * @param bool   $checkAccess Check access for originator or archiver. if false, caller MUST control access before or after
751
     * @throws
752
     * @return recordsManagement/archive object
753
     */
754
    public function retrieve($archiveId, $withBinary = false, $checkAccess = true, $isCommunication = false)
755
    {
756
757
758
        $accountController = \laabs::newController('auth/userAccount');
        $accountController->isAuthorized('user');

759
        if (is_scalar($archiveId)) {
Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
760
            $archive = $this->sdoFactory->read('recordsManagement/archive', $archiveId);
761
        } else {
Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
762
763
            $archive = $archiveId;
        }
764

765
766
767
768
        if ($isCommunication) {
            $this->checkRights($archive, $isCommunication);
            $checkAccess = false;
        } else {
769
770
            $this->checkRights($archive);
        }
771

772
        $this->getMetadata($archive, $checkAccess);
Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
773
        $archive->originatorOrg = $this->organizationController->getOrgByRegNumber($archive->originatorOrgRegNumber);
774

Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
775
776
777
778
779
780
        if (!empty($archive->archiverOrgRegNumber)) {
            $archive->archiverOrg = $this->organizationController->getOrgByRegNumber($archive->archiverOrgRegNumber);
        }
        if (!empty($archive->depositorOrgRegNumber)) {
            $archive->depositorOrg = $this->organizationController->getOrgByRegNumber($archive->depositorOrgRegNumber);
        }
781
782
        $this->getRelatedInformation($archive, $checkAccess);
        $this->listChildrenArchive($archive, true, $withBinary, $checkAccess);
783

Alexandre Morin's avatar
Alexandre Morin committed
784
785
        $this->getParentArchive($archive);

786
787
        if (!empty($archive->contents)) {
            foreach ($archive->contents as $child) {
788
                $this->retrieve($child, $withBinary, $checkAccess, $isCommunication);
Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
789
790
791
            }
        }

792
793
794
795
        $archive->communicability = true;
        if ($checkAccess) {
            $archive->communicability = $this->accessVerification($archive);
        }
Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
796

Cyril Vazquez's avatar
Cyril Vazquez committed
797
        $archive->messages = $this->getMessageByArchiveid($archive->archiveId);
Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
798

799
800
        return $archive;
    }
801

802
803
804
805
806
807
808
809
810
811
    /**
     * Get an archive life cycle event
     * @param string $archiveId The archive identifier
     *
     * @return array lifeCycle/event
     */
    public function getArchiveLifeCycleEvent($archiveId)
    {
        return $this->lifeCycleJournalController->getObjectEvents($archiveId, 'recordsManagement/archive');
    }
Alexis Ragot's avatar
Alexis Ragot committed
812

813
814
815
816
    /**
     * Get an archive relationship
     * @param string $archiveId The archive identifier
     *
817
     * @return array recordsManagement/archiveRelationship
818
819
820
821
     */
    public function getArchiveRelationship($archiveId)
    {
        $res = [];
822
        $res['childrenRelationships'] = $this->archiveRelationshipController->getByArchiveId($archiveId);
823
824
        foreach ($res['childrenRelationships'] as $childRelationship) {
            $relatedArchiveInfo = $this->read($childRelationship->relatedArchiveId);
Alexandre Morin's avatar
Alexandre Morin committed
825
            $childRelationship->relatedArchiveName = $relatedArchiveInfo->archiveName;
826
        }
827
        $res['parentRelationships'] = $this->archiveRelationshipController->getByRelatedArchiveId($archiveId);
828
829
        foreach ($res['parentRelationships'] as $parentRelationship) {
            $relatedArchiveInfo = $this->read($parentRelationship->archiveId);
Alexandre Morin's avatar
Alexandre Morin committed
830
            $parentRelationship->relatedArchiveName = $relatedArchiveInfo->archiveName;
831
        }
Alexis Ragot's avatar
Alexis Ragot committed
832

833
        return $res;
834
835
836
837
    }

    /**
     * Validate archive access
838
     *
839
     * @param string $archiveId The archive identifier
840
841
     *
     * @return boolean The result of the authorization access
842
843
844
     */
    public function accessVerification($archiveId)
    {
Shiva SIVANESARAJAH's avatar
Shiva SIVANESARAJAH committed
845
        $archive = $this->sdoFactory->read('recordsManagement/archive', $archiveId);
846
847
848
849
850
851
852
853

        $comDateAccess = $this->accessComDateVerification($archive);

        $currentService = \laabs::getToken("ORGANIZATION");
        if (!$currentService) {
            return false;
        }

854
855
856
857
        $userServiceOrgRegNumbers = array_merge(
            array($currentService->registrationNumber),
            $this->userPositionController->readDescandantService((string) $currentService->orgId)
        );
858
859
860
861
862
863
864
865
866

        foreach ($userServiceOrgRegNumbers as $userServiceOrgRegNumber) {
            $userService = $this->organizationController->getOrgByRegNumber($userServiceOrgRegNumber);

            // User orgUnit is owner
            if (isset($userService->orgRoleCodes) && (strpos((string) $userService->orgRoleCodes, 'owner') !== false)) {
                return true;
            }

Alexis Ragot's avatar
Alexis Ragot committed
867
            // Archiver or Originator
868
869
            if ($userServiceOrgRegNumber == (string) $archive->archiverOrgRegNumber
                || $userServiceOrgRegNumber == (string) $archive->originatorOrgRegNumber) {
870
871
872
873
874
875
876
877
                return true;
            }

            // If date is in the past, public communication is allowed
            if ($userService->ownerOrgId == $archive->originatorOwnerOrgId && $comDateAccess) {
                return true;
            }
        }
878
879
    }

880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
    /**
     * Verification of the communication date for access
     *
     * @param recordsManagement/archive $archive The archive to verify
     *
     * @return boolean The access right
     */
    private function accessComDateVerification($archive)
    {
        $access = true;

        if ($archive->accessRuleComDate) {
            $communicationDelay = $archive->accessRuleComDate->diff(\laabs::newTimestamp());
            $access = $communicationDelay->invert == 0 ? true : false;
        }

        return $access;
    }
Cyril Vazquez's avatar
Cyril Vazquez committed
898
899
900
901

    /**
     * Get archive assert
     * @param array $args
902
     * @param array $queryParams
903
     * @param bool  $checkAccess
904
     *
905
     * @return string Query
Cyril Vazquez's avatar
Cyril Vazquez committed
906
     */
907
    public function getArchiveAssert($args, &$queryParams, $checkAccess = true)
Cyril Vazquez's avatar
Cyril Vazquez committed
908
909
910
911
912
    {
        // Args on archive
        $currentDate = \laabs::newDate();
        $currentDateString = $currentDate->format('Y-m-d');

913
        $queryParts = [];
Cyril Vazquez's avatar
Cyril Vazquez committed
914
915
916
917
        if (!empty($args['archiveName'])) {
            $queryParts[] = "archiveName='*".$args['archiveName']."*'";
        }
        if (!empty($args['profileReference'])) {
Dylan's avatar
Dylan committed
918
919
            $queryParts['archivalProfileReference'] = "archivalProfileReference = :archivalProfileReference";
            $queryParams['archivalProfileReference']=$args['profileReference'];
Cyril Vazquez's avatar
Cyril Vazquez committed
920
921
        }
        if (!empty($args['agreementReference'])) {
Dylan's avatar
Dylan committed
922
923
            $queryParts['archivalAgreementReference'] = "archivalAgreementReference=:archivalAgreementReference";
            $queryParts['archivalAgreementReference'] = $args['agreementReference'];
Cyril Vazquez's avatar
Cyril Vazquez committed
924
925
        }
        if (!empty($args['archiveId'])) {
Dylan's avatar
Dylan committed
926
927
            $queryParts['archiveId'] = "archiveId=:archiveId";
            $queryParams['archiveId'] = $args['archiveId'];
Cyril Vazquez's avatar
Cyril Vazquez committed
928
929
        }
        if (!empty($args['status'])) {
Dylan's avatar
Dylan committed
930
931
            $queryParts['status'] = "status=:status";
            $queryParams['status'] = $args['status'];
Cyril Vazquez's avatar
Cyril Vazquez committed
932
        }
933
        if (!empty($args['retentionRuleCode'])) {
Dylan's avatar
Dylan committed
934
935
            $queryParts[] = "retentionRuleCode=:retentionRuleCode";
            $queryParams['retentionRuleCode'] = $args['retentionRuleCode'];
936
        }
937
        if (!empty($args['archiveExpired']) && $args['archiveExpired'] == "true") {
Dylan's avatar
Dylan committed
938
939
            $queryParts['disposalDate'] = "disposalDate<= :disposalDate";
            $queryParams['disposalDate'] = $currentDateString;
Cyril Vazquez's avatar
Cyril Vazquez committed
940
        }
941
        if (!empty($args['archiveExpired']) && $args['archiveExpired'] == "false") {
Dylan's avatar
Dylan committed
942
943
            $queryParts['disposalDate'] = "disposalDate>= :disposalDate";
            $queryParams['disposalDate'] = $currentDateString;
Cyril Vazquez's avatar
Cyril Vazquez committed
944
        }
945
        if (!empty($args['partialRetentionRule']) && $args['partialRetentionRule'] == "true") {
946
            $queryParts['partialRetentionRule'] = "(
947
948
            retentionDuration=NULL
            OR retentionStartDate=NULL
949
950
            OR retentionRuleCode=NULL
            )";
951
        }
Cyril Vazquez's avatar
Cyril Vazquez committed
952
        if (!empty($args['finalDisposition'])) {
Dylan's avatar
Dylan committed
953
954
            $queryParts['finalDisposition'] = "finalDisposition= :finalDisposition";
            $queryParams['finalDisposition'] =$args['finalDisposition'];
Cyril Vazquez's avatar
Cyril Vazquez committed
955
956
        }
        if (!empty($args['originatorOrgRegNumber'])) {
Dylan's avatar
Dylan committed
957
958
            $queryParts[] = "originatorOrgRegNumber= :originatorOrgRegNumber";
            $queryParams['originatorOrgRegNumber'] = $args['originatorOrgRegNumber'];
Cyril Vazquez's avatar
Cyril Vazquez committed
959
        }
960
        if (!empty($args['originatorArchiveId'])) {
Dylan's avatar
Dylan committed
961
962
            $queryParts['originatorArchiveId'] = "originatorArchiveId= :originatorArchiveId";
            $queryParams['originatorArchiveId'] = $args['originatorArchiveId'];
963
        }
964
965
966
967
        if (!empty($args['archiverArchiveId'])) {
            $queryParts['archiverArchiveId'] = "archiverArchiveId= :archiverArchiveId";
            $queryParams['archiverArchiveId'] = $args['archiverArchiveId'];
        }
968
        if (!empty($args['originatingDate'])) {
969
970
971
972
973
974
975
976
977
            if (!empty($args['originatingDate'][0]) && is_string($args['originatingDate'][0])) {
                $args['originatingDate'][0] = \laabs::newDate($args['originatingDate'][0]);
            }
            if (!empty($args['originatingDate'][1]) && is_string($args['originatingDate'][1])) {
                $args['originatingDate'][1] = \laabs::newDate($args['originatingDate'][1]);
            }

            if (!empty($args['originatingDate'][0])) { // originatingStartDate
                $args['originatingDate'][0] = $args['originatingDate'][0]->format('Y-m-d');
Dylan's avatar
Dylan committed
978
979
                $queryParts['originatingDate'] = "originatingDate>= :originatingDate";
                $queryParams['originatingDate'] =$args['originatingDate'][0];
980
            }
Dylan's avatar
Dylan committed
981
            if (!empty($args['originatingDate'][1])) { // originatingEndDate;
982
                $args['originatingDate'][1] = $args['originatingDate'][1]->format('Y-m-d');
Dylan's avatar
Dylan committed
983
984
                $queryParts['originatingDate'] = "originatingDate<= :originatingDate";
                $queryParams['originatingDate'] = $args['originatingDate'][1];
985
986
            }
        }
987
988
989
990
991
992
993
994
995
996
997

        if (!empty($args['depositStartDate']) && is_string($args['depositStartDate'])) {
            $args['depositStartDate'] = \laabs::newDate($args['depositStartDate']);
        }
        if (!empty($args['depositEndDate']) && is_string($args['depositEndDate'])) {
            $args['depositEndDate'] = \laabs::newDate($args['depositEndDate']);
        }

        if (!empty($args['depositStartDate']) && !empty($args['depositEndDate'])) {
            $args['depositStartDate'] = $args['depositStartDate']->format('Y-m-d').'T00:00:00';
            $args['depositEndDate'] = $args['depositEndDate']->format('Y-m-d').'T23:59:59';
Dylan's avatar
Dylan committed
998
999
1000
            $queryParts['depositDate'] = "depositDate <= :depositEndDate AND depositDate >= :depositStartDate";
            $queryParams['depositEndDate'] = $args['depositEndDate'];
            $queryParams['depositStartDate'] = $args['depositStartDate'];
For faster browsing, not all history is shown. View entire blame