src/Manager/AttributeManager.php line 152

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use App\Entity\Product;
  5. use App\Entity\ProductModel;
  6. use App\Entity\Category;
  7. use App\Entity\Attribute;
  8. use App\Entity\Language;
  9. use App\Entity\ProductAttribute;
  10. class AttributeManager {
  11. private $em;
  12. private $projectDir;
  13. private $attributeCache = [];
  14. /**
  15. * Ne garde que les id (pas les entités Language elles-mêmes) : contrairement
  16. * à $attributeCache, ce cache reste valide même après un em->clear().
  17. */
  18. private array $languageIdCache = [];
  19. public function __construct(EntityManagerInterface $em, \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface $parameter)
  20. {
  21. $this->em = $em;
  22. $this->projectDir = $parameter->get('kernel.project_dir').'/';
  23. }
  24. public function createAttribute(string $code): Attribute{
  25. $attribute = new Attribute();
  26. $attribute->setCode($code);
  27. $attribute->setStatus(0);
  28. $this->em->persist($attribute);
  29. return $attribute;
  30. }
  31. public function getAttribute(string $code): ?Attribute{
  32. if (isset($this->attributeCache[$code])) {
  33. return $this->attributeCache[$code];
  34. }
  35. $attribute = $this->em->getRepository(Attribute::class)->findOneByCode($code);
  36. if (!$attribute) {
  37. $attribute = $this->createAttribute($code);
  38. }
  39. $this->attributeCache[$code] = $attribute;
  40. return $attribute;
  41. }
  42. /**
  43. * The cached Attribute entities become detached after an em->clear(); call this right after
  44. * to avoid reusing stale references on the next lookups.
  45. */
  46. public function resetCache(): void {
  47. $this->attributeCache = [];
  48. }
  49. public function createAttributeDescription(Attribute $attribute, $isoCode = 'fr'): \App\Entity\AttributeDescription{
  50. $language = $this->getLanguageByCode($isoCode);
  51. $ad = new \App\Entity\AttributeDescription();
  52. $ad->setAttribute($attribute);
  53. $ad->setLanguage($language);
  54. $ad->setTitle('');
  55. return $ad;
  56. }
  57. public function createProductAttributeDescription(ProductAttribute $pa, $isoCode = 'fr'): \App\Entity\ProductAttributeDescription{
  58. $language = $this->getLanguageByCode($isoCode);
  59. $ad = new \App\Entity\ProductAttributeDescription();
  60. $ad->setProductAttribute($pa);
  61. $ad->setLanguage($language);
  62. $ad->setValue('');
  63. return $ad;
  64. }
  65. /**
  66. * Résout une langue par son code sans refaire une requête à chaque appel
  67. * (le nombre de codes distincts utilisés dans un import est très faible).
  68. */
  69. private function getLanguageByCode(string $isoCode): ?Language
  70. {
  71. if (!array_key_exists($isoCode, $this->languageIdCache)) {
  72. $language = $this->em->getRepository(Language::class)->findOneByCode($isoCode);
  73. $this->languageIdCache[$isoCode] = $language?->getId();
  74. }
  75. $id = $this->languageIdCache[$isoCode];
  76. return $id !== null ? $this->em->getReference(Language::class, $id) : null;
  77. }
  78. public function addToProduct(Attribute $attribute, Product $product, string $value, $locale = null, bool $flush = true) {
  79. $pa = $product->getAttribute($attribute->getCode());
  80. if(empty($pa)) {
  81. $pa = new \App\Entity\ProductAttribute();
  82. $pa->setAttribute($attribute);
  83. $pa->setProduct($product);
  84. }
  85. if(is_null($locale)) {
  86. $pa->setValue($value);
  87. }else{
  88. $description = $pa->getDescription($locale);
  89. if(empty($description)) {
  90. $description = $this->createProductAttributeDescription($pa, $locale);
  91. }
  92. $description->setValue($value);
  93. $this->em->persist($description);
  94. }
  95. $this->em->persist($pa);
  96. if ($flush) {
  97. $this->em->flush();
  98. }
  99. }
  100. public function removeFromProduct(Attribute $attribute, Product $product) {
  101. $pas = $this->em->getRepository('App:ProductAttribute')->findBy([
  102. 'product' => $product,
  103. 'attribute' => $attribute
  104. ]);
  105. foreach($pas as $pa){
  106. $this->em->remove($pa);
  107. }
  108. $this->em->flush();
  109. }
  110. public function addToProductModel(Attribute $attribute, ProductModel $product, string $value) {
  111. $pa = $this->em->getRepository('App:ProductModelAttribute')->findOneBy([
  112. 'attribute' => $attribute,
  113. 'productModel' => $product
  114. ]);
  115. if(empty($pa)) {
  116. $pa = new \App\Entity\ProductModelAttribute();
  117. $pa->setAttribute($attribute);
  118. $pa->setProductModel($product);
  119. }
  120. $pa->setValue($value);
  121. $this->em->persist($pa);
  122. $this->em->flush();
  123. }
  124. public function removeFromProductModel(Attribute $attribute, ProductModel $model) {
  125. $pas = $this->em->getRepository('App:ProductModelAttribute')->findBy([
  126. 'productModel' => $model,
  127. 'attribute' => $attribute
  128. ]);
  129. foreach($pas as $pa){
  130. $this->em->remove($pa);
  131. }
  132. $this->em->flush();
  133. }
  134. public function getAttributesByProductModel(ProductModel $productModel): array {
  135. $attributes = [];
  136. $variant = $productModel->getVariant();
  137. if (!$variant) {
  138. return $attributes;
  139. }
  140. foreach ([$variant->getAxis1(), $variant->getAxis2()] as $axis) {
  141. if (!$axis) {
  142. continue;
  143. }
  144. $codes = array_values(array_filter(array_map('trim', explode(',', $axis))));
  145. if (count($codes) === 1) {
  146. $attribute = $this->getAttribute($codes[0]);
  147. if ($attribute && $attribute->isVariant()) {
  148. $attribute->setValues($this->getAttributeValuesByProductModel($attribute, $productModel));
  149. $attributes[] = $attribute;
  150. }
  151. } else {
  152. $composite = $this->buildCompositeAxis($codes, $productModel);
  153. if ($composite) {
  154. $attributes[] = $composite;
  155. }
  156. }
  157. }
  158. return $attributes;
  159. }
  160. /**
  161. * Builds a synthetic axis object for multi-attribute axes (e.g. axis = "taille,couleur").
  162. * Returns a stdClass with code/title/display/values so Twig can render it like a regular Attribute.
  163. * Each value has ->value (combined label "XS / Rouge") and ->product (the matching Product).
  164. */
  165. private function buildCompositeAxis(array $codes, ProductModel $productModel): ?\stdClass {
  166. $axisAttributes = [];
  167. foreach ($codes as $code) {
  168. $attr = $this->em->getRepository(Attribute::class)->findOneByCode($code);
  169. if ($attr) {
  170. $axisAttributes[] = $attr;
  171. }
  172. }
  173. if (empty($axisAttributes)) {
  174. return null;
  175. }
  176. $values = [];
  177. $seen = [];
  178. foreach ($productModel->getAllProducts() as $product) {
  179. $parts = [];
  180. foreach ($axisAttributes as $attr) {
  181. $pa = $product->getAttribute($attr->getCode());
  182. $parts[] = $pa ? ($pa->getDisplayedValue() ?? '?') : '?';
  183. }
  184. $label = implode(' / ', $parts);
  185. if (in_array($label, $seen)) {
  186. continue;
  187. }
  188. $seen[] = $label;
  189. $value = new \stdClass();
  190. $value->value = $label;
  191. $value->product = $product;
  192. $values[] = $value;
  193. }
  194. $composite = new \stdClass();
  195. $composite->code = implode(',', $codes);
  196. $composite->title = implode(' / ', array_map(fn(Attribute $a) => $a->getTitle(), $axisAttributes));
  197. $composite->display = Attribute::DISPLAY_TYPE_COMBOBOX;
  198. $composite->values = $values;
  199. return $composite;
  200. }
  201. public function getAttributeValuesByProductModel(Attribute $attribute, ProductModel $productModel) {
  202. $values = [];
  203. $tmp = [];
  204. $products = $productModel->getAllProducts();
  205. foreach($products as $product) {
  206. $attr = $product->getAttribute($attribute->getCode());
  207. if($attr && !in_array($attr->getValue(), $tmp)) {
  208. $values[] = $attr;
  209. $tmp[] = $attr->getValue();
  210. }
  211. }
  212. return $values;
  213. }
  214. }