Passed
Pull Request — master (#155)
by
unknown
09:50
created

UpdateAccessRestrictions::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 13
rs 9.9332
1
<?php
2
/*
3
 * This file is part of the TYPO3 CMS project.
4
 *
5
 * It is free software; you can redistribute it and/or modify it under
6
 * the terms of the GNU General Public License, either version 2
7
 * of the License, or any later version.
8
 *
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 *
12
 * The TYPO3 project - inspiring people to share!
13
 */
14
15
if (!defined('TYPO3_MODE')) {
16
    die('Access denied.');
17
}
18
19
use EWW\Dpf\Domain\Repository\DocumentRepository;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Core\Registry;
22
use TYPO3\CMS\Extbase\Object\ObjectManager;
23
use EWW\Dpf\Security\Security;
24
25
26
class ext_update {
27
28
    // Ideally the version corresponds with the extension version
29
    const VERSION = "v3.0.0";
30
31
    public function access() {
32
        $registry = GeneralUtility::makeInstance(Registry::class);
33
        $version = $registry->get('tx_dpf','updatescript-'.self::VERSION);
34
35
        // If the version has already been registered in the table sys_register the updatscript will be blocked.
36
        if ($version) {
37
            return FALSE;
38
        }
39
40
        return TRUE;
41
    }
42
43
    public function main() {
44
        // This script registers itself into the sys_registry table to prevent a re-run with the same version number.
45
        $registry = GeneralUtility::makeInstance(Registry::class);
46
        $version = $registry->get('tx_dpf','updatescript-'.self::VERSION);
47
        if ($version) {
48
            return FALSE;
49
        } else {
50
51
            // The necessary updates.
52
            (new UpdateState)->execute();
53
            (new UpdateAccessRestrictions)->execute();
54
55
            $registry->set('tx_dpf','updatescript-'.self::VERSION,TRUE);
56
        }
57
58
        return "The extension has been successfully updated.";
59
    }
60
}
61
62
63
class UpdateState
64
{
65
    const OBJECT_STATE_NEW             = "NEW";
66
    const OBJECT_STATE_ACTIVE          = "ACTIVE";
67
    const OBJECT_STATE_INACTIVE        = "INACTIVE";
68
    const OBJECT_STATE_DELETED         = "DELETED";
69
    const OBJECT_STATE_LOCALLY_DELETED = "LOCALLY_DELETED";
70
71
    public function execute()
72
    {
73
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
74
        $documentRepository = $objectManager->get(DocumentRepository::class);
75
76
        $documents = $documentRepository->crossClientFindAll();
77
78
        foreach ($documents as $oldDocument) {
79
            $oldState = $oldDocument['state'];
80
            $objectIdentifier = $oldDocument['objectIdentifier'];
0 ignored issues
show
Unused Code introduced by
The assignment to $objectIdentifier is dead and can be removed.
Loading history...
81
82
            $newDocument = $documentRepository->findByUid($oldDocument['uid']);
83
84
            switch ($oldState) {
85
                case self::OBJECT_STATE_NEW:
86
                    $newDocument->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_NEW_NONE);
87
                    break;
88
                case self::OBJECT_STATE_ACTIVE:
89
                    $newDocument->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_IN_PROGRESS_ACTIVE);
90
                    break;
91
                case self::OBJECT_STATE_INACTIVE:
92
                    $newDocument->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_IN_PROGRESS_INACTIVE);
93
                    break;
94
                case self::OBJECT_STATE_DELETED:
95
                    $newDocument->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_IN_PROGRESS_DELETED);
96
                    break;
97
                case self::OBJECT_STATE_LOCALLY_DELETED:
98
                    $newDocument->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_NONE_NONE);
99
                    break;
100
            }
101
102
            $documentRepository->update($newDocument);
103
        }
104
    }
105
}
106
107
class UpdateAccessRestrictions
108
{
109
    public function execute() {
110
111
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
112
        $repositories[] = $objectManager->get(\EWW\Dpf\Domain\Repository\MetadataObjectRepository::class);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$repositories was never initialized. Although not strictly required by PHP, it is generally a good practice to add $repositories = array(); before regardless.
Loading history...
113
        $repositories[] = $objectManager->get(\EWW\Dpf\Domain\Repository\MetadataGroupRepository::class);
114
        $repositories[] = $objectManager->get(\EWW\Dpf\Domain\Repository\MetadataPageRepository::class);
115
116
        foreach ($repositories as $repository) {
117
            foreach ($repository->crossClientFindAll() as $record) {
118
                if ($record['backend_only']) {
119
                    $recordObject = $repository->findByUid($record['uid']);
120
                    $recordObject->setAccessRestrictionRoles(array(Security::ROLE_LIBRARIAN, Security::ROLE_RESEARCHER));
121
                    $repository->update($recordObject);
122
                }
123
            }
124
        }
125
    }
126
}