コード例 #1
0
 protected function importLang($lang)
 {
     $translationRepository = $this->getTranslationRepository();
     $translationDir = $this->getContainer()->getParameter('translation_dir') . '/lot_6/';
     $defaultLang = $this->getContainer()->getParameter('locale');
     $em = $this->getEntityManager();
     $translationfiles = scandir($translationDir . strtoupper($lang) . '/');
     $exclude = array('.', '..');
     $translationfiles = array_diff($translationfiles, $exclude);
     foreach (array_values($translationfiles) as $translationFile) {
         $file = $translationDir . strtoupper($lang) . '/' . $translationFile;
         $handle = fopen($file, "r");
         while (($line = fgetcsv($handle)) !== false) {
             if ($line and $hotelId = $line[0] and $hotel = $this->getRepository('SehBundle:Hotel')->findOneBy(array('codeAdherent' => $hotelId))) {
                 if ($lang != $defaultLang) {
                     // $translationRepository->translate($hotel, 'longDescription', $lang, $line[4]);
                     // $translationRepository->translate($hotel, 'shortDescription', $lang, $line[5]);
                     $highlights = $hotel->getHighlights();
                     /** @var HotelHighlight $highlight */
                     $i = 0;
                     foreach ($highlights as $highlight) {
                         $translationRepository->translate($highlight, 'name', $lang, $line[6 + $i]);
                         $em->persist($highlight);
                         $i++;
                     }
                 } else {
                     // $hotel->setLongDescription($line[4]);
                     // $hotel->setShortDescription($line[5]);
                     foreach ($hotel->getHighlights() as $highlight) {
                         $em->remove($highlight);
                     }
                     $em->flush();
                     for ($i = 0; $i < 3; $i++) {
                         $highlight = new HotelHighlight();
                         $highlight->setName($line[6 + $i]);
                         $hotel->addHighlight($highlight);
                         $em->persist($highlight);
                     }
                 }
                 $em->persist($hotel);
                 $em->flush();
             }
         }
     }
 }