Skip to content

Commit

Permalink
Merge pull request #6 from eawd/fix-symfony-4
Browse files Browse the repository at this point in the history
Fix multiple bugs
  • Loading branch information
Doctrs committed Jul 29, 2018
2 parents 91bdc36 + ec0bd16 commit a2e38dc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Command/SonataImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected function getSetMethod($name, $method = 'set') {
protected function setValue($value, FormBuilderInterface $formBuilder, AbstractAdmin $admin) {

$mappings = $this->getContainer()->getParameter('doctrs_sonata_import.mappings');
$type = $formBuilder->getType()->getName();
$type = $formBuilder->getType();

/**
* Проверяем кастомные типы форм на наличие в конфиге.
Expand Down
44 changes: 27 additions & 17 deletions Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Doctrs\SonataImportBundle\Entity\UploadFile;
use Doctrs\SonataImportBundle\Form\Type\UploadFileType;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Process\Process;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends CRUDController {

Expand All @@ -26,7 +29,7 @@ public function indexAction(Request $request) {
]);
$form->handleRequest($request);

if ($form->isValid()) {
if ($form->isSubmitted() && $form->isValid()) {
if (!$fileEntity->getFile()->getError()) {
$fileEntity->move($this->getParameter('doctrs_sonata_import.upload_dir'));

Expand Down Expand Up @@ -58,12 +61,14 @@ public function indexAction(Request $request) {

/**
* @param Request $request
* @param UploadFile $uploadFile
* @param string $id
* @return JsonResponse|\Symfony\Component\HttpFoundation\Response
*/
public function uploadAction(Request $request, UploadFile $uploadFile) {
public function uploadAction(Request $request, $id) {
$em = $this->getDoctrine()->getManager();

$uploadFile = $em->getRepository('DoctrsSonataImportBundle:UploadFile')->find($id);

$countImport = $em->getRepository('DoctrsSonataImportBundle:ImportLog')->count([
'uploadFile' => $uploadFile->getId()
]);
Expand All @@ -84,10 +89,12 @@ public function uploadAction(Request $request, UploadFile $uploadFile) {


/**
* @param UploadFile $uploadFile
* @param string $id
* @return JsonResponse
*/
public function importStatusAction(UploadFile $uploadFile) {
public function importStatusAction($id) {
$uploadFile = $this->getDoctrine()->getManager()->getRepository('DoctrsSonataImportBundle:UploadFile')->find($id);

$countImport = $this->getDoctrine()->getManager()->getRepository('DoctrsSonataImportBundle:ImportLog')->count([
'uploadFile' => $uploadFile->getId()
]);
Expand Down Expand Up @@ -118,15 +125,18 @@ private function getLetterArray() {
* @param UploadFile $fileEntity
*/
private function runCommand(UploadFile $fileEntity) {
$command = sprintf(
'/usr/bin/php %s/console doctrs:sonata:import %d "%s" "%s" %d > /dev/null 2>&1 &',
$this->get('kernel')->getRootDir(),
$fileEntity->getId(),
$this->admin->getCode(),
$fileEntity->getEncode() ? $fileEntity->getEncode() : 'utf8',
$fileEntity->getLoaderClass()
);
$process = new Process($command);
$process->run();
$application = new Application($this->get('kernel'));
$application->setAutoExit(false);

$input = new ArrayInput(array(
'command' => 'doctrs:sonata:import',
'csv_file' => $fileEntity->getId(),
'admin_code' => $this->admin->getCode(),
'encode' => $fileEntity->getEncode() ? $fileEntity->getEncode() : 'utf8',
'file_loader' => $fileEntity->getLoaderClass(),
));

$output = new NullOutput();
$application->run($input, $output);
}
}
2 changes: 1 addition & 1 deletion Form/Type/UploadFileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$loader = [];
$loaders_list = $this->container->getParameter('doctrs_sonata_import.class_loaders');
foreach ($loaders_list as $key => $item) {
$loader[$key] = $item['name'];
$loader[$item['name']] = $key;
}
$builder->add('loaderClass', ChoiceType::class, [
'choices' => $loader,
Expand Down

0 comments on commit a2e38dc

Please sign in to comment.