welcome.php 13.3 KB
Newer Older
Prosper De Laure's avatar
Prosper De Laure committed
1
2
<?php
/*
3
 * Copyright (C) 2017 Maarch
Prosper De Laure's avatar
Prosper De Laure committed
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
 *
 * 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 presentation\maarchRM\Presenter\recordsManagement;

/**
 * welcomePage serializer html
 *
 * @author Alexis Ragot <alexis.ragot@maarch.org>
 */
class welcome
{
    use \presentation\maarchRM\Presenter\exceptions\exceptionTrait;
Alexis Ragot's avatar
Alexis Ragot committed
30

Prosper De Laure's avatar
Prosper De Laure committed
31
    public $view;
32
    public $json;
Prosper De Laure's avatar
Prosper De Laure committed
33

34
35
    protected $userArchivalProfiles = [];

Prosper De Laure's avatar
Prosper De Laure committed
36
37
    /**
     * Constuctor of welcomePage html serializer
38
     * @param \dependency\html\Document   $view The view
39
     * @param \dependency\json\JsonObject $json Json utility
Prosper De Laure's avatar
Prosper De Laure committed
40
     */
Alexis Ragot's avatar
Alexis Ragot committed
41
    public function __construct(\dependency\html\Document $view, \dependency\json\JsonObject $json)
Prosper De Laure's avatar
Prosper De Laure committed
42
43
    {
        $this->view = $view;
44
        $this->view->translator->setCatalog('recordsManagement/messages');
Alexis Ragot's avatar
Alexis Ragot committed
45

46
47
        $this->json = $json;
        $this->json->status = true;
Prosper De Laure's avatar
Prosper De Laure committed
48
    }
Alexis Ragot's avatar
Alexis Ragot committed
49

Prosper De Laure's avatar
Prosper De Laure committed
50
    /**
51
     * Get a welcome page
Prosper De Laure's avatar
Prosper De Laure committed
52
53
54
55
56
     *
     * @return string
     */
    public function welcomePage()
    {
Jerome Boucher's avatar
Jerome Boucher committed
57
58
59
60
61
62
63
64
        $dateTimePickerPlugin = \laabs::newService('dependency/html/plugins/dateTimePicker/dateTimePicker', $this->view->getContainer());
        $dateTimePickerPlugin->translate();
        $this->view->setSource('dateTimePickerParams', $dateTimePickerPlugin->saveParameters());

        $datePickerPlugin = \laabs::newService('dependency/html/plugins/datePicker/datePicker', $this->view->getContainer());
        $datePickerPlugin->translate();
        $this->view->setSource('datePickerParams', $datePickerPlugin->saveParameters());

Alexis Ragot's avatar
Alexis Ragot committed
65
        $this->view->addContentFile("dashboard/mainScreen/main.html");
66
        $this->view->addFragmentFile("filePlan/folderNode.html");
Prosper De Laure's avatar
Prosper De Laure committed
67
68

        $this->view->translate();
Alexis Ragot's avatar
Alexis Ragot committed
69

Prosper De Laure's avatar
Prosper De Laure committed
70
        $accountToken = \laabs::getToken('AUTH');
71
72
        $userAccountController = \laabs::newController('auth/userAccount');
        $user = $userAccountController->get($accountToken->accountId);
73

74
        // File plan tree
Prosper De Laure's avatar
Prosper De Laure committed
75
        $filePlanPrivileges = \laabs::callService('auth/userAccount/readHasprivilege', "archiveManagement/filePlan");
76

Alexis Ragot's avatar
Alexis Ragot committed
77
78
79
        $syncImportPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveDeposit/deposit");
        $asyncImportPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveDeposit/transferImport");

80
81
        $filePlan = \laabs::callService('filePlan/filePlan/readTree');
        if ($filePlan) {
82
            $this->getOrgUnitArchivalProfiles($filePlan);
83

84
85
86
87
            $filePlan = [$filePlan];
            $this->markTreeLeaf($filePlan);

            $this->view->setSource("filePlan", $filePlan);
Prosper De Laure's avatar
Prosper De Laure committed
88
            $this->view->setSource("filePlanPrivileges", $filePlanPrivileges);
Prosper De Laure's avatar
Prosper De Laure committed
89
            $this->view->merge($this->view->getElementById('filePlanTree'));
90
        }
91

92
        // Retention
93
94
95
96
        $retentionRules = \laabs::callService('recordsManagement/retentionRule/readIndex');
        for ($i = 0, $count = count($retentionRules); $i < $count; $i++) {
            $retentionRules[$i]->durationText = (string) $retentionRules[$i]->duration;
        }
97
98

        // archival profiles for search form
99
100
        foreach ($this->userArchivalProfiles as $archivalProfile) {
            $archivalProfile->searchFields = [];
101
            foreach ($archivalProfile->archiveDescription as $archiveDescription) {
102
103
104
105
106
107
108
                switch ($archiveDescription->descriptionField->type) {
                    case 'text':
                    case 'name':
                    case 'date':
                    case 'number':
                    case 'boolean':
                        $archivalProfile->searchFields[] = $archiveDescription->descriptionField;
109
110
111
                }
            }
        }
112

113
        $archivalProfiles = \laabs::callService('recordsManagement/archivalProfile/readIndex');
114
115
116
117
        foreach ($archivalProfiles as $key => $archivalProfile) {
            $archiveDescriptions = \laabs::callService('recordsManagement/archivalProfile/readByreference_reference_', $archivalProfile->reference)->archiveDescription;
            $archivalProfiles[$key]->archiveDescription = $archiveDescriptions;
            $archivalProfiles[$key]->searchFields = [];
118
119
            foreach ($archivalProfile->archiveDescription as $k => $archiveDescription) {
                $archivalProfiles[$key]->archiveDescription[$k]->readonly = $archiveDescription->isImmutable;
120
121
122
123
124
125
126
127
128
129
130
                switch ($archiveDescription->descriptionField->type) {
                    case 'text':
                    case 'name':
                    case 'date':
                    case 'number':
                    case 'boolean':
                        $archivalProfiles[$key]->searchFields[] = $archiveDescription->descriptionField;
                }
            }
        }

131
        $depositPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveDeposit/deposit");
132
        $exportPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveManagement/export");
133

134
135
136
137
138
        $maxResults = null;
        if (isset(\laabs::configuration('presentation.maarchRM')['maxResults'])) {
            $maxResults = \laabs::configuration('presentation.maarchRM')['maxResults'];
        }

139
        $this->view->setSource("archivalProfiles", $archivalProfiles);
140
        $this->view->setSource("userArchivalProfiles", $this->userArchivalProfiles);
141
        $this->view->setSource("depositPrivilege", $depositPrivilege);
142
        $this->view->setSource("exportPrivilege", $exportPrivilege);
Alexis Ragot's avatar
Alexis Ragot committed
143
144
        $this->view->setSource("syncImportPrivilege", $syncImportPrivilege);
        $this->view->setSource("asyncImportPrivilege", $asyncImportPrivilege);
145
        $this->view->setSource("filePlanPrivileges", $filePlanPrivileges);
146
        $this->view->setSource("maxResults", $maxResults);
147

148
        $this->view->setSource('retentionRules', $retentionRules);
Prosper De Laure's avatar
Prosper De Laure committed
149
        $this->view->setSource('user', $user);
150
        $this->view->setSource('locale', \laabs::configuration('dependency.localisation')['lang']);
Prosper De Laure's avatar
Prosper De Laure committed
151
        $this->view->merge();
Cyril Vazquez's avatar
Cyril Vazquez committed
152
        
Prosper De Laure's avatar
Prosper De Laure committed
153
154
155
        return $this->view->saveHtml();
    }

Cyril Vazquez's avatar
Cyril Vazquez committed
156
    /**
Prosper De Laure's avatar
Prosper De Laure committed
157
158
     * Show the events search form
     * @param object $filePlan The root orgUnit of user with sub-orgUnits and folders
159
     *
Cyril Vazquez's avatar
Cyril Vazquez committed
160
161
     * @return string
     */
Prosper De Laure's avatar
Prosper De Laure committed
162
    public function showTree($filePlan)
Cyril Vazquez's avatar
Cyril Vazquez committed
163
    {
Prosper De Laure's avatar
Prosper De Laure committed
164
165
        $this->view->addContentFile('filePlan/filePlanTree.html');
        $this->markTreeLeaf([$filePlan]);
166

Prosper De Laure's avatar
Prosper De Laure committed
167
        $this->view->setSource("filePlan", [$filePlan]);
Cyril Vazquez's avatar
Cyril Vazquez committed
168
        $this->view->merge();
Prosper De Laure's avatar
Prosper De Laure committed
169
        $this->view->translate();
Cyril Vazquez's avatar
Cyril Vazquez committed
170
171
172
173

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

174
175
176
177
178
179
180
    /**
     * Show a document information
     *
     * @return string
     */
    public function documentInfo()
    {
181
182
        $this->view->addContentFile('dashboard/mainScreen/documentInformation.html');
        $this->view->translate();
183

184
185
        if (isset(\laabs::configuration('presentation.maarchRM')['displayableFormat'])) {
            $this->view->setSource("displayableFormat", json_encode(\laabs::configuration('presentation.maarchRM')['displayableFormat']));
186
187
        } else {
            $this->view->setSource("displayableFormat", json_encode(array()));
188
        }
189

190
191
        $this->view->merge();

192
193
        return $this->view->saveHtml();
    }
Prosper De Laure's avatar
Prosper De Laure committed
194
195
    /**
     * Display error
196
     * @param object $error
Prosper De Laure's avatar
Prosper De Laure committed
197
198
199
200
201
     *
     * @return string
     */
    public function error($error)
    {
Cyril Vazquez's avatar
Cyril Vazquez committed
202
        $this->view->addContentFile("dashboard/error.html");
Prosper De Laure's avatar
Prosper De Laure committed
203
204
205
206
207
208
209
210

        $this->view->translate();

        $this->view->setSource('error', $error);
        $this->view->merge();

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

Prosper De Laure's avatar
Prosper De Laure committed
212
    /**
213
     * Show a folder content
214
215
216
     *
     * @param array   $archives
     * @param integer $count    Archives count without limit
Prosper De Laure's avatar
Prosper De Laure committed
217
218
219
     *
     * @return string
     */
220
    public function folderContents($archives, $count)
Prosper De Laure's avatar
Prosper De Laure committed
221
    {
Alexis Ragot's avatar
Alexis Ragot committed
222
223
224
225
226
227
228
        $organizations = \laabs::callService('organization/organization/readIndex');
        $orgsName = [];

        foreach ($organizations as $organization) {
            $orgsName[$organization->registrationNumber] = $organization->displayName;
        }

Jerome Boucher's avatar
Jerome Boucher committed
229
230
231
232
233
234
235
        $profiles = \laabs::callService('recordsManagement/archivalProfile/readIndex');
        $profilesName = [];

        foreach ($profiles as $profile) {
            $profilesName[$profile->reference] = $profile->name;
        }

Alexis Ragot's avatar
Alexis Ragot committed
236
237
        foreach ($archives as $archive) {
            $archive->originatorOrgName = $orgsName[$archive->originatorOrgRegNumber];
Cyril Vazquez's avatar
Cyril Vazquez committed
238
            if (!empty($archive->archivalProfileReference) && isset($profilesName[$archive->archivalProfileReference])) {
Jerome Boucher's avatar
Jerome Boucher committed
239
240
                $archive->archivalProfileName = $profilesName[$archive->archivalProfileReference];
            }
Alexis Ragot's avatar
Alexis Ragot committed
241
242
        }

Prosper De Laure's avatar
Prosper De Laure committed
243
        $this->json->archives = $archives;
244
        $this->json->countWithoutLimit = $count;
Prosper De Laure's avatar
Prosper De Laure committed
245
246
247
248

        return $this->json->save();
    }

249
    /**
Prosper De Laure's avatar
Prosper De Laure committed
250
     * Show the result of movin²g an archive into a folder
251
     * @param int $result
252
253
254
     *
     * @return string
     */
255
    public function moveArchivesToFolder($result)
256
    {
257
        $this->view->translator->setCatalog('filePlan/messages');
258
259
        if ($result == 1) {
            $this->json->message = "The archive was moved.";
260
            $this->json->message = $this->view->translator->getText($this->json->message);
261

262
        } else {
Alexis Ragot's avatar
Alexis Ragot committed
263
            $this->json->message = '%1$s archives were moved.';
264
265
            $this->json->message = $this->view->translator->getText($this->json->message);
            $this->json->message = sprintf($this->json->message, $result);
266
        }
267
268
269
270

        return $this->json->save();
    }

271
272
273
274
    /**
     * Mark leaf for html merging
     * @param object $tree The tree
     */
275
276
    protected function markTreeLeaf($tree)
    {
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
        foreach ($tree as $node) {
            if (!isset($node->organization) && !isset($node->folder)) {
                $node->isLeaf = true;
            } else {
                if (isset($node->organization)) {
                    $this->markTreeLeaf($node->organization);
                }
                if (isset($node->folder)) {
                    $this->updateFolderPath($node->folder, $node->displayName);
                }
            }
        }
    }

    /**
     * Add owner organization name in folder path
     * @param object $tree      The tree
     * @param string $ownerName The owner organizaiton name
     */
296
297
    protected function updateFolderPath($tree, $ownerName)
    {
298
299
300
301
302
303
304
        foreach ($tree as $node) {
            $node->path = $ownerName.'/'.$node->path;
            if ($node->subFolders) {
                $this->updateFolderPath($node->subFolders, $ownerName);
            }
        }
    }
Alexis Ragot's avatar
Alexis Ragot committed
305
306
307

    protected function getOrgUnitArchivalProfiles($orgUnit)
    {
308
        $orgUnit->archivalProfiles = \laabs::callService('organization/organization/readOrgunitprofiles', $orgUnit->registrationNumber, true);
Alexis Ragot's avatar
Alexis Ragot committed
309

310
311
312
313
314
315
316
317
318
319
        foreach ($orgUnit->archivalProfiles as $i => $archivalProfile) {
            if ($archivalProfile == "*") {
                $orgUnit->acceptArchiveWithoutProfile = true;
                unset($orgUnit->archivalProfiles[$i]);
            } else {
                $this->userArchivalProfiles[$archivalProfile->reference] = $archivalProfile;
            }
        }

        $orgUnit->archivalProfiles = array_values($orgUnit->archivalProfiles);
320

321
322
323
        // Get scheme for array of objects, limit to one level for scheme recusions
        foreach ($orgUnit->archivalProfiles as $archivalProfile) {
            foreach ($archivalProfile->archiveDescription as $archiveDescription) {
324
325
326
327
328
                if (isset($archiveDescription->descriptionField)) {
                    $archiveDescription->descriptionField->required = $archiveDescription->required;
                    $archiveDescription->descriptionField->readonly = $archiveDescription->isImmutable;
                    $this->loadScheme($archiveDescription->descriptionField);
                }
329
330
            }
        }
Alexis Ragot's avatar
Alexis Ragot committed
331
332
333
334
335
336
        if (!empty($orgUnit->organization)) {
            foreach ($orgUnit->organization as $subOrgUnit) {
                $this->getOrgUnitArchivalProfiles($subOrgUnit);
            }
        }
    }
Cyril Vazquez's avatar
Cyril Vazquez committed
337
338
339

    protected function loadScheme($descriptionField)
    {
340
        if ($descriptionField->type == 'array' && isset($descriptionField->itemType) && is_string($descriptionField->itemType) && $descriptionField->itemType[0] == '#') {
Cyril Vazquez's avatar
Cyril Vazquez committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
            $objectType = new \StdClass();
            $objectType->type = 'object';

            $className = substr($descriptionField->itemType, 1);
            $objectType->properties = \laabs::callService('recordsManagement/descriptionScheme/read_name_Descriptionfields', $className);

            $descriptionField->itemType = $objectType;

            $this->loadScheme($objectType);
        }

        if ($descriptionField->type == 'object' && isset($descriptionField->properties)) {
            foreach ($descriptionField->properties as $property) {
                $this->loadScheme($property);
            }
        }
    }
Prosper De Laure's avatar
Prosper De Laure committed
358
}