ArchiveModification.php 6.55 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
<?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 ArchiveModification
{
    protected $sdoFactory;
30
    protected $archiveController;
Prosper De Laure's avatar
Prosper De Laure committed
31
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
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
82
83
84
85
86
87
88
89
90
91
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

    /**
     * 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");
    }

    /**
     * Modify the archive retention
     * @param recordsManagement/archiveRetentionRule $retentionRule The retention rule object
     * @param mixed                                  $archiveIds    The archives ids
     * @param string                                 $comment       The comment of modification
     * @param string                                 $identifier    Message identifier
     *
     * @return bool
     */
    public function modifyRetentionRule($retentionRule, $archiveIds, $comment = null, $identifier = null)
    {
        $res = $this->archiveController->modifyRetentionRule($retentionRule, $archiveIds);

        $this->sendModificationNotification($archiveIds, $comment, $identifier);

        return $res;
    }

    /**
     * Modify the archive access
     * @param recordsManagement/archiveAccessCode $accessRule The access rule object
     * @param array                               $archiveIds The archives ids
     * @param string                              $comment    The comment of modification
     * @param string                              $identifier Message identifier
     *
     * @return bool
     */
    public function modifyAccessRule($accessRule, $archiveIds, $comment = null, $identifier = null)
    {
        $res = $this->archiveController->modifyAccessRule($accessRule, $archiveIds);

        $this->sendModificationNotification($archiveIds, $comment, $identifier);

        return $res;
    }

    /**
     * Suspend archives
     * @param mixed  $archiveIds Array of archive identifier
     * @param string $comment    The comment of modification
     * @param string $identifier Message identifier
     *
     * @return array
     */
    public function freeze($archiveIds, $comment = null, $identifier = null)
    {
        if (!is_array($archiveIds)) {
            $archiveIds = array($archiveIds);
        }

        $res = $this->archiveController->freeze($archiveIds);

        $this->sendModificationNotification($archiveIds, $comment, $identifier);

        return $res;
    }

    /**
     * Liberate archives
     * @param mixed  $archiveIds Array of archive identifier
     * @param string $comment    The comment of modification
     * @param string $identifier Message identifier
     *
     * @return array
     */
    public function unfreeze($archiveIds, $comment = null, $identifier = null)
    {
        if (!is_array($archiveIds)) {
            $archiveIds = array($archiveIds);
        }

        $res = $this->archiveController->unfreeze($archiveIds);

        $this->sendModificationNotification($archiveIds, $comment, $identifier);

        return $res;
    }

    /**
     * Add a relationship to the archive
     * @param recordsManagement/archiveRelationship $archiveRelationship The relationship of the archive
     *
     * @return bool The result of the operation
     */
    public function addRelationship($archiveRelationship)
    {
        $this->archiveController->addRelationship($archiveRelationship);

        $archive = $this->getDescription($archiveRelationship->archiveId);

        $this->sendModificationNotification($archive, "Add relationship");

        return true;
    }

    /**
     * delete a relationship
     * @param recordsManagement/archiveRelationship $archiveRelationship The archive relationship object
     *
     * @return recordsManagement/archiveRelationship
     */
    public function deleteRelationship($archiveRelationship)
    {
        $this->archiveController->deleteRelationship($archiveRelationship);

        $archive = $this->getDescription($archiveRelationship->archiveId);

        $this->sendModificationNotification($archive, "Delete relationship");

        return true;
    }

    protected function sendModificationNotification($archiveIds, $comment, $identifier = null)
    {
        $archives = array();
        foreach ($archiveIds as $archiveId) {
158
            $archive = $this->archiveController->retrieve($archiveId);
Prosper De Laure's avatar
Prosper De Laure committed
159
160
161
162
163
164
165
166
167
168
169
170
171
            $archives[] = $archive;
        }

        $archivesByOriginator = array();
        foreach ($archives as $archive) {
            if (!isset($archivesByOriginator[$archive->originatorOrgRegNumber])) {
                $archivesByOriginator[$archive->originatorOrgRegNumber] = array();
            }

            $archivesByOriginator[$archive->originatorOrgRegNumber][] = $archive;
        }

        $archiveModificationNotificationController = \laabs::newController("medona/ArchiveModificationNotification");
172
173
174
175
176
177
        
        if (!$identifier) {
            $identifier = "archiveModificationNotification_".date("Y-m-d-H-i-s");
        }

        $reference = $identifier;
Prosper De Laure's avatar
Prosper De Laure committed
178
        foreach ($archivesByOriginator as $originatorOrgRegNumber => $archives) {
179
            $i = 1;
Prosper De Laure's avatar
Prosper De Laure committed
180
181
            $senderOrg = $archives[0]->archiverOrgRegNumber;
            $recipientOrg = $originatorOrgRegNumber;
182
183
184
185
186
187
188
189

            $unique = array(
                'type' => 'ArchiveModificationNotification',
                'senderOrgRegNumber' => $senderOrg,
                'reference' => $reference,
            );

            while ($this->sdoFactory->exists("medona/message", $unique)) {
Prosper De Laure's avatar
Prosper De Laure committed
190
                $i++;
191
                $unique['reference'] = $reference = $identifier.'_'.$i;
Prosper De Laure's avatar
Prosper De Laure committed
192
            }
193
194
            
            $message = $archiveModificationNotificationController->send($reference, $archives, $senderOrg, $recipientOrg, $comment);
Prosper De Laure's avatar
Prosper De Laure committed
195
196
197
        }
    }
}