예제 #1
0
 /**
  *
  * @return \Symfony\Component\HttpFoundation\RedirectResponse
  */
 public function importPostAction()
 {
     if (!$this->hasRole('ROLE_SUPERADMIN')) {
         return $this->redirect($this->generateUrl('_admin_homepage'));
     }
     $urlFrom = $this->getReferer();
     if (null == $urlFrom || trim($urlFrom) == '') {
         $urlFrom = $this->generateUrl('_admin_bulletinInfo_list');
     }
     $bulletinInfoImportForm = $this->createForm(BulletinInfoImportTForm::class);
     $request = $this->getRequest();
     $reqData = $request->request->all();
     if (isset($reqData['BulletinInfoImportForm'])) {
         $bulletinInfoImportForm->handleRequest($request);
         if ($bulletinInfoImportForm->isValid()) {
             ini_set('memory_limit', '4096M');
             ini_set('max_execution_time', '0');
             $extension = $bulletinInfoImportForm['excel']->getData()->guessExtension();
             if ($extension == 'zip') {
                 $extension = 'xlsx';
             }
             $filename = uniqid() . '.' . $extension;
             $bulletinInfoImportForm['excel']->getData()->move($this->getParameter('adapter_files'), $filename);
             $fullfilename = $this->getParameter('adapter_files');
             $fullfilename .= '/' . $filename;
             $excelObj = $this->get('phpexcel')->createPHPExcelObject($fullfilename);
             $log = '';
             $iterator = $excelObj->getWorksheetIterator();
             $activeSheetIndex = -1;
             $i = 0;
             $bulletinInfo = new BulletinInfo();
             foreach ($iterator as $worksheet) {
                 $worksheetTitle = $worksheet->getTitle();
                 $highestRow = $worksheet->getHighestRow();
                 // e.g. 10
                 $highestColumn = $worksheet->getHighestColumn();
                 // e.g 'F'
                 $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
                 $log .= "Feuille : '" . $worksheetTitle . "' trouvée contenant " . $highestRow . ' lignes et ' . $highestColumnIndex . ' colonnes avec comme plus grand index ' . $highestColumn . ' <br>';
                 if (\trim($worksheetTitle) == 'BulletinInfo') {
                     $activeSheetIndex = $i;
                 }
                 $i++;
             }
             if ($activeSheetIndex == -1) {
                 $log .= "Aucune Feuille de Titre 'BulletinInfo' trouvée tentative d'import depuis le première Feuille<br>";
                 $activeSheetIndex = 0;
             }
             $excelObj->setActiveSheetIndex($activeSheetIndex);
             $worksheet = $excelObj->getActiveSheet();
             $highestRow = $worksheet->getHighestRow();
             $lineRead = 0;
             $lineUnprocessed = 0;
             $bulletinInfoTitleNew = 0;
             $bulletinInfoContentNew = 0;
             $lineError = 0;
             $haserror = false;
             if ($highestRow < 3) {
                 $log .= 'Fichier Excel Invalide<br>';
                 $haserror = true;
             } else {
                 $lineRead++;
                 $num = \trim(\intval($worksheet->getCellByColumnAndRow(8, 3)->getValue()));
                 $dtStart = \PHPExcel_Shared_Date::ExcelToPHPObject($worksheet->getCellByColumnAndRow(10, 3)->getValue());
                 if ($num <= 0) {
                     $log .= 'Numéro de bulletin illisible (' . $num . ')<br>';
                     $haserror = true;
                     $lineError++;
                 }
                 if (!$dtStart instanceof \DateTime) {
                     $log .= 'Date du bulletin illisible (' . $dtStart . ')<br>';
                     $haserror = true;
                     $lineError++;
                 }
             }
             if (!$haserror) {
                 $em = $this->getEntityManager();
                 $bulletinInfoTest = $em->getRepository('AcfDataBundle:BulletinInfo')->findOneBy(array('num' => $num));
                 if (null != $bulletinInfoTest) {
                     $log .= 'Numéro de bulletin déjà existant<br>';
                     $haserror = true;
                 }
             }
             if (!$haserror) {
                 $bulletinInfo->setNum($num);
                 $bulletinInfo->setDtStart($dtStart);
                 $em->persist($bulletinInfo);
                 $lineRead = 5;
                 $isTitle = false;
                 $isContent = false;
                 $titles = array();
                 // $logger = $this->getLogger();
                 for ($row = $lineRead - 1; $row <= $highestRow; $row++) {
                     $isTitle = false;
                     $isContent = false;
                     $canBeTitle = false;
                     $canBeContent = false;
                     try {
                         $testNum = \trim(\strval($worksheet->getCellByColumnAndRow(1, $row)->getValue()));
                         if ($testNum != '') {
                             if (\stripos($testNum, '-')) {
                                 $canBeContent = true;
                             } elseif (\intval($testNum) > 0) {
                                 $canBeTitle = true;
                             }
                         }
                         if ($canBeTitle) {
                             if (!\array_key_exists($testNum, $titles)) {
                                 $isTitle = true;
                             } else {
                                 $lineError++;
                                 $log .= 'Titre : Numéro de Titre ' . $testNum . ' à  la ligne  ' . $lineRead . ' existe déja<br>';
                             }
                         }
                         if ($canBeContent) {
                             $titleContent = \explode('-', $testNum);
                             if (\count($titleContent) == 2) {
                                 $titleNum = \intval($titleContent[0]);
                                 $contentNum = \intval($titleContent[1]);
                                 if ($titleNum > 0 && $titleContent > 0) {
                                     if (!\array_key_exists($titleNum, $titles)) {
                                         $lineError++;
                                         $log .= 'Contenu : Numéro de Titre ' . $titleNum . ' à  la ligne  ' . $lineRead . ' inexistant<br>';
                                     } elseif (!\array_key_exists($contentNum, $titles[$titleNum]['contents'])) {
                                         $isContent = true;
                                     } else {
                                         $lineError++;
                                         $log .= 'Contenu : Numéro de Contenu ' . $contentNum . ' à  la ligne  ' . $lineRead . ' existe déjà<br>';
                                     }
                                 }
                             }
                         }
                         if ($isTitle) {
                             $btTitle = \trim(\strval($worksheet->getCellByColumnAndRow(2, $row)->getValue()));
                             if ($btTitle != '') {
                                 $bulletinInfoTitle = new BulletinInfoTitle();
                                 $bulletinInfoTitle->setBulletinInfo($bulletinInfo);
                                 $bulletinInfoTitle->setTitle($btTitle);
                                 $em->persist($bulletinInfoTitle);
                                 $titles[$testNum] = array('bulletinInfoTitle' => $bulletinInfoTitle, 'contents' => array());
                                 $bulletinInfoTitleNew++;
                             } else {
                                 $lineError++;
                                 $log .= 'Titre : Numéro de nouveau Titre ' . $testNum . ' trouvé à la ligne  ' . $lineRead . ' mais texte vide<br>';
                             }
                         }
                         if ($isContent) {
                             $bcTitle = \trim(\strval($worksheet->getCellByColumnAndRow(2, $row)->getValue()));
                             if ($bcTitle != '') {
                                 $bulletinInfoTitle = $titles[$titleNum]['bulletinInfoTitle'];
                                 $bulletinInfoContent = new BulletinInfoContent();
                                 $bulletinInfoContent->setBulletinInfoTitle($bulletinInfoTitle);
                                 $bulletinInfoContent->setTitle($bcTitle);
                                 $row++;
                                 $content = \trim(\strval($worksheet->getCellByColumnAndRow(3, $row)->getValue()));
                                 if ($content != '') {
                                     $bulletinInfoContent->setContent($content);
                                 }
                                 $theme = \trim(\strval($worksheet->getCellByColumnAndRow(8, $row)->getValue()));
                                 if ($theme != '') {
                                     $bulletinInfoContent->setTheme($theme);
                                 }
                                 $jort = \trim(\strval($worksheet->getCellByColumnAndRow(9, $row)->getValue()));
                                 if ($jort != '') {
                                     $bulletinInfoContent->setJort($jort);
                                 }
                                 $txtNum = \trim(\strval($worksheet->getCellByColumnAndRow(10, $row)->getValue()));
                                 if ($txtNum != '') {
                                     $bulletinInfoContent->setTxtNum($txtNum);
                                 }
                                 $artTxt = \trim(\strval($worksheet->getCellByColumnAndRow(11, $row)->getValue()));
                                 if ($artTxt != '') {
                                     $bulletinInfoContent->setArtTxt($artTxt);
                                 }
                                 $dtTxt = \trim(\strval($worksheet->getCellByColumnAndRow(12, $row)->getValue()));
                                 if ($dtTxt != '') {
                                     $bulletinInfoContent->setDtTxt($dtTxt);
                                 }
                                 $artCode = \trim(\strval($worksheet->getCellByColumnAndRow(13, $row)->getValue()));
                                 if ($artCode != '') {
                                     $bulletinInfoContent->setArtCode($artCode);
                                 }
                                 $companyType = \trim(\strval($worksheet->getCellByColumnAndRow(14, $row)->getValue()));
                                 if ($companyType != '') {
                                     $bulletinInfoContent->setCompanyType($companyType);
                                 }
                                 $dtApplication = \trim(\strval($worksheet->getCellByColumnAndRow(15, $row)->getValue()));
                                 if ($dtApplication != '') {
                                     $bulletinInfoContent->setDtApplication($dtApplication);
                                 }
                                 $em->persist($bulletinInfoContent);
                                 $titles[$titleNum]['contents'][$contentNum] = $bulletinInfoContent;
                                 $bulletinInfoContentNew++;
                             } else {
                                 $lineError++;
                                 $log .= 'Contenu : Numéro de Contenu ' . $testNum . ' trouvé à la ligne  ' . $lineRead . ' mais texte vide<br>';
                             }
                         } else {
                             $lineUnprocessed++;
                         }
                     } catch (\Exception $e) {
                         $lineError++;
                         $log .= 'Erreur de lecture à la ligne  ' . $lineRead . ' : ' . $e->getMessage() . '<br>';
                     }
                     $lineRead++;
                 }
                 $em->flush();
             }
             $log .= $lineRead . ' lignes lues<br>';
             $log .= $lineUnprocessed . ' lignes non traités<br>';
             $log .= $bulletinInfoTitleNew . " nouveaux Titres de Bulletin d'Informations<br>";
             $log .= $bulletinInfoContentNew . " nouveaux Contenus de Bulletin d'Informations<br>";
             $log .= $lineError . ' lignes contenant des erreurs<br>';
             $this->flashMsgSession('log', $log);
             if (!$haserror) {
                 $this->flashMsgSession('success', $this->translate('BulletinInfo.import.success', array('%bulletinInfo%' => $bulletinInfo->getId())));
                 return $this->redirect($this->generateUrl('_admin_bulletinInfo_editGet', array('uid' => $bulletinInfo->getId())));
             }
         }
     }
     $this->flashMsgSession('error', $this->translate('BulletinInfo.import.failure'));
     return $this->redirect($urlFrom);
 }