Esempio n. 1
1
 public function csvImport($csvFile, $entity, $user, $fileRecord)
 {
     // Create and configure the reader
     $headers = new ParagonArray();
     $file = new \SplFileObject($csvFile);
     $csvReader = new CsvReader($file);
     $csvReader->setHeaderRowNumber(0);
     $csvReader->setStrict(false);
     $csvReader->setColumnHeaders($headers->csvReaderArray());
     $workflow = new Workflow($csvReader);
     $doctrineWriter = new DoctrineWriter($this->em, $entity);
     $doctrineWriter->disableTruncate();
     $workflow->addWriter($doctrineWriter);
     $dateConverter = new DateConventer($fileRecord->getPlanDate());
     $weekNumberConverter = new WeekNumberConventer($fileRecord->getPlanDate());
     $planNameConverter = new PlanNameConventer($fileRecord->getName());
     $routeNameConventer = new RouteNameConventer($fileRecord->getPlanDate());
     $dateTimeNow = new DateTimeNow();
     $dateToMicroConventer = new TimeToMicroConventer();
     $timeConverter = new DateTimeValueConverter('H:i');
     $uploadedBy = new UploadedBy($user);
     $planDateConvert = new PlanDateConvert($fileRecord->getPlanDate());
     //\Doctrine\Common\Util\Debug::dump($date->format('Y-m-d'));
     //die;
     $date = $fileRecord->getPlanDate();
     $workflow->setSkipItemOnFailure(true)->addValueConverter('routeNo', $routeNameConventer)->addValueConverter('startTime', $dateConverter)->addValueConverter('sourceDepotDepartureTime', $dateConverter)->addValueConverter('arrivalTime', $dateConverter)->addValueConverter('departTime', $dateConverter)->addValueConverter('endDepotArrivalTime', $dateConverter)->addValueConverter('tripStart', $dateConverter)->addValueConverter('destDepotArrivalTime', $dateConverter)->addValueConverter('destDepotDepartureTime', $dateConverter)->addValueConverter('sourceDepotArrivalTime2', $dateConverter)->addValueConverter('sourceDepotDepartureTime2', $dateConverter)->addValueConverter('startDepotDepartureTime', $dateConverter)->addValueConverter('endTime', $dateConverter)->addValueConverter('callDuration', $dateToMicroConventer)->addValueConverter('dutyTime', $dateToMicroConventer)->addValueConverter('driveTime', $dateToMicroConventer)->addValueConverter('emptyTime', $dateToMicroConventer)->addValueConverter('timeWindowStart', $dateConverter)->addValueConverter('timeWindowEnd', $dateConverter)->addValueConverter('uploadDate', $dateTimeNow)->addValueConverter('uploadedBy', $uploadedBy)->addValueConverter('planDate', $planDateConvert)->addValueConverter('planName', $planNameConverter)->addValueConverter('weekNumber', $weekNumberConverter);
     //        \Doctrine\Common\Util\Debug::dump($doctrineWriter);
     //        die;
     $result = $workflow->process();
     //update query to generate route and trip combination
     $query = $this->em->createQuery("UPDATE NTPBundle:ParagonData p SET p.routeNo = CONCAT(p.routeNo,'0', p.tripNo) " . "WHERE p.planDate=:date")->setParameter('date', $date);
     $query->execute();
     return $result;
 }
Esempio n. 2
0
 public function getReader(\SplFileObject $file)
 {
     $reader = new CsvReader($file, $this->delimiter, $this->enclosure, $this->escape);
     if (null !== $this->headerRowNumber) {
         $reader->setHeaderRowNumber($this->headerRowNumber);
     }
     $reader->setStrict($this->strict);
     return $reader;
 }
Esempio n. 3
0
 /**
  * @param \SplFileObject $file
  * @param array          $data
  * @return array
  */
 public function getCsvHeader(\SplFileObject $file, &$data)
 {
     $csvReader = new CsvReader($file, $data['delimiter']);
     $csvReader->setHeaderRowNumber(0, 1);
     $csvReader->setStrict(false);
     $data['count'] = $csvReader->count();
     $data['header'] = $csvReader->getColumnHeaders();
     if ($csvReader->hasErrors() || count($csvReader->getFields()) <= 1) {
         $this->setError('error read file');
     } else {
         $row = $csvReader->getRow(1);
         foreach ($row as $key => $value) {
             $data['first'][$key] = $value;
         }
     }
     return $data;
 }
Esempio n. 4
0
 public function load()
 {
     $reader = new CsvReader($this->getFile(), $this->getOption('delimiter'), $this->getOption('enclosure'), $this->getOption('escape'));
     $headers = $this->getOption('headers');
     $headerRow = $this->getOption('header_row');
     if (false !== $headerRow) {
         $duplicateHeaders = $headers == 'merge' ? CsvReader::DUPLICATE_HEADERS_MERGE : CsvReader::DUPLICATE_HEADERS_INCREMENT;
         $reader->setHeaderRowNumber($headerRow, $duplicateHeaders);
     }
     if (is_array($headers)) {
         $reader->setColumnHeaders($headers);
     } elseif ('number' == $headers) {
         $headers = [];
         for ($i = 1; $i <= count($reader->getColumnHeaders()); ++$i) {
             $headers[] = 'field' . $i;
         }
         $reader->setColumnHeaders($headers);
     }
     $reader->setStrict($this->getOption('strict'));
     return $this->setReader($reader);
 }
 /**
  * {@inheritdoc}
  */
 public function processFile(\SplFileInfo $file, $replace = false)
 {
     $reader = new CsvReader($file->openFile());
     $reader->setStrict(false)->setHeaderRowNumber(0)->setColumnHeaders($this->getColumns());
     $workflow = $this->buildWorkflow($reader)->setSkipItemOnFailure(true);
     $this->result = $workflow->process();
 }
Esempio n. 6
0
 public function testMaximumNesting()
 {
     if (!function_exists('xdebug_is_enabled')) {
         $this->markTestSkipped('xDebug is not installed');
     }
     $xdebug_start = !xdebug_is_enabled();
     if ($xdebug_start) {
         xdebug_enable();
     }
     ini_set('xdebug.max_nesting_level', 200);
     $file = new \SplTempFileObject();
     for ($i = 0; $i < 500; $i++) {
         $file->fwrite("1,2,3\n");
     }
     $reader = new CsvReader($file);
     $reader->rewind();
     $reader->setStrict(true);
     $reader->setColumnHeaders(array('one', 'two'));
     $current = $reader->current();
     $this->assertEquals(null, $current);
     if ($xdebug_start) {
         xdebug_disable();
     }
 }
 private function formatToReader(Format $format, \SplFileInfo $file)
 {
     if ($format instanceof CsvFormat) {
         $reader = new CsvReader($file->openFile(), $format->getDelimiter(), $format->getEnclosure(), $format->getEscape());
         $reader->setStrict(false);
         if ($format->isHeaderInFirstRow()) {
             $reader->setHeaderRowNumber(0, CsvReader::DUPLICATE_HEADERS_MERGE);
             $reader->setColumnHeaders(array_map('trim', $reader->getColumnHeaders()));
             //TODO some header-collaborator?
         }
     } elseif ($format instanceof ExcelFormat) {
         $headerRowNumber = $format->isHeaderInFirstRow() ? 0 : null;
         $reader = new ExcelReader($file->openFile(), $headerRowNumber, $format->getActivesheet());
         if ($format->isHeaderInFirstRow()) {
             $reader->setColumnHeaders(array_map('trim', $reader->getColumnHeaders()));
             //TODO some header-collaborator?
         }
     } elseif ($format instanceof JsonFormat) {
         $array = json_decode(file_get_contents($file), true);
         $reader = new ArrayReader($array);
     } elseif ($format instanceof XmlFormat) {
         $reader = new XmlReader($file->openFile(), $format->getXpath());
     } elseif ($format instanceof CompressedFormat && $format->getSubFormat()) {
         $reader = $this->formatToReader($format->getSubFormat(), $format->getInsideStream($file));
     } else {
         throw new InvalidConfigurationException('Cannot build reader. Unknown format: ' . $format);
     }
     return $reader;
 }
Esempio n. 8
0
 /**
  * Import  from csv
  * using https://github.com/ddeboer/data-import
  *
  * @Route("/import/{filetype}/{entity}/{id}", name="import_upload")
  */
 public function importAction($id, $filetype, $entity)
 {
     $em = $this->getDoctrine()->getManager();
     // Create and configure the reader
     $upload = $em->getRepository('AppBundle:Upload')->find($id);
     $path = $this->get('kernel')->getRootDir() . '/../uploads/' . $upload->getName();
     $entity_namespace = 'AppBundle:' . $entity;
     $entity_redirect = $entity;
     $file = new \SplFileObject($path);
     $csvReader = new CsvReader($file);
     // Tell the reader that the first row in the CSV file contains column headers
     $csvReader->setHeaderRowNumber(0);
     $csvReader->setStrict(FALSE);
     // Create the workflow from the reader
     $workflow = new Workflow($csvReader);
     // Create a writer: you need Doctrine’s EntityManager.
     $doctrineWriter = new DoctrineWriter($em, $entity_namespace);
     $doctrineWriter->disableTruncate();
     $workflow->addWriter($doctrineWriter);
     // Process the workflow
     $workflow->process();
     return $this->redirect($this->generateUrl(strtolower($entity_redirect)));
 }
 /**
  * 添加管理者信息.
  *
  * @Route("/new", name="manager_new")
  * 
  * @Template("UserBundle:Manager:new.html.twig")
  */
 public function newAction(Request $request)
 {
     $manager = new Manager();
     $new_form = $this->createForm(new ManagerNewType(), $manager, array('action' => $this->generateUrl('manager_new'), 'method' => 'GET'));
     $new_form->handleRequest($request);
     if ($new_form->isValid()) {
         //添加成功跳转到列表页面,不成功跳转到本页面
         try {
             $em = $this->getDoctrine()->getManager();
             $manager->setEnabled(1);
             $success = $em->getRepository('UserBundle:Manager')->add($manager);
             if ($success) {
                 $this->addFlash('success', $manager->getName() . '添加成功');
             } else {
                 $this->addFlash('error', '网络原因或数据库故障,添加失败. 请重新尝试添加!');
                 return $this->redirect($this->generateUrl('manager_new'));
             }
             return $this->redirect($this->generateUrl('manager_index'));
         } catch (\Exception $e) {
             $this->addFlash('error', '网络原因或数据库故障,添加失败. 请重新尝试添加!');
             return $this->redirect($this->generateUrl('manager_new'));
         }
     }
     //批量导入管理员信息
     $import_form = $this->createFormBuilder()->setMethod('POST')->setAction($this->generateUrl('manager_new'))->add('fileUrl', 'file', array('label' => '文件位置:'))->add('import', 'submit', array('label' => '导入'))->add('cancel', 'reset', array('label' => '取消'))->getForm();
     $import_form->handleRequest($request);
     if ($import_form->isValid()) {
         if (!is_dir("upload/import/manager_import")) {
             mkdir("upload/import/manager_import");
         }
         $file = $import_form['fileUrl']->getData();
         $filename = explode(".", $file->getClientOriginalName());
         $extension = $filename[count($filename) - 1];
         $newefilename = $filename[0] . "_" . rand(1, 9999) . "." . $extension;
         $file->move("upload/import/manager_import", $newefilename);
         $upfile = new \SplFileObject("upload/import/manager_import/" . $newefilename);
         $csvReader = new CsvReader($upfile);
         $csvReader->setStrict(false)->setHeaderRowNumber(0)->setColumnHeaders(['username', 'name', 'email', 'telephone', 'roles' => 'FOS\\UserBundle\\Model\\User', 'password']);
         $em = $this->getDoctrine()->getManager();
         $doctrineWriter = new DoctrineWriter($em, 'UserBundle:Manager');
         $doctrineWriter->disableTruncate();
         $workflow = new Workflow($csvReader);
         $workflow->addWriter($doctrineWriter)->process();
     }
     return array('new_form' => $new_form->createView(), 'import_form' => $import_form->createView());
 }