Completed
Push — master ( a12ac5...2d0854 )
by Craig
06:25
created

BlocksModuleInstaller   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 96
c 0
b 0
f 0
dl 0
loc 142
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A uninstall() 0 4 1
B createDefaultData() 0 82 4
A install() 0 6 1
A upgrade() 0 30 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - 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\BlocksModule;
15
16
use Zikula\BlocksModule\Block\HtmlBlock;
17
use Zikula\BlocksModule\Entity\BlockEntity;
18
use Zikula\BlocksModule\Entity\BlockPlacementEntity;
19
use Zikula\BlocksModule\Entity\BlockPositionEntity;
20
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
21
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
22
use Zikula\SearchModule\Block\SearchBlock;
23
use Zikula\UsersModule\Block\LoginBlock;
24
25
class BlocksModuleInstaller extends AbstractExtensionInstaller
26
{
27
    private $entities = [
28
        BlockEntity::class,
29
        BlockPositionEntity::class,
30
        BlockPlacementEntity::class
31
    ];
32
33
    public function install(): bool
34
    {
35
        $this->schemaTool->create($this->entities);
36
        $this->setVar('collapseable', false);
37
38
        return true;
39
    }
40
41
    public function upgrade(string $oldVersion): bool
42
    {
43
        switch ($oldVersion) {
44
            // 3.9.6 shipped with Core-1.4.3
45
            // 3.9.8 shipped with Core-2.0.15
46
            // version number reset to 3.0.0 at Core 3.0.0
47
            case '2.9.9':
48
                $statement = $this->entityManager->getConnection()->executeQuery("SELECT * FROM blocks WHERE blocktype = 'Lang'");
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Doctrine\Persistence\ObjectManager. It seems like you code against a sub-type of Doctrine\Persistence\ObjectManager such as Doctrine\ORM\Decorator\EntityManagerDecorator or Doctrine\ORM\EntityManagerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
                $statement = $this->entityManager->/** @scrutinizer ignore-call */ getConnection()->executeQuery("SELECT * FROM blocks WHERE blocktype = 'Lang'");
Loading history...
49
                $blocks = $statement->fetchAll(\PDO::FETCH_ASSOC);
50
                if (count($blocks) > 0) {
51
                    $this->entityManager->getConnection()->executeQuery("UPDATE blocks set bkey=?, blocktype=?, properties=? WHERE blocktype = 'Lang'", [
52
                        'ZikulaSettingsModule:Zikula\SettingsModule\Block\LocaleBlock',
53
                        'Locale',
54
                        'a:0:{}'
55
                    ]);
56
                    $this->addFlash('success', 'All instances of LangBlock have been converted to LocaleBlock.');
57
                }
58
                $this->entityManager->getConnection()->executeQuery("UPDATE group_perms SET component = REPLACE(component, 'Languageblock', 'LocaleBlock') WHERE component LIKE 'Languageblock%'");
59
                $statement = $this->entityManager->getConnection()->executeQuery("SELECT * FROM blocks");
60
                $blocks = $statement->fetchAll(\PDO::FETCH_ASSOC);
61
                foreach ($blocks as $block) {
62
                    $bKey = $block['bkey'];
63
                    if (mb_strpos($bKey, ':')) {
64
                        [/*$moduleName*/, $bKey] = explode(':', $bKey);
65
                    }
66
                    $this->entityManager->getConnection()->executeUpdate('UPDATE blocks SET bKey=? WHERE bid=?', [trim($bKey, '\\'), $block['bid']]);
67
                }
68
        }
69
70
        return true;
71
    }
72
73
    public function uninstall(): bool
74
    {
75
        // Deletion not allowed
76
        return false;
77
    }
78
79
    /**
80
     * Add default block data for new installations.
81
     * This is called after a complete installation since the blocks
82
     * need to be populated with module id's which are only available
83
     * once the installation has been completed.
84
     */
85
    public function createDefaultData(): void
86
    {
87
        // create the default block positions - left, right and center for the traditional 3 column layout
88
        $positions = [
89
            'left' => $this->trans('Left blocks'),
90
            'right' => $this->trans('Right blocks'),
91
            'center' => $this->trans('Center blocks'),
92
            'search' => $this->trans('Search block'),
93
            'header' => $this->trans('Header block'),
94
            'footer' => $this->trans('Footer block'),
95
            'topnav' => $this->trans('Top navigation block'),
96
            'bottomnav' => $this->trans('Bottom navigation block')
97
        ];
98
        foreach ($positions as $name => $description) {
99
            $positions[$name] = new BlockPositionEntity();
100
            $positions[$name]->setName($name);
101
            $positions[$name]->setDescription($description);
102
            $this->entityManager->persist($positions[$name]);
0 ignored issues
show
Bug introduced by
$positions[$name] of type string is incompatible with the type object expected by parameter $object of Doctrine\Persistence\ObjectManager::persist(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
            $this->entityManager->persist(/** @scrutinizer ignore-type */ $positions[$name]);
Loading history...
103
        }
104
        $this->entityManager->flush();
105
106
        $hellomessage = $this->trans('<p><a href="https://ziku.la">Zikula</a> is an Open Source Content Application Framework built on top of Symfony.</p><p>With Zikula you get:</p><ul><li><strong>Power:</strong> You get the all the features of <a href="https://symfony.com">Symfony</a> PLUS: </li><li><strong>User Management:</strong> Built in User and Group management with Rights/Roles control</li><li><strong>Front end control:</strong> You can customise all aspects of the site\'s appearance through themes, with support for <a href="http://jquery.com">jQuery</a>, <a href="http://getbootstrap.com">Bootstrap</a> and many other modern technologies</li><li><strong>Internationalization (i18n):</strong> You can mark content as being suitable for either a single language or for all languages, and can control all aspects of localisation of your site</li><li><strong>Extensibility:</strong> you get a standard application-programming interface (API) that lets you easily extend your site\'s functionality through modules</li><li><strong>More:</strong> Admin UI, global categories, site-wide search, content blocks, menu creation, and more!</li><li><strong>Support:</strong> you can get help and support from the Zikula community of webmasters and developers at <a href="https://ziku.la">ziku.la</a>, <a href="https://github.com/zikula/core">Github</a> and <a href="https://zikula.slack.com/">Slack</a>.</li></ul><p>Enjoy using Zikula!</p><p><strong>The Zikula team</strong></p><p><em>Note: Zikula is Free Open Source Software (FOSS) licensed under the GNU General Public License.</em></p>');
107
108
        $blocks = [];
109
        $extensionRepo = $this->entityManager->getRepository(ExtensionEntity::class);
110
        $blocksModuleEntity = $extensionRepo->findOneBy(['name' => 'ZikulaBlocksModule']);
111
        $searchModuleEntity = $extensionRepo->findOneBy(['name' => 'ZikulaSearchModule']);
112
        $usersModuleEntity = $extensionRepo->findOneBy(['name' => 'ZikulaUsersModule']);
113
        $blocks[] = [
114
            'bkey' => SearchBlock::class,
115
            'blocktype' => 'Search',
116
            'language' => '',
117
            'module' => $searchModuleEntity,
118
            'title' => $this->trans('Search box'),
119
            'description' => $this->trans('Search block'),
120
            'properties' => [
121
                'displaySearchBtn' => true,
122
                'active' => ['ZikulaUsersModule' => 1]
123
            ],
124
            'position' => $positions['left']
125
        ];
126
        $blocks[] = [
127
            'bkey' => HtmlBlock::class,
128
            'blocktype' => 'Html',
129
            'language' => '',
130
            'module' => $blocksModuleEntity,
131
            'title' => $this->trans('This site is powered by Zikula!'),
132
            'description' => $this->trans('HTML block'),
133
            'properties' => ['content' => $hellomessage],
134
            'position' => $positions['center']
135
        ];
136
        $blocks[] = [
137
            'bkey' => LoginBlock::class,
138
            'blocktype' => 'Login',
139
            'language' => '',
140
            'module' => $usersModuleEntity,
141
            'title' => $this->trans('User log-in'),
142
            'description' => $this->trans('Login block'),
143
            'position' => $positions['topnav'],
144
            'order' => 1,
145
            'filters' => [[
146
                'attribute' => '_route',
147
                'queryParameter' => null,
148
                'comparator' => '!=',
149
                'value' => 'zikulausersmodule_access_login'
150
            ]]
151
        ];
152
153
        foreach ($blocks as $block) {
154
            $blockEntity = new BlockEntity();
155
            $position = $block['position'];
156
            $sortOrder = !empty($block['order']) ? $block['order'] : 0;
157
            unset($block['position'], $block['order']);
158
            $blockEntity->merge($block);
159
            $this->entityManager->persist($blockEntity);
160
            $placement = new BlockPlacementEntity();
161
            $placement->setBlock($blockEntity);
162
            $placement->setPosition($position);
163
            $placement->setSortorder($sortOrder);
164
            $this->entityManager->persist($placement);
165
        }
166
        $this->entityManager->flush();
167
    }
168
}
169