src/Controller/AttributeValueController.php line 333

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Attribute;
  4. use App\Entity\AttributeValue;
  5. use App\Entity\AttributeValueLang;
  6. use App\Entity\Image;
  7. use App\Entity\PSA;
  8. use App\Repository\AttributeLangRepository;
  9. use App\Repository\AttributeRepository;
  10. use App\Repository\AttributeValueLangRepository;
  11. use App\Repository\AttributeValueRepository;
  12. use App\Repository\ImageRepository;
  13. use App\Repository\LangRepository;
  14. use App\Repository\ProductAttributeRepository;
  15. use App\Repository\ERPCommercialDataRepository;
  16. use App\Repository\ERPCommercialDataLangRepository;
  17. use App\Repository\ERPLogisticDataRepository;
  18. use App\Repository\PSARepository;
  19. use App\Repository\SiteRepository;
  20. use App\Services\WebserviceUtils;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use PrestaShopWebservice;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  24. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  25. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\Response;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. use Symfony\Component\Security\Core\Security;
  30. use Symfony\Component\String\Slugger\SluggerInterface;
  31. use Symfony\Contracts\Translation\TranslatorInterface;
  32. #[Route('/attribute/{id}'name'app_attribute_value')]
  33. #[IsGranted('ROLE_CONTENT_MANAGER')]
  34. class AttributeValueController extends AbstractController
  35. {
  36.     public function __construct(Security $securityTranslatorInterface $translator)
  37.     {
  38.         $this->twigData = [];
  39.         $this->translator $translator;
  40.         $this->security $security;
  41.         $this->twigData = [
  42.             'user' => $security->getUser(),
  43.         ];
  44.     }
  45.     #[Route('/value'name'_index')]
  46.     public function index(Attribute $attributeAttributeLangRepository $attributeLangRepositoryLangRepository $langRepositoryAttributeValueRepository $attributeValueRepository): Response
  47.     {
  48.         $this->breadcrumb[0]['name'] = $this->translator->trans('page.catalog.Catalog');
  49.         $this->breadcrumb[0]['link'] = $this->generateUrl('app_catalog_index');
  50.         $this->breadcrumb[0]['last'] = false;
  51.         $this->breadcrumb[1]['name'] = $this->translator->trans('page.attribute.Attributes');
  52.         $this->breadcrumb[1]['link'] = $this->generateUrl('app_attribute_index');
  53.         $this->breadcrumb[1]['last'] = false;
  54.         
  55.         // Get the attribute name depending of the user's langs
  56.         $attributeName "";
  57.         $user $this->security->getUser();
  58.         $userInterfaceLang $user->getInterfaceLang();
  59.         $attributLang $attributeLangRepository->findOneBy(["attribute" => $attribute,'lang'=> $userInterfaceLang]);
  60.         if($attributLang != false){
  61.             $attributeName $attributLang->getName();
  62.         }
  63.         $this->breadcrumb[2]['name'] = $attributeName;
  64.         $this->breadcrumb[2]['last'] = true;
  65.         $this->twigData['breadcrumb'] = $this->breadcrumb;
  66.         $langs $langRepository->findAll();
  67.         $this->twigData['langs'] = $langs;
  68.         $this->twigData['attributeValues'] = $attributeValueRepository->findBy(['attribute'=>$attribute->getId()]);
  69.         $this->twigData['attributeName'] = $attributeName;
  70.         $this->twigData['attribute'] = $attribute;
  71.         return $this->render('attribute_value/index.html.twig'$this->twigData);
  72.     }
  73.     #[Route('/value/new'name'_new'methods: ['GET''POST'])]
  74.     public function new(Attribute $attributeLangRepository $langRepositoryAttributeValueRepository $attributeValueRepositoryAttributeValueLangRepository $attributeValueLangRepositoryRequest $requestEntityManagerInterface $entityManagerSluggerInterface $sluggerImageRepository $imageRepository): Response
  75.     {
  76.         $this->breadcrumb[0]['name'] = $this->translator->trans('page.catalog.Catalog');
  77.         $this->breadcrumb[0]['link'] = $this->generateUrl('app_catalog_index');
  78.         $this->breadcrumb[0]['last'] = false;
  79.         $this->breadcrumb[1]['name'] = $this->translator->trans('page.attribute.Attributes');
  80.         $this->breadcrumb[1]['link'] = $this->generateUrl('app_attribute_index');
  81.         $this->breadcrumb[1]['last'] = false;
  82.         
  83.         // Get the attribute name depending of the user's langs
  84.         $attributeName "";
  85.         $user $this->security->getUser();
  86.         $userLangs $user->getLang();
  87.         $attributeLangs $attribute->getAttributeLang();
  88.         foreach ($attributeLangs as $key => $attributeLang) {
  89.             if($userLangs->contains($attributeLang->getLang())){
  90.                 $attributeName $attributeLang;
  91.             }
  92.         }
  93.         // Fallback if the attribute do not have value for the user's lang, get the name of the first lang
  94.         if($attributeName == ""){
  95.             $attributeName $attributeLangs[0];
  96.         }
  97.         $this->breadcrumb[2]['name'] = $attributeName;
  98.         $this->breadcrumb[2]['link'] = $this->generateUrl('app_attribute_value_index',['id'=> $attribute->getId()]);
  99.         $this->breadcrumb[2]['last'] = false;
  100.         $this->breadcrumb[3]['name'] = $this->translator->trans('page.attributeValue.new');;
  101.         $this->breadcrumb[3]['last'] = true;
  102.         $this->twigData['breadcrumb'] = $this->breadcrumb;
  103.         $this->twigData['attribute'] = $attribute;
  104.         $langs $langRepository->findAll();
  105.         $this->twigData['langs'] = $langs;
  106.         $submit $request->get("submit");
  107.         if($submit == 1){
  108.             $attributeValue = new AttributeValue();
  109.             $attributeValue->setAttribute($attribute);
  110.             $position $attributeValueRepository->getLastPosition();
  111.             $position += 1;
  112.             $attributeValue->setPosition($position);
  113.             $file $request->files->get('attribute_texture');
  114.             if ($file) {
  115.                 $originalFilename pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  116.                 // this is needed to safely include the file name as part of the URL
  117.                 $safeFilename $slugger->slug($originalFilename);
  118.                 $newFilename $safeFilename.'-'.uniqid().'.'.$file->guessExtension();
  119.                 // $attributeTextureImage = $attributeValue->getTexture();
  120.                 // if ($attributeTextureImage != null) {
  121.                 //     $pathToFile = $this->getParameter('attributeTexture_image_directory').'/'.$attributeTextureImage->getName();
  122.                 //     if (file_exists($pathToFile)) {
  123.                 //         unlink($pathToFile);
  124.                 //     }
  125.                 // }
  126.                 // Move the file to the directory where attributeTexture image are stored
  127.                 try {
  128.                     $file->move(
  129.                         $this->getParameter('attributeTexture_image_directory'),
  130.                         $newFilename
  131.                     );
  132.                     $texture = new Image();
  133.                     $texture->setName($newFilename);
  134.                     $texture->setDescription('');
  135.                     $texture->setCover(0);
  136.                     $imageRepository->add($texture);
  137.                     $attributeValue->setTexture($texture);
  138.                 } catch (FileException $e) {
  139.                 }
  140.             }
  141.             
  142.             $attributeValueRepository->add($attributeValue);
  143.             $attributeFormData $request->get("attributeValue");
  144.             
  145.             $attributeValueLang $attributeFormData['value'];
  146.             foreach ($attributeValueLang as $lang_id => $item) {
  147.                 $attributeValueLang = new AttributeValueLang();
  148.                 $lang $langRepository->find($lang_id);
  149.                 $attributeValueLang->setLang($lang);
  150.                 $attributeValueLang->setAttributeValue($attributeValue);
  151.                 $attributeValueLang->setValue($item);
  152.                 $attributeValueLangRepository->add($attributeValueLang);
  153.             }
  154.             $entityManager->flush();
  155.             return $this->redirectToRoute('app_attribute_value_index', ['id'=>$attribute->getId()], Response::HTTP_SEE_OTHER);
  156.         }
  157.         return $this->render('attribute_value/new.html.twig'$this->twigData);
  158.     }
  159.     #[Route('/value/{id_value}/edit'name'_edit'methods: ['GET''POST'])]
  160.     public function edit(Attribute $attribute$id_valueLangRepository $langRepositoryAttributeValueRepository $attributeValueRepositoryAttributeValueLangRepository $attributeValueLangRepositoryRequest $requestEntityManagerInterface $entityManagerSluggerInterface $sluggerImageRepository $imageRepository): Response
  161.     {
  162.         $this->breadcrumb[0]['name'] = $this->translator->trans('page.catalog.Catalog');
  163.         $this->breadcrumb[0]['link'] = $this->generateUrl('app_catalog_index');
  164.         $this->breadcrumb[0]['last'] = false;
  165.         $this->breadcrumb[1]['name'] = $this->translator->trans('page.attribute.Attributes');
  166.         $this->breadcrumb[1]['link'] = $this->generateUrl('app_attribute_index');
  167.         $this->breadcrumb[1]['last'] = false;
  168.         
  169.         // Get the attribute name depending of the user's langs
  170.         $attributeName "";
  171.         $user $this->security->getUser();
  172.         $userLangs $user->getLang();
  173.         $attributeLangs $attribute->getAttributeLang();
  174.         foreach ($attributeLangs as $key => $attributeLang) {
  175.             if($userLangs->contains($attributeLang->getLang())){
  176.                 $attributeName $attributeLang;
  177.             }
  178.         }
  179.         // Fallback if the attribute do not have value for the user's lang, get the name of the first lang
  180.         if($attributeName == ""){
  181.             $attributeName $attributeLangs[0];
  182.         }
  183.         $this->breadcrumb[2]['name'] = $attributeName;
  184.         $this->breadcrumb[2]['link'] = $this->generateUrl('app_attribute_value_index',['id'=> $attribute->getId()]);
  185.         $this->breadcrumb[2]['last'] = false;
  186.         $this->breadcrumb[3]['name'] = $this->translator->trans('page.attributeValue.edit');
  187.         $this->breadcrumb[3]['last'] = true;
  188.         $this->twigData['breadcrumb'] = $this->breadcrumb;
  189.         $this->twigData['attribute'] = $attribute;
  190.         $langs $langRepository->findAll();
  191.         $this->twigData['langs'] = $langs;
  192.         $attributeValue $attributeValueRepository->find($id_value);
  193.         $this->twigData['attributeValue'] = $attributeValue;
  194.         $submit $request->get("submit");
  195.         if($submit == 1){
  196.             $attributeFormData $request->get("attributeValue");
  197.             if (isset($attributeFormData['isColor'])) {
  198.                 $file $request->files->get('attribute_texture');
  199.                 if ($file) {
  200.                     $originalFilename pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  201.                     // this is needed to safely include the file name as part of the URL
  202.                     $safeFilename $slugger->slug($originalFilename);
  203.                     $newFilename $safeFilename.'-'.uniqid().'.'.$file->guessExtension();
  204.                     $attributeTextureImage $attributeValue->getTexture();
  205.                     if ($attributeTextureImage != null) {
  206.                         $pathToFile $this->getParameter('attributeTexture_image_directory').'/'.$attributeTextureImage->getName();
  207.                         if (file_exists($pathToFile)) {
  208.                             unlink($pathToFile);
  209.                         }
  210.                     }
  211.                     // Move the file to the directory where attributeTexture image are stored
  212.                     try {
  213.                         $file->move(
  214.                             $this->getParameter('attributeTexture_image_directory'),
  215.                             $newFilename
  216.                         );
  217.                         $texture = new Image();
  218.                         $texture->setName($newFilename);
  219.                         $texture->setDescription('');
  220.                         $texture->setCover(0);
  221.                         $imageRepository->add($texture);
  222.                         $attributeValue->setTexture($texture);
  223.                     } catch (FileException $e) {
  224.                         dd($e);
  225.                     }
  226.                 }
  227.             }else{
  228.                 $attributeTextureImage $attributeValue->getTexture();
  229.                 if ($attributeTextureImage != null) {
  230.                     $pathToFile $this->getParameter('attributeTexture_image_directory').'/'.$attributeTextureImage->getName();
  231.                     if (file_exists($pathToFile)) {
  232.                         unlink($pathToFile);
  233.                     }
  234.                     $attributeValue->setTexture(null);
  235.                 }
  236.             }
  237.             
  238.             $attributeValueLang $attributeFormData['value'];
  239.             foreach ($attributeValueLang as $lang_id => $item) {
  240.                 $attributeValueLang $attributeValueLangRepository->findOneBy(['lang'=>$lang_id,'attributeValue'=>$attributeValue->getId()]);
  241.                 if($attributeValueLang == null){
  242.                     $attributeValueLang = new AttributeValueLang();
  243.                     $lang $langRepository->find($lang_id);
  244.                     $attributeValueLang->setLang($lang);
  245.                     $attributeValueLang->setAttributeValue($attributeValue);
  246.                 }
  247.                 $attributeValueLang->setValue($item);
  248.                 $attributeValueLangRepository->add($attributeValueLang);
  249.             }
  250.             if(isset($attributeFormData['isColor'])){
  251.                 $attributeValue->setColor($attributeFormData['color']);
  252.             }else{
  253.                 $attributeValue->setColor(null);
  254.             }
  255.             $attributeValueRepository->add($attributeValue);
  256.             
  257.             
  258.             $entityManager->flush();
  259.             return $this->redirectToRoute('app_attribute_value_index', ['id'=>$attribute->getId()], Response::HTTP_SEE_OTHER);
  260.         }
  261.         return $this->render('attribute_value/edit.html.twig'$this->twigData);
  262.     }
  263.     #[Route('/value/{id_value}'name'_delete'methods: ['POST'])]
  264.     public function delete(Attribute $attribute$id_valueAttributeValueRepository $attributeValueRepositoryProductAttributeRepository $productAttributeRepositoryAttributeValueLangRepository $attributeValueLangRepositoryRequest $requestERPCommercialDataLangRepository $ERPCommercialDataLangRepositoryERPCommercialDataRepository $ERPCommercialDataRepositoryERPLogisticDataRepository $ERPLogisticDataRepository): Response
  265.     {
  266.         $attributeValue $attributeValueRepository->find($id_value);
  267.         $attribute $attributeValue->getAttribute();
  268.         if ($this->isCsrfTokenValid('delete'.$id_value$request->request->get('_token'))) {
  269.             $attributeValueLang $attributeValueLangRepository->findBy(['attributeValue' => $id_value]);
  270.             foreach ($attributeValueLang as $key => $item) {
  271.                 $attributeValueLangRepository->remove($item);
  272.             }
  273.             $productAttributes $attributeValue->getProductAttributes();
  274.             foreach ($productAttributes as $key => $productAttribute) {
  275.                 $ERPCommercialDataLangs $ERPCommercialDataLangRepository->findBy(['productAttribute' => $productAttribute]);
  276.                 if ($ERPCommercialDataLangs != null) {
  277.                     foreach ($ERPCommercialDataLangs as $ERPCommercialDataLang) {
  278.                         $ERPCommercialDataLang $ERPCommercialDataLangRepository->find($ERPCommercialDataLang->getId());
  279.                         // dd($ERPCommercialDataLang);
  280.                         if ($ERPCommercialDataLang != null) {
  281.                             $ERPCommercialDataLang->setProductAttribute(null);
  282.                             $ERPCommercialDataLang->setProduct(null);
  283.                             $ERPCommercialDataLang->setERPCommercialData(null);
  284.                             $ERPCommercialDataLangRepository->remove($ERPCommercialDataLangtrue);
  285.                         }
  286.                     }
  287.                 }
  288.                 
  289.                 $ERPCommercialData $ERPCommercialDataRepository->findOneBy(['productAttribute' => $productAttribute]);
  290.                 if ($ERPCommercialData != null) {
  291.                     $ERPCommercialData->setProductAttribute(null);
  292.                     $ERPCommercialData->setProduct(null);
  293.                     $ERPCommercialDataRepository->remove($ERPCommercialDatatrue);
  294.                 }
  295.                 if ($productAttribute->getERPLogisticData() != null) {
  296.                     $productAttribute->getERPLogisticData()->setProductAttribute(null);
  297.                     $productAttribute->getERPLogisticData()->setProduct(null);
  298.                     $ERPLogisticDataRepository->remove($productAttribute->getERPLogisticData(), true);
  299.                 }
  300.                 
  301.                 $productAttributeRepository->remove($productAttributetrue);
  302.             }
  303.             $attributeValueRepository->remove($attributeValuetrue);
  304.         }
  305.         return $this->redirectToRoute('app_attribute_value_index', ['id'=>$attribute->getId()], Response::HTTP_SEE_OTHER);
  306.     }
  307.     #[Route('/sync/{id_value}'name'_sync'methods: ['GET'])]
  308.     public function sync(Attribute $attribute$id_valueAttributeLangRepository $attributeLangRepositoryAttributeValueRepository $attributeValueRepository,AttributeValueLangRepository $attributeValueLangRepositoryLangRepository $langRepositoryRequest $requestSiteRepository $siteRepositoryPSARepository $PSARepository): Response
  309.     {
  310.         $debug false;
  311.         $sites $siteRepository->findAll();
  312.         $attributeValue $attributeValueRepository->find($id_value);
  313.         $errors = [];
  314.         foreach ($sites as $key => $site) {
  315.             $attributeLangs $attribute->getAttributeLang();
  316.                 $attributeLang null;
  317.                 foreach ($attributeLangs as $keyAttributeLang => $attributeLang) {
  318.                     if ($attributeLang->getLang() == $this->security->getUser()->getInterfaceLang()) {
  319.                         $attributeLang $attributeLang;
  320.                         break;
  321.                     }
  322.                 }
  323.             $PSAAttribute $PSARepository->findOneBy(['entityName' => "attribute"'entityId' => $attribute->getId(), 'siteId' => $site->getId()]);
  324.             if($PSAAttribute == null){
  325.                 $error $this->translator->trans('page.attributeValue.error.AttributeValueNotSync', ['%attribute%' => $attributeLang->getName()]);
  326.                 if(in_array($error$errors) == false){
  327.                     $errors[] = $error;
  328.                 }
  329.             }
  330.         }
  331.         if(!empty($errors)){
  332.             foreach ($errors as $keyError => $error) {
  333.                 $this->addFlash('error', ['from'=>$this->translator->trans('page.product.Sync'),'message'=> $error]);
  334.             }
  335.             return $this->redirectToRoute('app_attribute_value_index', ['id'=>$attribute->getId()], Response::HTTP_SEE_OTHER);
  336.             die;
  337.         }
  338.         foreach ($sites as $key => $site) {
  339.             $url $site->getUrl();
  340.             $key $site->getApiKey();
  341.             $webService = new PrestaShopWebservice($url$key$debug);
  342.             $webServiceUtils = new WebserviceUtils($webService$url);
  343.             $langs $webServiceUtils->getSiteLangs();
  344.             $psaAttributeValue $PSARepository->findOneBy(['entityId'=> $attributeValue->getId(), "entityName"=>'attributeValue'"siteId"=>$site->getId()]);
  345.             $attributeValueColors $attributeValueRepository->findWithColor($attributeValue);
  346.             $attributeIsColor false;
  347.             $psaParentAttribute $PSARepository->findOneBy(['entityId'=> $attribute->getId(), "entityName"=>'attribute'"siteId"=>$site->getId()]);
  348.             $attributeXML $webService->get(['resource' => 'product_options''id'=>$psaParentAttribute->getInSiteId()]);
  349.             $attributeValueXML $attributeXML->product_option[0]->associations->product_option_values->product_option_value;
  350.             $attributeValuePosition count($attributeValueXML);
  351.             if(count($attributeValueColors) > 0){
  352.                 $attributeIsColor true;
  353.             }
  354.             if($psaAttributeValue != null){
  355.                 try {
  356.                     $psaParentAttribute $PSARepository->findOneBy(['entityId'=> $attribute->getId(), "entityName"=>'attribute'"siteId"=>$site->getId()]);
  357.                     $xmlResponse $webService->get(['resource' => 'product_option_values''id'=>$psaAttributeValue->getInSiteId()]);
  358.                     $productFeatureXML $xmlResponse->product_option_value[0];
  359.                     $productFeatureXML->id_attribute_group $psaParentAttribute->getInSiteId();
  360.                     if($attributeValue->getColor() != null){
  361.                         $productFeatureXML->color $attributeValue->getColor();
  362.                     }
  363.                     
  364.                     foreach ($langs as $keyLang => $lang) {
  365.                         if ($lang['icu'] == "en") {
  366.                             $lang['icu'] = "gb";
  367.                         }
  368.                         $langPimdam $langRepository->findOneBy(['ICU'=>$lang['icu']]);
  369.                         $attributeValueLang $attributeValueLangRepository->findOneBy(['lang'=> $langPimdam'attributeValue'=>$attributeValue->getId()]);
  370.                         $productFeatureXML->name->language[$keyLang] = $attributeValueLang->getValue();
  371.                     }
  372.                     $opt = ['resource' => 'product_option_values'];
  373.                     $opt['putXml'] = $xmlResponse->asXML();
  374.                     $opt['id'] = (int) $productFeatureXML->id;
  375.                     $return $webService->edit($opt);
  376.                 } catch (\Throwable $th) { 
  377.                     $PSARepository->Remove($psaAttributeValuetrue);
  378.                     return $this->redirectToRoute('app_attribute_value_sync', ['id'=>$attributeValue->getAttribute()->getId(), 'id_value'=>$id_value], Response::HTTP_SEE_OTHER);
  379.                 }
  380.             }else{
  381.                 $psaParentAttribute $PSARepository->findOneBy(['entityId'=> $attribute->getId(), "entityName"=>'attribute'"siteId"=>$site->getId()]);
  382.                 $xmlResponse $webService->get(['url' => $url '/api/product_option_values?schema=blank']);
  383.                 $productFeatureXML $xmlResponse->product_option_value[0];
  384.                 $productFeatureXML->position $attributeValuePosition;
  385.                 $productFeatureXML->id_attribute_group $psaParentAttribute->getInSiteId();
  386.                 if($attributeValue->getColor() != null){
  387.                     $productFeatureXML->color $attributeValue->getColor();
  388.                 }
  389.                 
  390.                 foreach ($langs as $keyLang => $lang) {
  391.                     if ($lang['icu'] == "en") {
  392.                         $lang['icu'] = "gb";
  393.                     }
  394.                     $langPimdam $langRepository->findOneBy(['ICU'=>$lang['icu']]);
  395.                     $attributeValueLang $attributeValueLangRepository->findOneBy(['lang'=> $langPimdam'attributeValue'=>$attributeValue->getId()]);
  396.                     $productFeatureXML->name->language[$keyLang] = $attributeValueLang->getValue();
  397.                 }
  398.                 $opt = ['resource' => 'product_option_values'];
  399.                 $opt['postXml'] = $xmlResponse->asXML();
  400.                 $return $webService->add($opt);
  401.                 $returnedId =  intval($return->product_option_value[0]->id);
  402.                 $psaAttributeValue = new PSA();
  403.                 $psaAttributeValue->setEntityId($attributeValue->getId());
  404.                 $psaAttributeValue->setEntityName('attributeValue');
  405.                 $psaAttributeValue->setInSiteId($returnedId);
  406.                 $psaAttributeValue->setSiteId($site->getId());
  407.                 $PSARepository->add($psaAttributeValuetrue);
  408.             }
  409.         }
  410.         return $this->redirectToRoute('app_attribute_value_index', ['id'=>$attribute->getId()], Response::HTTP_SEE_OTHER);
  411.     }
  412. }