src/Manager/AttributeManager.php line 153

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. $pa->translate($locale);
  89. $description = $pa->getDescription();
  90. if(empty($description)) {
  91. $description = $this->createProductAttributeDescription($pa, $locale);
  92. }
  93. $description->setValue($value);
  94. $this->em->persist($description);
  95. }
  96. $this->em->persist($pa);
  97. if ($flush) {
  98. $this->em->flush();
  99. }
  100. }
  101. public function removeFromProduct(Attribute $attribute, Product $product) {
  102. $pas = $this->em->getRepository('App:ProductAttribute')->findBy([
  103. 'product' => $product,
  104. 'attribute' => $attribute
  105. ]);
  106. foreach($pas as $pa){
  107. $this->em->remove($pa);
  108. }
  109. $this->em->flush();
  110. }
  111. public function addToProductModel(Attribute $attribute, ProductModel $product, string $value) {
  112. $pa = $this->em->getRepository('App:ProductModelAttribute')->findOneBy([
  113. 'attribute' => $attribute,
  114. 'productModel' => $product
  115. ]);
  116. if(empty($pa)) {
  117. $pa = new \App\Entity\ProductModelAttribute();
  118. $pa->setAttribute($attribute);
  119. $pa->setProductModel($product);
  120. }
  121. $pa->setValue($value);
  122. $this->em->persist($pa);
  123. $this->em->flush();
  124. }
  125. public function removeFromProductModel(Attribute $attribute, ProductModel $model) {
  126. $pas = $this->em->getRepository('App:ProductModelAttribute')->findBy([
  127. 'productModel' => $model,
  128. 'attribute' => $attribute
  129. ]);
  130. foreach($pas as $pa){
  131. $this->em->remove($pa);
  132. }
  133. $this->em->flush();
  134. }
  135. public function getAttributesByProductModel(ProductModel $productModel): array {
  136. $attributes = [];
  137. $variant = $productModel->getVariant();
  138. if (!$variant) {
  139. return $attributes;
  140. }
  141. foreach ([$variant->getAxis1(), $variant->getAxis2()] as $axis) {
  142. if (!$axis) {
  143. continue;
  144. }
  145. $codes = array_values(array_filter(array_map('trim', explode(',', $axis))));
  146. if (count($codes) === 1) {
  147. $attribute = $this->getAttribute($codes[0]);
  148. if ($attribute && $attribute->isVariant()) {
  149. $attribute->setValues($this->getAttributeValuesByProductModel($attribute, $productModel));
  150. $attributes[] = $attribute;
  151. }
  152. } else {
  153. $composite = $this->buildCompositeAxis($codes, $productModel);
  154. if ($composite) {
  155. $attributes[] = $composite;
  156. }
  157. }
  158. }
  159. return $attributes;
  160. }
  161. /**
  162. * Builds a synthetic axis object for multi-attribute axes (e.g. axis = "taille,couleur").
  163. * Returns a stdClass with code/title/display/values so Twig can render it like a regular Attribute.
  164. * Each value has ->value (combined label "XS / Rouge") and ->product (the matching Product).
  165. */
  166. private function buildCompositeAxis(array $codes, ProductModel $productModel): ?\stdClass {
  167. $axisAttributes = [];
  168. foreach ($codes as $code) {
  169. $attr = $this->em->getRepository(Attribute::class)->findOneByCode($code);
  170. if ($attr) {
  171. $axisAttributes[] = $attr;
  172. }
  173. }
  174. if (empty($axisAttributes)) {
  175. return null;
  176. }
  177. $values = [];
  178. $seen = [];
  179. foreach ($productModel->getAllProducts() as $product) {
  180. $parts = [];
  181. foreach ($axisAttributes as $attr) {
  182. $pa = $product->getAttribute($attr->getCode());
  183. $parts[] = $pa ? ($pa->getDisplayedValue() ?? '?') : '?';
  184. }
  185. $label = implode(' / ', $parts);
  186. if (in_array($label, $seen)) {
  187. continue;
  188. }
  189. $seen[] = $label;
  190. $value = new \stdClass();
  191. $value->value = $label;
  192. $value->product = $product;
  193. $values[] = $value;
  194. }
  195. $composite = new \stdClass();
  196. $composite->code = implode(',', $codes);
  197. $composite->title = implode(' / ', array_map(fn(Attribute $a) => $a->getTitle(), $axisAttributes));
  198. $composite->display = Attribute::DISPLAY_TYPE_COMBOBOX;
  199. $composite->values = $values;
  200. return $composite;
  201. }
  202. public function getAttributeValuesByProductModel(Attribute $attribute, ProductModel $productModel) {
  203. $values = [];
  204. $tmp = [];
  205. $products = $productModel->getAllProducts();
  206. foreach($products as $product) {
  207. $attr = $product->getAttribute($attribute->getCode());
  208. if($attr && !in_array($attr->getValue(), $tmp)) {
  209. $values[] = $attr;
  210. $tmp[] = $attr->getValue();
  211. }
  212. }
  213. return $values;
  214. }
  215. }