Skip to content

Commit

Permalink
Merge pull request #299 in PLUG_OPEN/backend_swagimportexport from ne…
Browse files Browse the repository at this point in the history
…xt to master

* commit '0f459384dea3520959231635aa2c6d7eed16ec5f': (27 commits)
  PT-7190 - Change combo sorting
  PT-7254 - Catch database update failures
  PT-7151 - Extend profile description
  PT-7234 - Fix newsletter recipient export
  PT-7234 - Fix recipient import for known users
  PT-7190 - Fix category selection reset
  PT-7281 - Implemented default profile for orders
  PT-7192 - Improve import error messages for extended datasets
  PT-7239 - Ignore empty protpertyValues
  PT-7234 - Fix newsletter recipient import
  PT-7151 - Fix graduation price processing
  PT-7190 - Fix store reloading
  NTR - Fix component margin
  PT-7297 - Changed wording for profile editor
  PT-7190 - Filtering profile selection
  PT-7198 - Optimize profile editor ui
  PT-7063 - Fix update query
  PT-7063 - Fix profile updater
  PT-7286 - Fix column creation
  PT-7063 - Implement descriptions for default profiles
  ...
  • Loading branch information
mitelg committed Feb 1, 2017
2 parents 9070e8e + 0f45938 commit 9e8de97
Show file tree
Hide file tree
Showing 102 changed files with 4,727 additions and 1,063 deletions.
42 changes: 24 additions & 18 deletions Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Shopware\Commands\SwagImportExport\ExportCommand;
use Shopware\Commands\SwagImportExport\ImportCommand;
use Shopware\Commands\SwagImportExport\ProfilesCommand;
use Shopware\Components\CacheManager;
use Shopware\Components\Model\ModelManager;
use Shopware\Components\SwagImportExport\Factories\DataFactory;
use Shopware\Components\SwagImportExport\Factories\DataTransformerFactory;
Expand All @@ -32,9 +33,11 @@
use Shopware\Setup\SwagImportExport\Install\MainMenuItemInstaller;
use Shopware\Setup\SwagImportExport\Install\OldAdvancedMenuInstaller;
use Shopware\Setup\SwagImportExport\SetupContext;
use Shopware\Setup\SwagImportExport\Update\DefaultProfileUpdater;
use Shopware\Setup\SwagImportExport\Update\Update01MainMenuItem;
use Shopware\Setup\SwagImportExport\Update\Update02RemoveForeignKeyConstraint;
use Shopware\Setup\SwagImportExport\Update\Update03DefaultProfileSupport;
use Shopware\Setup\SwagImportExport\Update\Update04CreateColumns;
use Shopware\Setup\SwagImportExport\Update\UpdaterInterface;

/**
Expand Down Expand Up @@ -144,8 +147,9 @@ public function install()
if (!$this->assertMinimumVersion('5.2.0')) {
throw new MinVersionException('This plugin requires Shopware 5.2.0 or a later version');
}

$this->clearDoctrineMetaDataCache();
/** @var CacheManager $cacheManager */
$cacheManager = $this->get('shopware.cache_manager');
$cacheManager->clearProxyCache();

$setupContext = new SetupContext(
$this->get('config')->get('version'),
Expand Down Expand Up @@ -191,7 +195,12 @@ public function update($oldVersion)
if (version_compare($oldVersion, '2.0.0', '<=')) {
$this->renameDuplicateProfileNames();
}
$this->clearDoctrineMetaDataCache();
/** @var CacheManager $cacheManager */
$cacheManager = $this->get('shopware.cache_manager');
$cacheManager->clearProxyCache();

$connection = $this->get('dbal_connection');
$connection->executeQuery('SET foreign_key_checks = 0;');

$setupContext = new SetupContext(
$this->get('config')->get('version'),
Expand All @@ -208,13 +217,19 @@ public function update($oldVersion)
$this->get('dbal_connection')->getSchemaManager()
);
$updaters[] = new Update03DefaultProfileSupport($setupContext, $this->get('dbal_connection'), $this->get('snippets'));
$updaters[] = new Update04CreateColumns($setupContext, $this->get('dbal_connection'));
$updaters[] = new DefaultProfileUpdater($setupContext, $this->get('dbal_connection'), $this->get('snippets'));

$this->registerControllers();
$this->createAclResource();
$this->registerEvents();
$this->createDirectories();
$this->createConfiguration();
$this->updateDatabase();

try {
$this->updateDatabase();
} catch (\Exception $e) {
}

/** @var UpdaterInterface $updater */
foreach ($updaters as $updater) {
Expand All @@ -223,6 +238,10 @@ public function update($oldVersion)
}
$updater->update();
}
$connection->executeQuery('SET foreign_key_checks = 1;');

$defaultProfileInstaller = new DefaultProfileInstaller($setupContext, $this->get('dbal_connection'));
$defaultProfileInstaller->install();

return [
'success' => true,
Expand Down Expand Up @@ -321,7 +340,6 @@ public function getDataFactory()
if ($this->dataFactory === null) {
$this->dataFactory = Enlight_Class::Instance('Shopware\Components\SwagImportExport\Factories\DataFactory');
}

return $this->dataFactory;
}

Expand Down Expand Up @@ -768,18 +786,6 @@ private function removeTablePrefix(SchemaTool $tool, array $classes)
return $tableNames;
}

/**
* Clear doctrine meta data cache to generate classes from class meta data by using SchemaTool::updateSchema()
* and SchemaTool::createSchema().
*/
private function clearDoctrineMetaDataCache()
{
$cacheDriver = $this->get('models')->getConfiguration()->getMetadataCacheImpl();
if ($cacheDriver) {
$cacheDriver->deleteAll();
}
}

/**
* Rename duplicate profile names to prevent integrity constraint mysql exceptions.
*/
Expand Down Expand Up @@ -816,7 +822,7 @@ private function addSuffixToProfileNames($profiles)

$dbalConnection->executeQuery(
'UPDATE s_import_export_profile SET name = :name WHERE id=:id',
[ 'name' => uniqid($profile['name'] . '_', true) , 'id' => $profile['id'] ]
[ 'name' => uniqid($profile['name'] . '_', true), 'id' => $profile['id'] ]
);
}
}
Expand Down
54 changes: 33 additions & 21 deletions Commands/SwagImportExport/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace Shopware\Commands\SwagImportExport;

use Exception;
use Shopware\Commands\ShopwareCommand;
use Shopware\Components\Model\ModelManager;
use Shopware\CustomModels\ImportExport\Profile;
use Shopware\CustomModels\ImportExport\Repository;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -20,18 +20,31 @@

class ExportCommand extends ShopwareCommand
{
/** @var string */
protected $profile;

/**
* @var Profile
*/
/** @var Profile $profileEntity */
protected $profileEntity;

/** @var string */
protected $exportVariants;

/** @var int */
protected $limit;

/** @var int */
protected $offset;

/** @var string */
protected $format;

/** @var string */
protected $filePath;

/** @var string */
protected $category;

/** @var int */
protected $sessionId;

/**
Expand Down Expand Up @@ -62,16 +75,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->registerErrorHandler($output);

$helper = new CommandHelper(
array(
[
'profileEntity' => $this->profileEntity,
'filePath' => Shopware()->DocPath() . $this->filePath,
'format' => $this->format,
'exportVariants' => $this->exportVariants,
'limit' => $this->limit,
'offset' => $this->offset,
'username' => 'Commandline',
'category' => $this->category ? array( $this->category ) : null
)
'category' => $this->category ? [$this->category] : null
]
);

$output->writeln('<info>' . sprintf("Using profile: %s.", $this->profile) . '</info>');
Expand All @@ -81,13 +94,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('<info>' . sprintf("Using category as filter: %s.", $this->category) . '</info>');
}

$return = $helper->prepareExport();
$count = $return['count'];
$preparationData = $helper->prepareExport();
$count = $preparationData['count'];
$output->writeln('<info>' . sprintf("Total count: %d.", $count) . '</info>');

$data = $helper->exportAction();
$position = $data['position'];
$output->writeln('<info>' . sprintf("Processed: %d.", $position) . '</info>');
$position = 0;

while ($position < $count) {
$data = $helper->exportAction();
Expand All @@ -98,15 +109,15 @@ protected function execute(InputInterface $input, OutputInterface $output)

/**
* @param InputInterface $input
* @throws Exception
* @throws \Exception
*/
protected function prepareExportInputValidation(InputInterface $input)
{
$this->profile = $input->getOption('profile');
$this->format = $input->getOption('format');
$this->exportVariants = $input->getOption('exportVariants');
$this->offset = $input->getOption('offset');
$this->limit = $input->getOption('limit');
$this->offset = (int) $input->getOption('offset');
$this->limit = (int) $input->getOption('limit');
$this->filePath = $input->getArgument('filepath');
$this->category = $input->getOption('category');

Expand All @@ -116,7 +127,7 @@ protected function prepareExportInputValidation(InputInterface $input)

$parts = explode('.', $this->filePath);

// get some service from container (formerly Shopware()->Bootstrap()->getResource())
/** @var ModelManager $em */
$em = $this->container->get('models');

/** @var Repository $profileRepository */
Expand All @@ -126,14 +137,15 @@ protected function prepareExportInputValidation(InputInterface $input)
if ($this->profile === null) {
foreach ($parts as $part) {
$part = strtolower($part);
$this->profileEntity = $profileRepository->findOneBy(array('name' => $part));
$this->profileEntity = $profileRepository->findOneBy(['name' => $part]);
if ($this->profileEntity !== null) {
$this->profile = $part;
break;
}
}
} else {
$this->profileEntity = $profileRepository->findOneBy(array('name' => $this->profile));
/** @var Profile profileEntity */
$this->profileEntity = $profileRepository->findOneBy(['name' => $this->profile]);
$this->validateProfiles($input);
}

Expand All @@ -146,8 +158,8 @@ protected function prepareExportInputValidation(InputInterface $input)
$this->format = strtolower($this->format);

// validate type
if (!in_array($this->format, array('csv', 'xml'))) {
throw new Exception(sprintf('Invalid format: \'%s\'! Valid formats are: CSV and XML.', $this->format));
if (!in_array($this->format, ['csv', 'xml'])) {
throw new \Exception(sprintf('Invalid format: \'%s\'! Valid formats are: CSV and XML.', $this->format));
}
}

Expand All @@ -157,7 +169,7 @@ protected function prepareExportInputValidation(InputInterface $input)
protected function validateProfiles(InputInterface $input)
{
if (!$this->profileEntity) {
throw new Exception(sprintf('Invalid profile: \'%s\'!', $this->profile));
throw new \Exception(sprintf('Invalid profile: \'%s\'!', $this->profile));
}

if ($this->profileEntity->getType() != 'articles' && $input->getOption('exportVariants')) {
Expand Down
48 changes: 32 additions & 16 deletions Commands/SwagImportExport/ImportCommand.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<?php
/**
* (c) shopware AG <info@shopware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Shopware\Commands\SwagImportExport;

use Shopware\Commands\ShopwareCommand;
use Shopware\Components\Model\ModelManager;
use Shopware\Components\SwagImportExport\Profile\Profile;
use Shopware\Components\SwagImportExport\UploadPathProvider;
use Shopware\CustomModels\ImportExport\Profile as ProfileEntity;
Expand All @@ -16,10 +23,19 @@

class ImportCommand extends ShopwareCommand
{
/** @var string */
protected $profile;

/** @var ProfileEntity */
protected $profileEntity;

/** @var string */
protected $format;

/** @var string */
protected $filePath;

/** @var int */
protected $sessionId;

/**
Expand All @@ -46,9 +62,10 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->start($output, $this->profileEntity, $this->filePath, $this->format);

$profilesMapper = array('articles', 'articlesImages');
$profilesMapper = ['articles', 'articlesImages'];

$uploadPathProvider = new UploadPathProvider(Shopware()->DocPath());
/** @var UploadPathProvider $uploadPathProvider */
$uploadPathProvider = $this->container->get('swag_import_export.upload_path_provider');

//loops the unprocessed data
$pathInfo = pathinfo($this->filePath);
Expand Down Expand Up @@ -80,29 +97,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
protected function start(OutputInterface $output, $profileModel, $file, $format)
{
$helper = new CommandHelper(
array(
[
'profileEntity' => $profileModel,
'filePath' => $file,
'format' => $format,
'username' => 'Commandline'
)
]
);

$output->writeln('<info>' . sprintf("Using profile: %s.", $profileModel->getName()) . '</info>');
$output->writeln('<info>' . sprintf("Using format: %s.", $format) . '</info>');
$output->writeln('<info>' . sprintf("Using file: %s.", $file) . '</info>');

$return = $helper->prepareImport();
$count = $return['count'];
$preparationData = $helper->prepareImport();
$count = $preparationData['count'];
$output->writeln('<info>' . sprintf("Total count: %d.", $count) . '</info>');

$return = $helper->importAction();
$position = $return['data']['position'];
$output->writeln('<info>' . sprintf("Processed: %d.", $position) . '</info>');
$position = 0;

while ($position < $count) {
$return = $helper->importAction();
$position = $return['data']['position'];
$data = $helper->importAction();
$this->container->get('models')->clear();
$position = $data['data']['position'];
$output->writeln('<info>' . sprintf("Processed: %d.", $position) . '</info>');
}
}
Expand All @@ -119,24 +135,24 @@ protected function prepareImportInputValidation(InputInterface $input)

$parts = explode('.', $this->filePath);

// get some service from container (formerly Shopware()->Bootstrap()->getResource())
/** @var ModelManager $em */
$em = $this->container->get('models');

/** @var Repository $profileRepository */
$profileRepository = $em->getRepository('Shopware\CustomModels\ImportExport\Profile');
$profileRepository = $em->getRepository(ProfileEntity::class);

// if no profile is specified try to find it from the filename
if ($this->profile === null) {
foreach ($parts as $part) {
$part = strtolower($part);
$this->profileEntity = $profileRepository->findOneBy(array('name' => $part));
$this->profileEntity = $profileRepository->findOneBy(['name' => $part]);
if ($this->profileEntity !== null) {
$this->profile = $part;
break;
}
}
} else {
$this->profileEntity = $profileRepository->findOneBy(array('name' => $this->profile));
$this->profileEntity = $profileRepository->findOneBy(['name' => $this->profile]);
}

// validate profile
Expand All @@ -153,7 +169,7 @@ protected function prepareImportInputValidation(InputInterface $input)
$this->format = strtolower($this->format);

// validate type
if (!in_array($this->format, array('csv', 'xml'))) {
if (!in_array($this->format, ['csv', 'xml'])) {
throw new \Exception(sprintf('Invalid format: \'%s\'! Valid formats are: CSV and XML.', $this->format));
}

Expand Down
Loading

0 comments on commit 9e8de97

Please sign in to comment.