src/Controller/ProductController.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ERPCommercialData;
  4. use App\Entity\ERPCommercialDataLang;
  5. use App\Entity\ERPLogisticData;
  6. use App\Entity\File;
  7. use App\Entity\Image;
  8. use App\Entity\ImageLang;
  9. use App\Entity\Product;
  10. use App\Entity\ProductAttribute;
  11. use App\Entity\ProductLang;
  12. use App\Entity\PSA;
  13. use App\Form\ProductLangType;
  14. use App\Form\ProductType;
  15. use App\Repository\AttributeRepository;
  16. use App\Repository\AttributeValueRepository;
  17. use App\Repository\ERPCommercialDataLangRepository;
  18. use App\Repository\ERPCommercialDataRepository;
  19. use App\Repository\ERPLogisticDataRepository;
  20. use App\Repository\FeatureRepository;
  21. use App\Repository\FeatureValueRepository;
  22. use App\Repository\FileRepository;
  23. use App\Repository\ImageLangRepository;
  24. use App\Repository\ImageRepository;
  25. use App\Repository\LangRepository;
  26. use App\Repository\ProductAttributeRepository;
  27. use App\Repository\ProductLangRepository;
  28. use App\Repository\ProductRepository;
  29. use App\Repository\PSARepository;
  30. use App\Repository\SiteRepository;
  31. use App\Services\WebserviceUtils;
  32. use Attribute;
  33. use CURLFile;
  34. use DateTime;
  35. use Doctrine\ORM\EntityManager;
  36. use Doctrine\Persistence\ManagerRegistry;
  37. use PrestaShopWebservice;
  38. use PrestaShopWebserviceException;
  39. use Psr\Log\LoggerInterface;
  40. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  41. use Symfony\Component\Security\Core\Security;
  42. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  43. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  44. use Symfony\Component\HttpFoundation\Request;
  45. use Symfony\Component\HttpFoundation\Response;
  46. use Symfony\Component\Routing\Annotation\Route;
  47. use Symfony\Component\String\Slugger\SluggerInterface;
  48. use Symfony\Contracts\Translation\TranslatorInterface;
  49. #[Route('/product'name'app_product')]
  50. #[IsGranted('ROLE_CONTENT_MANAGER')]
  51. class ProductController extends AbstractController
  52. {
  53.     public $twigData;
  54.     public $breadcrumb = [];
  55.     public $translator;
  56.     public $security;
  57.     public $em;
  58.     public function __construct(Security $securityTranslatorInterface $translatorManagerRegistry $doctrine)
  59.     {
  60.         $this->twigData = [];
  61.         $this->translator $translator;
  62.         $this->security $security;
  63.         $this->twigData = [
  64.             'user' => $security->getUser(),
  65.         ];
  66.         $this->em $doctrine->getManager();
  67.     }
  68.     #[Route('/'name'_index')]
  69.     public function index(LangRepository $langRepositoryProductRepository $productRepositoryProductLangRepository $productLangRepositorySiteRepository $siteRepository): Response
  70.     {
  71.         $this->breadcrumb[0]['name'] = $this->translator->trans('page.catalog.Catalog');
  72.         $this->breadcrumb[0]['link'] = $this->generateUrl('app_catalog_index');
  73.         $this->breadcrumb[0]['last'] = false;
  74.         $this->breadcrumb[1]['name'] = $this->translator->trans('page.product.Products');
  75.         $this->breadcrumb[1]['last'] = true;
  76.         $this->twigData['breadcrumb'] = $this->breadcrumb;
  77.         $langs $langRepository->findAll();
  78.         $products $productRepository->findBy([], ['id' => 'ASC']);
  79.         foreach ($products as $keyProduct => $product) {
  80.             $productLangs $product->getProductLangs();
  81.             foreach ($productLangs as $keyProductLang => $productLang) {
  82.                 $productLang->setProgression($productLang->calculateProgression());
  83.                 $productLangRepository->add($productLang);
  84.             }
  85.         }
  86.         $this->em->flush();
  87.         $langsPercent = [];
  88.         foreach ($langs as $keyLang => $lang) {
  89.             $langsPercent[$keyLang]['lang'] = $lang;
  90.         }
  91.         $this->twigData['langs'] = $langs;
  92.         $this->twigData['products'] = $products;
  93.         $this->twigData['sites'] = $siteRepository->findAll();
  94.         $user $this->security->getUser();
  95.         $userInterfaceLang $user->getInterfaceLang();
  96.         $this->twigData['userInterfaceLang'] = $userInterfaceLang;
  97.         return $this->render('product/index.html.twig'$this->twigData);
  98.     }
  99.     #[Route('/new'name'_new')]
  100.     public function new(LangRepository $langRepositoryRequest $requestProductRepository $productRepositoryProductLangRepository $productLangRepository): Response
  101.     {
  102.         $this->breadcrumb[0]['name'] = $this->translator->trans('page.catalog.Catalog');
  103.         $this->breadcrumb[0]['link'] = $this->generateUrl('app_catalog_index');
  104.         $this->breadcrumb[0]['last'] = false;
  105.         $this->breadcrumb[1]['name'] = $this->translator->trans('page.product.Products');
  106.         $this->breadcrumb[1]['link'] = $this->generateUrl('app_product_index');
  107.         $this->breadcrumb[1]['last'] = false;
  108.         $this->breadcrumb[2]['name'] = $this->translator->trans('page.product.new');
  109.         $this->breadcrumb[2]['last'] = true;
  110.         $this->twigData['breadcrumb'] = $this->breadcrumb;
  111.         $step $request->query->get('step');
  112.         $this->twigData['step'] = $step;
  113.         $product = new Product();
  114.         $formProduct $this->createForm(ProductType::class, $product);
  115.         $formProduct->handleRequest($request);
  116.         $this->twigData['formProduct'] = $formProduct->createView();
  117.         $langs $langRepository->findAll();
  118.         $formsProductLang = [];
  119.         foreach ($langs as $keyLangs => $lang) {
  120.             $productLang = new ProductLang();
  121.             $productLang->setLang($lang);
  122.             $formProductLang $this->createForm(ProductLangType::class, $productLang);
  123.             $formProductLang->handleRequest($request);
  124.             $formsProductLang[$lang->getId()]['form'] = $formProductLang;
  125.             $formsProductLang[$lang->getId()]['view'] = $formProductLang->createView();
  126.             $formsProductLang[$lang->getId()]['entity'] = $productLang;
  127.         }
  128.         if ($formProduct->isSubmitted() && $formProduct->isValid()) {
  129.             // Retrive datas from lang form
  130.             $productLangs $request->request->getIterator();
  131.             $productLangs $productLangs['product_lang'];
  132.             $names $productLangs['name'];
  133.             $descriptions $productLangs['description'];
  134.             $regulations $productLangs['regulations'];
  135.             $shortDescriptions $productLangs['short_description'];
  136.             $subTitles $productLangs['sub_title'];
  137.             $advices $productLangs['advices'];
  138.             $metaTitles $productLangs['meta_title'];
  139.             $metaDescriptions $productLangs['meta_description'];
  140.             // Assign datas to productLang
  141.             foreach ($formsProductLang as $lang_id => $formProductLang) {
  142.                 $name $names[$lang_id];
  143.                 $description $descriptions[$lang_id];
  144.                 $regulation $regulations[$lang_id];
  145.                 $advice $advices[$lang_id];
  146.                 $subTitle $subTitles[$lang_id];
  147.                 $shortDescription $shortDescriptions[$lang_id];
  148.                 $metaTitle $metaTitles[$lang_id];
  149.                 $metaDescription $metaDescriptions[$lang_id];
  150.                 $formProductLang['entity']->setProduct($product);
  151.                 $formProductLang['entity']->setName($name);
  152.                 $formProductLang['entity']->setDescription($description);
  153.                 $formProductLang['entity']->setRegulations($regulation);
  154.                 $formProductLang['entity']->setAdvices($advice);
  155.                 $formProductLang['entity']->setSubTitle($subTitle);
  156.                 $formProductLang['entity']->setShortDescription($shortDescription);
  157.                 $formProductLang['entity']->setMetaTitle($metaTitle);
  158.                 $formProductLang['entity']->setMetaDescription($metaDescription);
  159.                 $formProductLang['entity']->setProgression($formProductLang['entity']->calculateProgression());
  160.                 $productLangRepository->add($formProductLang['entity']);
  161.             }
  162.             $productRepository->add($producttrue);
  163.             return $this->redirectToRoute('app_product_edit', ['id' => $product->getId()]);
  164.         }
  165.         $this->twigData['formsProductLang'] = $formsProductLang;
  166.         $this->twigData['langs'] = $langs;
  167.         return $this->render('product/new.html.twig'$this->twigData);
  168.     }
  169.     #[Route('/all/edit'name'_all_edit')]
  170.     public function allEdit(FileRepository $fileRepositoryLangRepository $langRepositoryRequest $requestProductRepository $productRepositoryProductLangRepository $productLangRepositoryAttributeRepository $attributeRepositoryProductAttributeRepository $productAttributeRepositoryFeatureRepository $featureRepositoryFeatureValueRepository $featureValueRepository): Response
  171.     {
  172.         $products $productRepository->findAll();
  173.         $langs $langRepository->findAll();
  174.         foreach ($products as $product) {
  175.             foreach ($langs as $keyLangs => $lang) {
  176.                 $productLang $productLangRepository->findOneBy(['lang' => $lang'product' => $product]);
  177.                 if ($productLang != null) {
  178.                     $productLang->setProgression($productLang->calculateProgression());
  179.                     $productLangRepository->add($productLang);
  180.                     // if ($productLang->getRegulations() != null && json_decode($productLang->getRegulations()) == null) {
  181.                     //     $newRegulation = json_decode(json_encode($productLang->getRegulations()));
  182.                     //     $startHtml = strpos($newRegulation, '"html","data":{"html":');
  183.                     // }
  184.                     if ($productLang->getDescription() != null && json_decode($productLang->getDescription()) == null) {
  185.                         $newDescription = [
  186.                             "time" => 1675328680884,
  187.                             "blocks" => [
  188.                                 [
  189.                                     "id" => "9dCJlykOTY",
  190.                                     "type" => "html",
  191.                                     "data" => [
  192.                                         "html" => $productLang->getDescription()
  193.                                     ]
  194.                                 ]
  195.                             ],
  196.                             "version" => "2.26.4"
  197.                         ];
  198.                         // $newDescription = '{"time":1675328680884,"blocks":[{"id":"9dCJlykOTY","type":"html","data":{"html":"'.$descriptionEncode. '"}}],"version":"2.26.4"}';
  199.                         $newDescription json_encode($newDescription);
  200.                         $productLang->setDescription($newDescription);
  201.                     }
  202.                     if ($productLang->getShortDescription() != null && json_decode($productLang->getShortDescription()) == null) {
  203.                         $newDescription = [
  204.                             "time" => 1675328680884,
  205.                             "blocks" => [
  206.                                 [
  207.                                     "id" => "9dCJlykOTY",
  208.                                     "type" => "html",
  209.                                     "data" => [
  210.                                         "html" => $productLang->getShortDescription()
  211.                                     ]
  212.                                 ]
  213.                             ],
  214.                             "version" => "2.26.4"
  215.                         ];
  216.                         // $newDescription = '{"time":1675328680884,"blocks":[{"id":"9dCJlykOTY","type":"html","data":{"html":"'.$descriptionEncode. '"}}],"version":"2.26.4"}';
  217.                         $newDescription json_encode($newDescription);
  218.                         $productLang->setShortDescription($newDescription);
  219.                     }
  220.                     if ($productLang->getAdvices() != null && json_decode($productLang->getAdvices()) == null) {
  221.                         $newAdvices = [
  222.                             "time" => 1675328680884,
  223.                             "blocks" => [
  224.                                 [
  225.                                     "id" => "9dCJlykOTY",
  226.                                     "type" => "html",
  227.                                     "data" => [
  228.                                         "html" => $productLang->getAdvices()
  229.                                     ]
  230.                                 ]
  231.                             ],
  232.                             "version" => "2.26.4"
  233.                         ];
  234.                         // $newAdvices = '{"time":1675328680884,"blocks":[{"id":"9dCJlykOTY","type":"html","data":{"html":"'.$descriptionEncode. '"}}],"version":"2.26.4"}';
  235.                         $newAdvices json_encode($newAdvices);
  236.                         $productLang->setAdvices($newAdvices);
  237.                     }
  238.                     if ($productLang->getRegulations() != null && json_decode($productLang->getRegulations()) == null) {
  239.                         $newRegulation = [
  240.                             "time" => 1675328680884,
  241.                             "blocks" => [
  242.                                 [
  243.                                     "id" => "9dCJlykOTY",
  244.                                     "type" => "html",
  245.                                     "data" => [
  246.                                         "html" => $productLang->getRegulations()
  247.                                     ]
  248.                                 ]
  249.                             ],
  250.                             "version" => "2.26.4"
  251.                         ];
  252.                         // $newRegulation = '{"time":1675328680884,"blocks":[{"id":"9dCJlykOTY","type":"html","data":{"html":"'.$descriptionEncode. '"}}],"version":"2.26.4"}';
  253.                         $newRegulation json_encode($newRegulation);
  254.                         $productLang->setRegulations($newRegulation);
  255.                     }
  256.                     $productLangRepository->add($productLang);
  257.                 }
  258.                 $this->em->flush();
  259.             }
  260.         }
  261.         return $this->redirectToRoute('app_product_index');
  262.     }
  263.     #[Route('/{id}/edit'name'_edit')]
  264.     public function edit(Product $productSiteRepository $siteRepositoryERPLogisticDataRepository $ERPLogisticDataRepositoryERPCommercialDataRepository $ERPCommercialDataRepositoryERPCommercialDataLangRepository $ERPCommercialDataLangRepositoryFileRepository $fileRepositoryLangRepository $langRepositoryRequest $requestProductRepository $productRepositoryProductLangRepository $productLangRepositoryAttributeRepository $attributeRepositoryProductAttributeRepository $productAttributeRepositoryFeatureRepository $featureRepositoryFeatureValueRepository $featureValueRepository): Response
  265.     {
  266.         $this->breadcrumb[0]['name'] = $this->translator->trans('page.catalog.Catalog');
  267.         $this->breadcrumb[0]['link'] = $this->generateUrl('app_catalog_index');
  268.         $this->breadcrumb[0]['last'] = false;
  269.         $this->breadcrumb[1]['name'] = $this->translator->trans('page.product.Products');
  270.         $this->breadcrumb[1]['link'] = $this->generateUrl('app_product_index');
  271.         $this->breadcrumb[1]['last'] = false;
  272.         $this->breadcrumb[2]['name'] = $this->translator->trans('page.product.edit');
  273.         $this->breadcrumb[2]['last'] = true;
  274.         $this->twigData['breadcrumb'] = $this->breadcrumb;
  275.         $step $request->query->get('step');
  276.         $this->twigData['step'] = $step;
  277.         $formProduct $this->createForm(ProductType::class, $product);
  278.         $formProduct->handleRequest($request);
  279.         $user $this->security->getUser();
  280.         $userInterfaceLang $user->getInterfaceLang();
  281.         $attributes $attributeRepository->findAll();
  282.         $attributesDatas = [];
  283.         $attributesValuesDatas = [];
  284.         foreach ($attributes as $keyAttribute => $attribute) {
  285.             $attributesDatas[$attribute->getId()] = [
  286.                 'id' => $attribute->getId(),
  287.             ];
  288.             $attributeLangs $attribute->getAttributeLang();
  289.             foreach ($attributeLangs as $keyAttributeLang => $attributeLang) {
  290.                 if ($attributeLang->getLang() == $userInterfaceLang) {
  291.                     $attributesDatas[$attribute->getId()]['value'] = $attributeLang->getName();
  292.                     $attributesDatas[$attribute->getId()]['label'] = $attributeLang->getName();
  293.                 }
  294.             }
  295.             $attributeValues $attribute->getAttributeValues();
  296.             foreach ($attributeValues as $keyAttributeValue => $attributeValue) {
  297.                 $attributesValuesDatas[$attribute->getId() . "-" $attributeValue->getId()]['value'] = $attributeValue->getId();
  298.                 $attributeValueLang $attributeValue->getAttributeValueLangs();
  299.                 foreach ($attributeValueLang as $keyAttributeValueLang => $attributeValueLang) {
  300.                     if ($attributeValueLang->getLang() == $userInterfaceLang) {
  301.                         $attributesValuesDatas[$attribute->getId() . "-" $attributeValue->getId()]['name'] = $attributeValueLang->getValue();
  302.                         $attributesValuesDatas[$attribute->getId() . "-" $attributeValue->getId()]['class'] = $attributesDatas[$attribute->getId()]['label'];
  303.                     }
  304.                 }
  305.             }
  306.         }
  307.         $this->twigData['formProduct'] = $formProduct->createView();
  308.         $this->twigData['product'] = $product;
  309.         $this->twigData['attributes'] = $attributes;
  310.         $this->twigData['userInterfaceLang'] = $userInterfaceLang;
  311.         $this->twigData['files'] = $fileRepository->findAll();
  312.         $this->twigData['attributesDatas'] = json_encode($attributesDatas);
  313.         $this->twigData['attributesValuesDatas'] = json_encode($attributesValuesDatas);
  314.         $this->twigData['sites'] = $siteRepository->findAll();
  315.         //ERP
  316.         $productErp = [];
  317.         $productLogisticErp $product->getERPLogisticData();
  318.         $productCommercialErp $product->getERPCommercialData();
  319.         $productErp['logistic'] = $productLogisticErp;
  320.         $productErp['commercial'] = $productCommercialErp;
  321.         $this->twigData['productErp'] = $productErp;
  322.         $langs $langRepository->findAll();
  323.         $formsProductLang = [];
  324.         foreach ($langs as $keyLangs => $lang) {
  325.             $productLang $productLangRepository->findOneBy(['lang' => $lang'product' => $product]);
  326.             if ($productLang == null) {
  327.                 $productLang = new ProductLang();
  328.                 $productLang->setProduct($product);
  329.                 $productLang->setLang($lang);
  330.                 $productLang->setProgression(0);
  331.                 $productLangRepository->add($productLangtrue);
  332.             } else {
  333.                 if ($productLang->getRegulations() != null && json_decode($productLang->getRegulations()) == null) {
  334.                     $newRegulation json_decode(json_encode($productLang->getRegulations()));
  335.                     $startHtml strpos($newRegulation'"html","data":{"html":');
  336.                 }
  337.                 if ($productLang->getDescription() != null && json_decode($productLang->getDescription()) == null) {
  338.                     $newDescription = [
  339.                         "time" => 1675328680884,
  340.                         "blocks" => [
  341.                             [
  342.                                 "id" => "9dCJlykOTY",
  343.                                 "type" => "html",
  344.                                 "data" => [
  345.                                     "html" => $productLang->getDescription()
  346.                                 ]
  347.                             ]
  348.                         ],
  349.                         "version" => "2.26.4"
  350.                     ];
  351.                     // $newDescription = '{"time":1675328680884,"blocks":[{"id":"9dCJlykOTY","type":"html","data":{"html":"'.$descriptionEncode. '"}}],"version":"2.26.4"}';
  352.                     $newDescription json_encode($newDescription);
  353.                     $productLang->setDescription($newDescription);
  354.                 }
  355.                 if ($productLang->getShortDescription() != null && json_decode($productLang->getShortDescription()) == null) {
  356.                     $newDescription = [
  357.                         "time" => 1675328680884,
  358.                         "blocks" => [
  359.                             [
  360.                                 "id" => "9dCJlykOTY",
  361.                                 "type" => "html",
  362.                                 "data" => [
  363.                                     "html" => $productLang->getShortDescription()
  364.                                 ]
  365.                             ]
  366.                         ],
  367.                         "version" => "2.26.4"
  368.                     ];
  369.                     // $newDescription = '{"time":1675328680884,"blocks":[{"id":"9dCJlykOTY","type":"html","data":{"html":"'.$descriptionEncode. '"}}],"version":"2.26.4"}';
  370.                     $newDescription json_encode($newDescription);
  371.                     $productLang->setShortDescription($newDescription);
  372.                 }
  373.                 if ($productLang->getAdvices() != null && json_decode($productLang->getAdvices()) == null) {
  374.                     $newAdvices = [
  375.                         "time" => 1675328680884,
  376.                         "blocks" => [
  377.                             [
  378.                                 "id" => "9dCJlykOTY",
  379.                                 "type" => "html",
  380.                                 "data" => [
  381.                                     "html" => $productLang->getAdvices()
  382.                                 ]
  383.                             ]
  384.                         ],
  385.                         "version" => "2.26.4"
  386.                     ];
  387.                     // $newAdvices = '{"time":1675328680884,"blocks":[{"id":"9dCJlykOTY","type":"html","data":{"html":"'.$descriptionEncode. '"}}],"version":"2.26.4"}';
  388.                     $newAdvices json_encode($newAdvices);
  389.                     $productLang->setAdvices($newAdvices);
  390.                 }
  391.                 if ($productLang->getRegulations() != null && json_decode($productLang->getRegulations()) == null) {
  392.                     $newRegulation = [
  393.                         "time" => 1675328680884,
  394.                         "blocks" => [
  395.                             [
  396.                                 "id" => "9dCJlykOTY",
  397.                                 "type" => "html",
  398.                                 "data" => [
  399.                                     "html" => $productLang->getRegulations()
  400.                                 ]
  401.                             ]
  402.                         ],
  403.                         "version" => "2.26.4"
  404.                     ];
  405.                     // $newRegulation = '{"time":1675328680884,"blocks":[{"id":"9dCJlykOTY","type":"html","data":{"html":"'.$descriptionEncode. '"}}],"version":"2.26.4"}';
  406.                     $newRegulation json_encode($newRegulation);
  407.                     $productLang->setRegulations($newRegulation);
  408.                 }
  409.             }
  410.             $productLang->setProgression($productLang->calculateProgression());
  411.             $formProductLang $this->createForm(ProductLangType::class, $productLang);
  412.             $formsProductLang[$lang->getId()]['form'] = $formProductLang;
  413.             $formsProductLang[$lang->getId()]['view'] = $formProductLang->createView();
  414.             $formsProductLang[$lang->getId()]['entity'] = $productLang;
  415.             $formProductLang->handleRequest($request);
  416.         }
  417.         $productCommercialErp $ERPCommercialDataRepository->findOneBy(['product' => $product]);
  418.         $productLogisticErp $ERPLogisticDataRepository->findOneBy(['product' => $product]);
  419.         if ($productCommercialErp == null) {
  420.             $productCommercialErp = new ERPCommercialData();
  421.             $productCommercialErp->setProduct($product);
  422.         }
  423.         if ($productLogisticErp == null) {
  424.             $productLogisticErp = new ERPLogisticData();
  425.             $productLogisticErp->setProduct($product);
  426.         }
  427.         $this->twigData['productCommercialErp'] = $productCommercialErp;
  428.         $this->twigData['productLogisticErp'] = $productLogisticErp;
  429.         if ($formProduct->isSubmitted() && $formProduct->isValid()) {
  430.             // ERP
  431.             // -- commercial
  432.             // $productCommercialErpDatas = $request->request->getIterator();
  433.             // dd($productCommercialErpDatas);
  434.             // $productCommercialErpDatas = $productCommercialErpDatas['product_erpCommercial'];
  435.             // $productCommercialErp = $ERPCommercialDataRepository->findOneBy(['product' => $product]);
  436.             // if ($productCommercialErp == null) {
  437.             //     $productCommercialErp = new ERPCommercialData();
  438.             //     $productCommercialErp->setProduct($product);
  439.             // }
  440.             // $productCommercialErp->setManufacturer($productCommercialErpDatas['manufacturer']);
  441.             // $productCommercialErp->setBrand($productCommercialErpDatas['brand']);
  442.             // $productCommercialErp->setProductRange($productCommercialErpDatas['productRange']);
  443.             // $productCommercialErp->setCommercialReference($productCommercialErpDatas['commercialReference']);
  444.             // $productCommercialErp->setProductReference($productCommercialErpDatas['productReference']);
  445.             // $productCommercialErp->setinfoReference($productCommercialErpDatas['infoReference']);
  446.             // $productCommercialErp->setGTIN13($productCommercialErpDatas['GTIN13']);
  447.             // $productCommercialErp->setDateTarif($productCommercialErpDatas['dateTarif']);
  448.             // $productCommercialErp->setPriceWithoutTax($productCommercialErpDatas['priceWithoutTax']);
  449.             // $productCommercialErp->setDistributorPriceWithoutTax($productCommercialErpDatas['distributorPriceWithoutTax']);
  450.             // $productCommercialErp->setCurrency($productCommercialErpDatas['currency']);
  451.             // $productCommercialErp->setVat($productCommercialErpDatas['vat']);
  452.             // $productCommercialErp->setQuantity($productCommercialErpDatas['quantity']);
  453.             // $productCommercialErp->setBaseReferenceUnit($productCommercialErpDatas['baseReferenceUnit']);
  454.             // $productCommercialErp->setMinimalOrderQuantity($productCommercialErpDatas['minimalOrderQuantity']);
  455.             // $productCommercialErp->setMultiplePerOrder($productCommercialErpDatas['multiplePerOrder']);
  456.             // $productCommercialErp->setPackagingQtyMin($productCommercialErpDatas['packagingQtyMin']);
  457.             // $productCommercialErp->setManufacturerStoredProduct($productCommercialErpDatas['manufacturerStoredProduct']);
  458.             // $productCommercialErp->setDeliveryDelay($productCommercialErpDatas['deliveryDelay']);
  459.             // $productCommercialErp->setElectronicDataInterchange($productCommercialErpDatas['electronicDataInterchange']);
  460.             // $productCommercialErp->setWarrantyDuration($productCommercialErpDatas['warrantyDuration']);
  461.             // $productCommercialErp->setReferenceStatus($productCommercialErpDatas['referenceStatus']);
  462.             // $productCommercialErp->setTaric($productCommercialErpDatas['taric']);
  463.             // $productCommercialErp->setProductHeight($productCommercialErpDatas['productHeight']);
  464.             // $productCommercialErp->setProductHeightUnity($productCommercialErpDatas['productHeightUnity']);
  465.             // $productCommercialErp->setProductWidth($productCommercialErpDatas['productWidth']);
  466.             // $productCommercialErp->setProductWidthUnity($productCommercialErpDatas['productWidthUnity']);
  467.             // $productCommercialErp->setProductDepth($productCommercialErpDatas['productDepth']);
  468.             // $productCommercialErp->setProductDepthUnity($productCommercialErpDatas['productDepthUnity']);
  469.             // $productCommercialErp->setProductWeight($productCommercialErpDatas['productWeight']);
  470.             // $productCommercialErp->setProductWeightUnity($productCommercialErpDatas['productWeightUnity']);
  471.             // $productCommercialErp->setProductDiameter($productCommercialErpDatas['productDiameter']);
  472.             // $productCommercialErp->setProductDiameterUnity($productCommercialErpDatas['productDiameterUnity']);
  473.             // $productCommercialErp->setRecommendedStorageLimitDuration($productCommercialErpDatas['RecommendedStorageLimitDuration']);
  474.             // // -- commercial lang
  475.             // $productCommercialErpLangDatas = $productCommercialErpDatas['product_erpCommercial_lang'];
  476.             // foreach ($langs as $keyLangs => $lang) {
  477.             //     $lang = $langRepository->find($lang->getId());
  478.             //     $productCommercialErpLang = $ERPCommercialDataLangRepository->findOneBy(['lang' => $lang, 'product' => $product]);
  479.             //     if ($productCommercialErpLang == null) {
  480.             //         $productCommercialErpLang = new ERPCommercialDataLang();
  481.             //         $productCommercialErpLang->setLang($lang);
  482.             //         $productCommercialErpLang->setProduct($product);
  483.             //         $productCommercialErpLang->setERPCommercialData($productCommercialErp);
  484.             //     }
  485.             //     $productCommercialErpLang->setWording30($productCommercialErpLangDatas['wording30'][$lang->getId()]);
  486.             //     $productCommercialErpLang->setWording80($productCommercialErpLangDatas['wording80'][$lang->getId()]);
  487.             //     $productCommercialErpLang->setWording240($productCommercialErpLangDatas['wording240'][$lang->getId()]);
  488.             //     $ERPCommercialDataLangRepository->add($productCommercialErpLang);
  489.             // }
  490.             // $ERPCommercialDataRepository->add($productCommercialErp, true);
  491.             // // -- logistic
  492.             // $productLogisticErpDatas = $request->request->getIterator();
  493.             // $productLogisticErpDatas = $productLogisticErpDatas['product_erp_logistic'];
  494.             // $productLogisticErp = $ERPLogisticDataRepository->findOneBy(['product' => $product]);
  495.             // if ($productLogisticErp == null) {
  496.             //     $productLogisticErp = new ERPLogisticData();
  497.             //     $productLogisticErp->setProduct($product);
  498.             // }
  499.             // $productLogisticErp->setBrand($productLogisticErpDatas['brand']);
  500.             // $productLogisticErp->setCommercialReference($productLogisticErpDatas['commercialReference']);
  501.             // $productLogisticErp->setQuantity($productLogisticErpDatas['quantity']);
  502.             // $productLogisticErp->setGTIN14($productLogisticErpDatas['GTIN14']);
  503.             // $productLogisticErp->setProductHeight($productLogisticErpDatas['productHeight']);
  504.             // $productLogisticErp->setProductHeightUnity($productLogisticErpDatas['productHeightUnity']);
  505.             // $productLogisticErp->setProductWidth($productLogisticErpDatas['productWidth']);
  506.             // $productLogisticErp->setProductWidthUnity($productLogisticErpDatas['productWidthUnity']);
  507.             // $productLogisticErp->setProductDepth($productLogisticErpDatas['productDepth']);
  508.             // $productLogisticErp->setProductDepthUnity($productLogisticErpDatas['productDepthUnity']);
  509.             // $productLogisticErp->setProductWeight($productLogisticErpDatas['productWeight']);
  510.             // $productLogisticErp->setProductWeightUnity($productLogisticErpDatas['productWeightUnity']);
  511.             // $productLogisticErp->setProductDiameter($productLogisticErpDatas['productDiameter']);
  512.             // $productLogisticErp->setProductDiameterUnity($productLogisticErpDatas['productDiameterUnity']);
  513.             // $productLogisticErp->setProductVolume($productLogisticErpDatas['productVolume']);
  514.             // $productLogisticErp->setProductVolumeUnity($productLogisticErpDatas['productVolumeUnity']);
  515.             // $productLogisticErp->setDeposit($productLogisticErpDatas['deposit']);
  516.             // $productLogisticErp->setStackingLimitCount($productLogisticErpDatas['stackingLimitCount']);
  517.             // $ERPLogisticDataRepository->add($productLogisticErp, true);
  518.             $files $request->request->getIterator();
  519.             $currentFiles $product->getFiles();
  520.             foreach ($currentFiles as $keyCurrentFile => $currentFile) {
  521.                 $product->removeFile($currentFile);
  522.             }
  523.             if (isset($files['productFile'])) {
  524.                 $files $files['productFile'];
  525.                 foreach ($files as $keyFiles => $file) {
  526.                     $file $fileRepository->find($file);
  527.                     $product->addFile($file);
  528.                 }
  529.             }
  530.             $productLangs $request->request->getIterator();
  531.             $productLangs $productLangs['product_lang'];
  532.             $names $productLangs['name'];
  533.             $descriptions $productLangs['description'];
  534.             $regulations $productLangs['regulations'];
  535.             $advices $productLangs['advices'];
  536.             $shortDescriptions $productLangs['short_description'];
  537.             $subTitles $productLangs['sub_title'];
  538.             $metaTitles $productLangs['meta_title'];
  539.             $metaDescriptions $productLangs['meta_description'];
  540.             $currentProductFeatures $product->getFeatureValues();
  541.             foreach ($currentProductFeatures as $keyCurrentProductFeature => $currentProductFeature) {
  542.                 $product->removeFeatureValue($currentProductFeature);
  543.             }
  544.             $formProductFeatures $request->request->all('features');
  545.             if (count($formProductFeatures) > 0) {
  546.                 foreach ($formProductFeatures as $keyProductFeatures => $formProductFeature) {
  547.                     $productFeatureValue $featureValueRepository->find($formProductFeature['value']);
  548.                     if ($productFeatureValue) {
  549.                         $product->addFeatureValue($productFeatureValue);
  550.                     }
  551.                 }
  552.             }
  553.             foreach ($formsProductLang as $lang_id => $formProductLang) {
  554.                 $lang $langRepository->find($lang_id);
  555.                 $name $names[$lang_id];
  556.                 $description $descriptions[$lang_id];
  557.                 $regulation $regulations[$lang_id];
  558.                 $advice $advices[$lang_id];
  559.                 $shortDescription $shortDescriptions[$lang_id];
  560.                 $subTitle $subTitles[$lang_id];
  561.                 $metaTitle $metaTitles[$lang_id];
  562.                 $metaDescription $metaDescriptions[$lang_id];
  563.                 $productLang $productLangRepository->findOneBy(['lang' => $lang'product' => $product]);
  564.                 if ($productLang == false) {
  565.                     $productLang = new ProductLang();
  566.                     $productLang->setLang($lang);
  567.                     $productLang->setProduct($product);
  568.                 }
  569.                 $productLang->setName($name);
  570.                 $productLang->setDescription($description);
  571.                 $productLang->setRegulations($regulation);
  572.                 $productLang->setAdvices($advice);
  573.                 $productLang->setMetaTitle($metaTitle);
  574.                 $productLang->setMetaDescription($metaDescription);
  575.                 $productLang->setSubTitle($subTitle);
  576.                 $productLang->setShortDescription($shortDescription);
  577.                 $productLang->setProgression($productLang->calculateProgression());
  578.                 $productLangRepository->add($productLang);
  579.             }
  580.             $productRepository->add($producttrue);
  581.             $stay $request->request->get('stay');
  582.             if ($stay == '1') {
  583.                 $redirectUrl $this->generateUrl('app_product_edit', ['id' => $product->getId()]);
  584.                 if ($step != null) {
  585.                     $redirectUrl .= '?step=' $step;
  586.                 }
  587.                 return $this->redirect($redirectUrl);
  588.             }
  589.             return $this->redirectToRoute('app_product_index');
  590.         }
  591.         $this->twigData['formsProductLang'] = $formsProductLang;
  592.         $features $featureRepository->findAll();
  593.         $this->twigData['features'] = $features;
  594.         $productAttributes $productAttributeRepository->findBy(['product' => $product]);
  595.         $this->twigData['productAttributes'] = $productAttributes;
  596.         $this->twigData['langs'] = $langs;
  597.         return $this->render('product/edit.html.twig'$this->twigData);
  598.     }
  599.     #[Route('/{id}/generate-combination'name'_generateCombination')]
  600.     public function generateCombination(Product $productLangRepository $langRepositoryRequest $requestProductAttributeRepository $productAttributeRepositoryProductRepository $productRepositoryAttributeValueRepository $attributeValueRepositoryAttributeRepository $attributeRepository): Response
  601.     {
  602.         $requestBody = [];
  603.         if ($content $request->getContent()) {
  604.             $requestBody json_decode($contenttrue);
  605.         }
  606.         if (count($requestBody) > 0) {
  607.             $attributeValues $requestBody['attribute'];
  608.             // Make groups by attribute values
  609.             $attributeGroups = [];
  610.             foreach ($attributeValues as $keyAttributeValue => $attributeValueId) {
  611.                 $attributeValue $attributeValueRepository->find($attributeValueId);
  612.                 $attributeGroups[$attributeValue->getAttribute()->getId()][] = $attributeValueId;
  613.             }
  614.             // Make attribute id string (like 1-3-4). Numbers are attribute ids
  615.             $builderStrings = [];
  616.             $i 0;
  617.             foreach ($attributeGroups as $keyAttributeGroup => $attributeGroup) {
  618.                 foreach ($attributeGroup as $key => $attributeValue) {
  619.                     if ($i 0) {
  620.                         foreach ($builderStrings[$i 1] as $keyString => $string) {
  621.                             $builderStrings[$i][] = $string '-' $attributeValue;
  622.                         }
  623.                     } else {
  624.                         $builderStrings[$i][] = $attributeValue;
  625.                     }
  626.                 }
  627.                 $i += 1;
  628.             }
  629.             $builderStrings $builderStrings[$i 1];
  630.             $productAttributes $productAttributeRepository->findBy(['product' => $product]);
  631.             $existingStrings = [];
  632.             foreach ($productAttributes as $keyProductAttribute => $productAttribute) {
  633.                 $existingStrings[$keyProductAttribute] = "";
  634.                 foreach ($productAttribute->getAttributeValues() as $keyProductAttributeAttributeValue => $productAttributeAttributeValue) {
  635.                     if (count($productAttribute->getAttributeValues()) - == $keyProductAttributeAttributeValue) {
  636.                         $existingStrings[$keyProductAttribute] .= $productAttributeAttributeValue->getId();
  637.                     } else {
  638.                         $existingStrings[$keyProductAttribute] .= $productAttributeAttributeValue->getId() . "-";
  639.                     }
  640.                 }
  641.             }
  642.             foreach ($builderStrings as $keyAttributesIds => $attributesIds) {
  643.                 if (!in_array($attributesIds$existingStrings)) {
  644.                     $ids explode('-'$attributesIds);
  645.                     $productAttribute = new ProductAttribute();
  646.                     $productAttribute->setProduct($product);
  647.                     $productAttribute->setDefaultOn(0);
  648.                     $productAttributeRepository->add($productAttributefalse);
  649.                     foreach ($ids as $keyId => $id) {
  650.                         $attributeValue $attributeValueRepository->find($id);
  651.                         $productAttribute->addAttributeValue($attributeValue);
  652.                     }
  653.                 }
  654.             }
  655.             $this->em->flush();
  656.             $productAttributes $productAttributeRepository->findBy(['product' => $product]);
  657.             $this->twigData['productAttributes'] = $productAttributes;
  658.             $this->twigData['product'] = $product;
  659.             $user $this->security->getUser();
  660.             $userInterfaceLang $user->getInterfaceLang();
  661.             $this->twigData['userInterfaceLang'] = $userInterfaceLang;
  662.             $html $this->renderForm("product/_combinationList.html.twig"$this->twigData);
  663.             return $this->json(['code' => 200'html' => $html->getContent()]);
  664.         }
  665.         return $this->json(['code' => 200]);
  666.     }
  667.     #[Route('/{id}/delete-combinations'name'_deleteCombinations')]
  668.     public function deleteCombinations(Product $productLangRepository $langRepositoryRequest $requestProductAttributeRepository $productAttributeRepositoryProductRepository $productRepositoryAttributeValueRepository $attributeValueRepositoryAttributeRepository $attributeRepository): Response
  669.     {
  670.         $productAttributes $product->getProductAttributes();
  671.         foreach ($productAttributes as $keyProductAttribute => $productAttribute) {
  672.             $productAttributeRepository->remove($productAttributefalse);
  673.         }
  674.         $this->em->flush();
  675.         $productAttributes $productAttributeRepository->findBy(['product' => $product]);
  676.         $this->twigData['productAttributes'] = $productAttributes;
  677.         $this->twigData['product'] = $product;
  678.         $user $this->security->getUser();
  679.         $userInterfaceLang $user->getInterfaceLang();
  680.         $this->twigData['userInterfaceLang'] = $userInterfaceLang;
  681.         $html $this->renderForm("product/_combinationList.html.twig"$this->twigData);
  682.         return $this->json(['code' => 200'html' => $html->getContent()]);
  683.     }
  684.     #[Route('/{id}/delete'name'_delete'methods: ['POST'])]
  685.     public function delete(Product $productLangRepository $langRepositoryImageRepository $imageRepositoryProductAttributeRepository $productAttributeRepositoryFileRepository $fileRepositoryRequest $requestProductRepository $productRepositoryProductLangRepository $productLangRepository): Response
  686.     {
  687.         if ($this->isCsrfTokenValid('delete' $product->getId(), $request->request->get('_token'))) {
  688.             $productLangs $productLangRepository->findBy(['product' => $product]);
  689.             foreach ($productLangs as $key => $productLang) {
  690.                 $productLangRepository->remove($productLang);
  691.             }
  692.             foreach ($product->getImage() as $keyProductImage => $productImage) {
  693.                 foreach ($productImage->getImageLangs() as $keyImageLang => $imageLang) {
  694.                     $productImage->removeImageLang($imageLang);
  695.                 }
  696.                 $product->removeImage($productImage);
  697.                 $pathToFile $this->getParameter('product_image_directory') . '/' $productImage->getName();
  698.                 if (file_exists($pathToFile)) {
  699.                     unlink($pathToFile);
  700.                 }
  701.                 $imageRepository->remove($productImage);
  702.             }
  703.             foreach ($product->getProductAttributes() as $keyProductAttributes => $productAttributes) {
  704.                 $product->removeProductAttribute($productAttributes);
  705.                 $productAttributeRepository->remove($productAttributes);
  706.             }
  707.             $productRepository->remove($producttrue);
  708.         }
  709.         return $this->redirectToRoute('app_product_index', [], Response::HTTP_SEE_OTHER);
  710.     }
  711.     #[Route('/delete'name'_delete_multiple'methods: ['POST'])]
  712.     public function deleteMultiple(Request $requestProductRepository $productRepositoryImageRepository $imageRepositoryProductAttributeRepository $productAttributeRepositoryProductLangRepository $productLangRepository): Response
  713.     {
  714.         $idsData $request->request->all();
  715.         $ids $idsData['itemDelete'];
  716.         foreach ($ids as $key => $id) {
  717.             $product $productRepository->find($id);
  718.             $productLangs $productLangRepository->findBy(['product' => $product]);
  719.             foreach ($productLangs as $key => $productLang) {
  720.                 $productLangRepository->remove($productLang);
  721.             }
  722.             foreach ($product->getImage() as $keyProductImage => $productImage) {
  723.                 foreach ($productImage->getImageLangs() as $keyImageLang => $imageLang) {
  724.                     $productImage->removeImageLang($imageLang);
  725.                 }
  726.                 $product->removeImage($productImage);
  727.                 $pathToFile $this->getParameter('product_image_directory') . '/' $productImage->getName();
  728.                 if (file_exists($pathToFile)) {
  729.                     unlink($pathToFile);
  730.                 }
  731.                 $imageRepository->remove($productImage);
  732.             }
  733.             foreach ($product->getProductAttributes() as $keyProductAttributes => $productAttributes) {
  734.                 $product->removeProductAttribute($productAttributes);
  735.                 $productAttributeRepository->remove($productAttributes);
  736.             }
  737.             $productRepository->remove($producttrue);
  738.         }
  739.         //return json
  740.         return $this->json(['code' => 200]);
  741.     }
  742.     #[Route('/{id}/add/image'name'_add_image'methods: ['POST'])]
  743.     public function addImage(Product $productLangRepository $langRepositoryRequest $requestSluggerInterface $sluggerImageRepository $imageRepositoryImageLangRepository $imageLangRepository): Response
  744.     {
  745.         $file $request->files->get('file');
  746.         if ($file) {
  747.             $originalFilename pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  748.             // this is needed to safely include the file name as part of the URL
  749.             $safeFilename $slugger->slug($originalFilename);
  750.             $newFilename $safeFilename '-' uniqid() . '.' $file->guessExtension();
  751.             $productImage $product->getImage();
  752.             $imagePosition count($productImage) + 1;
  753.             $langs $langRepository->findAll();
  754.             // Move the file to the directory where product image are stored
  755.             try {
  756.                 $file->move(
  757.                     $this->getParameter('product_image_directory'),
  758.                     $newFilename
  759.                 );
  760.                 $image = new Image();
  761.                 $image->setName($newFilename);
  762.                 $image->setProduct($product);
  763.                 $image->setPosition($imagePosition);
  764.                 if (count($productImage) == 0) {
  765.                     $image->setCover(true);
  766.                 } else {
  767.                     $image->setCover(false);
  768.                 }
  769.                 foreach ($langs as $key => $lang) {
  770.                     $imageLang = new ImageLang();
  771.                     $imageLang->setLang($lang);
  772.                     $imageLang->setImage($image);
  773.                     $imageLang->setDescription('');
  774.                     $image->addImageLang($imageLang);
  775.                     $imageLangRepository->add($imageLangfalse);
  776.                 }
  777.                 $imageRepository->add($imagetrue);
  778.                 return $this->json(['code' => 200'message' => "Image added"]);
  779.             } catch (FileException $e) {
  780.                 return new Response($eResponse::HTTP_INTERNAL_SERVER_ERROR);
  781.             }
  782.         }
  783.     }
  784.     #[Route('/{id}/image/html'name'_html_image'methods: ['POST'])]
  785.     public function getHtmlImage(Product $product): Response
  786.     {
  787.         $this->twigData['product'] = $product;
  788.         $html $this->renderForm("product/_imageList.html.twig"$this->twigData);
  789.         return $this->json(['code' => 200'html' => $html->getContent()]);
  790.     }
  791.     #[Route('/{id}/save/image'name'_save_image'methods: ['POST'])]
  792.     public function saveImage(Product $productRequest $requestLangRepository $langRepositorySluggerInterface $sluggerImageLangRepository $imageLangRepositoryImageRepository $imageRepository): Response
  793.     {
  794.         $formData $request->request->all();
  795.         $descriptions $formData['description'];
  796.         $descriptions json_decode($descriptionstrue);
  797.         $imageId $request->request->get("imageId");
  798.         $image $imageRepository->find($imageId);
  799.         foreach ($descriptions as $langId => $description) {
  800.             $lang $langRepository->find($langId);
  801.             $imageLang $imageLangRepository->findOneBy(['image' => $image'lang' => $lang]);
  802.             if ($imageLang) {
  803.                 $imageLang->setDescription($description);
  804.                 $imageLangRepository->add($imageLangfalse);
  805.             } else {
  806.                 $imageLang = new ImageLang();
  807.                 $imageLang->setLang($lang);
  808.                 $imageLang->setImage($image);
  809.                 $imageLang->setDescription($description);
  810.                 $image->addImageLang($imageLang);
  811.                 $imageLangRepository->add($imageLangfalse);
  812.             }
  813.         }
  814.         $image->setCover($request->request->get('cover'));
  815.         $imageRepository->add($imagetrue);
  816.         $imagesCover $imageRepository->findBy(['cover' => 1"product" => $product]);
  817.         if (count($imagesCover) > 0) {
  818.             if ($image->isCover()) {
  819.                 foreach ($imagesCover as $key => $imageCover) {
  820.                     if ($imageCover->getId() != $image->getId()) {
  821.                         $imageCover->setCover(0);
  822.                         $imageRepository->add($imageCovertrue);
  823.                     }
  824.                 }
  825.             }
  826.         } else {
  827.             $firstImages $imageRepository->findOneBy(["product" => $product]);
  828.             $firstImages->setCover(1);
  829.             $imageRepository->add($firstImagestrue);
  830.         }
  831.         $this->twigData['product'] = $product;
  832.         $html $this->renderForm("product/_imageList.html.twig"$this->twigData);
  833.         return $this->json(['code' => 200'html' => $html->getContent()]);
  834.     }
  835.     #[Route('/{id}/remove/image'name'_remove_image'methods: ['POST'])]
  836.     public function removeImage(Product $productRequest $requestImageLangRepository $imageLangRepositorySluggerInterface $sluggerImageRepository $imageRepositoryLoggerInterface $logger): Response
  837.     {
  838.         $imageId $request->request->get("imageId");
  839.         $image $imageRepository->find($imageId);
  840.         $pathToFile $this->getParameter('product_image_directory') . '/' $image->getName();
  841.         if (file_exists($pathToFile)) {
  842.             unlink($pathToFile);
  843.         }
  844.         //delete all image lang
  845.         $imageLangs $image->getImageLangs();
  846.         foreach ($imageLangs as $key => $imageLang) {
  847.             $imageLangRepository->remove($imageLang);
  848.             $image->removeImageLang($imageLang);
  849.         }
  850.         $imageRepository->remove($imagetrue);
  851.         $imagesCover $imageRepository->findBy(['cover' => 1"product" => $product]);
  852.         if (count($imagesCover) == 0) {
  853.             $firstImages $imageRepository->findOneBy(["product" => $product]);
  854.             if ($firstImages->getName() != "") {
  855.                 $firstImages->setCover(1);
  856.                 $imageRepository->add($firstImagestrue);
  857.             }
  858.         }
  859.         $this->twigData['product'] = $product;
  860.         $html $this->renderForm("product/_imageList.html.twig"$this->twigData);
  861.         return $this->json(['code' => 200'html' => $html->getContent()]);
  862.     }
  863.     #[Route('/{id}/file/html'name'_html_file'methods: ['POST'])]
  864.     public function getHtmlFile(Product $product): Response
  865.     {
  866.         $this->twigData['product'] = $product;
  867.         $html $this->renderForm("product/_fileList.html.twig"$this->twigData);
  868.         return $this->json(['code' => 200'html' => $html->getContent()]);
  869.     }
  870.     #[Route('/generate-feature'name'_generateFeature')]
  871.     public function generateFeature(Request $requestFeatureRepository $featureRepository): Response
  872.     {
  873.         $nbSelect $request->request->get("nbSelect");
  874.         $nbSelect += 1;
  875.         $user $this->security->getUser();
  876.         $userInterfaceLang $user->getInterfaceLang();
  877.         $this->twigData['userInterfaceLang'] = $userInterfaceLang;
  878.         $features $featureRepository->findAll();
  879.         $this->twigData['features'] = $features;
  880.         $this->twigData['nbSelect'] = $nbSelect;
  881.         $this->twigData['exist'] = false;
  882.         $html $this->renderForm("product/feature/_featureSelects.html.twig"$this->twigData);
  883.         return $this->json(['code' => 200'html' => $html->getContent()]);
  884.     }
  885.     #[Route('/sync'name'_sync')]
  886.     public function sync(LangRepository $langRepositoryProductRepository $productRepositoryImageRepository $imageRepositoryRequest $requestSiteRepository $siteRepositoryPSARepository $PSARepositoryProductLangRepository $productLangRepositoryProductAttributeRepository $productAttributeRepositoryFileRepository $fileRepository): Response
  887.     {
  888.         $debug false;
  889.         $queryBody $request->request->all();
  890.         $selectSyncSites $queryBody['selectSyncSite'];
  891.         $productId $queryBody['productId'];
  892.         $sites = [];
  893.         foreach ($selectSyncSites as $key => $selectSyncSite) {
  894.             $site $siteRepository->find($selectSyncSite);
  895.             $sites[] = $site;
  896.         }
  897.         $product $productRepository->find($productId);
  898.         $errors = [];
  899.         foreach ($sites as $key => $site) {
  900.             $featureValues $product->getFeatureValues();
  901.             foreach ($featureValues as $keyFeatureValue => $featureValue) {
  902.                 $feature $featureValue->getFeature();
  903.                 $featureLangs $feature->getFeatureLang();
  904.                 $featureLang null;
  905.                 foreach ($featureLangs as $keyFeatureLang => $featureLang) {
  906.                     if ($featureLang->getLang() == $this->security->getUser()->getInterfaceLang()) {
  907.                         $featureLang $featureLang;
  908.                         break;
  909.                     }
  910.                 }
  911.                 $featureValueLangs $featureValue->getFeatureValueLangs();
  912.                 foreach ($featureValueLangs as $keyFeatureValueLang => $featureValueLang) {
  913.                     if ($featureValueLang->getLang() == $this->security->getUser()->getInterfaceLang()) {
  914.                         $featureValueLang $featureValueLang;
  915.                         break;
  916.                     }
  917.                 }
  918.                 $PSAFeature $PSARepository->findOneBy(['entityName' => "feature"'entityId' => $feature->getId(), 'siteId' => $site->getId()]);
  919.                 if ($PSAFeature == null) {
  920.                     $error $this->translator->trans('page.product.error.FeatureNotSync', ['%feature%' => $featureLang->getName()]);
  921.                     if (in_array($error$errors) == false) {
  922.                         $errors[] = $error;
  923.                     }
  924.                 }
  925.                 $PSAFeatureValue $PSARepository->findOneBy(['entityName' => "featureValue"'entityId' => $featureValue->getId(), 'siteId' => $site->getId()]);
  926.                 if ($PSAFeatureValue == null) {
  927.                     $error $this->translator->trans('page.product.error.FeatureValueNotSync', ['%featureValue%' => $featureValueLang->getValue(), '%feature%' => $featureLang->getName()]);
  928.                     if (in_array($error$errors) == false) {
  929.                         $errors[] = $error;
  930.                     }
  931.                 }
  932.             }
  933.             $productAttributes $product->getProductAttributes();
  934.             foreach ($productAttributes as $keyAttribute => $productAttribute) {
  935.                 $attributeValues $productAttribute->getAttributeValues();
  936.                 foreach ($attributeValues as $keyAttributeValue => $attributeValue) {
  937.                     $attribute $attributeValue->getAttribute();
  938.                     $attributeLangs $attribute->getAttributeLang();
  939.                     $attributeLang null;
  940.                     foreach ($attributeLangs as $keyAttributeLang => $attributeLang) {
  941.                         if ($attributeLang->getLang() == $this->security->getUser()->getInterfaceLang()) {
  942.                             $attributeLang $attributeLang;
  943.                             break;
  944.                         }
  945.                     }
  946.                     $attributeValueLangs $attributeValue->getAttributeValueLangs();
  947.                     foreach ($attributeValueLangs as $keyAttributeValueLang => $attributeValueLang) {
  948.                         if ($attributeValueLang->getLang() == $this->security->getUser()->getInterfaceLang()) {
  949.                             $attributeValueLang $attributeValueLang;
  950.                             break;
  951.                         }
  952.                     }
  953.                     $PSAAttribute $PSARepository->findOneBy(['entityName' => "attribute"'entityId' => $attribute->getId(), 'siteId' => $site->getId()]);
  954.                     if ($PSAAttribute == null) {
  955.                         $error $this->translator->trans('page.product.error.AttributeNotSync', ['%attribute%' => $attributeLang->getName()]);
  956.                         if (in_array($error$errors) == false) {
  957.                             $errors[] = $error;
  958.                         }
  959.                     }
  960.                     $PSAAttributeValue $PSARepository->findOneBy(['entityName' => "attributeValue"'entityId' => $attributeValue->getId(), 'siteId' => $site->getId()]);
  961.                     if ($PSAAttributeValue == null) {
  962.                         $error $this->translator->trans('page.product.error.AttributeValueNotSync', ['%attributeValue%' => $attributeValueLang->getValue(), '%attribute%' => $attributeLang->getName()]);
  963.                         if (in_array($error$errors) == false) {
  964.                             $errors[] = $error;
  965.                         }
  966.                     }
  967.                 }
  968.             }
  969.             $productFiles $product->getFiles();
  970.             foreach ($productFiles as $keyProductFile => $productFile) {
  971.                 $PSAFile $PSARepository->findOneBy(['entityName' => "attachments"'entityId' => $productFile->getId(), 'siteId' => $site->getId()]);
  972.                 if ($PSAFile == null) {
  973.                     $error $this->translator->trans('page.product.error.AttachmentsNoSync', ['%attachment%' => $productFile->getDisplayName()]);
  974.                     if (in_array($error$errors) == false) {
  975.                         $errors[] = $error;
  976.                     }
  977.                 }
  978.             }
  979.         }
  980.         if (!empty($errors)) {
  981.             foreach ($errors as $keyError => $error) {
  982.                 $this->addFlash('error', ['from' => $this->translator->trans('page.product.Sync'), 'message' => $error]);
  983.             }
  984.             return $this->redirectToRoute('app_product_index');
  985.             die;
  986.         }
  987.         foreach ($sites as $key => $site) {
  988.             $url $site->getUrl();
  989.             $apiKey $site->getApiKey();
  990.             $webService = new PrestaShopWebservice($url$apiKey$debug);
  991.             $webServiceUtils = new WebserviceUtils($webService$url);
  992.             $langs $webServiceUtils->getSiteLangs();
  993.             $psaProduct $PSARepository->findOneBy(['entityId' => $product->getId(), "entityName" => 'product'"siteId" => $site->getId()]);
  994.             // dump($product->getId(), $langs, $psaProduct, $site);
  995.             if ($psaProduct != null) {
  996.                 // product modification
  997.                 try {
  998.                     $xmlResponseProduct $webService->get(['resource' => 'products''id' => $psaProduct->getInSiteId()]);
  999.                     $productXML $xmlResponseProduct->product[0];
  1000.                     unset($productXML->manufacturer_name);
  1001.                     unset($productXML->quantity);
  1002.                     unset($productXML->associations->product_features);
  1003.                     $product_features_xml $productXML->associations->addChild('product_features');
  1004.                     foreach ($product->getFeatureValues() as $keyFeatureValue => $featureValue) {
  1005.                         $PSAFeature $PSARepository->findOneBy(['entityName' => 'feature''entityId' => $featureValue->getFeature()->getId(), 'siteId' => $site->getId()]);
  1006.                         $PSAFeatureValue $PSARepository->findOneBy(['entityName' => 'featureValue''entityId' => $featureValue->getId(), 'siteId' => $site->getId()]);
  1007.                         $product_feature_xml $product_features_xml->addChild("product_feature");
  1008.                         $product_feature_xml->addChild('id'$PSAFeature->getInSiteId());
  1009.                         $product_feature_xml->addChild('id_feature_value'$PSAFeatureValue->getInSiteId());
  1010.                     }
  1011.                     unset($productXML->position_in_category);
  1012.                     foreach ($langs as $keyLang => $lang) {
  1013.                         if ($lang['icu'] == "en") {
  1014.                             $lang['icu'] = "gb";
  1015.                         }
  1016.                         $langPimdam $langRepository->findOneBy(['ICU' => $lang['icu']]);
  1017.                         $productLang $productLangRepository->findOneBy(['lang' => $langPimdam'product' => $product->getId()]);
  1018.                         $productXML->name->language[$keyLang] = $productLang->getName();
  1019.                         $productXML->description->language[$keyLang] = $product->editorToHTML($productLang->getDescription());
  1020.                         $productXML->description_short->language[$keyLang] = $product->editorToHTML($productLang->getShortDescription());
  1021.                         $productXML->subtitle->language[$keyLang] = $productLang->getSubtitle();
  1022.                     }
  1023.                     // dd($productXML);
  1024.                     $productXML->reference $product->getReference();
  1025.                     $productXML->price $productXML->price;
  1026.                     unset($productXML->associations->combinations);
  1027.                     unset($productXML->associations->attachments);
  1028.                     $attachments_xml $productXML->associations->addChild('attachments');
  1029.                     foreach ($product->getFiles() as $keyProductFile => $productFile) {
  1030.                         //create attachment xml
  1031.                         $attachment_xml $attachments_xml->addChild('attachment');
  1032.                         $PSAFile $PSARepository->findOneBy(['entityName' => 'attachments''entityId' => $productFile->getId(), 'siteId' => $site->getId()]);
  1033.                         $attachment_xml->addChild('id'$PSAFile->getInSiteId());
  1034.                     }
  1035.                     
  1036.                     $optProduct = ['resource' => 'products'];
  1037.                     $optProduct['putXml'] = $xmlResponseProduct->asXML();
  1038.                     $optProduct['id'] = (int) $productXML->id;
  1039.                     // dd($optProduct);
  1040.                     $returnProduct $webService->edit($optProduct);
  1041.                     // dd($webService->edit($optProduct));
  1042.                     $psaProductFields $PSARepository->findOneBy(['entityId' => $product->getId(), "entityName" => 'productfields'"siteId" => $site->getId()]);
  1043.                     // dd($psaProductFields);
  1044.                     // dd($psaProduct, $psaProductFields);
  1045.                     // $xmlResponseNewFields = $webService->get(['url' => $url . '/api/productfields?filter[id_product]=[' . (int) $productXML->id . ']']);
  1046.                     // $xmlResponseNewFieldsId = (int)$xmlResponseNewFields->productfields->productfield[0]['id'];
  1047.                     if ($psaProductFields == null) {
  1048.                         $productFieldsXML $webService->get(['url' => $url '/api/productfields/']);
  1049.                         foreach ($productFieldsXML->productfields->productfield as $keyProductField => $productField) {
  1050.                             $xmlResponseNewFields $webService->get(['url' => $url '/api/productfields/' $productField['id']]);
  1051.                             if ($xmlResponseNewFields->productfield[0]->id_product == $psaProduct->getInSiteId()) break;
  1052.                         }
  1053.                     } else {
  1054.                         $xmlResponseNewFields $webService->get(['url' => $url '/api/productfields/' $psaProductFields->getInSiteId()]);
  1055.                     }
  1056.                     
  1057.                     $newFieldsXml $xmlResponseNewFields->productfield[0];
  1058.                     $newFieldsId = (int)$newFieldsXml->id;
  1059.                     // dd($newFieldsId);
  1060.                     foreach ($langs as $keyLang => $lang) {
  1061.                         if ($lang['icu'] == "en") {
  1062.                             $lang['icu'] = "gb";
  1063.                         }
  1064.                         $langPimdam $langRepository->findOneBy(['ICU' => $lang['icu']]);
  1065.                         $productLang $productLangRepository->findOneBy(['lang' => $langPimdam'product' => $product->getId()]);
  1066.                         $newFieldsXml->subtitle->language[$keyLang] = $productLang->getSubtitle();
  1067.                         $newFieldsXml->reglementation->language[$keyLang] = $product->editorToHTML($productLang->getRegulations());
  1068.                         $newFieldsXml->conseils->language[$keyLang] = $product->editorToHTML($productLang->getAdvices());
  1069.                     }
  1070.                     // dd($newFieldsXml);
  1071.                     $optNewFields['resource'] = 'productfields';
  1072.                     $optNewFields['putXml'] = $xmlResponseNewFields->asXML();
  1073.                     $optNewFields['id'] = $newFieldsId;
  1074.                     $returnNewFields $webService->edit($optNewFields);
  1075.                     $returnNewFieldsId = (int)$returnNewFields->productfield->id;
  1076.                     // dd($returnNewFields);
  1077.                     if (!$psaProductFields) {
  1078.                         $psaProductFields = new PSA();
  1079.                         $psaProductFields->setEntityId($product->getId());
  1080.                         $psaProductFields->setEntityName('productfields');
  1081.                         $psaProductFields->setInSiteId($returnNewFieldsId);
  1082.                         $psaProductFields->setSiteId($site->getId());
  1083.                         $PSARepository->add($psaProductFieldstrue);
  1084.                     }
  1085.                 } catch (\PrestaShopWebserviceException $th) {
  1086.                     dd($th);
  1087.                     if ($th->getMessage() == "This call to PrestaShop Web Services failed and returned an HTTP status of 404. That means: Not Found.") {
  1088.                         $PSARepository->Remove($psaProducttrue);
  1089.                         return $this->redirectToRoute('app_product_sync', ['id' => $product->getId()], Response::HTTP_SEE_OTHER);
  1090.                     } else {
  1091.                         $this->addFlash(
  1092.                             'error',
  1093.                             ['from' => $this->translator->trans('page.product.Products'), 'message' => $this->translator->trans('page.product.error.CantSync')]
  1094.                         );
  1095.                         return $this->redirectToRoute('app_product_index');
  1096.                     }
  1097.                 }
  1098.                 // Product is modified. Now, it's image time
  1099.                 $imageStatus "";
  1100.                 try {
  1101.                     $xmlImageResponse $webService->get(['resource' => 'images/products/' $psaProduct->getInSiteId()]);
  1102.                     $productImageXML $xmlImageResponse;
  1103.                     $productImage json_decode(json_encode((array)$productImageXML), true);
  1104.                 } catch (\Throwable $th) {
  1105.                     if ($th->getMessage() == "This call to PrestaShop Web Services failed and returned an HTTP status of 404. That means: Not Found.") {
  1106.                         $imageStatus "noImage";
  1107.                     }
  1108.                 }
  1109.                 if ($imageStatus == "") {
  1110.                     $imageStatus "images";
  1111.                 }
  1112.                 if ($imageStatus == "noImage") {
  1113.                     $productImages $imageRepository->findBy(['product' => $product->getId()]);
  1114.                     foreach ($productImages as $keyProductImage => $productImage) {
  1115.                         $psaProductImage $PSARepository->findOneBy(['entityName' => 'image''entityId' => $productImage->getId(), 'siteId' => $site->getId()]);
  1116.                         if ($psaProductImage != null) {
  1117.                             $PSARepository->Remove($psaProductImagetrue);
  1118.                         }
  1119.                     }
  1120.                     foreach ($product->getImage() as $keyImage => $image) {
  1121.                         $image_path $this->getParameter('product_image_directory') . '/' $image->getName();
  1122.                         $image_mime 'image/jpg';
  1123.                         $args['image'] = new CURLFile($image_path$image_mime);
  1124.                         $args['position'] = $image->getPosition();
  1125.                         $ch curl_init();
  1126.                         curl_setopt($chCURLOPT_HEADERtrue);
  1127.                         curl_setopt($chCURLINFO_HEADER_OUTtrue);
  1128.                         curl_setopt($chCURLOPT_URL$url "/api/images/products/" $psaProduct->getInSiteId());
  1129.                         curl_setopt($chCURLOPT_POSTtrue);
  1130.                         curl_setopt($chCURLOPT_USERPWD$apiKey);
  1131.                         curl_setopt($chCURLOPT_POSTFIELDS$args);
  1132.                         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  1133.                         $returnImage curl_exec($ch);
  1134.                         $header_size curl_getinfo($chCURLINFO_HEADER_SIZE);
  1135.                         $header substr($returnImage0$header_size);
  1136.                         $body substr($returnImage$header_size);
  1137.                         $returnImage $body;
  1138.                         $returnImage simplexml_load_string($returnImage);
  1139.                         $curlinfo curl_getinfo($ch);
  1140.                         curl_close($ch);
  1141.                         $returnImageId intval($returnImage->image[0]->id);
  1142.                         $psaImage = new PSA();
  1143.                         $psaImage->setEntityId($image->getId());
  1144.                         $psaImage->setEntityName('image');
  1145.                         $psaImage->setInSiteId($returnImageId);
  1146.                         $psaImage->setSiteId($site->getId());
  1147.                         $PSARepository->add($psaImagetrue);
  1148.                     }
  1149.                 }
  1150.                 if ($imageStatus == "images") {
  1151.                     $productImagesId = [];
  1152.                     foreach ($product->getImage() as $keyProductImage => $image) {
  1153.                         $productImagesId[$image->getId()] = false;
  1154.                     }
  1155.                     foreach ($productImage['image']['declination'] as $keySiteImage => $siteImage) {
  1156.                         $siteImageId 0;
  1157.                         if (count($productImage['image']['declination']) > 1) {
  1158.                             $siteImageId $siteImage['@attributes'];
  1159.                         } else {
  1160.                             $siteImageId $siteImage;
  1161.                         }
  1162.                         $psaSiteImage $PSARepository->findOneBy(['inSiteId' => $siteImageId"entityName" => 'image'"siteId" => $site->getId()]);
  1163.                         if ($psaSiteImage != null) {
  1164.                             if (isset($productImagesId[$psaSiteImage->getEntityId()])) {
  1165.                                 $productImagesId[$psaSiteImage->getEntityId()] = true;
  1166.                             } else {
  1167.                                 $webService->delete(array('resource' => 'images/products/' $psaProduct->getInSiteId(), 'id' => $psaSiteImage->getInSiteId()));
  1168.                                 $PSARepository->remove($psaSiteImagetrue);
  1169.                             }
  1170.                         }
  1171.                     }
  1172.                     // check all product image and add if not exist
  1173.                     foreach ($productImagesId as $keyProductImageId => $productImageId) {
  1174.                         if ($productImageId == false) {
  1175.                             $image $imageRepository->findOneBy(['id' => $keyProductImageId]);
  1176.                             $image_path $this->getParameter('product_image_directory') . '/' $image->getName();
  1177.                             $image_mime 'image/jpg';
  1178.                             $args['image'] = new CURLFile($image_path$image_mime);
  1179.                             $args['position'] = $image->getPosition();
  1180.                             $ch curl_init();
  1181.                             curl_setopt($chCURLOPT_HEADERtrue);
  1182.                             curl_setopt($chCURLINFO_HEADER_OUTtrue);
  1183.                             curl_setopt($chCURLOPT_URL$url "/api/images/products/" $psaProduct->getInSiteId());
  1184.                             curl_setopt($chCURLOPT_POSTtrue);
  1185.                             curl_setopt($chCURLOPT_USERPWD$apiKey);
  1186.                             curl_setopt($chCURLOPT_POSTFIELDS$args);
  1187.                             curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  1188.                             $returnImage curl_exec($ch);
  1189.                             $header_size curl_getinfo($chCURLINFO_HEADER_SIZE);
  1190.                             $header substr($returnImage0$header_size);
  1191.                             $body substr($returnImage$header_size);
  1192.                             $returnImage $body;
  1193.                             $returnImage simplexml_load_string($returnImage);
  1194.                             $curlinfo curl_getinfo($ch);
  1195.                             curl_close($ch);
  1196.                             $returnImageId intval($returnImage->image[0]->id);
  1197.                             $psaImage = new PSA();
  1198.                             $psaImage->setEntityId($image->getId());
  1199.                             $psaImage->setEntityName('image');
  1200.                             $psaImage->setInSiteId($returnImageId);
  1201.                             $psaImage->setSiteId($site->getId());
  1202.                             $PSARepository->add($psaImagetrue);
  1203.                         }
  1204.                     }
  1205.                 }
  1206.                 foreach ($product->getImage() as $keyImage => $image) {
  1207.                     $psaSiteImage $PSARepository->findOneBy(['entityId' => $image->getId(), "entityName" => 'image'"siteId" => $site->getId()]);
  1208.                     if ($psaSiteImage == null) {
  1209.                         $image_path $this->getParameter('product_image_directory') . '/' $image->getName();
  1210.                         $image_mime 'image/jpg';
  1211.                         $args['image'] = new CURLFile($image_path$image_mime);
  1212.                         $ch curl_init();
  1213.                         curl_setopt($chCURLOPT_HEADERtrue);
  1214.                         curl_setopt($chCURLINFO_HEADER_OUTtrue);
  1215.                         curl_setopt($chCURLOPT_URL$url "/api/images/products/" $psaProduct->getInSiteId());
  1216.                         curl_setopt($chCURLOPT_POSTtrue);
  1217.                         curl_setopt($chCURLOPT_USERPWD$apiKey);
  1218.                         curl_setopt($chCURLOPT_POSTFIELDS$args);
  1219.                         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  1220.                         $returnImage curl_exec($ch);
  1221.                         $header_size curl_getinfo($chCURLINFO_HEADER_SIZE);
  1222.                         $header substr($returnImage0$header_size);
  1223.                         $body substr($returnImage$header_size);
  1224.                         $returnImage $body;
  1225.                         $returnImage simplexml_load_string($returnImage);
  1226.                         $curlinfo curl_getinfo($ch);
  1227.                         curl_close($ch);
  1228.                         $returnImageId intval($returnImage->image[0]->id);
  1229.                         $psaImage = new PSA();
  1230.                         $psaImage->setEntityId($image->getId());
  1231.                         $psaImage->setEntityName('image');
  1232.                         $psaImage->setInSiteId($returnImageId);
  1233.                         $psaImage->setSiteId($site->getId());
  1234.                         $PSARepository->add($psaImagetrue);
  1235.                     }
  1236.                 }
  1237.                 // Now, it's time to combinations
  1238.                 $productCombinations json_decode(json_encode((array)$productXML->associations->combinations), true);
  1239.                 if (isset($productCombinations['combination'])) {
  1240.                     foreach ($productCombinations['combination'] as $keyCombination => $combination) {
  1241.                         $combinationId 0;
  1242.                         if ($keyCombination == "id") {
  1243.                             $combinationId $combination;
  1244.                         } else {
  1245.                             $combinationId $combination['id'];
  1246.                         }
  1247.                         $psaCombination $PSARepository->findOneBy(['inSiteId' => $combinationId"entityName" => 'combination'"siteId" => $site->getId()]);
  1248.                         if ($psaCombination != null) {
  1249.                             $pimCombination $productAttributeRepository->findOneBy(['id' => $psaCombination->getEntityId(), 'product' => $psaProduct->getEntityId()]);
  1250.                             if ($pimCombination == null) {
  1251.                                 $webService->delete(array('resource' => 'combinations''id' => $psaCombination->getInSiteId()));
  1252.                                 $PSARepository->remove($psaCombinationtrue);
  1253.                             } else {
  1254.                                 $xmlResponseCombination $webService->get(['resource' => 'combinations''id' => $combinationId]);
  1255.                                 $combinationXML $xmlResponseCombination->combination[0];
  1256.                                 $combinationXML->reference $pimCombination->getReference();
  1257.                                 $combinationXML->ean13 $pimCombination->getEan13();
  1258.                                 unset($combinationXML->associations->images);
  1259.                                 $combinationXML->associations->addChild('images');
  1260.                                 foreach ($pimCombination->getImages() as $keyProductAttributeImage => $productAttributeImage) {
  1261.                                     $psaImage $PSARepository->findOneBy(['entityId' => $productAttributeImage->getId(), "entityName" => 'image'"siteId" => $site->getId()]);
  1262.                                     // $combinationXML->associations->product_option_values->product_option_value->id = $psaAttributeValue->getInSiteId();
  1263.                                     $product_option_image $combinationXML->associations->images->addChild("image");
  1264.                                     $product_option_image->addChild('id'$psaImage->getInSiteId());
  1265.                                 }
  1266.                                 $webService->edit(array('resource' => 'combinations''id' => $psaCombination->getInSiteId(), 'putXml' => $xmlResponseCombination->asXML()));
  1267.                             }
  1268.                         }
  1269.                     }
  1270.                 }
  1271.                 $xmlResponseCombination $webService->get(['url' => $url '/api/combinations?schema=blank']);
  1272.                 foreach ($product->getProductAttributes() as $keyProductAttributes => $productAttribute) {
  1273.                     $psaCombination $PSARepository->findOneBy(['entityId' => $productAttribute->getId(), "entityName" => 'combination'"siteId" => $site->getId()]);
  1274.                     if ($psaCombination == null) {
  1275.                         $combinationXML $xmlResponseCombination->combination[0];
  1276.                         $combinationXML->id_product $psaProduct->getInSiteId();
  1277.                         $combinationXML->quantity 0;
  1278.                         $combinationXML->minimal_quantity 1;
  1279.                         $combinationXML->reference $productAttribute->getReference();
  1280.                         $combinationXML->ean13 $productAttribute->getEan13();
  1281.                         $combinationXML->price 0;
  1282.                         unset($combinationXML->associations->product_option_values->product_option_value);
  1283.                         foreach ($productAttribute->getAttributeValues() as $keyProductAttributeValue => $productAttributeValue) {
  1284.                             $psaAttributeValue $PSARepository->findOneBy(['entityId' => $productAttributeValue->getId(), "entityName" => 'attributeValue'"siteId" => $site->getId()]);
  1285.                             // $combinationXML->associations->product_option_values->product_option_value->id = $psaAttributeValue->getInSiteId();
  1286.                             $product_option_value $combinationXML->associations->product_option_values->addChild("product_option_value");
  1287.                             $product_option_value->addChild('id'$psaAttributeValue->getInSiteId());
  1288.                         }
  1289.                         foreach ($productAttribute->getImages() as $keyProductAttributeImage => $productAttributeImage) {
  1290.                             $psaImage $PSARepository->findOneBy(['entityId' => $productAttributeImage->getId(), "entityName" => 'image'"siteId" => $site->getId()]);
  1291.                             // $combinationXML->associations->product_option_values->product_option_value->id = $psaAttributeValue->getInSiteId();
  1292.                             $product_option_image $combinationXML->associations->images->addChild("image");
  1293.                             $product_option_image->addChild('id'$psaImage->getInSiteId());
  1294.                         }
  1295.                         $optCombination = ['resource' => 'combinations'];
  1296.                         $optCombination['postXml'] = $xmlResponseCombination->asXML();
  1297.                         $returnCombination $webService->add($optCombination);
  1298.                         $returnCombinationId intval($returnCombination->combination[0]->id);
  1299.                         $psaCombination = new PSA();
  1300.                         $psaCombination->setEntityId($productAttribute->getId());
  1301.                         $psaCombination->setEntityName('combination');
  1302.                         $psaCombination->setInSiteId($returnCombinationId);
  1303.                         $psaCombination->setSiteId($site->getId());
  1304.                         $PSARepository->add($psaCombinationtrue);
  1305.                     }
  1306.                 }
  1307.                 // Now, it's time to file
  1308.                 // unset($productXML->associations->attachments);
  1309.                 // $attachments_xml = $productXML->associations->addChild('attachments');
  1310.                 // foreach ($product->getFiles() as $keyProductFile => $productFile) {
  1311.                 //     //create attachment xml
  1312.                 //     $attachment_xml = $attachments_xml->addChild('attachment');
  1313.                 //     $PSAFile = $PSARepository->findOneBy(['entityName' => 'attachments', 'entityId' => $productFile->getId(), 'siteId' => $site->getId()]);
  1314.                 //     $attachment_xml->addChild('id', $PSAFile->getInSiteId());
  1315.                 // }
  1316.                 // $optProduct = ['resource' => 'products'];
  1317.                 // $optProduct['putXml'] = $xmlResponseProduct->asXML();
  1318.                 // $optProduct['id'] = (int) $productXML->id;
  1319.                 // $returnProduct = $webService->edit($optProduct);
  1320.             } else {
  1321.                 $existInSite false;
  1322.                 $xmlResponse $webService->get(['url' => $url '/api/products?filter[reference]=[' $product->getReference() . ']']);
  1323.                 if (isset($xmlResponse->products->product[0]['id'])) {
  1324.                     $xmlResponseProductId = (int)$xmlResponse->products->product[0]['id'];
  1325.                 } else {
  1326.                     $xmlResponseProductId null;
  1327.                 }
  1328.                 if ($xmlResponseProductId != null) {
  1329.                     $xmlResponse $webService->get(['url' => $url '/api/products/' $xmlResponseProductId]);
  1330.                     $existInSite true;
  1331.                 } else {
  1332.                     $xmlResponse $webService->get(['url' => $url '/api/products?schema=blank']);
  1333.                 }
  1334.                 $productXML $xmlResponse->product[0];
  1335.                 if ($existInSite == false) {
  1336.                     $productXML->id_category_default 2;
  1337.                     $productXML->price 0;
  1338.                     $productXML->position_in_category 1;
  1339.                     $productXML->state 1;
  1340.                 } else {
  1341.                     //unset manufacturer_name
  1342.                     unset($productXML->manufacturer_name);
  1343.                     unset($productXML->quantity);
  1344.                     //get position and if < 0, set to 1
  1345.                     if ((int)$productXML->position_in_category 1) {
  1346.                         $productXML->position_in_category 1;
  1347.                     }
  1348.                 }
  1349.                 foreach ($langs as $keyLang => $lang) {
  1350.                     if ($lang['icu'] == "en") {
  1351.                         $lang['icu'] = "gb";
  1352.                     }
  1353.                     $langPimdam $langRepository->findOneBy(['ICU' => $lang['icu']]);
  1354.                     $productLang $productLangRepository->findOneBy(['lang' => $langPimdam'product' => $product->getId()]);
  1355.                     $productXML->name->language[$keyLang] = $productLang->getName();
  1356.                     $productXML->description->language[$keyLang] = $product->editorToHTML($productLang->getDescription());
  1357.                     $productXML->description_short->language[$keyLang] = $product->editorToHTML($productLang->getShortDescription());
  1358.                     $productXML->subtitle->language[$keyLang] = $productLang->getSubtitle();
  1359.                     $productXML->meta_title->language[$keyLang] = $productLang->getMetaTitle();
  1360.                     $productXML->meta_description->language[$keyLang] = $productLang->getMetaDescription();
  1361.                 }
  1362.                 $productXML->ean13 $product->getEan13();
  1363.                 $productXML->reference $product->getReference();
  1364.                 $productXML->isbn $product->getIsbn();
  1365.                 $productXML->mpn $product->getMpn();
  1366.                 $productXML->width $product->getWidth();
  1367.                 $productXML->height $product->getHeight();
  1368.                 $productXML->depth $product->getDepth();
  1369.                 $productXML->weight $product->getWeight();
  1370.                 unset($productXML->associations->product_features);
  1371.                 $product_features_xml $productXML->associations->addChild('product_features');
  1372.                 foreach ($product->getFeatureValues() as $keyFeatureValue => $featureValue) {
  1373.                     $PSAFeature $PSARepository->findOneBy(['entityName' => 'feature''entityId' => $featureValue->getFeature()->getId(), 'siteId' => $site->getId()]);
  1374.                     $PSAFeatureValue $PSARepository->findOneBy(['entityName' => 'featureValue''entityId' => $featureValue->getId(), 'siteId' => $site->getId()]);
  1375.                     $product_feature_xml $product_features_xml->addChild("product_feature");
  1376.                     $product_feature_xml->addChild('id'$PSAFeature->getInSiteId());
  1377.                     $product_feature_xml->addChild('id_feature_value'$PSAFeatureValue->getInSiteId());
  1378.                 }
  1379.                 $opt = ['resource' => 'products'];
  1380.                 if ($existInSite == false) {
  1381.                     $productXML->associations->categories->category 2;
  1382.                     $opt['postXml'] = $xmlResponse->asXML();
  1383.                     $returnProduct $webService->add($opt);
  1384.                 } else {
  1385.                     $opt['putXml'] = $xmlResponse->asXML();
  1386.                     $opt['id'] = $xmlResponseProductId;
  1387.                     $returnProduct $webService->edit($opt);
  1388.                 }
  1389.                 $returnedId =  intval($returnProduct->product[0]->id);
  1390.                 $psaProduct = new PSA();
  1391.                 $psaProduct->setEntityId($product->getId());
  1392.                 $psaProduct->setEntityName('product');
  1393.                 $psaProduct->setInSiteId($returnedId);
  1394.                 $psaProduct->setSiteId($site->getId());
  1395.                 $PSARepository->add($psaProducttrue);
  1396.                 foreach ($product->getImage() as $keyImage => $image) {
  1397.                     $image_path $this->getParameter('product_image_directory') . '/' $image->getName();
  1398.                     $image_mime 'image/jpg';
  1399.                     $args['image'] = new CURLFile($image_path$image_mime);
  1400.                     $args['position'] = $image->getPosition();
  1401.                     $ch curl_init();
  1402.                     curl_setopt($chCURLOPT_HEADERtrue);
  1403.                     curl_setopt($chCURLINFO_HEADER_OUTtrue);
  1404.                     curl_setopt($chCURLOPT_URL$url "/api/images/products/" $returnedId);
  1405.                     curl_setopt($chCURLOPT_POSTtrue);
  1406.                     curl_setopt($chCURLOPT_USERPWD$apiKey);
  1407.                     curl_setopt($chCURLOPT_POSTFIELDS$args);
  1408.                     curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  1409.                     $returnImage curl_exec($ch);
  1410.                     $header_size curl_getinfo($chCURLINFO_HEADER_SIZE);
  1411.                     $header substr($returnImage0$header_size);
  1412.                     $body substr($returnImage$header_size);
  1413.                     $returnImage $body;
  1414.                     $returnImage simplexml_load_string($returnImage);
  1415.                     $curlinfo curl_getinfo($ch);
  1416.                     curl_close($ch);
  1417.                     $returnImageId intval($returnImage->image[0]->id);
  1418.                     $psaImage = new PSA();
  1419.                     $psaImage->setEntityId($image->getId());
  1420.                     $psaImage->setEntityName('image');
  1421.                     $psaImage->setInSiteId($returnImageId);
  1422.                     $psaImage->setSiteId($site->getId());
  1423.                     $PSARepository->add($psaImagetrue);
  1424.                 }
  1425.                 $xmlResponse $webService->get(['url' => $url '/api/combinations?schema=blank']);
  1426.                 foreach ($product->getProductAttributes() as $keyProductAttributes => $productAttribute) {
  1427.                     $combinationXML $xmlResponse->combination[0];
  1428.                     $combinationXML->id_product $psaProduct->getInSiteId();
  1429.                     $combinationXML->quantity 0;
  1430.                     $combinationXML->minimal_quantity 1;
  1431.                     $combinationXML->reference $productAttribute->getReference();
  1432.                     $combinationXML->ean13 $productAttribute->getEan13();
  1433.                     $combinationXML->price 0;
  1434.                     unset($combinationXML->associations->product_option_values->product_option_value);
  1435.                     foreach ($productAttribute->getAttributeValues() as $key => $productAttributeValue) {
  1436.                         $psaAttributeValue $PSARepository->findOneBy(['entityId' => $productAttributeValue->getId(), "entityName" => 'attributeValue'"siteId" => $site->getId()]);
  1437.                         // $combinationXML->associations->product_option_values->product_option_value->id = $psaAttributeValue->getInSiteId();
  1438.                         $product_option_value $combinationXML->associations->product_option_values->addChild("product_option_value");
  1439.                         $product_option_value->addChild('id'$psaAttributeValue->getInSiteId());
  1440.                     }
  1441.                     $opt = ['resource' => 'combinations'];
  1442.                     $opt['postXml'] = $xmlResponse->asXML();
  1443.                     try {
  1444.                         $returnCombination $webService->add($opt);
  1445.                     } catch (PrestaShopWebserviceException $th) {
  1446.                         dd($th);
  1447.                     }
  1448.                     $returnCombinationId intval($returnCombination->combination[0]->id);
  1449.                     $psaCombination = new PSA();
  1450.                     $psaCombination->setEntityId($productAttribute->getId());
  1451.                     $psaCombination->setEntityName('combination');
  1452.                     $psaCombination->setInSiteId($returnCombinationId);
  1453.                     $psaCombination->setSiteId($site->getId());
  1454.                     $PSARepository->add($psaCombinationtrue);
  1455.                 }
  1456.                 foreach ($product->getFiles() as $keyProductFile => $file) {
  1457.                     $psaAttachment $PSARepository->findOneBy(['entityId' => $file->getId(), "entityName" => 'attachments'"siteId" => $site->getId()]);
  1458.                     $file_path $this->getParameter('file_directory') . '/' $file->getName();
  1459.                     $file_mime mime_content_type($file_path);
  1460.                     $args['file'] = new CURLFile($file_path$file_mime$file->getDisplayName());
  1461.                     $ch curl_init();
  1462.                     curl_setopt($chCURLOPT_HEADERtrue);
  1463.                     curl_setopt($chCURLINFO_HEADER_OUTtrue);
  1464.                     curl_setopt($chCURLOPT_URL$url "/api/attachments/file/");
  1465.                     curl_setopt($chCURLOPT_POSTtrue);
  1466.                     curl_setopt($chCURLOPT_USERPWD$apiKey);
  1467.                     curl_setopt($chCURLOPT_POSTFIELDS$args);
  1468.                     curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  1469.                     $returnFile curl_exec($ch);
  1470.                     $header_size curl_getinfo($chCURLINFO_HEADER_SIZE);
  1471.                     $body substr($returnFile$header_size);
  1472.                     $curlinfo curl_getinfo($ch);
  1473.                     curl_close($ch);
  1474.                     $returnFile $body;
  1475.                     $returnFile simplexml_load_string($returnFile);
  1476.                     $returnFileId intval($returnFile->attachment[0]->id);
  1477.                     $xmlAttachmentsResponse $webService->get(['url' => $url '/api/attachments/' $returnFileId]);
  1478.                     $attachmentsXml $xmlAttachmentsResponse->attachment[0];
  1479.                     foreach ($langs as $keyLang => $lang) {
  1480.                         if ($lang['icu'] == "en") {
  1481.                             $lang['icu'] = "gb";
  1482.                         }
  1483.                         $attachmentsXml->description->language[$keyLang] = $file->getDescription();
  1484.                     }
  1485.                     $attachmentsXml->associations->products->product->id $psaProduct->getInSiteId();
  1486.                     $optAttachment = ['resource' => 'attachments'];
  1487.                     $optAttachment['putXml'] = $xmlAttachmentsResponse->asXML();
  1488.                     $optAttachment['id'] = $returnFileId;
  1489.                     try {
  1490.                         $returnAttachment $webService->edit($optAttachment);
  1491.                     } catch (PrestaShopWebserviceException $th) {
  1492.                         dd($th);
  1493.                     }
  1494.                     $returnAttachmentId $returnFileId;
  1495.                     $psaAttachment = new PSA();
  1496.                     $psaAttachment->setEntityId($file->getId());
  1497.                     $psaAttachment->setEntityName('attachments');
  1498.                     $psaAttachment->setInSiteId($returnAttachmentId);
  1499.                     $psaAttachment->setSiteId($site->getId());
  1500.                     $PSARepository->add($psaAttachmenttrue);
  1501.                 }
  1502.                 $xmlResponseNewFields $webService->get(['url' => $url '/api/productfields?filter[id_product]=[' $returnedId ']']);
  1503.                 $xmlResponseNewFieldsId = (int)$xmlResponseNewFields->productfields->productfield[0]['id'];
  1504.                 $xmlResponseNewFields $webService->get(['url' => $url '/api/productfields/' $xmlResponseNewFieldsId]);
  1505.                 $newFieldsXml $xmlResponseNewFields->productfield[0];
  1506.                 foreach ($langs as $keyLang => $lang) {
  1507.                     if ($lang['icu'] == "en") {
  1508.                         $lang['icu'] = "gb";
  1509.                     }
  1510.                     $langPimdam $langRepository->findOneBy(['ICU' => $lang['icu']]);
  1511.                     $productLang $productLangRepository->findOneBy(['lang' => $langPimdam'product' => $product->getId()]);
  1512.                     $newFieldsXml->subtitle->language[$keyLang] = $productLang->getSubtitle();
  1513.                     $newFieldsXml->reglementation->language[$keyLang] = $product->editorToHTML($productLang->getRegulations());
  1514.                     $newFieldsXml->conseils->language[$keyLang] = $product->editorToHTML($productLang->getAdvices());
  1515.                 }
  1516.                 $optNewFields = ['resource' => 'productfields'];
  1517.                 $optNewFields['putXml'] = $xmlResponseNewFields->asXML();
  1518.                 $optNewFields['id'] = $xmlResponseNewFieldsId;
  1519.                 $returnNewFields $webService->edit($optNewFields);
  1520.                 $returnNewFieldsId intval($returnNewFields->productfield[0]->id);
  1521.                 $psaProductFields = new PSA();
  1522.                 $psaProductFields->setEntityId($product->getId());
  1523.                 $psaProductFields->setEntityName('productfields');
  1524.                 $psaProductFields->setInSiteId($returnNewFieldsId);
  1525.                 $psaProductFields->setSiteId($site->getId());
  1526.                 $PSARepository->add($psaProductFieldstrue);
  1527.             }
  1528.         }
  1529.         return $this->redirectToRoute('app_product_index');
  1530.     }
  1531.     private function xml2array($xmlObject$out = array())
  1532.     {
  1533.         foreach ((array) $xmlObject as $index => $node) {
  1534.             $out[$index] = (is_object($node)) ? $this->xml2array($node) : $node;
  1535.         }
  1536.         return $out;
  1537.     }
  1538. }