/** * Import action * * @return Response * * @throws AccessDeniedException If access is not granted */ public function importAction(Request $request) { if (false === $this->admin->isGranted('IMPORT')) { throw new AccessDeniedException(); } $defaultData = array(); $form = $this->createFormBuilder($defaultData)->add('file', 'file')->getForm(); $form->handleRequest($request); $data = array(); if ($form->isValid()) { $fs = new Filesystem(); try { $fs->mkdir('/tmp/import'); } catch (IOExceptionInterface $e) { echo "An error occurred while creating your directory at " . $e->getPath(); } $form['file']->getData()->move('/tmp/import', 'data.csv'); $file = new \SplFileObject('/tmp/import/data.csv'); // this must be done to import CSVs where one of the data-field has CRs within! $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_AHEAD); $reader = new Reader\CsvReader($file, ';'); $reader->setHeaderRowNumber(0); if ($request->request->has('btn_preview')) { foreach ($reader as $row) { $data[] = $row; } } else { // Set Database into "nonchecking Foreign Keys" $em = $this->getDoctrine()->getManager(); $em->getConnection()->exec("SET FOREIGN_KEY_CHECKS=0;"); $writer = new DoctrineWriter($em, "AppBundle:Partner"); $writer->setTruncate(false); $metadata = $em->getClassMetaData("AppBundle:Partner"); $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE); $mapping = new Step\MappingStep(); $mapping->map('[country_code]', '[country]'); $mapping->map('[category]', '[partnerCategory]'); $converter = new StringToObjectConverter($em->getRepository("AppBundle:PartnerCategory"), 'name'); $valueConverterStep = new Step\ValueConverterStep(); $valueConverterStep->add('[partnerCategory]', $converter); // $converter = new CustomValueConverter(); // $converterStep = new Step\ConverterStep(); // $converterStep->add($converter); $workflow = new Workflow\StepAggregator($reader); $workflow->addStep($mapping); $workflow->addStep($valueConverterStep); // $workflow->addStep($converterStep); $workflow->addWriter($writer); $workflow->process(); // Resetting Database Check Status $em->getConnection()->exec("SET FOREIGN_KEY_CHECKS=1;"); } } $formView = $form->createView(); // set the theme for the current Admin Form $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme()); return $this->render('CRUD/import_partner.html.twig', array('action' => 'import', 'form' => $formView, 'data' => $data)); }
public function testWorkflowWithObjects() { $reader = new ArrayReader(array(new Dummy('foo'), new Dummy('bar'), new Dummy('foobar'))); $data = array(); $writer = new ArrayWriter($data); $workflow = new StepAggregator($reader); $workflow->addWriter($writer); $converterStep = new ConverterStep([function ($item) { return array('name' => $item->name); }]); $valueStep = new ValueConverterStep(); $valueStep->add('[name]', function ($name) { return strrev($name); }); $workflow->addStep($converterStep)->addStep($valueStep); $workflow->process(); $this->assertEquals(array(array('name' => 'oof'), array('name' => 'rab'), array('name' => 'raboof')), $data); }
/** * ImportProcess action * * @param $request * @return Response * * @throws AccessDeniedException If access is not granted */ public function importProcessAction(Request $request) { if (false === $this->admin->isGranted('IMPORT')) { throw new AccessDeniedException(); } $file = new \SplFileObject('/tmp/import/product_import.csv'); // this must be done to import CSVs where one of the data-field has CRs within! $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_AHEAD); $reader = new Reader\CsvReader($file, ';'); $reader->setHeaderRowNumber(0); $choices = array(null => 'Nicht importieren', 'id' => 'Id', 'productNumber' => 'Artikelnummer', 'productCategory' => 'Artikelkategorie', 'translations.name' => 'Name', 'translations.description' => 'Beschreibung', 'memo' => 'Memo', 'enabled' => 'Aktiv', 'basePrice' => 'Basispreis', 'unitOfQuantity' => 'Mengeneinheit'); $defaultData = array(); $builder = $this->createFormBuilder($defaultData); $builder->add('language', ChoiceType::class, array('choices' => array('de' => 'Deutsch', 'en' => 'Englisch', 'fr' => 'Franzoesisch', 'it' => 'Italienisch'))); $builder->add('onlyUpdate', CheckboxType::class, array('label' => 'Nur Update, keine neuen Saetze importieren', 'required' => false)); $headers = array(); foreach ($reader->getColumnHeaders() as $header) { preg_match_all('/\\((.*?)\\)/', $header, $out); $header = $out[1] ? $out[1][0] : $header; $headers[] = $header; $builder->add($header, 'choice', array('required' => false, 'label' => false, 'choices' => $choices)); } $form = $builder->getForm(); $form->handleRequest($request); $data = array(); foreach ($reader as $row) { $data[] = $row; } $mapping = null; if ($form->isValid()) { if ($request->request->has('btn_confirm')) { $formData = $form->getData(); // Set Database into "nonchecking Foreign Keys" $em = $this->getDoctrine()->getManager(); $em->getConnection()->exec("SET FOREIGN_KEY_CHECKS=0;"); $writer = new DoctrineWriter($em, "AppBundle:Product", array('productNumber'), $formData['language']); $writer->setTruncate(false); $metadata = $em->getClassMetaData("AppBundle:Product"); $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE); // ************************************** // Excel Verwurschtelt die Artikelnummer. // ************************************** $filter = new Step\ConverterStep(); $filter->add(function ($v) { unset($v['--NONE--']); if (array_key_exists('id', $v)) { $v['id'] = intval($v['id']); } return $v; }); $filter->add(function ($v) { if (array_key_exists('basePrice', $v)) { $v['basePrice'] = intval($v['basePrice']); } return $v; }); $filter->add(function ($v) { if (!array_key_exists('enabled', $v)) { $v['enabled'] = true; } return $v; }); $mapping = new Step\MappingStep(); foreach ($headers as $header) { $newMap = $formData[$header] ? $formData[$header] : '--NONE--'; $mapping->map('[' . $header . ']', '[' . $newMap . ']'); } $converter = new Step\ValueConverterStep(); $converter->add('[productCategory]', function ($id) { $repository = $this->getDoctrine()->getRepository('AppBundle:ProductCategory'); $category = $repository->findOneById($id); return $category; }); $converter->add('[unitOfQuantity]', function ($id) { $repository = $this->getDoctrine()->getRepository('AppBundle:UnitOfQuantity'); $uoq = $repository->findOneById($id); return $uoq; }); $workflow = new Workflow\StepAggregator($reader); $workflow->addStep($mapping); $workflow->addStep($filter); $workflow->addStep($converter); $workflow->addWriter($writer); $workflow->process(); // Resetting Database Check Status $em->getConnection()->exec("SET FOREIGN_KEY_CHECKS=1;"); return $this->render('CRUD/import_product_category_finish.html.twig', array('action' => 'import')); } } $formView = $form->createView(); // set the theme for the current Admin Form $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme()); return $this->render('CRUD/import_product_category_process.html.twig', array('action' => 'import', 'form' => $formView, 'data' => $data, 'headers' => $headers)); }