Passed
Push — master ( 8a455a...42a228 )
by Craig
07:01
created

AdminModuleInstaller::createDefaultData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 24
nc 2
nop 0
dl 0
loc 42
rs 9.536
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\AdminModule;
15
16
use Exception;
17
use Zikula\AdminModule\Entity\AdminCategoryEntity;
18
use Zikula\AdminModule\Entity\AdminModuleEntity;
19
use Zikula\Core\AbstractExtensionInstaller;
20
21
/**
22
 * Installation and upgrade routines for the admin module.
23
 */
24
class AdminModuleInstaller extends AbstractExtensionInstaller
25
{
26
    public function install(): bool
27
    {
28
        try {
29
            $this->schemaTool->create([
30
                AdminCategoryEntity::class,
31
                AdminModuleEntity::class
32
            ]);
33
        } catch (Exception $exception) {
34
            return false;
35
        }
36
37
        $this->setVar('modulesperrow', 3);
38
        $this->setVar('itemsperpage', 15);
39
        $this->setVar('defaultcategory', 5);
40
        $this->setVar('admingraphic', 1);
41
        $this->setVar('startcategory', 1);
42
        // change below to 0 before release - just makes it easier doing development meantime - drak
43
        // we can now leave this at 0 since the code also checks the development flag (config.php) - markwest
44
        $this->setVar('ignoreinstallercheck', 0);
45
        $this->setVar('admintheme');
46
        $this->setVar('displaynametype', 1);
47
48
        $this->createDefaultData();
49
50
        // Initialisation successful
51
        return true;
52
    }
53
54
    public function upgrade(string $oldVersion): bool
55
    {
56
        // Upgrade dependent on old version number
57
        switch ($oldVersion) {
58
            case '1.9.1':
59
                // ensure there is a proper sortorder for modulecategories
60
                // has the sort order already been set?
61
                $categories = $this->entityManager->getRepository('ZikulaAdminModule:AdminCategoryEntity')
62
                    ->findBy(['sortorder' => 0]);
63
                if (count($categories) > 1) {
64
                    // sort categories by id
65
                    $dql = "
66
                        UPDATE Zikula\\AdminModule\\Entity\\AdminCategoryEntity ac
67
                        SET ac.sortorder = ac.cid - 1
68
                    ";
69
                    $query = $this->entityManager->createQuery($dql);
70
                    $query->execute();
71
                }
72
            case '1.9.2':
73
            // future upgrade routines
74
        }
75
76
        // Update successful
77
        return true;
78
    }
79
80
    public function uninstall(): bool
81
    {
82
        return false;
83
    }
84
85
    /**
86
     * Create the default data for the Admin module.
87
     */
88
    public function createDefaultData(): void
89
    {
90
        $records = [
91
            [
92
                'name' => $this->__('System'),
93
                'description' => $this->__('Core modules at the heart of operation of the site.'),
94
                'sortorder' => 0
95
            ],
96
            [
97
                'name' => $this->__('Layout'),
98
                'description' => $this->__("Layout modules for controlling the site's look and feel."),
99
                'sortorder' => 1
100
            ],
101
            [
102
                'name' => $this->__('Users'),
103
                'description' => $this->__('Modules for controlling user membership, access rights and profiles.'),
104
                'sortorder' => 2
105
            ],
106
            [
107
                'name' => $this->__('Content'),
108
                'description' => $this->__('Modules for providing content to your users.'),
109
                'sortorder' => 3
110
            ],
111
            [
112
                'name' => $this->__('Uncategorised'),
113
                'description' => $this->__('Newly-installed or uncategorized modules.'),
114
                'sortorder' => 4
115
            ],
116
            [
117
                'name' => $this->__('Security'),
118
                'description' => $this->__('Modules for managing the site\'s security.'),
119
                'sortorder' => 5
120
            ]
121
        ];
122
123
        foreach ($records as $record) {
124
            $item = new AdminCategoryEntity();
125
            $item->merge($record);
126
            $this->entityManager->persist($item);
127
        }
128
129
        $this->entityManager->flush();
130
    }
131
}
132