/**
  * {@inheritdoc}
  */
 public function importAction()
 {
     set_time_limit(1200);
     $this->getLockingAccessDenied();
     $import_counts = array();
     $messages = array();
     if (!empty($_FILES) && !empty($_FILES["inputFile"])) {
         $file = TMP_UPLOAD_PATH . '/' . $_FILES["inputFile"]["name"];
         $tmpFile = $_FILES["inputFile"]["tmp_name"];
         $inputFile = $_FILES['inputFile'];
         $error_counts = 0;
         if ($this->importFileValidate($inputFile)) {
             if (move_uploaded_file($tmpFile, $file)) {
                 $objReader = \PHPExcel_IOFactory::createReaderForFile($file);
                 if (get_class($objReader) == 'PHPExcel_Reader_CSV') {
                     unset($objReader);
                     $this->get('session')->setFlash('sonata_flash_error', $this->admin->trans('Fichier non lisible'));
                     return $this->render(':redirects:back.html.twig');
                 }
                 $_FILES = array();
                 //create fake process id
                 $pid = time();
                 $user = $this->get('security.context')->getToken()->getUser();
                 $kernel = $this->get('kernel');
                 $command = 'php ' . $kernel->getRootDir() . '/console clientoperationsbundle:import:excel ' . $user->getId() . ' ' . $this->client_id . ' application.sonata.admin.v01tva ' . $file . ' ' . $inputFile['name'] . ' ' . $this->getLocking() . ' ' . $this->_year . ' ' . $this->_month . ' ' . $pid . ' --env=' . $kernel->getEnvironment() . ' --no-debug ';
                 /* var_dump($command);
                   	exit; */
                 $process = new \Symfony\Component\Process\Process($command);
                 $process->setTimeout(3600);
                 $process->start();
                 $start = microtime(true);
                 while ($process->isRunning()) {
                     $total = microtime(true) - $start;
                     if ($total / 60 >= 2) {
                         // if process is too long (2 minutes)
                         //var_dump(($total/60));
                         $em = $this->getDoctrine()->getManager();
                         $importNotif = new ImportNotification();
                         $importNotif->setPid($pid)->setUser($user)->setClientId($this->client_id);
                         $em->persist($importNotif);
                         $em->flush();
                         $this->_hasImportErrors = true;
                         $this->get('session')->setFlash('sonata_flash_error', $this->admin->trans('There are too many data to be processed. We will just notify you once it\'s done. Check your email (' . $user->getEmail() . ') within few hours.'));
                         break;
                         return $this->render(':redirects:back.html.twig');
                     }
                 }
                 $output = unserialize($process->getOutput());
                 $messages = $output['messages'];
                 $import_counts = $output['import_counts'];
                 $error_counts = $output['error_counts'];
             }
         }
     }
     if (!empty($messages) || $error_counts) {
         $this->get('session')->setFlash('sonata_flash_info|raw', implode("<br/>", $messages));
     } else {
         $message = trim($this->get('session')->getFlash('sonata_flash_info|raw'));
         if ($message == '') {
             $this->get('session')->setFlash('sonata_flash_info|raw', $this->admin->trans('Imported : %count%', array('%count%' => 0)));
         } else {
             $this->get('session')->setFlash('sonata_flash_info|raw', $message);
         }
     }
     if (isset($import_counts['rows']['errors']) && !empty($import_counts['rows']['errors']) || $this->_hasImportErrors) {
         return $this->render(':redirects:back.html.twig');
     } else {
         return $this->redirect($this->generateUrl('rapprochement_index', array('client_id' => $this->client_id, 'month' => $this->_query_month, 'fromImport' => 1)));
     }
 }
 public function clientAction()
 {
     set_time_limit(0);
     //ini_set('max_execution_time', 0);
     //ini_set('memory_limit', '256M');
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         if (!empty($_FILES) && !empty($_FILES["inputFile"]["name"])) {
             $file = TMP_UPLOAD_PATH . '/' . str_replace(array(' ', '+', '(', ')'), '', $_FILES["inputFile"]["name"]);
             $tmpFile = $_FILES["inputFile"]["tmp_name"];
             $inputFile = $_FILES['inputFile'];
             if (move_uploaded_file($tmpFile, $file)) {
                 /* @var $objReader \PHPExcel_Reader_Excel2007 */
                 $objReader = \PHPExcel_IOFactory::createReaderForFile($file);
                 if (get_class($objReader) == 'PHPExcel_Reader_CSV') {
                     $this->get('session')->setFlash('sonata_flash_error', $this->admin->trans('Fichier non lisible'));
                     return $this->render(':redirects:back.html.twig');
                 }
                 $_FILES = array();
                 //create fake process id
                 $pid = time();
                 $user = $this->get('security.context')->getToken()->getUser();
                 $kernel = $this->get('kernel');
                 $command = 'php ' . $kernel->getRootDir() . '/console clientbundle:import:initial client ' . $user->getId() . ' ' . realpath($file) . ' ' . $pid . ' --env=' . $kernel->getEnvironment();
                 // . ' --no-debug ';
                 /* var_dump($command);
                 			exit; */
                 $process = new \Symfony\Component\Process\Process($command);
                 $process->setTimeout(3600);
                 $process->start();
                 $start = microtime(true);
                 while ($process->isRunning()) {
                     $total = microtime(true) - $start;
                     if ($total / 60 >= 2) {
                         // if process is too long (2 minutes)
                         //var_dump(($total/60));
                         $em = $this->getDoctrine()->getManager();
                         $importNotif = new ImportNotification();
                         $importNotif->setPid($pid)->setUser($user)->setClientId(0);
                         $em->persist($importNotif);
                         $em->flush();
                         $this->_hasImportErrors = true;
                         $this->get('session')->setFlash('sonata_flash_error', \AppKernel::getStaticContainer()->get('translator')->trans('There are too many data to be processed. We will just notify you once it\'s done. Check your email (' . $user->getEmail() . ') within few hours.'));
                         break;
                         return $this->render(':redirects:back.html.twig');
                     }
                 }
                 $output = unserialize($process->getOutput());
                 $messages = $output['messages'];
                 $import_counts = $output['import_counts'];
             }
         } else {
             $this->get('session')->setFlash('sonata_flash_error|raw', 'Please upload a file');
         }
         //$messages = $this->getCountMessageImports();
         if (!empty($messages)) {
             $this->get('session')->setFlash('sonata_flash_info|raw', implode("<br />", $messages));
         }
         return $this->render(':redirects:back.html.twig');
     }
     return $this->render('ApplicationSonataClientBundle:InitialImport:client.html.twig');
 }