Skip to content

Commit

Permalink
Add update process
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpoensgen committed Feb 9, 2019
1 parent 56b565f commit 136b901
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 1 deletion.
122 changes: 122 additions & 0 deletions Bootstrap/Updater.php
@@ -0,0 +1,122 @@
<?php

namespace FroshShareBasket\Bootstrap;

use Doctrine\DBAL\Connection;
use Shopware\Components\Model\ModelManager;
use Shopware\Models\Shop\DetachedShop;

class Updater
{
/**
* @var Connection
*/
private $connection;

/**
* @var \sRewriteTable
*/
private $rewriteTableModule;

/**
* @var ModelManager
*/
private $modelManager;

/**
* @var string
*/
private $pluginDir;

/**
* Updater constructor.
*
* @param Connection $connection
* @param \sRewriteTable $rewriteTableModule
* @param ModelManager $modelManager
* @param string $pluginDir
*/
public function __construct(
Connection $connection,
\sRewriteTable $rewriteTableModule,
ModelManager $modelManager,
string $pluginDir
) {
$this->connection = $connection;
$this->rewriteTableModule = $rewriteTableModule;
$this->modelManager = $modelManager;
$this->pluginDir = $pluginDir;
}

/**
* @param string $oldVersion
*
* @throws \Doctrine\DBAL\DBALException
*/
public function update($oldVersion)
{
if (version_compare($oldVersion, '1.1.0', '<')) {
$this->prepareBaskets();
}
}

/**
* @throws \Doctrine\DBAL\DBALException
*/
private function prepareBaskets()
{
$sql = file_get_contents($this->pluginDir . '/Resources/sql/update.1.1.0.sql');
$this->connection->query($sql);

$sql = 'SELECT * FROM `s_plugin_sharebasket_baskets`';

$baskets = $this->connection->executeQuery($sql)->fetchAll();

foreach ($baskets as $basket) {
$hash = sha1(serialize($basket));
$sql = 'UPDATE `s_plugin_sharebasket_baskets` SET hash = :hash WHERE id = :id';
$this->connection->executeUpdate($sql, ['hash' => $hash, ':id' => $basket['id']]);

$articles = json_decode($basket['articles'], true);
foreach ($articles as $article) {
$data = [
':share_basket_id' => $basket['id'],
':ordernumber' => $article['ordernumber'],
':quantity' => $article['quantity'],
':mode' => $article['modus'],
':attributes' => $article['attributes'] ? serialize($article['attributes']) : null,
];

$sql = 'INSERT INTO `s_plugin_sharebasket_articles` (`share_basket_id`, `ordernumber`, `quantity`, `mode`, `attributes`) VALUES (:share_basket_id, :ordernumber, :quantity, :mode, :attributes)';
$this->connection->executeUpdate($sql, $data);
}

$this->generateSeoUrl($basket['basketID']);
}
}

/**
* @param string $basketId
*
* @throws \Doctrine\DBAL\DBALException
*/
private function generateSeoUrl($basketId)
{
$shops = $this->connection->executeQuery('SELECT id FROM `s_core_shops` WHERE active = 1')->fetchAll(\PDO::FETCH_COLUMN);
$path = 'sharebasket/load/bID/' . $basketId;

/** @var \Shopware\Models\Shop\Repository $repository */
$repository = $this->modelManager->getRepository(\Shopware\Models\Shop\Shop::class);

foreach ($shops as $shopId) {
/** @var DetachedShop|null $shop */
$shop = $repository->getActiveById($shopId);
if ($shop === null) {
throw new \Doctrine\DBAL\DBALException('No valid shop id passed');
}
$shop->registerResources();

$this->rewriteTableModule->sInsertUrl('sViewport=FroshShareBasket&sAction=load&bID=' . $basketId, $path);
}
}
}
24 changes: 23 additions & 1 deletion FroshShareBasket.php
Expand Up @@ -2,10 +2,13 @@

namespace FroshShareBasket;

use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use FroshShareBasket\Bootstrap\Updater;
use FroshShareBasket\Models\Article;
use FroshShareBasket\Models\Basket;
use Shopware\Components\Model\ModelManager;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
Expand All @@ -25,11 +28,30 @@ public function install(InstallContext $context)

/**
* @param UpdateContext $context
*
* @throws \Doctrine\DBAL\DBALException
*/
public function update(UpdateContext $context)
{
parent::update($context);
/** @var Connection $connection */
$connection = $this->container->get('dbal_connection');

/** @var \sRewriteTable $rewriteTableModule */
$rewriteTableModule = $this->container->get('modules')->getModule('sRewriteTable');

/** @var ModelManager $modelManager */
$modelManager = $this->container->get('models');

$updater = new Updater(
$connection,
$rewriteTableModule,
$modelManager,
$this->getPath()
);
$updater->update($context->getCurrentVersion());

$this->installSchema();
$context->scheduleClearCache(ActivateContext::CACHE_LIST_DEFAULT);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions Resources/sql/update.1.1.0.sql
@@ -0,0 +1,9 @@
ALTER TABLE `s_plugin_sharebasket_baskets` ADD `hash` VARCHAR(255) NOT NULL AFTER `created`;
CREATE TABLE `s_plugin_sharebasket_articles` (
`id` int(11) NOT NULL,
`share_basket_id` int(11) DEFAULT NULL,
`ordernumber` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`quantity` int(11) NOT NULL,
`mode` int(11) NOT NULL,
`attributes` longtext COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

0 comments on commit 136b901

Please sign in to comment.