<?php
namespace App\Controller;
use App\AppBundle\Service\ImagesService;
use App\AppBundle\Service\ProductsBDD;
use App\AppBundle\Service\ProductsDolibarr;
use App\Entity\Images;
use App\Entity\Product;
use App\Form\CreateProductType;
use App\Repository\ProductRepository;
use Doctrine\ORM\EntityManagerInterface;
use Intervention\Image\ImageManagerStatic;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ProductController extends AbstractController
{
//////////////////////////////////////////////////////////////////////
/// Show all active products
//////////////////////////////////////////////////////////////////////
#[Route('/product', name: 'app_product')]
public function showActiveProducts(Request $request, EntityManagerInterface $entityManager): Response
{
$user = $this->getUser();
if (!$user){
return $this->render('product/not_connected.html.twig');
}
$page = $request->query->getInt('page', 1);
$quantity = 100;
$imagesRepository = $entityManager->getRepository(Images::class);
$productsRepository = $entityManager->getRepository(Product::class);
$imagesCount = $imagesRepository->countImages();
$productsCount = $productsRepository->countProducts();
$nb_pages = ceil($productsCount[0][1]/$quantity);
$offset = ($page - 1) * $quantity;
$products = $productsRepository->getActiveProductsWithOffset($offset, $quantity);
$images = [];
foreach ($products as $product){
$newImages = $imagesRepository->getImagesOfProduct($product);
$images = array_merge($images, $newImages);
}
return $this->render('product/index.html.twig', [
'products' => $products,
'images' => $images,
'title' => "Active products",
'nb_pages' => $nb_pages,
]);
}
//////////////////////////////////////////////////////////////////////
/// Copy products to DB from Excel document
//////////////////////////////////////////////////////////////////////
#[Route('/copyProductsToDBFromExcel', name: 'app_add_products_to_db')]
public function addProductToDBFromExcel(ProductsBDD $productsBDD)
{
ini_set('max_execution_time', 300);
$productsBDD->copyProductsFromExcel();
return $this->redirectToRoute('app_product');
}
//////////////////////////////////////////////////////////////////////
/// Update products in DB from Excel document
//////////////////////////////////////////////////////////////////////
#[Route('/updateProductsInDBFromExcel', name: 'app_update_products_in_db')]
public function updateProductsInDBFromExcel(ProductsBDD $productsBDD)
{
ini_set('max_execution_time', 300);
$productsBDD->updateProductsFromExcel();
return $this->redirectToRoute('app_product');
}
//////////////////////////////////////////////////////////////////////
/// Add image from folder to BDD
/// a photo whose name is in the Excel file but is not in the "./assets/img/upload" folder will not be added!
//////////////////////////////////////////////////////////////////////
#[Route('/copyImagesFromFileWithExcelBasedNames', name: 'app_add_image_from_file')]
public function addImageFromFolder(EntityManagerInterface $entityManager,
ProductsBDD $productsBDD,
ImagesService $imagesService)
{
ini_set('max_execution_time', 600);
// enter the document name and its extension (only xlsx for now)
$nameExcelDocument = 'plik.xlsx';
// Columns count start at 0!
//
$name_place = 1;
// position of the first column with photos
$first_image = 96;
// position of the last column with photos
$last_image = 104;
$imagesService->copyImagesFromFileWithExcelBasedNames($productsBDD, $nameExcelDocument, $name_place, $first_image, $last_image);
return $this->redirectToRoute('app_product');
}
//////////////////////////////////////////////////////////////////////
/// Make a transfer of active products from BDD only to Dolibarr.
/// (use "ini_set('max_execution_time', 0)" for exec w/o will stop after 120 sec).
//////////////////////////////////////////////////////////////////////
#[Route('/api/product/copyonlytodolibarr', name: 'api_copy_products_to_dolibarr', methods: ["GET"] )]
public function copyOnlyToDolibarr(
EntityManagerInterface $entityManager,
ProductsBDD $getProductsBDD,
ProductsDolibarr $productsDolibarr,
ProductRepository $productRepository){
if (!$this->getUser()) {
return $this->redirectToRoute('app_product');
}
ini_set('max_execution_time', 600); //limit time (in sec) of execution. 120 sec in standard, 0 = no limit
$limit = 50;
$offset = 0;
do {
// $products = $getProductsBDD->fetchActiveBdd($offset*$limit, $limit);
$products = $productRepository->getActiveProductsWithOffset($offset*$limit, $limit);
foreach ($products as $product) {
$productsDolibarr->updateInfoInDolibarr($product);
}
$offset++;
}while($offset <= 40);
}
//////////////////////////////////////////////////////////////////////
/// Copy images to Dolibarr
//////////////////////////////////////////////////////////////////////
#[Route('/copyImagesToDoblibbarr', name: 'app_add_image_to_dolibarr')]
public function addImageToDolibarrFromDb(EntityManagerInterface $entityManager,
ImagesService $imagesService)
{
ini_set('max_execution_time', 600);
$directory = "./assets/img/upload/";
$quantity = 100;
$images = glob($directory . "*.*");
$images_in_db = $entityManager->getRepository(Images::class)->countImages();
$start = 0;
$stop = 4;
$nb_pages = round($images_in_db[0][1]/$quantity);
if($stop > $nb_pages){
$stop = $nb_pages;
}
if($start>$stop){
dd("END");
}
for ($i = $start; $i <$stop; $i++ ){
$response = $entityManager->getRepository(Images::class)->get100images($i*$quantity, $quantity);
foreach ($response as $image_in_db){
$this_image_product_code_article = $image_in_db->getProduct()->getCodeArticle();
foreach ($images as $image_upload){
if($image_in_db->getName() == basename($image_upload)){
$imagesService->saveImageInDolibarrFolder($image_upload, $this_image_product_code_article);
}
}
}
}
return $this->redirectToRoute('app_product');
}
//////////////////////////////////////////////////////////////////////
///Create new product in BDD and copy it to Dolibarr and Shopify.
//////////////////////////////////////////////////////////////////////
#[Route('/product/create', name: 'api_create_product')]
public function createNewProduct(
Request $request,
EntityManagerInterface $entityManager,
ProductRepository $productManager,
ProductsDolibarr $productsDolibarr){
$user = $this->getUser();
if (!$user){
return $this->render('product/not_connected.html.twig');
}
$newProduct = new Product();
$form = $this->createForm(CreateProductType::class, $newProduct);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$productManager->save($newProduct,true);
$images = $form->get('image')->getData();
$i = 0;
foreach ($images as $image){
$fullName = $_FILES["create_product"]["name"]["image"][$i];
$arrayName = explode(".",$fullName);
$type = array_pop($arrayName);
$name = $arrayName[0];
$size = $_FILES["create_product"]["size"]["image"][$i];
$content = file_get_contents($_FILES["create_product"]["tmp_name"]["image"][$i]);
$createImage = new Images();
$createImage->setName($name);
$createImage->setType($type);
$createImage->setSize($size);
$createImage->setBin($content);
$createImage->setProduct($newProduct);
$entityManager->persist($createImage);
$entityManager->flush();
$i++;
}
$createdProduct = $productManager->getProductByCodeArticle($newProduct->getCodeArticle());
$images = $entityManager->getRepository(Images::class)->findAll();
$newProduct = $createdProduct[0];
$newProduct->setIdDolibarr($productsDolibarr->createInDolibarr($newProduct));
$warehouse = 10; //int nr of warehouse
$productsDolibarr->updateQtyInDolibarr($newProduct, $warehouse);
$productManager->save($newProduct,true);
return $this->redirectToRoute('app_product');
}
return $this->render('product/create_product.html.twig', [
'form' => $form->createView(),
]);
}
//////////////////////////////////////////////////////////////////////
/// Edit product in BDD and copy it to Dolibarr.
//////////////////////////////////////////////////////////////////////
#[Route('/product/{id}/edit', name: 'app_edit_product')]
public function editProduct(
int $id,
Request $request,
EntityManagerInterface $entityManager,
ProductRepository $productManager,
ProductsDolibarr $productsDolibarr){
$user = $this->getUser();
if (!$user){
return $this->render('product/not_connected.html.twig');
}
$editProduct = $entityManager->getRepository(Product::class)->find($id);
$images = $entityManager->getRepository(Images::class)->findBy(['product'=>$id]);
if (!$editProduct) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
$form = $this->createForm(CreateProductType::class, $editProduct);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$productManager->save($editProduct,true);
// edit in dolibarr
$productsDolibarr->updateInfoInDolibarr($editProduct);
$images = $form->get('image')->getData();
$i = 0;
foreach ($images as $image){
$fullName = $_FILES["create_product"]["name"]["image"][$i];
$arrayName = explode(".",$fullName);
$type = end($arrayName);
$del = strlen($type);
$name = substr($fullName, 0, -($del+1));
$size = $_FILES["create_product"]["size"]["image"][$i];
$content = file_get_contents($_FILES["create_product"]["tmp_name"]["image"][$i]);
$createImage = new Images();
$createImage->setName($name);
$createImage->setType($type);
$createImage->setSize($size);
$createImage->setBin($content);
$createImage->setProduct($editProduct);
$entityManager->persist($createImage);
$entityManager->flush();
$i++;
}
return $this->redirectToRoute('app_product');
}
return $this->render('product/edit_product.html.twig', [
'form' => $form->createView(),
'id' => $id,
'images' => $images,
]);
}
//////////////////////////////////////////////////////////////////////
/// Make a update of active products from BDD only to Dolibarr.
/// (use "ini_set('max_execution_time', 0)" for exec w/o will stop after 120 sec).
//////////////////////////////////////////////////////////////////////
#[Route('/api/product/updateindolibarr', name: 'api_update_products_in_dolibarr', methods: ["GET"] )]
public function updateInDolibarr(
ProductsDolibarr $productsDolibarr,
ProductRepository $productRepository){
// dd("6");
if (!$this->getUser()) {
return $this->redirectToRoute('app_product');
}
ini_set('max_execution_time', 0); //limit time (in sec) of execution. 120 sec in standard, 0 = no limit
$limit = 50;
$offset = 100;
do {
$products = $productRepository->getActiveProductsWithOffset($offset*$limit, $limit);
foreach ($products as $product) {
$productsDolibarr->updateInfoInDolibarr($product);
}
$offset++;
}while($offset <110);
dd("6");
return $this->redirectToRoute('app_product');
}
//////////////////////////////////////////////////////////////////////
/// Delete image from BDD
//////////////////////////////////////////////////////////////////////
#[Route('/product/{id}/image/{idImage}/delete', name: 'app_delete_image')]
public function deleteImage(
int $id,
int $idImage,
EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->render('product/not_connected.html.twig');
}
$image = $entityManager->getRepository(Images::class)->find($idImage);
if (!$image) {
throw $this->createNotFoundException(
'No image found with id '.$idImage
);
}
$entityManager->remove($image);
$entityManager->flush();
return $this->redirectToRoute('app_edit_product', [
'id' => $id
]);
}
}