Skip to content

Commit

Permalink
Merge pull request #73 from oveleon/develop
Browse files Browse the repository at this point in the history
Fix migration (#71)
  • Loading branch information
doishub authored Dec 6, 2022
2 parents cc737da + 2b966bd commit d7f5677
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/Command/ObjectConversionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class ObjectConversionCommand extends Command
protected ContaoFramework $framework;
private Sync $sync;

public function __construct(ContaoFramework $contaoFramework)
public function __construct(ContaoFramework $contaoFramework, Sync $sync)
{
$this->framework = $contaoFramework;
$this->framework->initialize();

$this->sync = new Sync();
$this->sync = $sync;

parent::__construct();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Migration/Version30/ObjectConversionMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class ObjectConversionMigration extends AbstractMigration
*/
private $sync;

public function __construct(Connection $connection, ContaoFramework $framework)
public function __construct(Connection $connection, ContaoFramework $framework, Sync $sync)
{
$this->connection = $connection;

$this->framework = $framework;
$this->framework->initialize();

$this->sync = new Sync();
$this->sync = $sync;
}

public function shouldRun(): bool
Expand Down
73 changes: 49 additions & 24 deletions src/StyleManager/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,53 @@

use Contao\Backend;
use Contao\Controller;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\DataContainer;
use Contao\File;
use Contao\Message;
use Contao\Model\Collection;
use Contao\StringUtil;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use DOMDocument;
use DOMElement;
use DOMNode;
use Oveleon\ContaoComponentStyleManager\Model\StyleManagerArchiveModel;
use Oveleon\ContaoComponentStyleManager\Model\StyleManagerModel;

class Sync extends Backend
class Sync
{
public function __construct()
protected ContaoFramework $framework;
protected Connection $connection;

public function __construct(ContaoFramework $framework, Connection $connection)
{
parent::__construct();
$this->framework = $framework;
$this->framework->initialize();
$this->connection = $connection;
}

/**
* Check if the object conversion should be performed
*
* @throws Exception
*/
public function shouldRunObjectConversion($table = null): bool
{
$arrTables = $this->Database->listTables();
$schemaManager = method_exists($this->connection, 'createSchemaManager') ?
$this->connection->createSchemaManager() :
$this->connection->getSchemaManager()
;

if(null === $table || !in_array($table, $arrTables))
if(null === $table || !$schemaManager->tablesExist([$table]))
{
return false;
}

$objConfig = $this->Database->query("SELECT styleManager FROM " . $table . " WHERE styleManager IS NOT NULL LIMIT 0,1");
$objConfig = $this->connection->fetchFirstColumn("SELECT styleManager FROM " . $table . " WHERE styleManager IS NOT NULL");
$archives = StyleManagerArchiveModel::countAll();

if($objConfig && $archives > 0 && $arrConfig = StringUtil::deserialize($objConfig->styleManager))
if(count($objConfig) && $archives > 0 && $arrConfig = StringUtil::deserialize($objConfig[0]))
{
$key = array_key_first($arrConfig);

Expand All @@ -59,17 +72,24 @@ public function shouldRunObjectConversion($table = null): bool

/**
* Perform the object conversion
*
* @throws Exception
*/
public function performObjectConversion($table = null): void
{
$arrTables = $this->Database->listTables();
$schemaManager = method_exists($this->connection, 'createSchemaManager') ?
$this->connection->createSchemaManager() :
$this->connection->getSchemaManager()
;

if(null === $table || !in_array($table, $arrTables))
if(null === $table || !$schemaManager->tablesExist([$table]))
{
return;
}

if($objRows = $this->Database->query("SELECT id, styleManager FROM " . $table . " WHERE styleManager IS NOT NULL"))
$objRows = $this->connection->fetchAllAssociative("SELECT id, styleManager FROM " . $table . " WHERE styleManager IS NOT NULL");

if(!empty($objRows))
{
$objArchives = StyleManagerArchiveModel::findAll();
$arrArchives = [];
Expand All @@ -84,18 +104,17 @@ public function performObjectConversion($table = null): void
$arrArchives[ $objArchive->id ] = $objArchive->identifier;
}

$arrConfigs = $objRows->fetchEach('styleManager');
$arrIds = [];

foreach ($arrConfigs as $sttConfig)
foreach ($objRows as $rows)
{
if($arrConfig = StringUtil::deserialize($sttConfig))
if($arrConfig = StringUtil::deserialize($rows['styleManager']))
{
$arrIds = array_merge($arrIds, array_keys($arrConfig));
}
}

$objGroups = StyleManagerModel::findMultipleByIds($arrIds);
$objGroups = StyleManagerModel::findMultipleByIds(array_unique($arrIds));
$arrGroups = [];

if(null !== $objGroups)
Expand All @@ -105,7 +124,7 @@ public function performObjectConversion($table = null): void
$arrGroups[ $objGroup->id ] = $objGroup;
}

foreach ($objRows->fetchAllAssoc() as $arrRow)
foreach ($objRows as $arrRow)
{
$config = StringUtil::deserialize($arrRow['styleManager']);

Expand All @@ -128,12 +147,14 @@ public function performObjectConversion($table = null): void

$newConfig = array_combine($arrAliasPairKeys, $config);

$this->Database
->prepare("UPDATE " . $table . " SET styleManager=? WHERE id=?")
->execute(
serialize($newConfig),
$arrRow['id']
);
$this->connection->executeStatement("
UPDATE
".$table."
SET
styleManager = ?
WHERE
id = ?
", [serialize($newConfig), $arrRow['id']]);
}
}
}
Expand Down Expand Up @@ -257,7 +278,7 @@ public function export(?DataContainer $dc, $objArchives = null, bool $blnSendToB
if (null === $objArchives)
{
Message::addError($GLOBALS['TL_LANG']['ERR']['noStyleManagerConfigFound']);
self::redirect(self::getReferer());
Backend::redirect(Backend::getReferer());
}

// Root element
Expand Down Expand Up @@ -291,7 +312,7 @@ public function export(?DataContainer $dc, $objArchives = null, bool $blnSendToB
*/
protected function addArchiveData(DOMDocument $xml, DOMNode $archives, Collection $objArchive): void
{
$this->loadDataContainer('tl_style_manager_archive');
Controller::loadDataContainer('tl_style_manager_archive');

// Add archive node
$row = $xml->createElement('archive');
Expand All @@ -307,6 +328,8 @@ protected function addArchiveData(DOMDocument $xml, DOMNode $archives, Collectio

/**
* Add a children data row to the XML document
*
* @throws \DOMException
*/
protected function addChildrenData(DOMDocument $xml, DOMElement $archive, int $pid): void
{
Expand All @@ -321,7 +344,7 @@ protected function addChildrenData(DOMDocument $xml, DOMElement $archive, int $p
return;
}

$this->loadDataContainer('tl_style_manager');
Controller::loadDataContainer('tl_style_manager');

while($objChildren->next())
{
Expand All @@ -336,6 +359,8 @@ protected function addChildrenData(DOMDocument $xml, DOMElement $archive, int $p

/**
* Add field data to the XML document
*
* @throws \DOMException
*/
protected function addRowData(DOMDocument $xml, DOMNode $row, array $arrData): void
{
Expand Down

0 comments on commit d7f5677

Please sign in to comment.