ArchiveDestruction.php 3.39 KB
Newer Older
Prosper De Laure's avatar
Prosper De Laure committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php

/*
 * Copyright (C) 2015 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 ext\thirdPartyArchiving\bundle\medona\Controller;

/**
 * Archives modification
 */
class ArchiveDestruction
{
    protected $sdoFactory;

    /**
     * Constructor
     * @param \dependency\sdo\Factory $sdoFactory The sdo factory
     */
    public function __construct(\dependency\sdo\Factory $sdoFactory)
    {
        $this->sdoFactory = $sdoFactory;
        $this->archiveController = \laabs::newController("recordsManagement/archive");
    }

    /**
     * Flag for disposal
     * @param array  $archiveIds The archives ids
     * @param string $comment    The comment of modification
     * @param string $identifier Message identifier
     *
     * @return bool
     */
    public function dispose($archiveIds, $comment = null, $identifier = null)
    {
        $archiveList = $this->archiveController->dispose($archiveIds);

53
        $archiveDestructionRequestController = \laabs::newController("medona/ArchiveDestructionRequest");
Prosper De Laure's avatar
Prosper De Laure committed
54
55
56
57
58
59
60
61
62
63

        $archivesByOriginator = array();
        foreach ($archiveList['success'] as $archiveId) {
            $archive = $this->archiveController->retrieve($archiveId);
            if (!isset($archivesByOriginator[$archive->originatorOrgRegNumber])) {
                $archivesByOriginator[$archive->originatorOrgRegNumber] = array();
            }

            $archivesByOriginator[$archive->originatorOrgRegNumber][] = $archive;
        }
64
65
66
67
        $requesterOrg = null;
        if (!$requesterOrg) {
            $requesterOrg = \laabs::getToken('ORGANIZATION');
            if (!$requesterOrg) {
Prosper De Laure's avatar
Prosper De Laure committed
68
69
70
                throw \laabs::newException('medona/invalidMessageException', "No current organization choosen");
            }
        }
71
        $requesterOrgRegNumber = $requesterOrg->registrationNumber;
Prosper De Laure's avatar
Prosper De Laure committed
72

73
74
75
76
77
        if (!$identifier) {
            $identifier = "archiveDestructionRequest_".date("Y-m-d-H-i-s");
        }

        $reference = $identifier;
Prosper De Laure's avatar
Prosper De Laure committed
78
        foreach ($archivesByOriginator as $originatorOrgRegNumber => $archives) {
79
            $i = 1;
80
            $recipientOrgRegNumber = $archives[0]->archiverOrgRegNumber;
Prosper De Laure's avatar
Prosper De Laure committed
81

82
83
84
85
86
87
88
89
90
            $unique = array(
                'type' => 'ArchiveDestructionRequest',
                'senderOrgRegNumber' => $requesterOrgRegNumber,
                'reference' => $reference,
            );

            while ($this->sdoFactory->exists("medona/message", $unique)) {
                $i++;
                $unique['reference'] = $reference = $identifier.'_'.$i;
Prosper De Laure's avatar
Prosper De Laure committed
91
92
            }

93
            $archiveDestructionRequestController->send($reference, $archives, $comment, $requesterOrgRegNumber, $recipientOrgRegNumber, $originatorOrgRegNumber);
Prosper De Laure's avatar
Prosper De Laure committed
94
95
96
97
98
        }

        return $archiveList;
    }
}