src/ImportBundle/Controller/UploadProductsController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\ImportBundle\Controller;
  3. use Doctrs\SonataImportBundle\Entity\UploadFile;
  4. use Doctrs\SonataImportBundle\Form\Type\UploadFileType;
  5. use Pmag\ApiBundle\Entity\Product;
  6. use Pmag\ApiBundle\Entity\ProductPrice;
  7. use Pmag\ApiBundle\Entity\ProductSubFamily;
  8. use Pmag\ApiBundle\Entity\Supplier;
  9. use Pmag\ApiBundle\Util\PmagTools;
  10. use Sonata\AdminBundle\Controller\CRUDController;
  11. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Symfony\Component\Form\FormError;
  14. use Symfony\Component\HttpFoundation\Request;
  15. /**
  16.  * UploadProdutsController
  17.  * @package App\ImportBundle\Controller
  18.  * @author Daly Ala <rafin_ala03@hotmail.fr>
  19.  **/
  20. class UploadProductsController extends CRUDController
  21. {
  22.     const BATCH_SIZE 20;
  23.     public function __invoke(Request $request)
  24.     {
  25.         $fileEntity = new UploadFile();
  26.         $form $this->createForm(UploadFileType::class, $fileEntity, [
  27.             'method' => 'POST'
  28.         ]);
  29.         $subFamilies $this->getDoctrine()->getRepository(ProductSubFamily::class)->findAll();
  30.         $form->add('categories'EntityType::class, [
  31.             'class' => ProductSubFamily::class,
  32.             'required' => true,
  33.             'mapped' => false,
  34.             'label' => 'Catégorie',
  35.             'choices' => $subFamilies
  36.         ]);
  37.         $form->handleRequest($request);
  38.         if ($form->isSubmitted()) {
  39.             if ($form->isValid()) {
  40.                 if (!$fileEntity->getFile()->getError()) {
  41.                     $fileEntity->move($this->getParameter('doctrs_sonata_import.upload_dir'));
  42.                     $data = [];
  43.                     $this->getDoctrine()->getManager()->persist($fileEntity);
  44.                     $this->getDoctrine()->getManager()->flush($fileEntity);
  45.                     $filee $this->getDoctrine()->getRepository('DoctrsSonataImportBundle:UploadFile')->find($fileEntity);
  46.                     $extension pathinfo($filee->getFile(), PATHINFO_EXTENSION);
  47.                     $i 0;
  48.                     if ($extension === "txt") {
  49.                         $this->getDoctrine()->getConnection()->getConfiguration()->setSQLLogger(null);
  50.                         $category $this->getDoctrine()->getRepository(ProductSubFamily::class)->findOneBy(['id' => $form->get('categories')->getData()->getId()]);
  51.                         $data PmagTools::csv_to_array($filee->getFile(), ',');
  52.                         $supplier $this->getDoctrine()->getRepository(Supplier::class)->findOneBy(array('title' => 'Pmag'));
  53.                         foreach ($data as $value) {
  54.                             $existingProduct $this->getDoctrine()->getRepository(Product::class)->findOneBy(['code' => $value['code']]);
  55.                             if ($existingProduct !== null) {
  56.                                 continue;
  57.                             }
  58.                             $product = new Product();
  59.                             $product
  60.                                 ->setActive(false)
  61.                                 ->setName($value['name'])
  62.                                 ->setCode($value['code'])
  63.                                 ->setShortCode($value['shortCode'])
  64.                                 ->setShortName($value['shortName'])
  65.                                 ->setSupplier($supplier)
  66.                                 ->setSubFamily($category)
  67.                                 ->setActive(true)
  68.                                 ->setIsVrac((boolean)$value['isVrac']);
  69.                             $productPrice = new ProductPrice();
  70.                             $productPrice->setProduct($product);
  71.                             $productPrice->setSuggestedPrice((int)$value['price']);
  72.                             $this->getDoctrine()->getManager()->persist($product);
  73.                             $this->getDoctrine()->getManager()->persist($productPrice);
  74.                             $i++;
  75.                             if (($i self::BATCH_SIZE) === 0) {
  76.                                 $this->getDoctrine()->getManager()->flush();
  77.                                 $this->getDoctrine()->getManager()->clear();
  78.                             }
  79.                         }
  80.                         $this->getDoctrine()->getManager()->flush();
  81.                         $this->getDoctrine()->getManager()->clear();
  82.                     } else {
  83.                         $form->get('file')->addError(new FormError('Unsupported extension '));
  84.                     }
  85.                 } else {
  86.                     $form->get('file')->addError(new FormError($fileEntity->getFile()->getErrorMessage()));
  87.                 }
  88.             }
  89.         }
  90.         $builder $this->get('sonata.admin.pool')
  91.             ->getInstance($this->admin->getCode())
  92.             ->getExportFields();
  93.         return $this->render('@Import/Import/indexProducts.html.twig', [
  94.             'form' => $form->createView(),
  95.             'baseTemplate' => $this->getBaseTemplate(),
  96.             'builder' => $builder,
  97.             'action' => 'import',
  98.         ]);
  99.     }
  100. }