Sequence.php 3.04 KB
Newer Older
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
41
42
43
44
45
46
47
48
49
50
51
52
<?php

/*
 * Copyright (C) 2020 Maarch
 *
 * 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 bundle\recordsManagement\Generator;

class Sequence implements \bundle\recordsManagement\Controller\archiverArchiveIdGeneratorInterface
{
    /**
     * Sdo Factory for management of archive persistance
     * @var dependency/sdo/Factory
     */
    protected $sdoFactory;

    public function __construct(\dependency\sdo\Factory $sdoFactory)
    {
        $this->sdoFactory = $sdoFactory;
    }
    /**
     * Generate archiverArchiveId
     *
     * @param  recordsManagement/archive $archive
     *
     * @return recordsManagement/archive $archive
     */
    public function generate($archive)
    {
        $sequenceConfiguration = \laabs::configuration('recordsManagement')['archiveIdGenerator']['archiverArchiveIdRules'];
        if (is_null($sequenceConfiguration) || empty($sequenceConfiguration)) {
            return;
        }

        if (!empty($archive->archiverArchiveId) && !$sequenceConfiguration['isAutomaticallyForced']) {
            return;
        }

Jerome Boucher's avatar
Jerome Boucher committed
53
        if (isset($archive->parentArchiveId) && !empty($archive->parentArchiveId) && $sequenceConfiguration['isInheritedFromParent']) {
54
            $parentArchive = $this->sdoFactory->read('recordsManagement/archive', $archive->parentArchiveId);
55
56
            $parentArchiveId = (string) $parentArchive->archiveId;
            $directChildrenCount = $this->sdoFactory->count('recordsManagement/archive', "parentArchiveId='$parentArchiveId'");
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

            $archive->archiverArchiveId = $parentArchive->archiverArchiveId . $sequenceConfiguration['sequenceSeparator'] . str_pad($directChildrenCount + 1, 7, 0, STR_PAD_LEFT);
        } else {
            $sequenceId = $this->getNewSequenceId();
            $year = date("Y");
            $archive->archiverArchiveId = sprintf($sequenceConfiguration['format'], $sequenceConfiguration['sequenceSeparator'], $year, str_pad($sequenceId, 7, 0, STR_PAD_LEFT));
        }
    }

    protected function getNewSequenceId()
    {
        $sequenceId = null;

        $query = <<<EOT
            SELECT NEXTVAL('"recordsManagement"."archiverArchiveIdSequence"');
EOT;
        $stmt = $this->sdoFactory->das->pdo->prepare($query);
        $stmt->execute();
        while ($result = $stmt->fetch(\PDO::FETCH_ASSOC)) {
            $sequenceId = $result['nextval'];
        }

        return $sequenceId;
    }
}