welcome.php 13.9 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

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

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

Prosper De Laure's avatar
Prosper De Laure committed
48
    /**
49
     * Get a welcome page
Prosper De Laure's avatar
Prosper De Laure committed
50
51
52
53
54
55
56
     *
     * @return string
     */
    public function welcomePage()
    {
        //$this->view->addHeaders();
        //$this->view->useLayout();
Alexis Ragot's avatar
Alexis Ragot committed
57
        $this->view->addContentFile("dashboard/mainScreen/main.html");
Prosper De Laure's avatar
Prosper De Laure committed
58
59

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

61
        $currentOrganization = \laabs::getToken("ORGANIZATION");
Prosper De Laure's avatar
Prosper De Laure committed
62
63
64
        $accountToken = \laabs::getToken('AUTH');
        $user = \laabs::newController('auth/userAccount')->get($accountToken->accountId);

65
        // File plan tree
66
67
        $filePlanPrivileges = \laabs::callService('auth/userAccount/readHasprivilege', "adminArchive/filePlan");

68
69
        $filePlan = \laabs::callService('filePlan/filePlan/readTree');
        if ($filePlan) {
70
            $this->getOrgUnitArchivalProfiles($filePlan);
71

72
73
74
75
            $filePlan = [$filePlan];
            $this->markTreeLeaf($filePlan);

            $this->view->setSource("filePlan", $filePlan);
76
        }
77

78
        // Retention
79
80
81
82
        $retentionRules = \laabs::callService('recordsManagement/retentionRule/readIndex');
        for ($i = 0, $count = count($retentionRules); $i < $count; $i++) {
            $retentionRules[$i]->durationText = (string) $retentionRules[$i]->duration;
        }
83
84
85

        // archival profiles for search form
        $archivalProfileController = \laabs::newController("recordsManagement/archivalProfile");
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
        
        if (!empty($currentOrganization->registrationNumber)) {
            $archivalProfiles = \laabs::callService('recordsManagement/archivalProfile/readOrgunitprofiles', $currentOrganization->registrationNumber);

            foreach ($archivalProfiles as $archivalProfile) {
                $archivalProfileController->readDetail($archivalProfile);
                $archivalProfile->searchFields = [];
                foreach ($archivalProfile->archiveDescription as $archiveDescription) {
                    switch ($archiveDescription->descriptionField->type) {
                        case 'text':
                        case 'name':
                        case 'date':
                        case 'number':
                        case 'boolean':
                            $archivalProfile->searchFields[] = $archiveDescription->descriptionField;
                    }
102
103
                }
            }
104

105
        }
106
        
107

108
109
        $depositPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveDeposit/deposit");

110
111
        $this->view->translate();

112
113
114
        if (!empty($currentOrganization->registrationNumber)) {
            $this->view->setSource("userArchivalProfiles", $archivalProfiles);
        }
115
        $this->view->setSource("depositPrivilege", $depositPrivilege);
116
        $this->view->setSource("filePlanPrivileges", $filePlanPrivileges);
117
        
118
119
120
121
122

        foreach ($this->view->getElementsByClass('dateRangePicker') as $dateRangePickerInput) {
            $this->view->translate($dateRangePickerInput);
        }

123
        $this->view->setSource('retentionRules', $retentionRules);
Prosper De Laure's avatar
Prosper De Laure committed
124
125
126
127
128
129
        $this->view->setSource('user', $user);
        $this->view->merge();

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

Cyril Vazquez's avatar
Cyril Vazquez committed
130
    /**
Prosper De Laure's avatar
Prosper De Laure committed
131
132
     * Show the events search form
     * @param object $filePlan The root orgUnit of user with sub-orgUnits and folders
133
     *
Cyril Vazquez's avatar
Cyril Vazquez committed
134
135
     * @return string
     */
Prosper De Laure's avatar
Prosper De Laure committed
136
    public function showTree($filePlan)
Cyril Vazquez's avatar
Cyril Vazquez committed
137
    {
Prosper De Laure's avatar
Prosper De Laure committed
138
139
        $this->view->addContentFile('filePlan/filePlanTree.html');
        $this->markTreeLeaf([$filePlan]);
140

141
        $this->view->translate();
Prosper De Laure's avatar
Prosper De Laure committed
142
        $this->view->setSource("filePlan", [$filePlan]);
Cyril Vazquez's avatar
Cyril Vazquez committed
143
        $this->view->merge();
Cyril Vazquez's avatar
Cyril Vazquez committed
144
145
146
147
148

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

    /**
149
     * Show an archive information
Alexis Ragot's avatar
Alexis Ragot committed
150
     * @param recordsManagement/archive $archive The archive
151
152
153
     *
     * @return string
     */
Prosper De Laure's avatar
Prosper De Laure committed
154
    public function archiveInfo($archive)
155
    {
156
        $this->view->addContentFile('dashboard/mainScreen/archiveInformation.html');
157
158
159
160
161
        // Retention
        $retentionRules = \laabs::callService('recordsManagement/retentionRule/readIndex');
        for ($i = 0, $count = count($retentionRules); $i < $count; $i++) {
            $retentionRules[$i]->durationText = (string) $retentionRules[$i]->duration;
        }
Alexis Ragot's avatar
Alexis Ragot committed
162

Prosper De Laure's avatar
Prosper De Laure committed
163
        $archive->depositDate = $archive->depositDate->format('Y-m-d H:i:s');
164
        $this->view->translate();
165

166
        $this->view->setSource("status", $archive->status);
Alexis Ragot's avatar
Alexis Ragot committed
167

168
169
        $archive->status = $this->view->translator->getText($archive->status, false, "recordsManagement/messages");
        $archive->finalDisposition = $this->view->translator->getText($archive->finalDisposition, false, "recordsManagement/messages");
170
        $depositPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveDeposit/deposit");
Alexis Ragot's avatar
Alexis Ragot committed
171

172
        $this->getDescription($archive);
173
        $this->view->setSource('retentionRules', $retentionRules);
Prosper De Laure's avatar
Prosper De Laure committed
174
        $this->view->setSource("archive", $archive);
175
        $this->view->setSource("depositPrivilege", $depositPrivilege);
176
        $this->view->merge();
177

178
179
180
        return $this->view->saveHtml();
    }

181
182
183
184
185
186
187
    /**
     * Show a document information
     *
     * @return string
     */
    public function documentInfo()
    {
188
189
        $this->view->addContentFile('dashboard/mainScreen/documentInformation.html');
        $this->view->translate();
190

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

        $this->view->translate();

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

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

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

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

        foreach ($archives as $archive) {
            $archive->originatorOrgName = $orgsName[$archive->originatorOrgRegNumber];
        }

Prosper De Laure's avatar
Prosper De Laure committed
230
231
232
233
234
        $this->json->archives = $archives;

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

235
236
237
238
239
240
241
242
    /**
     * Show an archive content
     * @param object $archive
     *
     * @return string
     */
    public function archiveContent($archive)
    {
243
244
245
246
247
248
249
        if (isset($archive->digitalResources)) {
            $this->json->digitalResources = $archive->digitalResources;
        }

        if (isset($archive->childrenArchives)) {
            $this->json->childrenArchives = $archive->childrenArchives;
        }
250
251
252
253

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

254
255
    /**
     * Show the result of moving an archive into a folder
256
     * @param int $result
257
258
259
     *
     * @return string
     */
260
    public function moveArchivesToFolder($result)
261
    {
262
        $this->view->translator->setCatalog('filePlan/messages');
263
264
        if ($result == 1) {
            $this->json->message = "The archive was moved.";
265
266
            $this->json->message = $this->view->translator->getText($this->json->message);
            
267
        } else {
Alexis Ragot's avatar
Alexis Ragot committed
268
            $this->json->message = '%1$s archives were moved.';
269
270
            $this->json->message = $this->view->translator->getText($this->json->message);
            $this->json->message = sprintf($this->json->message, $result);
271
        }
272
273
274
275

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

276
277
278
279
280
281
282
283
    /**
     * Get archive description
     * @param archive $archive
     *
     * @return string
     */
    protected function getDescription($archive)
    {
284
        $archivalProfile = null;
285

286
287
288
289
290
        if (!empty($archive->archivalProfileReference)) {
            $archivalProfile = \laabs::callService('recordsManagement/archivalProfile/readByreference_reference_', $archive->archivalProfileReference);
            $archive->archivalProfileName = $archivalProfile->name;
        }

Alexis Ragot's avatar
Alexis Ragot committed
291
292
293
294
295
        if (!empty($archive->descriptionClass)) {
            $presenter = \laabs::newPresenter($archive->descriptionClass);
            $descriptionHtml = $presenter->read($archive->descriptionObject);
        } else {
            $descriptionHtml = '<table">';
296

297
            if (isset($archive->descriptionObject)) {
298
299
300
301
302
                if (isset($archivalProfile->archiveDescription)) {
                    foreach ($archivalProfile->archiveDescription as $archiveDescription) {
                        if (!array_key_exists($archiveDescription->fieldName, $archive->descriptionObject)) {
                            $archive->descriptionObject->{$archiveDescription->fieldName} = "";
                        }
Alexis Ragot's avatar
Alexis Ragot committed
303
304
305
                    }
                }
            
306
307
308
309
310
311
312
313
314
                foreach ($archive->descriptionObject as $name => $value) {
                    $label = $type = $archivalProfileField = null;
                    if ($archivalProfile) {
                        foreach ($archivalProfile->archiveDescription as $archiveDescription) {
                            if ($archiveDescription->fieldName == $name) {
                                $label = $archiveDescription->descriptionField->label;
                                $archivalProfileField = true;
                                $type = $archiveDescription->descriptionField->type;
                            }
315
                        }
316
317
                    }

318
319
320
                    if (empty($label)) {
                        $label = $this->view->translator->getText($name, false, "recordsManagement/archive");
                    }
321

322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
                    if (empty($type) && $value != "") {
                        $type = 'text';
                        switch (gettype($value)) {
                            case 'boolean':
                                $type = 'boolean';
                                break;

                            case 'integer':
                            case 'double':
                                $type = 'number';
                                break;

                            case 'string':
                                if (preg_match("#\d{4}\-\d{2}\-\d{2}#", $value)) {
                                    $type = 'date';
                                }
                                break;
                        }
340
                    }
341

342
343
344
345
346
347
348
349
350
                    if ($archivalProfileField) {
                        $descriptionHtml .= '<tr class="archivalProfileField">';
                    } else {
                        $descriptionHtml .= '<tr>';
                    }

                    $descriptionHtml .= '<th title="'.$label.'" name="'.$name.'" data-type="'.$type.'">'.$label.'</th>';
                    $descriptionHtml .= '<td title="'.$value.'">'.$value.'</td>';
                    $descriptionHtml .= '</tr>';
351
352
                }

353
                $descriptionHtml .= '</table>';
354
            }
Alexis Ragot's avatar
Alexis Ragot committed
355
356
357
358
359
360
361
        }

        if ($descriptionHtml) {
            $node = $this->view->getElementById("metadata");
            $this->view->addContent($descriptionHtml, $node);
        } else {
            unset($archive->descriptionObject);
362
363
        }
    }
Prosper De Laure's avatar
Prosper De Laure committed
364

365
366
367
368
369
    /**
     * Mark leaf for html merging
     * @param object $tree The tree
     *
     */
370
371
    protected function markTreeLeaf($tree)
    {
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
        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
     *
     */
392
393
    protected function updateFolderPath($tree, $ownerName)
    {
394
395
396
397
398
399
400
        foreach ($tree as $node) {
            $node->path = $ownerName.'/'.$node->path;
            if ($node->subFolders) {
                $this->updateFolderPath($node->subFolders, $ownerName);
            }
        }
    }
Alexis Ragot's avatar
Alexis Ragot committed
401
402
403
404
405
406
407
408
409
410
411

    protected function getOrgUnitArchivalProfiles($orgUnit)
    {
        $orgUnit->archivalProfiles = \laabs::callService('recordsManagement/archivalProfile/readOrgunitprofiles', $orgUnit->registrationNumber);

        if (!empty($orgUnit->organization)) {
            foreach ($orgUnit->organization as $subOrgUnit) {
                $this->getOrgUnitArchivalProfiles($subOrgUnit);
            }
        }
    }
Prosper De Laure's avatar
Prosper De Laure committed
412
}