src/Controller/Admin/AdminStoresController.php line 44

  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Controller\Bootstrap\DefaultSalesHubController;
  4. use App\Entity\Cms;
  5. use App\Entity\Configuration;
  6. use App\Entity\ProductRelation;
  7. use App\Entity\Store;
  8. use App\Entity\SyncLog;
  9. use App\Service\ProductRelationTools;
  10. use App\Utils\Breadcrumb;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Doctrine\ORM\Query\Expr\Join;
  13. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  14. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  15. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  16. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  17. use Symfony\Component\Form\Extension\Core\Type\TextType;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use App\Service\Prestashop\PrestashopProductManager;
  23. use App\Service\Wordpress\WordpressProductManager;
  24. use App\Service\Shopify\ShopifyProductManager;
  25. class AdminStoresController extends DefaultSalesHubController
  26. {
  27.     public string $page_name 'Stores';
  28.     public $productRelationTools;
  29.     public const PRESTASHOP_PRODUCT_PER_PAGE 500;
  30.     public const WORDPRESS_PRODUCT_PER_PAGE 100//Wordpress max value is 100
  31.     public const SHOPIFY_PRODUCT_PER_PAGE 20;
  32.     public function assignBreadcrumb(){
  33.         $this->breadcrumb = [
  34.             (new Breadcrumb('Dashboard'$this->router->generate('dashboard'))),
  35.             (new Breadcrumb('Stores')),
  36.         ];
  37.     }
  38.     #[Route('/admin/stores'name'stores')]
  39.     public function index(
  40.         EntityManagerInterface $entityManager,
  41.         Request $request,
  42.         ProductRelationTools $productRelationTools,
  43.         PrestashopProductManager $prestashopProductManager,
  44.         WordpressProductManager $wordpressProductManager,
  45.         ShopifyProductManager $shopifyProductManager
  46.     ): Response
  47.     {
  48.         $this->productRelationTools $productRelationTools;
  49.         $this->theme->addJsDef([
  50.             'ajax_url' => $this->generateUrl('stores', ['ajax' => 1])
  51.         ]);
  52.         if($request->request->get('ajax') || $request->query->get('ajax')){
  53.             return $this->handleAjax($entityManager$request$prestashopProductManager$wordpressProductManager$shopifyProductManager);
  54.         }
  55.         if($request->query->has('set_store')){
  56.             return $this->form($entityManager$request);
  57.         }elseif($request->query->has('set_products')){
  58.             return $this->productsForm($entityManager$request);
  59.         }elseif($request->query->has('delete_store')){
  60.             $this->deleteEntity($entityManager$request);
  61.         }
  62.         return $this->listing($entityManager);
  63.     }
  64.     protected function listing(EntityManagerInterface $entityManager){
  65.         $cms $entityManager->getRepository(Cms::class)->findAll();
  66.         $cmsFilter = [];
  67.         foreach ($cms as $cms_single){
  68.             $cmsFilter[$cms_single->getId()] = $cms_single->getName();
  69.         }
  70.         $datatable = [
  71.             'name' => 'Stores',
  72.             'selector' => 'stores',
  73.             'columns' => [
  74.                 ['display_name' => 'ID''data' => 'id''name' => 'store.id'],
  75.                 ['display_name' => 'Cms''data' => 'cms_name''name' => 'cms.name'],
  76.                 ['display_name' => 'Name''data' => 'name''name' => 'store.name'],
  77.                 ['display_name' => 'Base url''data' => 'base_url''name' => 'store.base_url'],
  78.                 ['display_name' => 'Extra price amount''data' => 'extra_price_amount''name' => 'store.extra_price_amount'],
  79.                 ['display_name' => 'Extra price percentage''data' => 'extra_price_percentage''name' => 'store.extra_price_percentage'],
  80.                 ['display_name' => 'Active''data' => 'active''name' => 'store.active'],
  81.             ],
  82.             'column_actions' => [
  83.                 [
  84.                     'name' => 'Edit',
  85.                     'url' => $this->generateUrl('stores', ['set_store' => 1'id_store' => '__the_id__']),
  86.                 ],
  87.                 [
  88.                     'name' => 'Set Products',
  89.                     'url' => $this->generateUrl('stores', ['set_products' => 1'id_store' => '__the_id__']),
  90.                 ],
  91.                 [
  92.                     'name' => 'Delete',
  93.                     'url' => $this->generateUrl('stores', ['delete_store' => 1'id_store' => '__the_id__']),
  94.                 ],
  95.             ],
  96.             'filter' => [
  97.                 'field' => 'cms.id',
  98.                 'options' => $cmsFilter
  99.             ],
  100.             'columnDefs' => [
  101.                 [
  102.                     'targets' => 6,
  103.                     'orderable' => false,
  104.                     'render' => [
  105.                         'val_check' => 'active',
  106.                         'if_true' => '<span class="badge py-3 px-4 fs-7 badge-light-success">Active</span>',
  107.                         'if_false' => '<span class="badge py-3 px-4 fs-7 badge-light-danger">Inactive</span>',
  108.                     ]
  109.                 ]
  110.             ]
  111.         ];
  112.         $this->theme->addJavascriptFile('js/custom/utilities/datatables.js');
  113.         $this->theme->addJsDef([
  114.             'datatables' => [
  115.                 $datatable
  116.             ]
  117.         ]);
  118.         $rendered_datatable $this->render('partials/datatable.html.twig', [
  119.             'datatable' => $datatable,
  120.         ])->getContent();
  121.         return $this->render('pages/stores/index.html.twig', [
  122.             'rendered_datatable' => $rendered_datatable,
  123.             'add_product_url' => $this->generateUrl('stores', ['set_store' => 1]),
  124.         ]);
  125.     }
  126.     protected function form(EntityManagerInterface $entityManagerRequest $request)
  127.     {
  128.         $this->theme->addJavascriptFile('js/custom/pages/stores/general.js');
  129.         
  130.         $cms $entityManager->getRepository(Cms::class)->findAll();
  131.         $cms_choices = [];
  132.         foreach ($cms as $cms_single){
  133.             $cms_choices[$cms_single->getName()] = $cms_single->getId();
  134.         }
  135.         $form $this->createFormBuilder(null)
  136.             ->add('cms'ChoiceType::class, [
  137.                 'choices' => $cms_choices
  138.             ])
  139.             ->add('name'TextType::class)
  140.             ->add('admin_id_manufacturer'IntegerType::class)
  141.             ->add('admin_id_supplier'IntegerType::class)
  142.             ->add('base_url'TextType::class, [
  143.                 'required' => false,
  144.             ])
  145.             ->add('api_key'TextType::class, [
  146.                 'required' => false,
  147.             ])
  148.             ->add('api_secret'TextType::class, [
  149.                 'required' => false,
  150.             ])
  151.             ->add('extra_price_amount'IntegerType::class)
  152.             ->add('extra_price_percentage'IntegerType::class)
  153.             ->add('active'CheckboxType::class, [
  154.                 'required' => false,
  155.             ])
  156.             ->add('unlimited_stock'CheckboxType::class, [
  157.                 'required' => false,
  158.             ])
  159.             ->add('save'SubmitType::class, ['label' => 'Save'])
  160.             ->getForm();
  161.         $form->handleRequest($request);
  162.         if ($form->isSubmitted() && $form->isValid()) {
  163.             $form_data $form->getData();
  164.             $id_store $request->query->get('id_store');
  165.             if($id_store){
  166.                 $storeObj $entityManager->getRepository(Store::class)->findOneBy(['id' => $id_store]);
  167.             }else{
  168.                 $storeObj = new Store();
  169.             }
  170.             $cmsObj $entityManager->getRepository(Cms::class)->findOneBy(['id' => $form_data['cms']]);
  171.             $storeObj->setCms($cmsObj);
  172.             $storeObj->setName($form_data['name']);
  173.             $storeObj->setAdminIdManufacturer($form_data['admin_id_manufacturer']);
  174.             $storeObj->setAdminIdSupplier($form_data['admin_id_supplier']);
  175.             $storeObj->setBaseUrl($form_data['base_url']);
  176.             $storeObj->setApiKey((string)$form_data['api_key']);
  177.             $storeObj->setApiSecret((string)$form_data['api_secret']);
  178.             $storeObj->setExtraPriceAmount($form_data['extra_price_amount']);
  179.             $storeObj->setExtraPricePercentage($form_data['extra_price_percentage']);
  180.             $storeObj->setActive($form_data['active']);
  181.             $storeObj->setUnlimitedStock($form_data['unlimited_stock']);
  182.             $entityManager->persist($storeObj);
  183.             $entityManager->flush();
  184.         }else{
  185.             $id_store $request->query->get('id_store');
  186.             if($id_store){
  187.                 $storeObj $entityManager->getRepository(Store::class)->findOneBy(['id' => $id_store]);
  188.                 $form->setData([
  189.                     'cms' => ($storeObj->getCms())->getId(),
  190.                     'name' => $storeObj->getName(),
  191.                     'admin_id_manufacturer' => $storeObj->getAdminIdManufacturer(),
  192.                     'admin_id_supplier' => $storeObj->getAdminIdSupplier(),
  193.                     'base_url' => $storeObj->getBaseUrl(),
  194.                     'api_key' => $storeObj->getApiKey(),
  195.                     'api_secret' => $storeObj->getApiSecret(),
  196.                     'extra_price_amount' => $storeObj->getExtraPriceAmount(),
  197.                     'extra_price_percentage' => $storeObj->getExtraPricePercentage(),
  198.                     'active' => $storeObj->isActive(),
  199.                     'unlimited_stock' => $storeObj->isUnlimitedStock(),
  200.                 ]);
  201.             }else{
  202.                 $form->setData([
  203.                     'extra_price_amount' => 0,
  204.                     'extra_price_percentage' => 0,
  205.                 ]);
  206.             }
  207.         }
  208.         return $this->render('pages/stores/form.html.twig', [
  209.             'form' => $form->createView(),
  210.         ]);
  211.     }
  212.     protected function productsForm(EntityManagerInterface $entityManagerRequest $request)
  213.     {
  214.         $id_store $request->query->get('id_store');
  215.         $storeObj $entityManager->getRepository(Store::class)->findOneBy(['id' => $id_store]);
  216.         $this->theme->addJavascriptFile('js/custom/pages/stores/products_wordpress.js');
  217.         $this->theme->addJsDef([
  218.             'id_store' => $id_store,
  219.             'id_cms' => $storeObj->getCms()->getId()
  220.         ]);
  221.         $datatable = [
  222.             'name' => 'ProductRelations',
  223.             'selector' => 'product_relations_'.$id_store,
  224.             'columns' => [
  225.                 ['display_name' => 'ID''data' => 'id''name' => 'product_relation.id'],
  226.                 ['display_name' => 'Id product''data' => 'id_product''name' => 'product_relation.id_product'],
  227.                 ['display_name' => 'Name''data' => 'name''name' => 'product_relation.name'],
  228.                 ['display_name' => 'Image''data' => 'sample_image_url''name' => 'product_relation.sample_image_url'],
  229.                 ['display_name' => 'Admin Id product''data' => 'admin_id_product''name' => 'product_relation.admin_id_product'],
  230.             ],
  231.             'filter' => [],
  232.             'columnDefs' => [
  233.                 [
  234.                     'targets' => 3,
  235.                     'orderable' => false,
  236.                     'render' => [
  237.                         'handle_function' => 'renderProductImage',
  238.                     ]
  239.                 ],
  240.                 [
  241.                     'targets' => 4,
  242.                     'orderable' => false,
  243.                     'render' => [
  244.                         'handle_function' => 'renderAdminIdProduct',
  245.                     ]
  246.                 ]
  247.             ]
  248.         ];
  249.         $this->theme->addJavascriptFile('js/custom/utilities/datatables.js');
  250.         $this->theme->addJsDef([
  251.             'datatables' => [
  252.                 $datatable
  253.             ]
  254.         ]);
  255.         $rendered_datatable $this->render('partials/datatable.html.twig', [
  256.             'datatable' => $datatable,
  257.         ])->getContent();
  258.         return $this->render('pages/stores/productsForm.html.twig', [
  259.             'rendered_datatable' => $rendered_datatable
  260.         ]);
  261.     }
  262.     protected function deleteEntity(EntityManagerInterface $entityManagerRequest $request)
  263.     {
  264.         $id_store $request->query->get('id_store');
  265.         if($id_store){
  266.             $storeObj $entityManager->getRepository(Store::class)->findOneBy(['id' => $id_store]);
  267.             $entityManager->remove($storeObj);
  268.             $entityManager->flush();
  269.         }
  270.     }
  271.     public function handleAjax(EntityManagerInterface $entityManagerRequest $requestPrestashopProductManager $prestashopProductManagerWordpressProductManager $wordpressProductManagerShopifyProductManager $shopifyProductManager): JsonResponse
  272.     {
  273.         $ajaxData json_decode($request->getContent(), true);
  274.         if($draw $request->query->get('draw')){
  275.             $selector $request->query->get('selector');
  276.             if($selector && substr($selector0strlen('product_relations')) == 'product_relations'){
  277.                 $id_store substr($selectorstrlen('product_relations_'));
  278.                 return $this->drawProductsForm($entityManager$request$id_store);
  279.             }else{
  280.                 return $this->drawListingTable($entityManager$request$draw);
  281.             }
  282.         }elseif(isset($ajaxData['ajaxAction']) && $ajaxData['ajaxAction'] == 'loadProducts'){
  283.             return $this->loadProducts($entityManager$prestashopProductManager$wordpressProductManager$shopifyProductManager$ajaxData['id_store'], $ajaxData['page'], $ajaxData['total_products'], $ajaxData['after']);
  284.         }elseif(isset($ajaxData['ajaxAction']) && $ajaxData['ajaxAction'] == 'saveAdminIdProduct'){
  285.             return $this->saveAdminIdProduct($entityManager$ajaxData['id_product_relation'], $ajaxData['admin_id_product']);
  286.         }elseif(isset($ajaxData['ajaxAction']) && $ajaxData['ajaxAction'] == 'checkApi'){
  287.             return $this->checkApi($prestashopProductManager$wordpressProductManager$shopifyProductManager$ajaxData['id_cms'], $ajaxData['base_url'], $ajaxData['api_key'], $ajaxData['api_secret']);
  288.         }
  289.     }
  290.     protected function drawListingTable(EntityManagerInterface $entityManagerRequest $request$draw) : JsonResponse
  291.     {
  292.         $options $request->query->all();
  293.         $order_by 'id';
  294.         $order_way 'DESC';
  295.         $where null;
  296.         $limit = (isset($options['length']) ? $options['length'] : 10);
  297.         $offset = (isset($options['start']) ? $options['start'] : 0);
  298.         if(isset($options['order']) && isset($options['order'][0])){
  299.             $order_by $options['columns'][$options['order'][0]['column']]['name'];
  300.             $order_way $options['order'][0]['dir'];
  301.         }
  302.         if(isset($options['search']) && isset($options['search']['value']) && $options['search']['value']){
  303.             $where $options['search']['value'];
  304.         }
  305.         $query $entityManager->createQueryBuilder()
  306.             ->select('store')
  307.             ->from('App\Entity\Store''store')
  308.             ->leftJoin(
  309.                 'App\Entity\Cms',
  310.                 'cms',
  311.                 Join::WITH,
  312.                 'store.cms = cms.id'
  313.             );
  314.         if($where){
  315.             $query->where($where);
  316.         }
  317.         $stores $query
  318.             ->orderBy($order_by$order_way)
  319.             ->setMaxResults($limit)
  320.             ->setFirstResult($offset)
  321.             ->getQuery()
  322.             ->getResult();
  323.         $resultTotal $entityManager->getRepository(Store::class)->count([]);
  324.         $resultDatatable = [];
  325.         foreach ($stores as $store){
  326.             $this_cms $store->getCms();
  327.             $this_data = [
  328.                 'id' => $store->getId(),
  329.                 'cms_id' => $this_cms->getId(),
  330.                 'cms_name' => $this_cms->getName(),
  331.                 'name' => $store->getName(),
  332.                 'base_url' => $store->getBaseUrl(),
  333.                 'extra_price_amount' => $store->getExtraPriceAmount(),
  334.                 'extra_price_percentage' => $store->getExtraPricePercentage(),
  335.                 'active' => $store->isActive(),
  336.             ];
  337.             $resultDatatable[] = $this_data;
  338.         }
  339.         return JsonResponse::fromJsonString(json_encode([
  340.             'draw' => $draw,
  341.             'recordsTotal' => $resultTotal,
  342.             'recordsFiltered' => $resultTotal,
  343.             'data' => $resultDatatable
  344.         ]));
  345.     }
  346.     protected function drawProductsForm(EntityManagerInterface $entityManagerRequest $request$id_store) : JsonResponse
  347.     {
  348.         $options $request->query->all();
  349.         $order_by 'id';
  350.         $order_way 'ASC';
  351.         $where null;
  352.         $limit = (isset($options['length']) ? $options['length'] : 10);
  353.         $offset = (isset($options['start']) ? $options['start'] : 0);
  354.         if(isset($options['order']) && isset($options['order'][0])){
  355.             $order_by $options['columns'][$options['order'][0]['column']]['name'];
  356.             $order_way $options['order'][0]['dir'];
  357.         }
  358.         if(isset($options['search']) && isset($options['search']['value']) && $options['search']['value']){
  359.             $where $options['search']['value'];
  360.         }
  361.         $query $entityManager->createQueryBuilder()
  362.             ->select('product_relation')
  363.             ->from('App\Entity\ProductRelation''product_relation')
  364.             ->where('product_relation.store = '.$id_store.' AND product_relation.deleted = 0');
  365.         if($where){
  366.             $query->where($where);
  367.         }
  368.         $resultTotal $entityManager->createQueryBuilder()
  369.             ->select('count(product_relation)')
  370.             ->from('App\Entity\ProductRelation''product_relation')
  371.             ->where('product_relation.store = '.$id_store.' AND product_relation.deleted = 0')
  372.             ->getQuery()
  373.             ->getSingleScalarResult();
  374.         $productRelations $query
  375.             ->orderBy($order_by$order_way)
  376.             ->setMaxResults($limit)
  377.             ->setFirstResult($offset)
  378.             ->getQuery()
  379.             ->getResult();
  380.         $resultDatatable = [];
  381.         foreach ($productRelations as $productRelation){
  382.             $this_data = [
  383.                 'id' => $productRelation->getId(),
  384.                 'id_product' => $productRelation->getIdProduct(),
  385.                 'name' => $productRelation->getName(),
  386.                 'sample_image_url' => $productRelation->getSampleImageUrl(),
  387.                 'admin_id_product' => $productRelation->getAdminIdProduct(),
  388.             ];
  389.             $resultDatatable[] = $this_data;
  390.         }
  391.         return JsonResponse::fromJsonString(json_encode([
  392.             'draw' => $options['draw'],
  393.             'recordsTotal' => $resultTotal,
  394.             'recordsFiltered' => $resultTotal,
  395.             'data' => $resultDatatable
  396.         ]));
  397.     }
  398.     protected function loadProducts(
  399.         EntityManagerInterface $entityManager,
  400.         PrestashopProductManager $prestashopProductManager,
  401.         WordpressProductManager $wordpressProductManager,
  402.         ShopifyProductManager $shopifyProductManager,
  403.         int $id_store,
  404.         int $page,
  405.         $total_products,
  406.         $after
  407.     ) : JsonResponse
  408.     {
  409.         $storeObj $entityManager->getRepository(Store::class)->findOneBy(['id' => $id_store]);
  410.         $productRelationRepository $entityManager->getRepository(ProductRelation::class);
  411.         $extra_params = [];
  412.         $products_per_page 10;
  413.         switch ($storeObj->getCms()->getId()) {
  414.             case Cms::PRESTASHOP_ID:
  415.                 $products_per_page self::PRESTASHOP_PRODUCT_PER_PAGE;
  416.                 $prestashopProductManager->setExternalApiDataByStore($storeObj);
  417.                 $prestashopProductManager->assignExternalApiData();
  418.                 //First time loaded
  419.                 if(!$total_products){
  420.                     $this->productRelationTools->disableAllProducts($id_store);
  421.                     $total_products count($prestashopProductManager->getAllProducts());
  422.                 }
  423.                 $prestashopProducts $prestashopProductManager->getProducts(null$page$products_per_page);
  424.                 foreach ($prestashopProducts as $product){
  425.                     $productRelation $productRelationRepository->findOneBy([
  426.                         'id_product' => $product['id'],
  427.                         'store' => $storeObj->getId(),
  428.                     ]);
  429.                     if(!$productRelation){
  430.                         $productRelation = new ProductRelation();
  431.                         $productRelation->setStore($storeObj);
  432.                         $productRelation->setIdProduct($product['id']);
  433.                         $productRelation->setName(reset($product['name']));
  434.                     }
  435.                     if(!$productRelation->getSampleImageUrl() && $product['images']){
  436.                         $img_url_parts explode('/'reset($product['images']));
  437.                         $id_image end($img_url_parts);
  438.                         $image_url $storeObj->getBaseUrl().'img/p/' implode('/'str_split($id_image)) . '/' $id_image '.jpg';
  439.                         $productRelation->setSampleImageUrl($image_url);
  440.                     }
  441.                     $productRelation->setDeleted(false);
  442.                     $entityManager->persist($productRelation);
  443.                 }
  444.                 $entityManager->flush();
  445.                 break;
  446.             case Cms::WORDPRESS_ID:
  447.                 $products_per_page self::WORDPRESS_PRODUCT_PER_PAGE;
  448.                 try {
  449.                     $wordpressProductManager->setConnectionByStore($storeObj);
  450.                     //First time loaded
  451.                     if(!$total_products){
  452.                         $this->productRelationTools->disableAllProducts($id_store);
  453.                     }
  454.                     $wordpressResponse $wordpressProductManager->getProducts($page$products_per_pagetrue);
  455.                     foreach ($wordpressResponse['products'] as $product){
  456.                         $productRelation $productRelationRepository->findOneBy([
  457.                             'id_product' => $product['id'],
  458.                             'store' => $storeObj->getId(),
  459.                         ]);
  460.                         if(!$productRelation){
  461.                             $productRelation = new ProductRelation();
  462.                             $productRelation->setStore($storeObj);
  463.                             $productRelation->setIdProduct($product['id']);
  464.                             $productRelation->setName($product['name']);
  465.                         }
  466.                         if(!$productRelation->getSampleImageUrl() && $product['images']){
  467.                             $productRelation->setSampleImageUrl($product['images'][0]['src']);
  468.                         }
  469.                         $productRelation->setDeleted(false);
  470.                         $entityManager->persist($productRelation);
  471.                     }
  472.                     $entityManager->flush();
  473.                     $total_products $wordpressResponse['total_products'];
  474.                     break;
  475.                 }catch (\Exception $e){
  476.                     return JsonResponse::fromJsonString(json_encode([
  477.                         'error' => $e->getMessage(),
  478.                     ]));
  479.                 }
  480.             case Cms::SHOPIFY_ID:
  481.                 $products_per_page self::SHOPIFY_PRODUCT_PER_PAGE;
  482.                 $shopifyProductManager->setConnectionByStore($storeObj);
  483.                 //First time loaded
  484.                 if(!$total_products){
  485.                     $this->productRelationTools->disableAllProducts($id_store);
  486.                     $total_products count($shopifyProductManager->getAllProductIds());
  487.                 }
  488.                 $response $shopifyProductManager->getProducts($products_per_page$after);
  489.                 foreach ($response['products'] as $product){
  490.                     $productRelation $productRelationRepository->findOneBy([
  491.                         'id_product' => $product['id'],
  492.                         'store' => $storeObj->getId(),
  493.                     ]);
  494.                     if(!$productRelation){
  495.                         $productRelation = new ProductRelation();
  496.                         $productRelation->setStore($storeObj);
  497.                         $productRelation->setIdProduct($product['id']);
  498.                         $productRelation->setName($product['name']);
  499.                     }
  500.                     if(!$productRelation->getSampleImageUrl() && $product['images']){
  501.                         $productRelation->setSampleImageUrl($product['images'][0]);
  502.                     }
  503.                     $productRelation->setDeleted(false);
  504.                     $entityManager->persist($productRelation);
  505.                 }
  506.                 $entityManager->flush();
  507.                 $extra_params['after'] = $response['after'];
  508.                 break;
  509.         }
  510.         $pending_products 'unknown';
  511.         $percentage_progress 'unknown';
  512.         $percentage_progress_formatted 'unknown';
  513.         if(is_integer((int)$total_products) && $total_products){
  514.             $pending_products $total_products - ($page $products_per_page);
  515.             $percentage_progress = ($page $products_per_page) * 100 $total_products;
  516.             if($pending_products <= 0){
  517.                 $pending_products 0;
  518.                 $percentage_progress 100;
  519.             }
  520.             $percentage_progress round($percentage_progress2);
  521.             $percentage_progress_formatted number_format($percentage_progress2',''');
  522.         }
  523.         return JsonResponse::fromJsonString(json_encode([
  524.             'total_products' => $total_products,
  525.             'pending_products' => $pending_products,
  526.             'percentage_progress' => $percentage_progress,
  527.             'percentage_progress_formatted' => $percentage_progress_formatted,
  528.             'id_cms' => $storeObj->getCms()->getId(),
  529.             'extra_params' => $extra_params
  530.         ]));
  531.     }
  532.     protected function saveAdminIdProduct(EntityManagerInterface $entityManager$id_product_relation$admin_id_product) : JsonResponse
  533.     {
  534.         if($productRelation $entityManager->getRepository(ProductRelation::class)->findOneBy(['id' => $id_product_relation])) {
  535.             $productRelation->setAdminIdProduct(($admin_id_product $admin_id_product null));
  536.             $entityManager->persist($productRelation);
  537.             $entityManager->flush();
  538.         }
  539.         return JsonResponse::fromJsonString(json_encode([
  540.             'success' => true,
  541.         ]));
  542.     }
  543.     protected function checkApi(
  544.         PrestashopProductManager $prestashopProductManager,
  545.         WordpressProductManager $wordpressProductManager,
  546.         ShopifyProductManager $shopifyProductManager,
  547.         $id_cms,
  548.         $base_url,
  549.         $api_key,
  550.         $api_secret
  551.     ) : JsonResponse
  552.     {
  553.         switch ($id_cms) {
  554.             case Cms::PRESTASHOP_ID:
  555.                 $prestashopProductManager->prestashop_url $base_url;
  556.                 $prestashopProductManager->prestashop_apikey $api_key;
  557.                 try {
  558.                     $prestashopProductManager->checkEndpoints();
  559.                     return JsonResponse::fromJsonString(json_encode([
  560.                         'error' => false,
  561.                     ]));
  562.                 }catch (\Exception $e){
  563.                     return JsonResponse::fromJsonString(json_encode([
  564.                         'error' => $e->getMessage(),
  565.                     ]));
  566.                 }
  567.                 break;
  568.             case Cms::WORDPRESS_ID:
  569.                 $wordpressProductManager->setUrl($base_url);
  570.                 $wordpressProductManager->setApiKey($api_key);
  571.                 $wordpressProductManager->setApiSecret($api_secret);
  572.                 try {
  573.                     $wordpressProductManager->checkEndpoints();
  574.                     return JsonResponse::fromJsonString(json_encode([
  575.                         'error' => false,
  576.                     ]));
  577.                 }catch (\Exception $e){
  578.                     return JsonResponse::fromJsonString(json_encode([
  579.                         'error' => $e->getMessage(),
  580.                     ]));
  581.                 }
  582.                 break;
  583.             case Cms::SHOPIFY_ID:
  584.                 $shopifyProductManager->setUrl($base_url);
  585.                 $shopifyProductManager->setAccessToken($api_key);
  586.                 try {
  587.                     $shopifyProductManager->checkEndpoints();
  588.                     return JsonResponse::fromJsonString(json_encode([
  589.                         'error' => false,
  590.                     ]));
  591.                 }catch (\Exception $e){
  592.                     return JsonResponse::fromJsonString(json_encode([
  593.                         'error' => $e->getMessage(),
  594.                     ]));
  595.                 }
  596.                 break;
  597.         }
  598.     }
  599. }