286 lines
10 KiB
PHP
286 lines
10 KiB
PHP
<?php
|
|
|
|
namespace KupShop\ContentBundle\Resources\upgrade;
|
|
|
|
use FilipSedivy\EET\Utils\UUID;
|
|
use KupShop\ContentBundle\Util\Block;
|
|
use KupShop\KupShopBundle\Query\JsonOperator;
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
use Query\Operator;
|
|
|
|
class ProducersUpgrade extends \UpgradeNew
|
|
{
|
|
protected function isAllowed()
|
|
{
|
|
return findModule(\Modules::PRODUCERS);
|
|
}
|
|
|
|
public function check_dataColumn(): bool
|
|
{
|
|
return $this->checkColumnExists('producers', 'data');
|
|
}
|
|
|
|
/** producers: add data column */
|
|
public function upgrade_dataColumn(): void
|
|
{
|
|
sqlQuery('ALTER TABLE producers ADD COLUMN data MEDIUMTEXT DEFAULT NULL');
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_trigger_producers(): bool
|
|
{
|
|
return !$this->checkIfTriggerExists('trigger_producers');
|
|
}
|
|
|
|
/** Drops trigger */
|
|
public function upgrade_trigger_producers(): void
|
|
{
|
|
sqlQuery('DROP TRIGGER IF EXISTS trigger_producers');
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_topProducers(): bool
|
|
{
|
|
return $this->checkColumnExists('producers', 'top');
|
|
}
|
|
|
|
/** Add top to producers and set top = Y for all existing records */
|
|
public function upgrade_topProducers(): void
|
|
{
|
|
sqlQuery('
|
|
ALTER TABLE producers
|
|
ADD COLUMN `top` enum ("Y","N") DEFAULT "N" AFTER name;
|
|
');
|
|
|
|
sqlQueryBuilder()->update('producers')->directValues(['top' => 'Y'])->execute();
|
|
}
|
|
|
|
public function check_blocksForProducers(): bool
|
|
{
|
|
return $this->checkColumnExists('producers', 'id_block');
|
|
}
|
|
|
|
/** Add id_block to producers and move content from producers.descr to blocks */
|
|
public function upgrade_blocksForProducers(): void
|
|
{
|
|
sqlQuery('
|
|
ALTER TABLE producers
|
|
ADD COLUMN `id_block` int(11) UNSIGNED DEFAULT NULL AFTER id,
|
|
ADD CONSTRAINT `fk_producers_blocks` FOREIGN KEY (id_block) REFERENCES blocks(id)
|
|
ON DELETE SET NULL ON UPDATE CASCADE;
|
|
');
|
|
|
|
$translations = findModule(\Modules::TRANSLATIONS);
|
|
|
|
$producers = sqlQueryBuilder()->select('*')->from('producers')->execute();
|
|
foreach ($producers as $producer) {
|
|
if (!($producer['descr'] ?? null)) {
|
|
continue;
|
|
}
|
|
|
|
// create root block and assign it to producers.id_block
|
|
$this->insertSQL('blocks', []);
|
|
$rootBlockID = sqlInsertId();
|
|
$this->updateSQL('producers', ['id_block' => $rootBlockID], ['id' => $producer['id']]);
|
|
|
|
$uuid = UUID::v4();
|
|
$blockObj = new \stdClass();
|
|
$blockObj->type = 'legacy';
|
|
$blockObj->id = $uuid;
|
|
$settings = new \stdClass();
|
|
$settings->html = $producer['descr'];
|
|
$blockObj->settings = $settings;
|
|
|
|
$json = json_encode([$blockObj]);
|
|
|
|
$this->insertSQL('blocks', [
|
|
'id_root' => $rootBlockID,
|
|
'id_parent' => $rootBlockID,
|
|
'position' => 1,
|
|
'name' => $producer['name'],
|
|
'content' => $producer['descr'],
|
|
'json_content' => $json,
|
|
]);
|
|
$id_block = sqlInsertId();
|
|
|
|
if ($translations) {
|
|
$producer_translations = sqlQuery('SELECT name, descr, id_language, id_admin FROM producers_translations WHERE id_producer='.$producer['id']);
|
|
foreach ($producer_translations as $producer_tr) {
|
|
$blockObj = new \stdClass();
|
|
$blockObj->type = 'legacy';
|
|
$blockObj->id = $uuid;
|
|
$settings = new \stdClass();
|
|
$settings->html = $producer_tr['descr'];
|
|
$blockObj->settings = $settings;
|
|
|
|
$json = json_encode([$blockObj]);
|
|
|
|
$this->insertSQL('blocks_translations', [
|
|
'id_block' => $id_block,
|
|
'id_language' => $producer_tr['id_language'],
|
|
'id_admin' => $producer_tr['id_admin'],
|
|
'name' => $producer_tr['name'],
|
|
'content' => $producer_tr['descr'],
|
|
'json_content' => $json,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// sqlQuery('ALTER TABLE producers DROP COLUMN `descr`;');
|
|
|
|
if ($translations) {
|
|
// sqlQuery('ALTER TABLE producers_translations DROP COLUMN `descr`;');
|
|
}
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_producersOrderByEnum()
|
|
{
|
|
$categoryView = \KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService(\KupShop\CatalogBundle\View\CategoryView::class);
|
|
|
|
$values = '';
|
|
$this->checkEnumExists('producers', 'orderby', '', $values);
|
|
|
|
foreach ($categoryView->getSortOptions() as $value => $_) {
|
|
if (strstr($values, "'{$value}'") === false) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/** Update producers.orderby enum */
|
|
public function upgrade_producersOrderByEnum()
|
|
{
|
|
$categoryView = \KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService(\KupShop\CatalogBundle\View\CategoryView::class);
|
|
$enums = implode(',', array_map(function ($x) { return '\''.$x.'\''; }, array_keys($categoryView->getSortOptions())));
|
|
|
|
sqlQuery('ALTER TABLE producers CHANGE orderby orderby ENUM('.$enums.') DEFAULT "title" ');
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_producersDateUpdated()
|
|
{
|
|
return $this->checkColumnExists('producers', 'date_updated');
|
|
}
|
|
|
|
/** Add date_updated to producers*/
|
|
public function upgrade_producersDateUpdated()
|
|
{
|
|
sqlQuery('ALTER TABLE producers ADD COLUMN date_updated DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP');
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_blocksForProducersSections(): bool
|
|
{
|
|
return $this->checkColumnExists('section_producer', 'id_block');
|
|
}
|
|
|
|
/** Add id_block to section_producer and move content from descr to blocks */
|
|
public function upgrade_blocksForProducersSections(): void
|
|
{
|
|
sqlQuery('
|
|
ALTER TABLE section_producer
|
|
ADD COLUMN `id_block` int(11) UNSIGNED DEFAULT NULL AFTER id,
|
|
ADD CONSTRAINT `fk_section_producer_blocks` FOREIGN KEY (id_block) REFERENCES blocks(id)
|
|
ON DELETE SET NULL ON UPDATE CASCADE;
|
|
');
|
|
|
|
$translations = findModule(\Modules::TRANSLATIONS);
|
|
$block = ServiceContainer::getService(Block::class);
|
|
|
|
$section_producer = sqlQueryBuilder()->select('*')->from('section_producer')->execute();
|
|
foreach ($section_producer as $producer) {
|
|
if (!($producer['text'] ?? null)) {
|
|
continue;
|
|
}
|
|
|
|
// create root block and assign it to section_producer.id_block
|
|
$id_block = $block->insertFirstBlock('section_producer', $producer['id'], $producer['text']);
|
|
$uuid = sqlQueryBuilder()
|
|
->select(JsonOperator::extract('json_content', '$[0].id'))
|
|
->from('blocks')
|
|
->andWhere(Operator::equals(['id' => $id_block]))
|
|
->execute()->fetchOne();
|
|
|
|
if ($translations) {
|
|
$producer_translations = sqlQuery('SELECT text, id_language, id_admin FROM section_producer_translations WHERE id_section_producer='.$producer['id']);
|
|
foreach ($producer_translations as $producer_tr) {
|
|
$blockObj = new \stdClass();
|
|
$blockObj->type = 'legacy';
|
|
$blockObj->id = $uuid;
|
|
$settings = new \stdClass();
|
|
$settings->html = $producer_tr['text'];
|
|
$blockObj->settings = $settings;
|
|
|
|
$json = json_encode([$blockObj]);
|
|
|
|
$this->insertSQL('blocks_translations', [
|
|
'id_block' => $id_block,
|
|
'id_language' => $producer_tr['id_language'],
|
|
'id_admin' => $producer_tr['id_admin'],
|
|
'name' => '',
|
|
'content' => $producer_tr['text'],
|
|
'json_content' => $json,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_producersGPSRData()
|
|
{
|
|
return $this->checkColumnExists('producers', 'company_name');
|
|
}
|
|
|
|
/** Add data for GPSR to producers*/
|
|
public function upgrade_producersGPSRData()
|
|
{
|
|
sqlQuery('
|
|
ALTER TABLE producers
|
|
ADD COLUMN company_name VARCHAR(250) DEFAULT null,
|
|
ADD COLUMN company_street VARCHAR(100) DEFAULT null,
|
|
ADD COLUMN company_city VARCHAR(50) DEFAULT null ,
|
|
ADD COLUMN company_zip VARCHAR(50) DEFAULT null,
|
|
ADD COLUMN company_country VARCHAR(50) DEFAULT null,
|
|
ADD COLUMN company_email VARCHAR(100) DEFAULT null,
|
|
ADD COLUMN company_web VARCHAR(100) DEFAULT null
|
|
AFTER orderdir
|
|
');
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_importersGPSRData()
|
|
{
|
|
return $this->checkColumnExists('producers', 'import_company_name');
|
|
}
|
|
|
|
/** Add data for GPSR importers to producers*/
|
|
public function upgrade_importersGPSRData()
|
|
{
|
|
sqlQuery('
|
|
ALTER TABLE producers
|
|
ADD COLUMN import_company_name VARCHAR(250) DEFAULT null,
|
|
ADD COLUMN import_company_street VARCHAR(100) DEFAULT null,
|
|
ADD COLUMN import_company_city VARCHAR(50) DEFAULT null ,
|
|
ADD COLUMN import_company_zip VARCHAR(50) DEFAULT null,
|
|
ADD COLUMN import_company_country VARCHAR(50) DEFAULT null,
|
|
ADD COLUMN import_company_email VARCHAR(100) DEFAULT null,
|
|
ADD COLUMN import_company_web VARCHAR(100) DEFAULT null
|
|
AFTER company_web
|
|
');
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
}
|