<?php declare(strict_types=1);
namespace Crehler\ProductFiles;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Crehler\ProductFiles\Installer\CustomFieldInstaller;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class CrehlerProductFiles extends Plugin
{
const IMPORT_1_PROFILE_ID = 'de9264aee0444a7c87facf63e7c0a612';
const IMPORT_2_PROFILE_ID = '032450b4ccb042ee93eb38cac00029af';
/**
* @param ActivateContext $activateContext
*/
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
(new CustomFieldInstaller($this->container))->upsertCustomFields($activateContext->getContext());
}
/**
* @param UninstallContext $uninstallContext
*/
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$connection = $this->container->get(Connection::class);
$connection->executeUpdate('DROP TABLE IF EXISTS `product_files`');
(new CustomFieldInstaller($this->container))->removeCustomFields($uninstallContext->getContext());
}
public function install(InstallContext $installContext): void
{
$this->upsertImportExportProfile();
parent::install($installContext);
}
public function update(UpdateContext $updateContext): void
{
$this->upsertImportExportProfile();
parent::update($updateContext);
}
private function upsertImportExportProfile()
{
$data = [
[
'id' => self::IMPORT_1_PROFILE_ID,
'name' => 'Pliki do pobrania - linki',
'label' => 'Pliki do pobrania - linki',
'systemDefault' => true,
'sourceEntity' => 'product_files_links',
'fileType' => 'text/csv',
'delimiter' => ';',
'enclosure' => '"',
'mapping' => [
[
'key' => 'id',
'mappedKey' => 'id'
],
[
'key' => 'productNumber',
'mappedKey' => 'kod'
], [
'key' => 'name',
'mappedKey' => 'name'
], [
'key' => 'link',
'mappedKey' => 'link'
]
]
],
[
'id' => self::IMPORT_2_PROFILE_ID,
'name' => 'Pliki do pobrania - pliki',
'label' => 'Pliki do pobrania - pliki',
'systemDefault' => true,
'sourceEntity' => 'product_files',
'fileType' => 'text/csv',
'delimiter' => ';',
'enclosure' => '"',
'mapping' => [
[
'key' => 'id',
'mappedKey' => 'id'
],
[
'key' => 'productNumber',
'mappedKey' => 'productNumber'
], [
'key' => 'name',
'mappedKey' => 'name'
], [
'key' => 'link',
'mappedKey' => 'link'
]
]
]
];
$entityRepository = $this->container->get('import_export_profile.repository');
$entityRepository->upsert($data, Context::createDefaultContext());
}
}