<?php
namespace App\Manager;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Product;
use App\Entity\ProductModel;
use App\Entity\Category;
use App\Entity\Attribute;
use App\Entity\Language;
use App\Entity\ProductAttribute;
class AttributeManager {
private $em;
private $projectDir;
public function __construct(EntityManagerInterface $em, \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface $parameter)
{
$this->em = $em;
$this->projectDir = $parameter->get('kernel.project_dir').'/';
}
public function createAttribute(string $code): Attribute{
$attribute = new Attribute();
$attribute->setCode($code);
$attribute->setStatus(0);
$this->em->persist($attribute);
$this->em->flush();
return $attribute;
}
public function getAttribute(string $code): ?Attribute{
$attribute = $this->em->getRepository(Attribute::class)->findOneByCode($code);
return $attribute ? $attribute : $this->createAttribute($code);
}
public function createAttributeDescription(Attribute $attribute, $isoCode = 'fr'): \App\Entity\AttributeDescription{
$language = $this->em->getRepository(\App\Entity\Language::class)->findOneByCode($isoCode);
$ad = new \App\Entity\AttributeDescription();
$ad->setAttribute($attribute);
$ad->setLanguage($language);
$ad->setTitle('');
return $ad;
}
public function createProductAttributeDescription(ProductAttribute $pa, $isoCode = 'fr'): \App\Entity\ProductAttributeDescription{
$language = $this->em->getRepository(\App\Entity\Language::class)->findOneByCode($isoCode);
$ad = new \App\Entity\ProductAttributeDescription();
$ad->setProductAttribute($pa);
$ad->setProductAttribute($pa);
$ad->setLanguage($language);
$ad->setValue('');
return $ad;
}
public function addToProduct(Attribute $attribute, Product $product, string $value, $locale = null) {
$pa = $this->em->getRepository('App:ProductAttribute')->findOneBy([
'attribute' => $attribute,
'product' => $product
]);
if(empty($pa)) {
$pa = new \App\Entity\ProductAttribute();
$pa->setAttribute($attribute);
$pa->setProduct($product);
}
if(is_null($locale)) {
$pa->setValue($value);
}else{
$description = $pa->getDescription($locale);
if(empty($description)) {
$description = $this->createProductAttributeDescription($pa, $locale);
}
$description->setValue($value);
$this->em->persist($description);
}
$this->em->persist($pa);
$this->em->flush();
}
public function removeFromProduct(Attribute $attribute, Product $product) {
$pas = $this->em->getRepository('App:ProductAttribute')->findBy([
'product' => $product,
'attribute' => $attribute
]);
foreach($pas as $pa){
$this->em->remove($pa);
}
$this->em->flush();
}
public function addToProductModel(Attribute $attribute, ProductModel $product, string $value) {
$pa = $this->em->getRepository('App:ProductModelAttribute')->findOneBy([
'attribute' => $attribute,
'productModel' => $product
]);
if(empty($pa)) {
$pa = new \App\Entity\ProductModelAttribute();
$pa->setAttribute($attribute);
$pa->setProductModel($product);
}
$pa->setValue($value);
$this->em->persist($pa);
$this->em->flush();
}
public function removeFromProductModel(Attribute $attribute, ProductModel $model) {
$pas = $this->em->getRepository('App:ProductModelAttribute')->findBy([
'productModel' => $model,
'attribute' => $attribute
]);
foreach($pas as $pa){
$this->em->remove($pa);
}
$this->em->flush();
}
public function getAttributesByProductModel(ProductModel $productModel): array {
$attributes = [];
$variant = $productModel->getVariant();
if (!$variant) {
return $attributes;
}
foreach ([$variant->getAxis1(), $variant->getAxis2()] as $axis) {
if (!$axis) {
continue;
}
$codes = array_values(array_filter(array_map('trim', explode(',', $axis))));
if (count($codes) === 1) {
$attribute = $this->getAttribute($codes[0]);
if ($attribute && $attribute->isVariant()) {
$attribute->setValues($this->getAttributeValuesByProductModel($attribute, $productModel));
$attributes[] = $attribute;
}
} else {
$composite = $this->buildCompositeAxis($codes, $productModel);
if ($composite) {
$attributes[] = $composite;
}
}
}
return $attributes;
}
/**
* Builds a synthetic axis object for multi-attribute axes (e.g. axis = "taille,couleur").
* Returns a stdClass with code/title/display/values so Twig can render it like a regular Attribute.
* Each value has ->value (combined label "XS / Rouge") and ->product (the matching Product).
*/
private function buildCompositeAxis(array $codes, ProductModel $productModel): ?\stdClass {
$axisAttributes = [];
foreach ($codes as $code) {
$attr = $this->em->getRepository(Attribute::class)->findOneByCode($code);
if ($attr) {
$axisAttributes[] = $attr;
}
}
if (empty($axisAttributes)) {
return null;
}
$values = [];
$seen = [];
foreach ($productModel->getAllProducts() as $product) {
$parts = [];
foreach ($axisAttributes as $attr) {
$pa = $product->getAttribute($attr->getCode());
$parts[] = $pa ? ($pa->getDisplayedValue() ?? '?') : '?';
}
$label = implode(' / ', $parts);
if (in_array($label, $seen)) {
continue;
}
$seen[] = $label;
$value = new \stdClass();
$value->value = $label;
$value->product = $product;
$values[] = $value;
}
$composite = new \stdClass();
$composite->code = implode(',', $codes);
$composite->title = implode(' / ', array_map(fn(Attribute $a) => $a->getTitle(), $axisAttributes));
$composite->display = Attribute::DISPLAY_TYPE_COMBOBOX;
$composite->values = $values;
return $composite;
}
public function getAttributeValuesByProductModel(Attribute $attribute, ProductModel $productModel) {
$values = [];
$tmp = [];
$products = $productModel->getAllProducts();
foreach($products as $product) {
$attr = $product->getAttribute($attribute->getCode());
if($attr && !in_array($attr->getValue(), $tmp)) {
$values[] = $attr;
$tmp[] = $attr->getValue();
}
}
return $values;
}
}