Completed
Pull Request — master (#4281)
by Craig
05:42 queued 57s
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\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
25
use Zikula\ExtensionsModule\AbstractExtension;
26
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
27
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
28
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
29
use Zikula\SearchModule\Block\SearchBlock;
30
use Zikula\UsersModule\Block\LoginBlock;
31
32
class BlocksModuleInstaller extends AbstractExtensionInstaller
33
{
34
    /**
35
     * @var array
36
     */
37
    private $entities = [
38
        BlockEntity::class,
39
        BlockPositionEntity::class,
40
        BlockPlacementEntity::class
41
    ];
42
43
    /**
44
     * @var ZikulaHttpKernelInterface
45
     */
46
    private $kernel;
47
48
    public function __construct(
49
        ZikulaHttpKernelInterface $kernel,
50
        AbstractExtension $extension,
51
        ManagerRegistry $managerRegistry,
52
        SchemaHelper $schemaTool,
53
        RequestStack $requestStack,
54
        TranslatorInterface $translator,
55
        VariableApiInterface $variableApi
56
    ) {
57
        $this->kernel = $kernel;
58
        parent::__construct($extension, $managerRegistry, $schemaTool, $requestStack, $translator, $variableApi);
59
    }
60
61
    public function install(): bool
62
    {
63
        $this->schemaTool->create($this->entities);
64
        $this->setVar('collapseable', false);
65
66
        return true;
67
    }
68
69
    public function upgrade(string $oldVersion): bool
70
    {
71
        switch ($oldVersion) {
72
            // 3.9.6 shipped with Core-1.4.3
73
            // 3.9.8 shipped with Core-2.0.15
74
            // version number reset to 3.0.0 at Core 3.0.0
75
            case '2.9.9':
76
                $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

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

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