213 lines
6.7 KiB
PHP
213 lines
6.7 KiB
PHP
<?php
|
|
|
|
class stTaskSchedulerImportCategoryMapper
|
|
{
|
|
/**
|
|
*
|
|
* @var stTaskSchedulerImportBaseAbstract
|
|
*/
|
|
protected $import;
|
|
|
|
/**
|
|
*
|
|
* @var Category|null
|
|
*/
|
|
protected $rootCategory = null;
|
|
|
|
protected $currentProduct = null;
|
|
|
|
protected $currentProductcategoryIds = null;
|
|
|
|
public function __construct(stTaskSchedulerImportBaseAbstract $import)
|
|
{
|
|
$this->import = $import;
|
|
}
|
|
|
|
/**
|
|
* Dodaje kategorie do produktu
|
|
*
|
|
* @param Product $product Obiekt modelu produktu
|
|
* @param string $path Ścieżka kategorii
|
|
* @param string $separator Separator ścieżki domyślnie '/'
|
|
* @return stTaskSchedulerImportCategoryMapping
|
|
*/
|
|
public function addCategory(Product $product, string $path, bool $default = false, ?string $separator = '/'): ?stTaskSchedulerImportCategoryMapping
|
|
{
|
|
$categories = array_map('trim', null !== $separator ? explode($separator, $path) : [$path]);
|
|
|
|
$current = $this->getTargetCategory();
|
|
|
|
$current->setCulture($product->getCulture());
|
|
|
|
$mapping = $this->getMapping($path, $separator, true);
|
|
|
|
if ($mapping->getIsExcluded())
|
|
{
|
|
return $mapping;
|
|
}
|
|
|
|
$category = $mapping->getCategory();
|
|
|
|
if (null !== $category)
|
|
{
|
|
$category->setCulture($product->getCulture());
|
|
}
|
|
|
|
if (null === $category)
|
|
{
|
|
foreach ($categories as $name)
|
|
{
|
|
if (empty($name)) {
|
|
break;
|
|
}
|
|
|
|
$c = new Criteria();
|
|
$c->add(CategoryPeer::OPT_NAME, $name);
|
|
$c->add(CategoryPeer::PARENT_ID, $current->getId());
|
|
$category = CategoryPeer::doSelectOne($c);
|
|
|
|
if (null === $category)
|
|
{
|
|
$category = new Category();
|
|
$category->setIsActive($current->getIsActive());
|
|
$category->setCulture($product->getCulture());
|
|
$category->insertAsLastChildOf($current);
|
|
$category->setName($name);
|
|
$category->save();
|
|
}
|
|
|
|
$current = $category->reload();
|
|
$current->setCulture($product->getCulture());
|
|
}
|
|
}
|
|
|
|
if ($category)
|
|
{
|
|
$category->setCulture($product->getCulture());
|
|
|
|
if ($product->isNew() || !$this->import->getConfigurationParameter('disable_categories_update') && !$this->isProductAssignedToCategory($product, $category))
|
|
{
|
|
$phc = new ProductHasCategory();
|
|
$phc->setIsDefault($default);
|
|
$phc->setCategoryId($category->getId());
|
|
$phc->setImportHashId($this->import->getConfiguration()->getHashId());
|
|
$product->addProductHasCategory($phc);
|
|
}
|
|
|
|
if (!$mapping->getCategory())
|
|
{
|
|
$mapping->setCategory($category);
|
|
}
|
|
|
|
$mapping->save();
|
|
}
|
|
|
|
return $mapping;
|
|
}
|
|
|
|
/**
|
|
* Pobiera mapowanie po scieżce kategorii
|
|
*
|
|
* @param string $path Scieżka kategorii (Nazwa1/Nazwa2/Nazwa3/...)
|
|
* @param string $separator Separator ścieżki domyślnie '/'
|
|
* @return stTaskSchedulerImportCategoryMapping|null
|
|
* @throws PropelException
|
|
*/
|
|
public function getMapping(string $path, ?string $separator = '/', $create = false): ?stTaskSchedulerImportCategoryMapping
|
|
{
|
|
$categories = array_map('trim', null !== $separator ? explode($separator, $path) : [$path]);
|
|
$path = implode(' / ', $categories);
|
|
|
|
$mapping = stTaskSchedulerImportCategoryMappingPeer::retrieveByCategoryPath($this->import->getConfiguration()->getHashId(), $path);
|
|
|
|
if (null === $mapping)
|
|
{
|
|
$mapping = new stTaskSchedulerImportCategoryMapping();
|
|
$mapping->setCategoryPath($path);
|
|
}
|
|
|
|
$mapping->setTaskSchedulerImport($this->import);
|
|
|
|
return $mapping;
|
|
}
|
|
|
|
/**
|
|
* Pobiera mapowanie po instancji produktu
|
|
*
|
|
* @param Product $product Instancja produktu
|
|
* @return stTaskSchedulerImportCategoryMapping|null
|
|
*/
|
|
public function getMappingByProduct(Product $product): ?stTaskSchedulerImportCategoryMapping
|
|
{
|
|
if ($product->getDefaultCategory())
|
|
{
|
|
$c = new Criteria();
|
|
$c->addDescendingOrderByColumn(stTaskSchedulerImportCategoryMappingPeer::PRICE_MARGIN);
|
|
$c->add(stTaskSchedulerImportCategoryMappingPeer::CATEGORY_ID, $product->getDefaultCategory()->getId());
|
|
$mapping = stTaskSchedulerImportCategoryMappingPeer::doSelectOne($c);
|
|
|
|
if (null !== $mapping)
|
|
{
|
|
$mapping->setTaskSchedulerImport($this->import);
|
|
}
|
|
|
|
return $mapping;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected function getTargetCategory(): ?Category
|
|
{
|
|
if (null === $this->rootCategory)
|
|
{
|
|
$categoryId = $this->import->getConfigurationParameter('category');
|
|
|
|
$category = $categoryId ? CategoryPeer::retrieveByPK($categoryId) : null;
|
|
|
|
if (null === $category)
|
|
{
|
|
$category = CategoryPeer::retrieveCategoryTreeByName($this->import->getConfiguration()->getLabel());
|
|
}
|
|
|
|
if (null === $category)
|
|
{
|
|
$category = new Category();
|
|
$category->setCulture(stLanguage::getOptLanguage());
|
|
$category->setName($this->import->getConfiguration()->getLabel());
|
|
$category->setIsActive(false);
|
|
$category->makeRoot();
|
|
$category->save();
|
|
|
|
if (null === $category->getScope())
|
|
{
|
|
$category->setScope($category->getId());
|
|
$category->save();
|
|
}
|
|
}
|
|
|
|
$this->rootCategory = $category;
|
|
}
|
|
|
|
return $this->rootCategory->reload();
|
|
}
|
|
|
|
/**
|
|
* Sprawdza czy produkt jest przypisany do kategorii
|
|
*
|
|
* @param Product $product
|
|
* @param Category $category
|
|
* @return bool
|
|
* @throws PropelException
|
|
*/
|
|
private function isProductAssignedToCategory(Product $product, Category $category): bool
|
|
{
|
|
if (null === $this->currentProductcategoryIds || $this->currentProduct && $this->currentProduct->getId() != $product->getId())
|
|
{
|
|
$this->currentProduct = $product;
|
|
$this->currentProductcategoryIds = ProductHasCategoryPeer::doSelectCategoryIdsByProduct($product);
|
|
}
|
|
|
|
return isset($this->currentProductcategoryIds[$category->getId()]);
|
|
}
|
|
} |