<?php
namespace App\ImportBundle\Controller;
use Doctrs\SonataImportBundle\Entity\UploadFile;
use Doctrs\SonataImportBundle\Form\Type\UploadFileType;
use Pmag\ApiBundle\Entity\Product;
use Pmag\ApiBundle\Entity\ProductPrice;
use Pmag\ApiBundle\Entity\ProductSubFamily;
use Pmag\ApiBundle\Entity\Supplier;
use Pmag\ApiBundle\Util\PmagTools;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
/**
* UploadProdutsController
* @package App\ImportBundle\Controller
* @author Daly Ala <rafin_ala03@hotmail.fr>
**/
class UploadProductsController extends CRUDController
{
const BATCH_SIZE = 20;
public function __invoke(Request $request)
{
$fileEntity = new UploadFile();
$form = $this->createForm(UploadFileType::class, $fileEntity, [
'method' => 'POST'
]);
$subFamilies = $this->getDoctrine()->getRepository(ProductSubFamily::class)->findAll();
$form->add('categories', EntityType::class, [
'class' => ProductSubFamily::class,
'required' => true,
'mapped' => false,
'label' => 'Catégorie',
'choices' => $subFamilies
]);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
if (!$fileEntity->getFile()->getError()) {
$fileEntity->move($this->getParameter('doctrs_sonata_import.upload_dir'));
$data = [];
$this->getDoctrine()->getManager()->persist($fileEntity);
$this->getDoctrine()->getManager()->flush($fileEntity);
$filee = $this->getDoctrine()->getRepository('DoctrsSonataImportBundle:UploadFile')->find($fileEntity);
$extension = pathinfo($filee->getFile(), PATHINFO_EXTENSION);
$i = 0;
if ($extension === "txt") {
$this->getDoctrine()->getConnection()->getConfiguration()->setSQLLogger(null);
$category = $this->getDoctrine()->getRepository(ProductSubFamily::class)->findOneBy(['id' => $form->get('categories')->getData()->getId()]);
$data = PmagTools::csv_to_array($filee->getFile(), ',');
$supplier = $this->getDoctrine()->getRepository(Supplier::class)->findOneBy(array('title' => 'Pmag'));
foreach ($data as $value) {
$existingProduct = $this->getDoctrine()->getRepository(Product::class)->findOneBy(['code' => $value['code']]);
if ($existingProduct !== null) {
continue;
}
$product = new Product();
$product
->setActive(false)
->setName($value['name'])
->setCode($value['code'])
->setShortCode($value['shortCode'])
->setShortName($value['shortName'])
->setSupplier($supplier)
->setSubFamily($category)
->setActive(true)
->setIsVrac((boolean)$value['isVrac']);
$productPrice = new ProductPrice();
$productPrice->setProduct($product);
$productPrice->setSuggestedPrice((int)$value['price']);
$this->getDoctrine()->getManager()->persist($product);
$this->getDoctrine()->getManager()->persist($productPrice);
$i++;
if (($i % self::BATCH_SIZE) === 0) {
$this->getDoctrine()->getManager()->flush();
$this->getDoctrine()->getManager()->clear();
}
}
$this->getDoctrine()->getManager()->flush();
$this->getDoctrine()->getManager()->clear();
} else {
$form->get('file')->addError(new FormError('Unsupported extension '));
}
} else {
$form->get('file')->addError(new FormError($fileEntity->getFile()->getErrorMessage()));
}
}
}
$builder = $this->get('sonata.admin.pool')
->getInstance($this->admin->getCode())
->getExportFields();
return $this->render('@Import/Import/indexProducts.html.twig', [
'form' => $form->createView(),
'baseTemplate' => $this->getBaseTemplate(),
'builder' => $builder,
'action' => 'import',
]);
}
}