welcome.php 17.5 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
57
58
     *
     * @return string
     */
    public function welcomePage()
    {
        //$this->view->addHeaders();
        //$this->view->useLayout();
Alexis Ragot's avatar
Alexis Ragot committed
59
        $this->view->addContentFile("dashboard/mainScreen/main.html");
Prosper De Laure's avatar
Prosper De Laure committed
60
61

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

63
        $currentOrganization = \laabs::getToken("ORGANIZATION");
Prosper De Laure's avatar
Prosper De Laure committed
64
        $accountToken = \laabs::getToken('AUTH');
65
66
67
        $userAccountController = \laabs::newController('auth/userAccount');
        $user = $userAccountController->get($accountToken->accountId);
        
68
        // File plan tree
Prosper De Laure's avatar
Prosper De Laure committed
69
        $filePlanPrivileges = \laabs::callService('auth/userAccount/readHasprivilege', "archiveManagement/filePlan");
70

71
72
        $filePlan = \laabs::callService('filePlan/filePlan/readTree');
        if ($filePlan) {
73
            $this->getOrgUnitArchivalProfiles($filePlan);
74

75
76
77
78
            $filePlan = [$filePlan];
            $this->markTreeLeaf($filePlan);

            $this->view->setSource("filePlan", $filePlan);
79
        }
80

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

        // archival profiles for search form
88
89
90
91
        foreach ($this->userArchivalProfiles as $archivalProfile) {
            /*if ($archivalProfile == "*") {
                $currentOrganization->acceptArchiveWithoutProfile = true;
            }
92

93
94
95
96
97
98
99
100
101
102
103
            $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;
104
105
106
                }
            }
        }
107
        
108
        $depositPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveDeposit/deposit");
109
110
        $this->view->translate();

111
        $this->view->setSource("userArchivalProfiles", $this->userArchivalProfiles);
112
        $this->view->setSource("depositPrivilege", $depositPrivilege);
113
        $this->view->setSource("filePlanPrivileges", $filePlanPrivileges);
114
        
115
116
117
118
119

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

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

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

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

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

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

    /**
146
     * Show an archive information
Alexis Ragot's avatar
Alexis Ragot committed
147
     * @param recordsManagement/archive $archive The archive
148
149
150
     *
     * @return string
     */
Prosper De Laure's avatar
Prosper De Laure committed
151
    public function archiveInfo($archive)
152
    {
153
        $this->view->addContentFile('dashboard/mainScreen/archiveInformation.html');
154
155

        // Archive
156
        $originatorOrg = \laabs::callService('organization/organization/readByregnumber', $archive->originatorOrgRegNumber);
157
158
159
        $archive->originatorOrgName = $originatorOrg->displayName;

        $archive->depositDate = $archive->depositDate->format('Y-m-d H:i:s');
160
        if ($archive->originatingDate) {
161
            $archive->originatingDate = $archive->originatingDate;
162
        }
163

164
165
166
167
168
        // Retention
        $retentionRules = \laabs::callService('recordsManagement/retentionRule/readIndex');
        for ($i = 0, $count = count($retentionRules); $i < $count; $i++) {
            $retentionRules[$i]->durationText = (string) $retentionRules[$i]->duration;
        }
Prosper De Laure's avatar
Prosper De Laure committed
169

170
171
172
        $archivalProfileList = [];
        $acceptArchiveWithoutProfile = $acceptUserIndex = false;

173
        // Add a sub archive
Prosper De Laure's avatar
Prosper De Laure committed
174
        $depositPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveDeposit/deposit");
175
        $fileplanLevel = false;
176
177
178
179
180
181
182
        if ($depositPrivilege) {
            if (!empty($archive->archivalProfileReference)) {
                $archivalProfile = \laabs::callService('recordsManagement/archivalProfile/readByreference_reference_', $archive->archivalProfileReference);
                $archive->archivalProfileName = $archivalProfile->name;
                
                $list = [];

183
                if (count($archivalProfile->containedProfiles)) {
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
                     $list = $archivalProfile->containedProfiles;
                }

                if (count($list)) {
                    foreach ($list as $profile) {
                        $profileObject = new \stdClass();
                        $profileObject->reference = $profile->reference;
                        $profileObject->name = $profile->name;
                        $profileObject->json = json_encode($profile);

                        $archivalProfileList[] = $profileObject;
                    }
                }

                if (!count($archivalProfileList) && !$archivalProfile->acceptArchiveWithoutProfile ) {
                    $depositPrivilege = false;
                }
201
202

                $acceptArchiveWithoutProfile = $archivalProfile->acceptArchiveWithoutProfile;
203
                $fileplanLevel = $archivalProfile->fileplanLevel;
204
                $acceptUserIndex = $archivalProfile->acceptUserIndex;
205
206
            } else {
                $acceptArchiveWithoutProfile = true;
207
                $fileplanLevel = true;
Prosper De Laure's avatar
Prosper De Laure committed
208
            }
Prosper De Laure's avatar
Prosper De Laure committed
209
210
        }

211
        $this->view->translate();
212

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

215
216
        $archive->status = $this->view->translator->getText($archive->status, false, "recordsManagement/messages");
        $archive->finalDisposition = $this->view->translator->getText($archive->finalDisposition, false, "recordsManagement/messages");
Prosper De Laure's avatar
Prosper De Laure committed
217

218
        $this->getDescription($archive);
219
        $this->view->setSource('retentionRules', $retentionRules);
Prosper De Laure's avatar
Prosper De Laure committed
220
        $this->view->setSource("archive", $archive);
221
        $this->view->setSource("depositPrivilege", $depositPrivilege);
222
        $this->view->setSource("archivalProfileList", $archivalProfileList);
223
        $this->view->setSource("fileplanLevel", $fileplanLevel);
224
225
        $this->view->setSource("acceptArchiveWithoutProfile", $acceptArchiveWithoutProfile);
        $this->view->setSource("acceptUserIndex", $acceptUserIndex);
226
        $this->view->setSource('managementPrivilege', \laabs::callService('auth/userAccount/readHasprivilege', "archiveManagement/modify"));
227

228
        $this->view->merge();
229

230
231
232
        return $this->view->saveHtml();
    }

233
234
235
236
237
238
239
    /**
     * Show a document information
     *
     * @return string
     */
    public function documentInfo()
    {
240
241
        $this->view->addContentFile('dashboard/mainScreen/documentInformation.html');
        $this->view->translate();
242

243
244
        if (isset(\laabs::configuration('presentation.maarchRM')['displayableFormat'])) {
            $this->view->setSource("displayableFormat", json_encode(\laabs::configuration('presentation.maarchRM')['displayableFormat']));
245
246
        } else {
            $this->view->setSource("displayableFormat", json_encode(array()));
247
        }
248

249
250
        $this->view->merge();

251
252
        return $this->view->saveHtml();
    }
Prosper De Laure's avatar
Prosper De Laure committed
253
254
    /**
     * Display error
255
     * @param object $error
Prosper De Laure's avatar
Prosper De Laure committed
256
257
258
259
260
     *
     * @return string
     */
    public function error($error)
    {
Cyril Vazquez's avatar
Cyril Vazquez committed
261
        $this->view->addContentFile("dashboard/error.html");
Prosper De Laure's avatar
Prosper De Laure committed
262
263
264
265
266
267
268
269

        $this->view->translate();

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

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

Prosper De Laure's avatar
Prosper De Laure committed
271
    /**
272
     * Show a folder content
Prosper De Laure's avatar
Prosper De Laure committed
273
274
275
276
277
278
     * @param array $archives
     *
     * @return string
     */
    public function folderContents($archives)
    {
Alexis Ragot's avatar
Alexis Ragot committed
279
280
281
282
283
284
285
286
287
288
289
        $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
290
291
292
293
294
        $this->json->archives = $archives;

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

295
296
297
298
299
300
301
302
    /**
     * Show an archive content
     * @param object $archive
     *
     * @return string
     */
    public function archiveContent($archive)
    {
303
304
305
306
307
308
309
        if (isset($archive->digitalResources)) {
            $this->json->digitalResources = $archive->digitalResources;
        }

        if (isset($archive->childrenArchives)) {
            $this->json->childrenArchives = $archive->childrenArchives;
        }
310
311
312
313

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

314
    /**
Prosper De Laure's avatar
Prosper De Laure committed
315
     * Show the result of movin²g an archive into a folder
316
     * @param int $result
317
318
319
     *
     * @return string
     */
320
    public function moveArchivesToFolder($result)
321
    {
322
        $this->view->translator->setCatalog('filePlan/messages');
323
324
        if ($result == 1) {
            $this->json->message = "The archive was moved.";
325
326
            $this->json->message = $this->view->translator->getText($this->json->message);
            
327
        } else {
Alexis Ragot's avatar
Alexis Ragot committed
328
            $this->json->message = '%1$s archives were moved.';
329
330
            $this->json->message = $this->view->translator->getText($this->json->message);
            $this->json->message = sprintf($this->json->message, $result);
331
        }
332
333
334
335

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

336
337
338
339
340
341
342
343
    /**
     * Get archive description
     * @param archive $archive
     *
     * @return string
     */
    protected function getDescription($archive)
    {
344
        $archivalProfile = null;
345
346
        $modificationPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveManagement/modifyDescription");

347
348
349
350
351
        if (!empty($archive->archivalProfileReference)) {
            $archivalProfile = \laabs::callService('recordsManagement/archivalProfile/readByreference_reference_', $archive->archivalProfileReference);
            $archive->archivalProfileName = $archivalProfile->name;
        }

Alexis Ragot's avatar
Alexis Ragot committed
352
353
354
        if (!empty($archive->descriptionClass)) {
            $presenter = \laabs::newPresenter($archive->descriptionClass);
            $descriptionHtml = $presenter->read($archive->descriptionObject);
355
            $modificationPrivilege = false;
Prosper De Laure's avatar
Prosper De Laure committed
356

Alexis Ragot's avatar
Alexis Ragot committed
357
358
        } else {
            $descriptionHtml = '<table">';
359

360
361
362
363
364
365
366
367
368
            if (isset($archive->descriptionObject)) {
                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;
Dylan's avatar
Dylan committed
369
                                $isImmutable = $archiveDescription->isImmutable;
370
                            }
371
                        }
372
373
                    }

374
375
376
                    if (empty($label)) {
                        $label = $this->view->translator->getText($name, false, "recordsManagement/archive");
                    }
377

378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
                    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;
                        }
396
                    }
Dylan's avatar
Dylan committed
397
                    if(!is_array($value)){
398

Dylan's avatar
Dylan committed
399
400
401
402
403
                        if ($archivalProfileField) {
                            $descriptionHtml .= '<tr class="archivalProfileField">';
                        } else {
                            $descriptionHtml .= '<tr>';
                        }
404

Dylan's avatar
Dylan committed
405
                    $descriptionHtml .= '<th title="'.$label.'" name="'.$name.'" data-type="'.$type.'"'.'data-Immutable="'.$isImmutable.'">'.$label.'</th>';
406
407
408
409
410
                    if ($type == "date") {
                            $textValue = \laabs::newDate($value);
                    } else {
                        $textValue = $value;

Dylan's avatar
Dylan committed
411
412
413
414
415
416
417
                        }
                        if ($type == 'boolean') {
                            $textValue = $value ? '<i class="fa fa-check" data-value="1"/>' : '<i class="fa fa-times" data-value="0"/>';
                        }

                        $descriptionHtml .= '<td title="'.$value.'">'.$textValue.'</td>';
                        $descriptionHtml .= '</tr>';
418
                    }
419
420
421
                    if ($type == 'boolean') {
                        $textValue = $value ? '<i class="fa fa-check" data-value="1"/>' : '<i class="fa fa-times" data-value="0"/>';
                    }
422
423
                }

424
            }
425
            $descriptionHtml .= '</table>';
Alexis Ragot's avatar
Alexis Ragot committed
426
427
428
429
430
431
432
        }

        if ($descriptionHtml) {
            $node = $this->view->getElementById("metadata");
            $this->view->addContent($descriptionHtml, $node);
        } else {
            unset($archive->descriptionObject);
433
        }
434
435

        $this->view->setSource('modificationPrivilege', $modificationPrivilege);
436
    }
Prosper De Laure's avatar
Prosper De Laure committed
437

438
439
440
441
    /**
     * Mark leaf for html merging
     * @param object $tree The tree
     */
442
443
    protected function markTreeLeaf($tree)
    {
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
        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
     */
463
464
    protected function updateFolderPath($tree, $ownerName)
    {
465
466
467
468
469
470
471
        foreach ($tree as $node) {
            $node->path = $ownerName.'/'.$node->path;
            if ($node->subFolders) {
                $this->updateFolderPath($node->subFolders, $ownerName);
            }
        }
    }
Alexis Ragot's avatar
Alexis Ragot committed
472
473
474

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

477
478
479
480
481
482
483
484
485
486
487
        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);

Alexis Ragot's avatar
Alexis Ragot committed
488
489
490
491
492
493
        if (!empty($orgUnit->organization)) {
            foreach ($orgUnit->organization as $subOrgUnit) {
                $this->getOrgUnitArchivalProfiles($subOrgUnit);
            }
        }
    }
Prosper De Laure's avatar
Prosper De Laure committed
494
}