Test Setup Failed
Pull Request — master (#4281)
by Craig
04:35
created

BlocksModuleInstaller::upgrade()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
c 0
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 __construct(
40
        AbstractExtension $extension,
41
        ManagerRegistry $managerRegistry,
42
        SchemaHelper $schemaTool,
43
        RequestStack $requestStack,
44
        TranslatorInterface $translator,
45
        VariableApiInterface $variableApi
46
    ) {
47
        parent::__construct($extension, $managerRegistry, $schemaTool, $requestStack, $translator, $variableApi);
48
    }
49
50
    public function install(): bool
51
    {
52
        $this->schemaTool->create($this->entities);
53
        $this->setVar('collapseable', false);
54
55
        return true;
56
    }
57
58
    public function upgrade(string $oldVersion): bool
59
    {
60
        switch ($oldVersion) {
61
            // 3.9.6 shipped with Core-1.4.3
62
            // 3.9.8 shipped with Core-2.0.15
63
            // version number reset to 3.0.0 at Core 3.0.0
64
            case '2.9.9':
65
                $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

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

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