Skip to content

Commit

Permalink
Merge pull request #7 from akeneo/release/100.1.0
Browse files Browse the repository at this point in the history
Release/100.1.0
  • Loading branch information
Dnd-Gimix authored Jul 3, 2019
2 parents a45d1c9 + 134decc commit 246e896
Show file tree
Hide file tree
Showing 12 changed files with 353 additions and 42 deletions.
132 changes: 110 additions & 22 deletions Console/Command/AkeneoConnectorImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Magento\Framework\Data\Collection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use Akeneo\Connector\Api\ImportRepositoryInterface;
use Akeneo\Connector\Api\ImportRepositoryInterface\Proxy;
use Akeneo\Connector\Job\Import;
use \Symfony\Component\Console\Command\Command;
use \Symfony\Component\Console\Input\InputInterface;
Expand All @@ -26,7 +26,6 @@
*/
class AkeneoConnectorImportCommand extends Command
{

/**
* This constant contains a string
*
Expand All @@ -49,12 +48,12 @@ class AkeneoConnectorImportCommand extends Command
/**
* AkeneoConnectorImportCommand constructor.
*
* @param ImportRepositoryInterface\Proxy $importRepository
* @param Proxy $importRepository
* @param State $appState
* @param null $name
* @param null $name
*/
public function __construct(
ImportRepositoryInterface\Proxy $importRepository,
Proxy $importRepository,
State $appState,
$name = null
) {
Expand All @@ -69,9 +68,11 @@ public function __construct(
*/
protected function configure()
{
$this->setName('akeneo_connector:import')
->setDescription('Import Akeneo data to Magento')
->addOption(self::IMPORT_CODE,null,InputOption::VALUE_REQUIRED);
$this->setName('akeneo_connector:import')->setDescription('Import Akeneo data to Magento')->addOption(
self::IMPORT_CODE,
null,
InputOption::VALUE_REQUIRED
);
}

/**
Expand All @@ -91,27 +92,63 @@ protected function execute(InputInterface $input, OutputInterface $output)
$code = $input->getOption(self::IMPORT_CODE);
if (!$code) {
$this->usage($output);
} else {
$this->checkEntities($code, $output);
}
}


/**
* Check if multiple entities have been specified
* in the command line
*
* @param string $code
* @param OutputInterface $output
*
* @return void
*/
protected function checkEntities(string $code, OutputInterface $output)
{
/** @var string[] $entities */
$entities = explode(',', $code);
if (count($entities) > 1) {
$this->multiImport($entities, $output);
} else {
$this->import($code, $output);
}
}

/**
* Run import for multiple entities
*
* @param array $entities
* @param OutputInterface $output
*
* @return void
*/
protected function multiImport(array $entities, OutputInterface $output)
{
foreach ($entities as $entity) {
$this->import($entity, $output);
}
}

/**
* Run import
*
* @param string $code
* @param string $code
* @param OutputInterface $output
*
* @return bool
*/
protected function import($code, OutputInterface $output)
protected function import(string $code, OutputInterface $output)
{
/** @var Import $import */
$import = $this->importRepository->getByCode($code);
if (!$import) {
/** @var Phrase $message */
$message = __('Import code not found');
$output->writeln('<error>' . $message . '</error>');
$this->displayError($message, $output);

return false;
}
Expand All @@ -122,26 +159,26 @@ protected function import($code, OutputInterface $output)
while ($import->canExecute()) {
/** @var string $comment */
$comment = $import->getComment();
$output->writeln($comment);
$this->displayInfo($comment, $output);

$import->execute();

/** @var string $message */
$message = $import->getMessage();
if (!$import->getStatus()) {
$message = '<error>' . $message . '</error>';
$this->displayError($message, $output);
} else {
$this->displayComment($message, $output);
}

$output->writeln($message);

if ($import->isDone()) {
break;
}
}
} catch (\Exception $exception) {
/** @var string $message */
$message = $exception->getMessage();
$output->writeln($message);
$this->displayError($message, $output);
}

return true;
Expand All @@ -160,15 +197,15 @@ protected function usage(OutputInterface $output)
$imports = $this->importRepository->getList();

// Options
$output->writeln('<comment>' . __('Options:') . '</comment>');
$output->writeln('<info>' . __('--code') . '</info>');
$this->displayComment(__('Options:'), $output);
$this->displayInfo(__('--code'), $output);
$output->writeln('');

// Codes
$output->writeln('<comment>' . __('Available codes:') . '</comment>');
$this->displayComment(__('Available codes:'), $output);
/** @var Import $import */
foreach ($imports as $import) {
$output->writeln('<info>' . $import->getCode() . '</info>');
$this->displayInfo($import->getCode(), $output);
}
$output->writeln('');

Expand All @@ -178,8 +215,59 @@ protected function usage(OutputInterface $output)
/** @var string $code */
$code = $import->getCode();
if ($code) {
$output->writeln('<comment>' . __('Example:') . '</comment>');
$output->writeln('<info>' . __('akeneo_connector:import --code=%1', $code) . '</info>');
$this->displayComment(__('Example:'), $output);
$this->displayInfo(__('akeneo-connector:import --code=%1', $code), $output);
}
}

/**
* Display info in console
*
* @param string $message
* @param OutputInterface $output
*
* @return void
*/
public function displayInfo(string $message, OutputInterface $output)
{
if (!empty($message)) {
/** @var string $coloredMessage */
$coloredMessage = '<info>' . $message . '</info>';
$output->writeln($coloredMessage);
}
}

/**
* Display comment in console
*
* @param string $message
* @param OutputInterface $output
*
* @return void
*/
public function displayComment(string $message, OutputInterface $output)
{
if (!empty($message)) {
/** @var string $coloredMessage */
$coloredMessage = '<comment>' . $message . '</comment>';
$output->writeln($coloredMessage);
}
}

/**
* Display error in console
*
* @param string $message
* @param OutputInterface $output
*
* @return void
*/
public function displayError(string $message, OutputInterface $output)
{
if (!empty($message)) {
/** @var string $coloredMessage */
$coloredMessage = '<error>' . $message . '</error>';
$output->writeln($coloredMessage);
}
}
}
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Import/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public function execute()
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Akeneo_Connector_Api::import');
return $this->_authorization->isAllowed('Akeneo_Connector::import');
}
}
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Log/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public function execute()
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Akeneo_Connector_Log::akeneo_connector_log');
return $this->_authorization->isAllowed('Akeneo_Connector::akeneo_connector_log');
}
}
11 changes: 11 additions & 0 deletions Helper/Config.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Config extends AbstractHelper
const PRODUCTS_CATEGORY_IS_ACTIVE = 'akeneo_connector/category/is_active';
const PRODUCTS_CATEGORY_INCLUDE_IN_MENU = 'akeneo_connector/category/include_in_menu';
const PRODUCTS_CATEGORY_IS_ANCHOR = 'akeneo_connector/category/is_anchor';
const PRODUCTS_CATEGORY_CATEGORIES = 'akeneo_connector/category/categories';
const PRODUCT_ATTRIBUTE_MAPPING = 'akeneo_connector/product/attribute_mapping';
const PRODUCT_CONFIGURABLE_ATTRIBUTES = 'akeneo_connector/product/configurable_attributes';
const PRODUCT_TAX_CLASS = 'akeneo_connector/product/tax_class';
Expand Down Expand Up @@ -367,6 +368,16 @@ public function getIsCategoryAnchor()
return $this->scopeConfig->getValue(self::PRODUCTS_CATEGORY_IS_ANCHOR);
}

/**
* Retrieve the categories to filter the category import
*
* @return string
*/
public function getCategoriesFilter()
{
return $this->scopeConfig->getValue(self::PRODUCTS_CATEGORY_CATEGORIES);
}

/**
* Get Admin Website Default Channel from configuration
*
Expand Down
56 changes: 47 additions & 9 deletions Job/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,17 +426,55 @@ public function addAttributes()

foreach ($attributeSetIds as $attributeSetId) {
if (is_numeric($attributeSetId)) {
$this->eavSetup->addAttributeGroup(
$this->getEntityTypeId(),
$attributeSetId,
ucfirst($row['group'])
);
$this->eavSetup->addAttributeToSet(
$this->getEntityTypeId(),
$attributeSetId,
/* Verify if the group already exists */
/** @var int $setId */
$setId = $this->eavSetup->getAttributeSetId($this->getEntityTypeId(), $attributeSetId);
/** @var int $groupId */
$groupId = $this->eavSetup->getSetup()->getTableRow(
'eav_attribute_group',
'attribute_group_name',
ucfirst($row['group']),
$row['_entity_id']
'attribute_group_id',
'attribute_set_id',
$setId
);

if ($groupId) {
/* The group already exists, update it */
/** @var string[] $dataGroup */
$dataGroup = [
'attribute_set_id' => $setId,
'attribute_group_name' => ucfirst($row['group']),
];

$this->eavSetup->updateAttributeGroup(
$this->getEntityTypeId(),
$setId,
$groupId,
$dataGroup
);

$this->eavSetup->addAttributeToSet(
$this->getEntityTypeId(),
$attributeSetId,
$groupId,
$row['_entity_id']
);
} else {
/* The group doesn't exists, create it */
$this->eavSetup->addAttributeGroup(
$this->getEntityTypeId(),
$attributeSetId,
ucfirst($row['group'])
);

$this->eavSetup->addAttributeToSet(
$this->getEntityTypeId(),
$attributeSetId,
ucfirst($row['group']),
$row['_entity_id']
);
}
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions Job/Category.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,45 @@ public function updateChildrenCount()
');
}

/**
* Remove categories from category filter configuration
*
* @return void
*/
public function removeCategoriesByFilter()
{
/** @var string|string[] $filteredCategories */
$filteredCategories = $this->configHelper->getCategoriesFilter();
if (!$filteredCategories || empty($filteredCategories)) {
$this->setMessage(
__('No category to ignore')
);
return;
}
/** @var string $tableName */
$tableName = $this->entitiesHelper->getTableName($this->getCode());
/** @var AdapterInterface $connection */
$connection = $this->entitiesHelper->getConnection();
$filteredCategories = explode(',', $filteredCategories);
/** @var mixed[]|null $categoriesToDelete */
$categoriesToDelete = $connection->fetchAll(
$connection->select()->from($tableName)->where('code IN (?)', $filteredCategories)
);
if (!$categoriesToDelete) {
$this->setMessage(
__('No category found')
);
return;
}
foreach ($categoriesToDelete as $category) {
if (!isset($category['_entity_id'])) {
continue;
}
$connection->delete($tableName, ['path LIKE ?' => '%/' . $category['_entity_id'] . '/%']);
$connection->delete($tableName, ['path LIKE ?' => '%/' . $category['_entity_id']]);
}
}

/**
* Set Url Rewrite
*
Expand Down
Loading

0 comments on commit 246e896

Please sign in to comment.