namespace Lc\CaracoleBundle\Builder\Product; | namespace Lc\CaracoleBundle\Builder\Product; | ||||
use Doctrine\ORM\EntityManagerInterface; | |||||
use Lc\CaracoleBundle\Container\Section\SectionContainer; | |||||
use Lc\CaracoleBundle\Model\Merchant\MerchantInterface; | |||||
use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface; | use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface; | ||||
class ProductFamilyBuilder | class ProductFamilyBuilder | ||||
{ | { | ||||
protected EntityManagerInterface $entityManager; | |||||
protected SectionContainer $sectionContainer; | |||||
public function __construct( | |||||
EntityManagerInterface $entityManager, | |||||
SectionContainer $sectionContainer | |||||
) | |||||
{ | |||||
$this->entityManager = $entityManager; | |||||
$this->sectionContainer = $sectionContainer; | |||||
} | |||||
public function setMerchant(ProductFamilyInterface $productFamily, MerchantInterface $merchant): void | |||||
{ | |||||
$sectionStore = $this->sectionContainer->getStore()->setMerchant($merchant); | |||||
// Les ProductFamilySectionProperty sont créées en double, on rectifie ici | |||||
$productFamilySectionPropertyArray = []; | |||||
foreach($productFamily->getProductFamilySectionProperties() as $productFamilySectionProperty) { | |||||
$productFamilySectionPropertyArray[$productFamilySectionProperty->getId()] = $productFamilySectionProperty; | |||||
$productFamily->removeProductFamilySectionProperty($productFamilySectionProperty); | |||||
} | |||||
foreach($productFamilySectionPropertyArray as $productFamilySectionProperty) { | |||||
$oldSection = $productFamilySectionProperty->getSection(); | |||||
$newSection = $sectionStore->getOneByDevAlias($oldSection->getDevAlias()); | |||||
if($newSection) { | |||||
$productFamilySectionProperty->setProductFamily($productFamily); | |||||
$productFamilySectionProperty->setSection($newSection); | |||||
$productFamily->addProductFamilySectionProperty($productFamilySectionProperty); | |||||
} | |||||
else { | |||||
$this->entityManager->remove($productFamilySectionProperty); | |||||
$productFamily->removeProductFamilySectionProperty($productFamilySectionProperty); | |||||
} | |||||
} | |||||
$productFamily->initProductCategories(); | |||||
} | |||||
} | } |
$this->userMerchantStore = $userMerchantStore; | $this->userMerchantStore = $userMerchantStore; | ||||
} | } | ||||
public function createIfNotExist(UserInterface $user, MerchantInterface $merchant): UserMerchantInterface | |||||
public function createIfNotExist(UserInterface $user, MerchantInterface $merchant, $flush = true): UserMerchantInterface | |||||
{ | { | ||||
$userMerchant = $this->userMerchantStore | $userMerchant = $this->userMerchantStore | ||||
->setMerchant($merchant) | ->setMerchant($merchant) | ||||
if (!$userMerchant) { | if (!$userMerchant) { | ||||
$userMerchantFactory = new UserMerchantFactory(); | $userMerchantFactory = new UserMerchantFactory(); | ||||
$userMerchant = $userMerchantFactory->create($merchant, $user); | $userMerchant = $userMerchantFactory->create($merchant, $user); | ||||
$this->entityManager->create($userMerchant); | $this->entityManager->create($userMerchant); | ||||
$this->entityManager->flush(); | |||||
if($flush) { | |||||
$this->entityManager->flush(); | |||||
} | |||||
}elseif(!$userMerchant->getActive()){ | }elseif(!$userMerchant->getActive()){ | ||||
$userMerchant->setActive(true); | $userMerchant->setActive(true); | ||||
$this->entityManager->update($userMerchant); | $this->entityManager->update($userMerchant); | ||||
$this->entityManager->flush(); | |||||
if($flush) { | |||||
$this->entityManager->flush(); | |||||
} | |||||
} | } | ||||
return $userMerchant; | return $userMerchant; |
$newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance()); | $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance()); | ||||
$merchant = $duplicateOtherMerchantForm->get('merchants')->getData(); | $merchant = $duplicateOtherMerchantForm->get('merchants')->getData(); | ||||
// | |||||
if($this->isInstanceOf(ProductFamilyInterface::class)) { | if($this->isInstanceOf(ProductFamilyInterface::class)) { | ||||
$sectionStore = $this->getSectionContainer()->getStore()->setMerchant($merchant); | |||||
// Les ProductFamilySectionproperty sont créées en double, on rectifie ici | |||||
// @TODO : j'imagine qu'on peut faire mieux que ça. Résoudre le problème à la base par exemple. | |||||
$productFamilySectionPropertyArray = []; | |||||
foreach($newEntity->getProductFamilySectionProperties() as $productFamilySectionProperty) { | |||||
$productFamilySectionPropertyArray[$productFamilySectionProperty->getId()] = $productFamilySectionProperty; | |||||
$newEntity->removeProductFamilySectionProperty($productFamilySectionProperty); | |||||
} | |||||
foreach($productFamilySectionPropertyArray as $productFamilySectionProperty) { | |||||
$oldSection = $productFamilySectionProperty->getSection(); | |||||
$newSection = $sectionStore->getOneByDevAlias($oldSection->getDevAlias()); | |||||
if($newSection) { | |||||
$productFamilySectionProperty->setProductFamily($newEntity); | |||||
$productFamilySectionProperty->setSection($newSection); | |||||
$newEntity->addProductFamilySectionProperty($productFamilySectionProperty); | |||||
} | |||||
else { | |||||
$entityManager->remove($productFamilySectionProperty); | |||||
$newEntity->removeProductFamilySectionProperty($productFamilySectionProperty); | |||||
} | |||||
} | |||||
$newEntity->initProductCategories(); | |||||
} | |||||
$this->getProductFamilyContainer()->getBuilder()->setMerchant($newEntity, $merchant); | |||||
} | |||||
else { | else { | ||||
$newEntity->setMerchant($merchant); | $newEntity->setMerchant($merchant); | ||||
} | } | ||||
// | |||||
// | |||||
$entityManager->create($newEntity, false); | $entityManager->create($newEntity, false); | ||||
$entityManager->flush(); | $entityManager->flush(); | ||||
public function __construct() | public function __construct() | ||||
{ | { | ||||
$this->productCategories = new ArrayCollection(); | $this->productCategories = new ArrayCollection(); | ||||
$this->products = new ArrayCollection(); | |||||
$this->qualityLabels = new ArrayCollection(); | $this->qualityLabels = new ArrayCollection(); | ||||
$this->initProducts(); | |||||
} | |||||
public function initProducts() | |||||
{ | |||||
$this->products = new ArrayCollection(); | |||||
} | } | ||||
public function __toString() | public function __toString() |
return $this->andWhere('productFamilies.status = 1'); | return $this->andWhere('productFamilies.status = 1'); | ||||
} | } | ||||
public function filterByTitle(string $title): self | |||||
{ | |||||
return $this->andWhere('.title LIKE :title')->setParameter('title', $title); | |||||
} | |||||
} | } |
$query->filterByDevAlias($devAlias); | $query->filterByDevAlias($devAlias); | ||||
return $query->find(); | return $query->find(); | ||||
} | } | ||||
public function getOneByTitle(string $title): ?ProductCategoryInterface | |||||
{ | |||||
$query = $this->createDefaultQuery(); | |||||
$query->filterByTitle($title); | |||||
return $query->findOne(); | |||||
} | |||||
} | } |
return $this; | return $this; | ||||
} | } | ||||
public function filterByTitle(string $title): self | |||||
{ | |||||
return $this->andWhere('.title LIKE :title')->setParameter('title', $title); | |||||
} | |||||
} | } |
return $productFamiliesToReturn; | return $productFamiliesToReturn; | ||||
} | } | ||||
public function getOneByTitle(string $title): ?ProductFamilyInterface | |||||
{ | |||||
$query = $this->createDefaultQuery(); | |||||
$query->filterByTitle($title); | |||||
return $query->findOne(); | |||||
} | |||||
} | } |
use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository; | use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository; | ||||
use Lc\CaracoleBundle\Repository\Merchant\MerchantStore; | use Lc\CaracoleBundle\Repository\Merchant\MerchantStore; | ||||
use Lc\CaracoleBundle\Repository\User\UserMerchantRepository; | use Lc\CaracoleBundle\Repository\User\UserMerchantRepository; | ||||
use Lc\CaracoleBundle\Solver\Merchant\MerchantSolver; | |||||
use Lc\SovBundle\Model\User\UserInterface; | use Lc\SovBundle\Model\User\UserInterface; | ||||
use Lc\SovBundle\Resolver\UrlResolver; | use Lc\SovBundle\Resolver\UrlResolver; | ||||
use Lc\SovBundle\Solver\Setting\SettingSolver; | use Lc\SovBundle\Solver\Setting\SettingSolver; | ||||
protected MerchantStore $merchantStore; | protected MerchantStore $merchantStore; | ||||
protected ParameterBagInterface $parameterBag; | protected ParameterBagInterface $parameterBag; | ||||
protected SettingSolver $settingSolver; | protected SettingSolver $settingSolver; | ||||
protected MerchantSolver $merchantSolver; | |||||
public function __construct( | public function __construct( | ||||
EntityManagerInterface $entityManager, | EntityManagerInterface $entityManager, | ||||
UrlGeneratorInterface $router, | UrlGeneratorInterface $router, | ||||
MerchantStore $merchantStore, | MerchantStore $merchantStore, | ||||
ParameterBagInterface $parameterBag, | ParameterBagInterface $parameterBag, | ||||
SettingSolver $settingSolver | |||||
SettingSolver $settingSolver, | |||||
MerchantSolver $merchantSolver | |||||
) { | ) { | ||||
$this->requestStack = $requestStack; | $this->requestStack = $requestStack; | ||||
$this->em = $entityManager; | $this->em = $entityManager; | ||||
$this->router = $router; | $this->router = $router; | ||||
$this->parameterBag = $parameterBag; | $this->parameterBag = $parameterBag; | ||||
$this->settingSolver = $settingSolver; | $this->settingSolver = $settingSolver; | ||||
$this->merchantSolver = $merchantSolver; | |||||
} | } | ||||
public function isOutOfMerchant() | public function isOutOfMerchant() |