Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter on unwanted categories #30

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/code/community/Pimgento/Api/Helper/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ class Pimgento_Api_Helper_Configuration extends Mage_Core_Helper_Abstract
* @var string $productsFiltersFamilies
*/
private $productsFiltersFamilies = 'families';
/**
* Product Filter Families config field
*
* @var string $productsFiltersFamilies
*/
private $productsFiltersCategories = 'categories';
/**
* Product Filter Updated Mode config field
*
Expand Down Expand Up @@ -1067,6 +1073,16 @@ public function getFamiliesFilter()
return $this->getProductFilterConfigValue($this->productsFiltersFamilies);
}

/**
* Retrieve the families to filter the products on
*
* @return string
*/
public function getCategoriesFilter()
{
return $this->getProductFilterConfigValue($this->productsFiltersCategories);
}

/**
* Retrieve the advance filters
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* Class Pimgento_Api_Model_Adminhtml_System_Config_Source_Filters_Category
*
* @category Class
* @package Pimgento_Api_Model_Adminhtml_System_Config_Source_Filters_Category
* @author Agence Dn'D <[email protected]>
* @copyright 2019 Agence Dn'D
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://www.dnd.fr/
*/
class Pimgento_Api_Model_Adminhtml_System_Config_Source_Filters_Category
{
/**
* List of options
*
* @var array $options
*/
protected $options = [];

/**
* Pimgento_Api_Model_Adminhtml_System_Config_Source_Filters_Family constructor
*/
public function __construct()
{
$this->init();
}

/**
* Initialize options
*
* @return void
*/
public function init()
{
/** @var Pimgento_Api_Helper_Client $helperClient */
$helperClient = Mage::helper('pimgento_api/client');

try {
/** @var Akeneo\Pim\ApiClient\AkeneoPimClientInterface|Akeneo\PimEnterprise\ApiClient\AkeneoPimEnterpriseClientInterface $client */
$client = $helperClient->getApiClient();

$this->options[''] = Mage::helper('pimgento_api')->__('None');

if (empty($client)) {
return;
}

/** @var Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface $categories */
$categories = $client->getCategoryApi()->all();
/** @var mixed[] $category */
foreach ($categories as $category) {
if (!isset($category['code']) || isset($category['parent'])) {
continue;
}
$this->options[$category['code']] = $category['code'];
}
} catch (Exception $exception) {
Mage::logException($exception);
}
}

/**
* Retrieve option list
*
* @return array
*/
public function toOptions()
{
return $this->options;
}

/**
* Retrieve options value and label in an array
*
* @return array
*/
public function toOptionArray()
{
/** @var array $optionArray */
$optionArray = [];
/**
* @var int $optionValue
* @var string $optionLabel
*/
foreach ($this->options as $optionValue => $optionLabel) {
$optionArray[] = [
'value' => $optionValue,
'label' => $optionLabel,
];
}

return $optionArray;
}
}
67 changes: 61 additions & 6 deletions app/code/community/Pimgento/Api/Model/Job/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,62 @@ public function setLevel($task)
}

/**
* Set categories position (Step 6)
* Remove categories from category filter configuration (Step 6)
*
* @param Task_Executor_Model_Task $task
*
* @return void
* @throws Pimgento_Api_Exception
*/
public function removeCategoriesByFilter($task)
{
/** @var string|string[] $filteredCategories */
$filteredCategories = $this->getConfigurationHelper()->getCategoriesFilter();

if (!$filteredCategories || empty($filteredCategories)) {
$task->setStepWarning($this->getHelper()->__('No category to ignore'));
$task->setStepMessage($this->getHelper()->__('Step skipped.'));

return;
}

/** @var string $tableName */
$tableName = $this->getTableName();
/** @var Mage_Core_Model_Resource $resource */
$resource = Mage::getModel('core/resource');
/** @var Varien_Db_Adapter_Interface $adapter */
$adapter = $resource->getConnection(Mage_Core_Model_Resource::DEFAULT_WRITE_RESOURCE);

$filteredCategories = explode(',', $filteredCategories);

/** @var mixed[]|null $categoriesToDelete */
$categoriesToDelete = $adapter->fetchAll(
$adapter->select()->from($tableName)->where('code IN (?)', $filteredCategories)
);
if (!$categoriesToDelete) {
$task->setStepWarning($this->getHelper()->__('No category found'));
$task->setStepMessage($this->getHelper()->__('Step skipped.'));

return;
}

foreach ($categoriesToDelete as $category) {
if (!isset($category['_entity_id'])) {
continue;
}
$adapter->delete($tableName, ['path LIKE ?' => '%/' . $category['_entity_id'] . '/%']);
$adapter->delete($tableName,[
'path LIKE ?' => '%/' . $category['_entity_id'],
'path NOT LIKE ?' => '%/' . $category['_entity_id'] . '%',
]
);
}

$task->setStepMessage($this->getHelper()->__('Unwanted categories removed'));
}

/**
* Set categories position (Step 7)
*
* @param Task_Executor_Model_Task $task
*
Expand Down Expand Up @@ -316,7 +371,7 @@ public function setPosition($task)
}

/**
* Create category entities (Step 7)
* Create category entities (Step 8)
*
* @param Task_Executor_Model_Task $task
*
Expand Down Expand Up @@ -369,7 +424,7 @@ public function createEntities($task)
}

/**
* Set values to attributes (Step 8)
* Set values to attributes (Step 9)
*
* @param Task_Executor_Model_Task $task
*
Expand Down Expand Up @@ -440,7 +495,7 @@ public function setValues($task)
}

/**
* Update Children Count (Step 9)
* Update Children Count (Step 10)
*
* @param Task_Executor_Model_Task $task
*
Expand All @@ -467,7 +522,7 @@ public function updateChildrenCount($task)
}

/**
* Set Url Keys (Step 10)
* Set Url Keys (Step 11)
*
* @param Task_Executor_Model_Task $task
*
Expand Down Expand Up @@ -591,7 +646,7 @@ public function setUrlKey($task)
}

/**
* Drop table (Step 11)
* Drop table (Step 12)
*
* @param Task_Executor_Model_Task $task
*
Expand Down
18 changes: 11 additions & 7 deletions app/code/community/Pimgento/Api/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,38 @@ protected function category($task)
'method' => 'pimgento_api/job_category::setLevel',
],
6 => [
'comment' => $helper->__('Remove Categories By Filter'),
'method' => 'pimgento_api/job_category::removeCategoriesByFilter',
],
7 => [
'comment' => $helper->__('Prepare categories position'),
'method' => 'pimgento_api/job_category::setPosition',
],
7 => [
8 => [
'comment' => $helper->__('Create and update category entities'),
'method' => 'pimgento_api/job_category::createEntities',
],
8 => [
9 => [
'comment' => $helper->__('Set values to attributes'),
'method' => 'pimgento_api/job_category::setValues',
],
9 => [
10 => [
'comment' => $helper->__('Child categories count'),
'method' => 'pimgento_api/job_category::updateChildrenCount',
],
10 => [
11 => [
'comment' => $helper->__('Update URL keys'),
'method' => 'pimgento_api/job_category::setUrlKey',
],
11 => [
12 => [
'comment' => $helper->__('Drop temporary table'),
'method' => 'pimgento_api/job_category::dropTable',
],
12 => [
13 => [
'comment' => $helper->__('Reindex Data'),
'method' => 'pimgento_api/job_category::reindex',
],
13 => [
14 => [
'comment' => $helper->__('Clear cache'),
'method' => 'pimgento_api/job_category::cleanCache',
],
Expand Down
17 changes: 16 additions & 1 deletion app/code/community/Pimgento/Api/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,24 @@
<mode>standard</mode>
</depends>
</families>
<categories>
<label>Categories</label>
<sort_order>170</sort_order>
<frontend_type>multiselect</frontend_type>
<source_model>pimgento_api/adminhtml_system_config_source_filters_category</source_model>
<comment>
<![CDATA[Select the categories you don't want to import.<br/> If the multiselect is empty, it means you don't have any categories in your Akeneo, or the provided credentials are wrong.]]>
</comment>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<depends>
<mode>standard</mode>
</depends>
</categories>
<advanced_filter translate="label comment">
<label>Advanced Filter</label>
<sort_order>170</sort_order>
<sort_order>180</sort_order>
<frontend_type>textarea</frontend_type>
<comment>
<![CDATA[Build your query using a json.<br/>
Expand Down
4 changes: 4 additions & 0 deletions app/locale/de_DE/Pimgento_Api.csv
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@
"Pimgento_Api::No axes for code: %s","Keine Achsen für Code: %s"
"Pimgento_Api::No cache to clear for import: %s","Kein Cache, der für den Import geleert werden muss: %s"
"Pimgento_Api::No Category data to insert in temp table","Keine Kategoriedaten zum Einfügen in die Temp-Tabelle"
"Pimgento_Api::No category found","Keine Kategorie gefunden"
"Pimgento_Api::No category to ignore","Keine Kategorie zum Ignorieren"
"Pimgento_Api::No condition","Keine Kondition"
"Pimgento_Api::No eav entity attribute set for axis: %s","Kein eav entity attribute für die Achse gesetzt: %s"
"Pimgento_Api::No Family data to insert in temp table","Keine Familiendaten in die Temp-Tabelle einzufügen"
Expand Down Expand Up @@ -209,6 +211,7 @@
"Pimgento_Api::Reindex successful for %s import","Reindex erfolgreich für %s Import"
"Pimgento_Api::Remove columns from product model table","Spalten aus der Produktmodelltabelle entfernen"
"Pimgento_Api::Secret","Geheimnis"
"Pimgento_Api::Select the categories you don't want to import.<br/> If the multiselect is empty, it means you don't have any categories in your Akeneo, or the provided credentials are wrong.","Wählen Sie die Kategorien aus, die Sie nicht importieren möchten.<br/> Wenn der Multiselect leer ist, bedeutet dies, dass du keine Kategorien in deinem Akeneo hast, oder die angegebenen Zugangsdaten falsch sind"
"Pimgento_Api::Select the channel to apply the completeness filter on","Wählen Sie den Kanal, auf dem der Vollständigkeitsfilter angewendet werden soll"
"Pimgento_Api::Select the families you don't want to retrieve products from. If the multiselect is empty, it means you don't have any families in your Akeneo, or the provided credentials are wrong.","Wähle die Familien, aus denen du keine Produkte abrufen möchtest, aus. Wenn der Multiselect leer ist, bedeutet dies, dass du keine Familien in deinem Akeneo hast, oder die angegebenen Zugangsdaten falsch sind"
"Pimgento_Api::Select the locales to apply the completeness filter on","Wählen Sie die Gebietsschemata, in denen der Vollständigkeitsfilter angewendet werden soll"
Expand All @@ -229,6 +232,7 @@
"Pimgento_Api::Test API credentials","Test API Anmeldedaten"
"Pimgento_Api::Test","Test"
"Pimgento_Api::Unknown","Unbekannt"
"Pimgento_Api::Unwanted categories removed","Unerwünscht Kategorien entfernt"
"Pimgento_Api::Update Axes column","Spalte Achsen aktualisieren"
"Pimgento_Api::Update column name","Spaltenname aktualisieren"
"Pimgento_Api::Update column values for options","Aktualisieren von Spaltenwerten für Optionen"
Expand Down
4 changes: 4 additions & 0 deletions app/locale/en_US/Pimgento_Api.csv
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@
"Pimgento_Api::No axes for code: %s","No axes for code: %s"
"Pimgento_Api::No cache to clear for import: %s","No cache to clear for import: %s"
"Pimgento_Api::No Category data to insert in temp table","No Category data to insert in temp table"
"Pimgento_Api::No category found","No category found"
"Pimgento_Api::No category to ignore","No category to ignore"
"Pimgento_Api::No condition","No condition"
"Pimgento_Api::No eav entity attribute set for axis: %s","No eav entity attribute set for axis: %s"
"Pimgento_Api::No Family data to insert in temp table","No Family data to insert in temp table"
Expand Down Expand Up @@ -209,6 +211,7 @@
"Pimgento_Api::Reindex successful for %s import","Reindex successful for %s import"
"Pimgento_Api::Remove columns from product model table","Remove columns from product model table"
"Pimgento_Api::Secret","Secret"
"Pimgento_Api::Select the categories you don't want to import.<br/> If the multiselect is empty, it means you don't have any categories in your Akeneo, or the provided credentials are wrong.","Select the categories you don't want to import.<br/> If the multiselect is empty, it means you don't have any categories in your Akeneo, or the provided credentials are wrong."
"Pimgento_Api::Select the channel to apply the completeness filter on","Select the channel to apply the completeness filter on"
"Pimgento_Api::Select the families you don't want to retrieve products from. If the multiselect is empty, it means you don't have any families in your Akeneo, or the provided credentials are wrong.","Select the families you don't want to retrieve products from. If the multiselect is empty, it means you don't have any families in your Akeneo, or the provided credentials are wrong."
"Pimgento_Api::Select the locales to apply the completeness filter on","Select the locales to apply the completeness filter on"
Expand All @@ -229,6 +232,7 @@
"Pimgento_Api::Test API credentials","Test API credentials"
"Pimgento_Api::Test","Test"
"Pimgento_Api::Unknown","Unknown"
"Pimgento_Api::Unwanted categories removed","Unwanted categories removed"
"Pimgento_Api::Update Axes column","Update Axes column"
"Pimgento_Api::Update column name","Update column name"
"Pimgento_Api::Update column values for options","Update column values for options"
Expand Down
4 changes: 4 additions & 0 deletions app/locale/fr_FR/Pimgento_Api.csv
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@
"Pimgento_Api::No axes for code: %s","Aucun axe pour le code : %s"
"Pimgento_Api::No cache to clear for import: %s","Aucun cache à vider pour l'import : %s"
"Pimgento_Api::No Category data to insert in temp table","Aucune donnée de catégorie à insérer dans la table temporaire"
"Pimgento_Api::No category found","Aucune catégorie trouvée"
"Pimgento_Api::No category to ignore","Aucune catégorie à ignorer"
"Pimgento_Api::No condition","Aucune condition"
"Pimgento_Api::No eav entity attribute set for axis: %s","Aucun jeux d'attribut ne correspond à l'axe : %s"
"Pimgento_Api::No Family data to insert in temp table","Aucune donnée de famille à insérer dans la table temporaire"
Expand Down Expand Up @@ -209,6 +211,7 @@
"Pimgento_Api::Reindex successful for %s import","Réindexation terminée pour l'import %s"
"Pimgento_Api::Remove columns from product model table","Suppression des colonnes de la table des modèles produit"
"Pimgento_Api::Secret","Secret"
"Pimgento_Api::Select the categories you don't want to import.<br/> If the multiselect is empty, it means you don't have any categories in your Akeneo, or the provided credentials are wrong.","Sélectionnez les catégories que vous ne voulez pas récupérer.<br/>Si le multiselect est vide, ça signifie que vous n'avez pas de catégories dans votre Akeneo, ou que les identifiants fournis sont erronés."
"Pimgento_Api::Select the channel to apply the completeness filter on","Sélectionner le canal sur lequel le filtre de complétude doit être appliqué"
"Pimgento_Api::Select the families you don't want to retrieve products from. If the multiselect is empty, it means you don't have any families in your Akeneo, or the provided credentials are wrong.","Selectionner les familles à exclure de l'import produit. Si ce champ est vide, vous n'avez peut-être pas de famille dans Akeneo, ou que vos identifiants sont incorrects."
"Pimgento_Api::Select the locales to apply the completeness filter on","Sélectionner la locale sur laquelle le filtre de complétude doit être appliqué"
Expand All @@ -229,6 +232,7 @@
"Pimgento_Api::Test API credentials","Test accès API"
"Pimgento_Api::Test","Test"
"Pimgento_Api::Unknown","Inconnu(e)"
"Pimgento_Api::Unwanted categories removed","Catégories à ignorer retirées"
"Pimgento_Api::Update Axes column","Mise à jour de la colonne Axes"
"Pimgento_Api::Update column name","Mise à jour de la colonne Nom"
"Pimgento_Api::Update column values for options","Mise à jour de la colonne des valeurs d'options"
Expand Down