welcome.php 16.8 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
65
66
        $accountToken = \laabs::getToken('AUTH');
        $user = \laabs::newController('auth/userAccount')->get($accountToken->accountId);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

163
164
165
166
167
        // 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
168

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

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

181
                if (count($archivalProfile->containedProfiles)) {
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
                     $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;
                }
199
200
201

                $acceptArchiveWithoutProfile = $archivalProfile->acceptArchiveWithoutProfile;
                $acceptUserIndex = $archivalProfile->acceptUserIndex;
202
203
            } else {
                $acceptArchiveWithoutProfile = true;
Prosper De Laure's avatar
Prosper De Laure committed
204
            }
Prosper De Laure's avatar
Prosper De Laure committed
205
206
        }

207
        $this->view->translate();
208

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

211
212
        $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
213

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

223
        $this->view->merge();
224

225
226
227
        return $this->view->saveHtml();
    }

228
229
230
231
232
233
234
    /**
     * Show a document information
     *
     * @return string
     */
    public function documentInfo()
    {
235
236
        $this->view->addContentFile('dashboard/mainScreen/documentInformation.html');
        $this->view->translate();
237

238
239
240
241
        if (isset(\laabs::configuration('presentation.maarchRM')['displayableFormat'])) {
            $this->view->setSource("displayableFormat", json_encode(\laabs::configuration('presentation.maarchRM')['displayableFormat']));
            $this->view->merge();
        }
242

243
244
        return $this->view->saveHtml();
    }
Prosper De Laure's avatar
Prosper De Laure committed
245
246
    /**
     * Display error
247
     * @param object $error
Prosper De Laure's avatar
Prosper De Laure committed
248
249
250
251
252
     *
     * @return string
     */
    public function error($error)
    {
Cyril Vazquez's avatar
Cyril Vazquez committed
253
        $this->view->addContentFile("dashboard/error.html");
Prosper De Laure's avatar
Prosper De Laure committed
254
255
256
257
258
259
260
261

        $this->view->translate();

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

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

Prosper De Laure's avatar
Prosper De Laure committed
263
    /**
264
     * Show a folder content
Prosper De Laure's avatar
Prosper De Laure committed
265
266
267
268
269
270
     * @param array $archives
     *
     * @return string
     */
    public function folderContents($archives)
    {
Alexis Ragot's avatar
Alexis Ragot committed
271
272
273
274
275
276
277
278
279
280
281
        $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
282
283
284
285
286
        $this->json->archives = $archives;

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

287
288
289
290
291
292
293
294
    /**
     * Show an archive content
     * @param object $archive
     *
     * @return string
     */
    public function archiveContent($archive)
    {
295
296
297
298
299
300
301
        if (isset($archive->digitalResources)) {
            $this->json->digitalResources = $archive->digitalResources;
        }

        if (isset($archive->childrenArchives)) {
            $this->json->childrenArchives = $archive->childrenArchives;
        }
302
303
304
305

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

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

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

328
329
330
331
332
333
334
335
    /**
     * Get archive description
     * @param archive $archive
     *
     * @return string
     */
    protected function getDescription($archive)
    {
336
        $archivalProfile = null;
337
338
        $modificationPrivilege = \laabs::callService('auth/userAccount/readHasprivilege', "archiveManagement/modifyDescription");

339

340
341
342
343
344
        if (!empty($archive->archivalProfileReference)) {
            $archivalProfile = \laabs::callService('recordsManagement/archivalProfile/readByreference_reference_', $archive->archivalProfileReference);
            $archive->archivalProfileName = $archivalProfile->name;
        }

Alexis Ragot's avatar
Alexis Ragot committed
345
346
347
        if (!empty($archive->descriptionClass)) {
            $presenter = \laabs::newPresenter($archive->descriptionClass);
            $descriptionHtml = $presenter->read($archive->descriptionObject);
348
            $modificationPrivilege = false;
Prosper De Laure's avatar
Prosper De Laure committed
349

Alexis Ragot's avatar
Alexis Ragot committed
350
351
        } else {
            $descriptionHtml = '<table">';
352

353
354
355
356
357
358
359
360
361
362
            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;
                            }
363
                        }
364
365
                    }

366
367
368
                    if (empty($label)) {
                        $label = $this->view->translator->getText($name, false, "recordsManagement/archive");
                    }
369

370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
                    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;
                        }
388
                    }
389

390
391
392
393
394
395
396
                    if ($archivalProfileField) {
                        $descriptionHtml .= '<tr class="archivalProfileField">';
                    } else {
                        $descriptionHtml .= '<tr>';
                    }

                    $descriptionHtml .= '<th title="'.$label.'" name="'.$name.'" data-type="'.$type.'">'.$label.'</th>';
397
398
399
400
401
402
403
                    if ($type == "date") {
                            $textValue = \laabs::newDate($value);
                            $textValue = $textValue->format("d/m/Y");
                    } else {
                        $textValue = $value;

                    }
404
405
406
407
                    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>';
408
                    $descriptionHtml .= '</tr>';
409
410
                }

411
            }
412
            $descriptionHtml .= '</table>';
Alexis Ragot's avatar
Alexis Ragot committed
413
414
415
416
417
418
419
        }

        if ($descriptionHtml) {
            $node = $this->view->getElementById("metadata");
            $this->view->addContent($descriptionHtml, $node);
        } else {
            unset($archive->descriptionObject);
420
        }
421
422

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

425
426
427
428
    /**
     * Mark leaf for html merging
     * @param object $tree The tree
     */
429
430
    protected function markTreeLeaf($tree)
    {
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
        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
     */
450
451
    protected function updateFolderPath($tree, $ownerName)
    {
452
453
454
455
456
457
458
        foreach ($tree as $node) {
            $node->path = $ownerName.'/'.$node->path;
            if ($node->subFolders) {
                $this->updateFolderPath($node->subFolders, $ownerName);
            }
        }
    }
Alexis Ragot's avatar
Alexis Ragot committed
459
460
461

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

464
465
466
467
468
469
470
471
472
473
474
        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
475
476
477
478
479
480
        if (!empty($orgUnit->organization)) {
            foreach ($orgUnit->organization as $subOrgUnit) {
                $this->getOrgUnitArchivalProfiles($subOrgUnit);
            }
        }
    }
Prosper De Laure's avatar
Prosper De Laure committed
481
}