Completed
Pull Request — master (#4281)
by Craig
05:33
created

BlocksModuleInstaller::upgrade()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 30
rs 9.3222
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 Doctrine\Persistence\ManagerRegistry;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
use Symfony\Contracts\Translation\TranslatorInterface;
19
use Zikula\BlocksModule\Block\HtmlBlock;
20
use Zikula\BlocksModule\Entity\BlockEntity;
21
use Zikula\BlocksModule\Entity\BlockPlacementEntity;
22
use Zikula\BlocksModule\Entity\BlockPositionEntity;
23
use Zikula\Bundle\CoreBundle\Doctrine\Helper\SchemaHelper;
24
use Zikula\ExtensionsModule\AbstractExtension;
25
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
26
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
27
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
28
use Zikula\SearchModule\Block\SearchBlock;
29
use Zikula\UsersModule\Block\LoginBlock;
30
31
class BlocksModuleInstaller extends AbstractExtensionInstaller
32
{
33
    private $entities = [
34
        BlockEntity::class,
35
        BlockPositionEntity::class,
36
        BlockPlacementEntity::class
37
    ];
38
39
    public function install(): bool
40
    {
41
        $this->schemaTool->create($this->entities);
42
        $this->setVar('collapseable', false);
43
44
        return true;
45
    }
46
47
    public function upgrade(string $oldVersion): bool
48
    {
49
        switch ($oldVersion) {
50
            // 3.9.6 shipped with Core-1.4.3
51
            // 3.9.8 shipped with Core-2.0.15
52
            // version number reset to 3.0.0 at Core 3.0.0
53
            case '2.9.9':
54
                $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

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

108
            $this->entityManager->persist(/** @scrutinizer ignore-type */ $positions[$name]);
Loading history...
109
        }
110
        $this->entityManager->flush();
111
112
        $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>');
113
114
        $blocks = [];
115
        $extensionRepo = $this->entityManager->getRepository(ExtensionEntity::class);
116
        $blocksModuleEntity = $extensionRepo->findOneBy(['name' => 'ZikulaBlocksModule']);
117
        $searchModuleEntity = $extensionRepo->findOneBy(['name' => 'ZikulaSearchModule']);
118
        $usersModuleEntity = $extensionRepo->findOneBy(['name' => 'ZikulaUsersModule']);
119
        $blocks[] = [
120
            'bkey' => SearchBlock::class,
121
            'blocktype' => 'Search',
122
            'language' => '',
123
            'module' => $searchModuleEntity,
124
            'title' => $this->trans('Search box'),
125
            'description' => $this->trans('Search block'),
126
            'properties' => [
127
                'displaySearchBtn' => true,
128
                'active' => ['ZikulaUsersModule' => 1]
129
            ],
130
            'position' => $positions['left']
131
        ];
132
        $blocks[] = [
133
            'bkey' => HtmlBlock::class,
134
            'blocktype' => 'Html',
135
            'language' => '',
136
            'module' => $blocksModuleEntity,
137
            'title' => $this->trans('This site is powered by Zikula!'),
138
            'description' => $this->trans('HTML block'),
139
            'properties' => ['content' => $hellomessage],
140
            'position' => $positions['center']
141
        ];
142
        $blocks[] = [
143
            'bkey' => LoginBlock::class,
144
            'blocktype' => 'Login',
145
            'language' => '',
146
            'module' => $usersModuleEntity,
147
            'title' => $this->trans('User log-in'),
148
            'description' => $this->trans('Login block'),
149
            'position' => $positions['topnav'],
150
            'order' => 1,
151
            'filters' => [[
152
                'attribute' => '_route',
153
                'queryParameter' => null,
154
                'comparator' => '!=',
155
                'value' => 'zikulausersmodule_access_login'
156
            ]]
157
        ];
158
159
        foreach ($blocks as $block) {
160
            $blockEntity = new BlockEntity();
161
            $position = $block['position'];
162
            $sortOrder = !empty($block['order']) ? $block['order'] : 0;
163
            unset($block['position'], $block['order']);
164
            $blockEntity->merge($block);
165
            $this->entityManager->persist($blockEntity);
166
            $placement = new BlockPlacementEntity();
167
            $placement->setBlock($blockEntity);
168
            $placement->setPosition($position);
169
            $placement->setSortorder($sortOrder);
170
            $this->entityManager->persist($placement);
171
        }
172
        $this->entityManager->flush();
173
    }
174
}
175