src/Controller/ProductController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\AppBundle\Service\ImagesService;
  4. use App\AppBundle\Service\ProductsBDD;
  5. use App\AppBundle\Service\ProductsDolibarr;
  6. use App\Entity\Images;
  7. use App\Entity\Product;
  8. use App\Form\CreateProductType;
  9. use App\Repository\ProductRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Intervention\Image\ImageManagerStatic;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class ProductController extends AbstractController
  17. {
  18.     //////////////////////////////////////////////////////////////////////
  19.     ///                 Show all active products
  20.     //////////////////////////////////////////////////////////////////////
  21.     #[Route('/product', name: 'app_product')]
  22.     public function showActiveProducts(Request $request, EntityManagerInterface $entityManager): Response
  23.     {
  24.         $user = $this->getUser();
  25.         if (!$user){
  26.             return $this->render('product/not_connected.html.twig');
  27.         }
  28.         $page = $request->query->getInt('page', 1);
  29.         $quantity = 100;
  30.         $imagesRepository = $entityManager->getRepository(Images::class);
  31.         $productsRepository = $entityManager->getRepository(Product::class);
  32.         $imagesCount = $imagesRepository->countImages();
  33.         $productsCount = $productsRepository->countProducts();
  34.         $nb_pages = ceil($productsCount[0][1]/$quantity);
  35.         $offset = ($page - 1) * $quantity;
  36.         $products = $productsRepository->getActiveProductsWithOffset($offset, $quantity);
  37.         $images = [];
  38.         foreach ($products as $product){
  39.             $newImages = $imagesRepository->getImagesOfProduct($product);
  40.             $images = array_merge($images, $newImages);
  41.         }
  42.         return $this->render('product/index.html.twig', [
  43.             'products' => $products,
  44.             'images' => $images,
  45.             'title' => "Active products",
  46.             'nb_pages' => $nb_pages,
  47.         ]);
  48.     }
  49.     //////////////////////////////////////////////////////////////////////
  50.     ///         Copy products to DB from Excel document
  51.     //////////////////////////////////////////////////////////////////////
  52.     #[Route('/copyProductsToDBFromExcel', name: 'app_add_products_to_db')]
  53.     public function addProductToDBFromExcel(ProductsBDD $productsBDD)
  54.     {
  55.         ini_set('max_execution_time', 300);
  56.         $productsBDD->copyProductsFromExcel();
  57.         return $this->redirectToRoute('app_product');
  58.     }
  59.     
  60.     
  61.     //////////////////////////////////////////////////////////////////////
  62.     ///         Update products in DB from Excel document
  63.     //////////////////////////////////////////////////////////////////////
  64.     #[Route('/updateProductsInDBFromExcel', name: 'app_update_products_in_db')]
  65.     public function updateProductsInDBFromExcel(ProductsBDD $productsBDD)
  66.     {
  67.         ini_set('max_execution_time', 300);
  68.         $productsBDD->updateProductsFromExcel();
  69.         return $this->redirectToRoute('app_product');
  70.     }
  71.     //////////////////////////////////////////////////////////////////////
  72.     ///         Add image from folder to BDD
  73.     ///         a photo whose name is in the Excel file but is not in the "./assets/img/upload" folder will not be added!
  74.     //////////////////////////////////////////////////////////////////////
  75.     #[Route('/copyImagesFromFileWithExcelBasedNames', name: 'app_add_image_from_file')]
  76.     public function addImageFromFolder(EntityManagerInterface $entityManager,
  77.                                        ProductsBDD $productsBDD,
  78.                                        ImagesService $imagesService)
  79.     {
  80.         ini_set('max_execution_time', 600);
  81.         // enter the document name and its extension (only xlsx for now)
  82.         $nameExcelDocument = 'plik.xlsx';
  83.         // Columns count start at 0!
  84.         //
  85.         $name_place = 1;
  86.         // position of the first column with photos
  87.         $first_image = 96;
  88.         // position of the last column with photos
  89.         $last_image = 104;
  90.         $imagesService->copyImagesFromFileWithExcelBasedNames($productsBDD, $nameExcelDocument, $name_place, $first_image, $last_image);
  91.         return $this->redirectToRoute('app_product');
  92.     }
  93.     //////////////////////////////////////////////////////////////////////
  94.     ///     Make a transfer of active products from BDD only to Dolibarr.
  95.     ///     (use "ini_set('max_execution_time', 0)" for exec w/o will stop after 120 sec).
  96.     //////////////////////////////////////////////////////////////////////
  97.     #[Route('/api/product/copyonlytodolibarr', name: 'api_copy_products_to_dolibarr', methods: ["GET"] )]
  98.     public function copyOnlyToDolibarr(
  99.         EntityManagerInterface $entityManager,
  100.         ProductsBDD            $getProductsBDD,
  101.         ProductsDolibarr       $productsDolibarr,
  102.         ProductRepository      $productRepository){
  103.         if (!$this->getUser()) {
  104.             return $this->redirectToRoute('app_product');
  105.         }
  106.         
  107.         ini_set('max_execution_time', 600); //limit time (in sec) of execution. 120 sec in standard, 0 = no limit
  108.         $limit = 50;
  109.         $offset = 0;
  110.         do {
  111.             // $products = $getProductsBDD->fetchActiveBdd($offset*$limit, $limit);
  112.             $products = $productRepository->getActiveProductsWithOffset($offset*$limit, $limit);
  113.             foreach ($products as $product) {
  114.                     $productsDolibarr->updateInfoInDolibarr($product);
  115.             }
  116.                 $offset++;
  117.         }while($offset <= 40);
  118.         
  119.     }
  120.     //////////////////////////////////////////////////////////////////////
  121.     ///         Copy images to Dolibarr
  122.     //////////////////////////////////////////////////////////////////////
  123.     #[Route('/copyImagesToDoblibbarr', name: 'app_add_image_to_dolibarr')]
  124.     public function addImageToDolibarrFromDb(EntityManagerInterface $entityManager,
  125.                                              ImagesService $imagesService)
  126.     {
  127.         ini_set('max_execution_time', 600);
  128.         $directory =  "./assets/img/upload/";
  129.         $quantity = 100;
  130.         $images = glob($directory . "*.*");
  131.         $images_in_db = $entityManager->getRepository(Images::class)->countImages();
  132.         $start = 0;
  133.         $stop = 4;
  134.         $nb_pages = round($images_in_db[0][1]/$quantity);
  135.         if($stop > $nb_pages){
  136.             $stop = $nb_pages;
  137.         }
  138.         if($start>$stop){
  139.             dd("END");
  140.         }
  141.         for ($i = $start; $i <$stop; $i++ ){
  142.             $response = $entityManager->getRepository(Images::class)->get100images($i*$quantity, $quantity);
  143.             foreach ($response as $image_in_db){
  144.                 $this_image_product_code_article = $image_in_db->getProduct()->getCodeArticle();
  145.                 foreach ($images as $image_upload){
  146.                     if($image_in_db->getName() == basename($image_upload)){
  147.                         $imagesService->saveImageInDolibarrFolder($image_upload, $this_image_product_code_article);
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.         return $this->redirectToRoute('app_product');
  153.     }
  154.     //////////////////////////////////////////////////////////////////////
  155.     ///Create new product in BDD and copy it to Dolibarr and Shopify.
  156.     //////////////////////////////////////////////////////////////////////
  157.     #[Route('/product/create', name: 'api_create_product')]
  158.     public function createNewProduct(
  159.         Request $request,
  160.         EntityManagerInterface $entityManager,
  161.         ProductRepository $productManager,
  162.         ProductsDolibarr       $productsDolibarr){
  163.         $user = $this->getUser();
  164.         if (!$user){
  165.             return $this->render('product/not_connected.html.twig');
  166.         }
  167.         $newProduct = new Product();
  168.         $form = $this->createForm(CreateProductType::class, $newProduct);
  169.         $form->handleRequest($request);
  170.         if ($form->isSubmitted() && $form->isValid()) {
  171.             $productManager->save($newProduct,true);
  172.             $images = $form->get('image')->getData();
  173.             $i = 0;
  174.             foreach ($images as $image){
  175.                 $fullName = $_FILES["create_product"]["name"]["image"][$i];
  176.                 $arrayName = explode(".",$fullName);
  177.                 $type = array_pop($arrayName);
  178.                 $name = $arrayName[0];
  179.                 $size = $_FILES["create_product"]["size"]["image"][$i];
  180.                 $content = file_get_contents($_FILES["create_product"]["tmp_name"]["image"][$i]);
  181.                 $createImage = new Images();
  182.                 $createImage->setName($name);
  183.                 $createImage->setType($type);
  184.                 $createImage->setSize($size);
  185.                 $createImage->setBin($content);
  186.                 $createImage->setProduct($newProduct);
  187.                 $entityManager->persist($createImage);
  188.                 $entityManager->flush();
  189.                 $i++;
  190.             }
  191.             $createdProduct = $productManager->getProductByCodeArticle($newProduct->getCodeArticle());
  192.             $images = $entityManager->getRepository(Images::class)->findAll();
  193.             $newProduct = $createdProduct[0];
  194.             $newProduct->setIdDolibarr($productsDolibarr->createInDolibarr($newProduct));
  195.             $warehouse = 10; //int nr of warehouse
  196.             $productsDolibarr->updateQtyInDolibarr($newProduct, $warehouse);
  197.             $productManager->save($newProduct,true);
  198.             return $this->redirectToRoute('app_product');
  199.         }
  200.         return $this->render('product/create_product.html.twig', [
  201.             'form' => $form->createView(),
  202.         ]);
  203.     }
  204.     //////////////////////////////////////////////////////////////////////
  205.     ///         Edit product in BDD and copy it to Dolibarr.
  206.     //////////////////////////////////////////////////////////////////////
  207.     #[Route('/product/{id}/edit', name: 'app_edit_product')]
  208.     public function editProduct(
  209.         int $id,
  210.         Request $request,
  211.         EntityManagerInterface $entityManager,
  212.         ProductRepository $productManager,
  213.         ProductsDolibarr       $productsDolibarr){
  214.         $user = $this->getUser();
  215.         if (!$user){
  216.             return $this->render('product/not_connected.html.twig');
  217.         }
  218.         $editProduct = $entityManager->getRepository(Product::class)->find($id);
  219.         $images = $entityManager->getRepository(Images::class)->findBy(['product'=>$id]);
  220.         if (!$editProduct) {
  221.             throw $this->createNotFoundException(
  222.                 'No product found for id '.$id
  223.             );
  224.         }
  225.         $form = $this->createForm(CreateProductType::class, $editProduct);
  226.         $form->handleRequest($request);
  227.         if ($form->isSubmitted() && $form->isValid()) {
  228.             $productManager->save($editProduct,true);
  229. // edit in dolibarr
  230.             $productsDolibarr->updateInfoInDolibarr($editProduct);
  231.             $images = $form->get('image')->getData();
  232.             $i = 0;
  233.             foreach ($images as $image){
  234.                 $fullName = $_FILES["create_product"]["name"]["image"][$i];
  235.                 $arrayName = explode(".",$fullName);
  236.                 $type = end($arrayName);
  237.                 $del = strlen($type);
  238.                 $name = substr($fullName, 0, -($del+1));
  239.                 $size = $_FILES["create_product"]["size"]["image"][$i];
  240.                 $content = file_get_contents($_FILES["create_product"]["tmp_name"]["image"][$i]);
  241.                 $createImage = new Images();
  242.                 $createImage->setName($name);
  243.                 $createImage->setType($type);
  244.                 $createImage->setSize($size);
  245.                 $createImage->setBin($content);
  246.                 $createImage->setProduct($editProduct);
  247.                 $entityManager->persist($createImage);
  248.                 $entityManager->flush();
  249.                 $i++;
  250.             }
  251.             return $this->redirectToRoute('app_product');
  252.         }
  253.         return $this->render('product/edit_product.html.twig', [
  254.             'form' => $form->createView(),
  255.             'id' => $id,
  256.             'images' => $images,
  257.         ]);
  258.     }
  259.     
  260.     
  261.     //////////////////////////////////////////////////////////////////////
  262.     ///     Make a update of active products from BDD only to Dolibarr.
  263.     ///     (use "ini_set('max_execution_time', 0)" for exec w/o will stop after 120 sec).
  264.     //////////////////////////////////////////////////////////////////////
  265.     #[Route('/api/product/updateindolibarr', name: 'api_update_products_in_dolibarr', methods: ["GET"] )]
  266.     public function updateInDolibarr(
  267.         ProductsDolibarr       $productsDolibarr,
  268.         ProductRepository      $productRepository){
  269. // dd("6");
  270.         if (!$this->getUser()) {
  271.             return $this->redirectToRoute('app_product');
  272.         }
  273.         ini_set('max_execution_time', 0); //limit time (in sec) of execution. 120 sec in standard, 0 = no limit
  274.         $limit = 50;
  275.         $offset = 100;
  276.         do {
  277.             $products = $productRepository->getActiveProductsWithOffset($offset*$limit, $limit);
  278.             foreach ($products as $product) {
  279.                 $productsDolibarr->updateInfoInDolibarr($product);
  280.             }
  281.             $offset++;
  282.         }while($offset <110);
  283. dd("6");
  284.         return $this->redirectToRoute('app_product');
  285.     }
  286.     //////////////////////////////////////////////////////////////////////
  287.     ///         Delete image from BDD
  288.     //////////////////////////////////////////////////////////////////////
  289.     #[Route('/product/{id}/image/{idImage}/delete', name: 'app_delete_image')]
  290.     public function deleteImage(
  291.         int $id,
  292.         int $idImage,
  293.         EntityManagerInterface $entityManager): Response
  294.     {
  295.         if (!$this->getUser()) {
  296.             return $this->render('product/not_connected.html.twig');
  297.         }
  298.         $image = $entityManager->getRepository(Images::class)->find($idImage);
  299.         if (!$image) {
  300.             throw $this->createNotFoundException(
  301.                 'No image found with id '.$idImage
  302.             );
  303.         }
  304.         $entityManager->remove($image);
  305.         $entityManager->flush();
  306.         return $this->redirectToRoute('app_edit_product', [
  307.             'id' => $id
  308.         ]);
  309.     }
  310. }