Completed
Push — master ( 622ced...91aa98 )
by Craig
05:56
created

BlocksModuleInstaller::removeSlashFromBKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 10
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
                $this->updateLangToLocaleBlock();
49
                $this->removeSlashFromBKey();
50
                $this->convertTemplatePaths();
51
        }
52
53
        return true;
54
    }
55
56
    private function updateLangToLocaleBlock(): void
57
    {
58
        // for Core-1.4.4
59
        $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

59
        $statement = $this->entityManager->/** @scrutinizer ignore-call */ getConnection()->executeQuery("SELECT * FROM blocks WHERE blocktype = 'Lang'");
Loading history...
60
        $blocks = $statement->fetchAll(\PDO::FETCH_ASSOC);
61
        if (count($blocks) > 0) {
62
            $this->entityManager->getConnection()->executeQuery("UPDATE blocks set bkey=?, blocktype=?, properties=? WHERE blocktype = 'Lang'", [
63
                'ZikulaSettingsModule:Zikula\SettingsModule\Block\LocaleBlock',
64
                'Locale',
65
                'a:0:{}'
66
            ]);
67
            $this->addFlash('success', 'All instances of LangBlock have been converted to LocaleBlock.');
68
        }
69
        $this->entityManager->getConnection()->executeQuery(
70
            "UPDATE group_perms SET component = REPLACE(component, 'Languageblock', 'LocaleBlock') WHERE component LIKE 'Languageblock%'"
71
        );
72
    }
73
74
    private function removeSlashFromBKey(): void
75
    {
76
        // for Core-3.0.0
77
        $statement = $this->entityManager->getConnection()->executeQuery("SELECT * FROM blocks");
78
        $blocks = $statement->fetchAll(\PDO::FETCH_ASSOC);
79
        foreach ($blocks as $block) {
80
            $bKey = $block['bkey'];
81
            if (mb_strpos($bKey, ':')) {
82
                [/*$moduleName*/, $bKey] = explode(':', $bKey);
83
            }
84
            $this->entityManager->getConnection()->executeUpdate('UPDATE blocks SET bKey=? WHERE bid=?', [trim($bKey, '\\'), $block['bid']]);
85
        }
86
    }
87
88
    private function convertTemplatePaths(): void
89
    {
90
        // for Core-3.0.0
91
        $statement = $this->entityManager->getConnection()->executeQuery("SELECT * FROM blocks WHERE blocktype = 'Menu'");
92
        $blocks = $statement->fetchAll(\PDO::FETCH_ASSOC);
93
        foreach ($blocks as $block) {
94
            $properties = unserialize($block['properties']);
95
            if (isset($properties['template'])) {
96
                $properties['template'] = '@' . str_replace(':', '/', $properties['template']);
97
                $this->entityManager->getConnection()->executeUpdate('UPDATE blocks SET properties=? WHERE bid=?', [serialize($properties), $block['bid']]);
98
            }
99
        }
100
    }
101
102
    public function uninstall(): bool
103
    {
104
        // Deletion not allowed
105
        return false;
106
    }
107
108
    /**
109
     * Add default block data for new installations.
110
     * This is called after a complete installation since the blocks
111
     * need to be populated with module id's which are only available
112
     * once the installation has been completed.
113
     */
114
    public function createDefaultData(): void
115
    {
116
        // create the default block positions - left, right and center for the traditional 3 column layout
117
        $positions = [
118
            'left' => $this->trans('Left blocks'),
119
            'right' => $this->trans('Right blocks'),
120
            'center' => $this->trans('Center blocks'),
121
            'search' => $this->trans('Search block'),
122
            'header' => $this->trans('Header block'),
123
            'footer' => $this->trans('Footer block'),
124
            'topnav' => $this->trans('Top navigation block'),
125
            'bottomnav' => $this->trans('Bottom navigation block')
126
        ];
127
        foreach ($positions as $name => $description) {
128
            $positions[$name] = new BlockPositionEntity();
129
            $positions[$name]->setName($name);
130
            $positions[$name]->setDescription($description);
131
            $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

131
            $this->entityManager->persist(/** @scrutinizer ignore-type */ $positions[$name]);
Loading history...
132
        }
133
        $this->entityManager->flush();
134
135
        $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>');
136
137
        $blocks = [];
138
        $extensionRepo = $this->entityManager->getRepository(ExtensionEntity::class);
139
        $blocksModuleEntity = $extensionRepo->findOneBy(['name' => 'ZikulaBlocksModule']);
140
        $searchModuleEntity = $extensionRepo->findOneBy(['name' => 'ZikulaSearchModule']);
141
        $usersModuleEntity = $extensionRepo->findOneBy(['name' => 'ZikulaUsersModule']);
142
        $blocks[] = [
143
            'bkey' => SearchBlock::class,
144
            'blocktype' => 'Search',
145
            'language' => '',
146
            'module' => $searchModuleEntity,
147
            'title' => $this->trans('Search box'),
148
            'description' => $this->trans('Search block'),
149
            'properties' => [
150
                'displaySearchBtn' => true,
151
                'active' => ['ZikulaUsersModule' => 1]
152
            ],
153
            'position' => $positions['left']
154
        ];
155
        $blocks[] = [
156
            'bkey' => HtmlBlock::class,
157
            'blocktype' => 'Html',
158
            'language' => '',
159
            'module' => $blocksModuleEntity,
160
            'title' => $this->trans('This site is powered by Zikula!'),
161
            'description' => $this->trans('HTML block'),
162
            'properties' => ['content' => $hellomessage],
163
            'position' => $positions['center']
164
        ];
165
        $blocks[] = [
166
            'bkey' => LoginBlock::class,
167
            'blocktype' => 'Login',
168
            'language' => '',
169
            'module' => $usersModuleEntity,
170
            'title' => $this->trans('User log-in'),
171
            'description' => $this->trans('Login block'),
172
            'position' => $positions['topnav'],
173
            'order' => 1,
174
            'filters' => [[
175
                'attribute' => '_route',
176
                'queryParameter' => null,
177
                'comparator' => '!=',
178
                'value' => 'zikulausersmodule_access_login'
179
            ]]
180
        ];
181
182
        foreach ($blocks as $block) {
183
            $blockEntity = new BlockEntity();
184
            $position = $block['position'];
185
            $sortOrder = !empty($block['order']) ? $block['order'] : 0;
186
            unset($block['position'], $block['order']);
187
            $blockEntity->merge($block);
188
            $this->entityManager->persist($blockEntity);
189
            $placement = new BlockPlacementEntity();
190
            $placement->setBlock($blockEntity);
191
            $placement->setPosition($position);
192
            $placement->setSortorder($sortOrder);
193
            $this->entityManager->persist($placement);
194
        }
195
        $this->entityManager->flush();
196
    }
197
}
198