/**
  * Execute
  *
  * @param InputInterface  $input  input
  * @param OutputInterface $output output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('<info>Import services images</info>');
     try {
         $entityManager = $this->getEntityManager();
         $portfolioPath = $this->getContainer()->getParameter('bigfoot.core.upload_dir') . $this->getContainer()->getParameter('bigfoot.media.portfolio_dir');
         $servicesPath = $portfolioPath . 'services/';
         $webDir = $this->getContainer()->get('kernel')->getRootDir() . '/../web/';
         $imagesDir = $webDir . $servicesPath;
         $mediaRepo = $entityManager->getRepository('BigfootMediaBundle:Media');
         $mediaUsageRepo = $entityManager->getRepository('BigfootMediaBundle:MediaUsage');
         $serviceRepo = $entityManager->getRepository('SehBundle:HotelService');
         /**
          * @var Filesystem $fs
          */
         $fs = $this->getContainer()->get('filesystem');
         if (!$fs->exists($imagesDir)) {
             throw new FileNotFoundException($imagesDir);
         }
         $images = scandir($imagesDir);
         $exclude = array('.', '..');
         $images = array_diff($images, $exclude);
         $progress = $this->getHelperSet()->get('progress');
         $progress->start($output, count($images));
         foreach ($images as $image) {
             $slug = basename($image, sprintf('.%s', $extension = pathinfo($image, PATHINFO_EXTENSION)));
             $fileName = $servicesPath . $image;
             $newMedia = false;
             if (!($media = $mediaRepo->findOneByFile($fileName))) {
                 $media = new Media();
                 $media->setFile($fileName);
                 $media->setType($extension);
                 $entityManager->persist($media);
                 $newMedia = true;
             }
             if ($service = $serviceRepo->findOneBySlug($slug)) {
                 if ($newMedia or !($usage = $mediaUsageRepo->findOneBy(array('mediaId' => $media, 'tableRef' => 'SehBundle:HotelService', 'columnRef' => 'media', 'elementId' => $service->getId())))) {
                     $mediaUsage = new MediaUsage();
                     $mediaUsage->setMediaId($media);
                     $mediaUsage->setTableRef('SehBundle:Hotel');
                     $mediaUsage->setColumnRef('gallery');
                     $mediaUsage->setElementId($service->getId());
                     $entityManager->persist($mediaUsage);
                 }
                 $service->setMedia($media->getId());
                 $entityManager->persist($service);
             } else {
                 $output->writeln(sprintf(' > <error>Service "%s" not found - the image was saved but not associated to the service</error>', $slug));
             }
             $progress->advance();
         }
         $progress->finish();
         $output->writeln(' > <info>Flushing</info>');
         $entityManager->flush();
         $output->writeln(' > <comment>OK</comment>');
     } catch (Exception $e) {
         $output->writeln(' > <error>Erreur : ' . $e->getMessage() . '</error>');
     }
 }
Exemple #2
0
 /**
  * Execute
  *
  * @param InputInterface  $input  input
  * @param OutputInterface $output output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('<info>Import hotel images</info>');
     try {
         $entityManager = $this->getEntityManager();
         $descriptorFilesDir = $this->getContainer()->getParameter('images_dir') . '/hotels/';
         $portfolioPath = $this->getContainer()->getParameter('bigfoot.core.upload_dir') . $this->getContainer()->getParameter('bigfoot.media.portfolio_dir');
         $webDir = $this->getContainer()->get('kernel')->getRootDir() . '/../web/';
         $mediaRepo = $entityManager->getRepository('BigfootMediaBundle:Media');
         $mediaUsageRepo = $entityManager->getRepository('BigfootMediaBundle:MediaUsage');
         $brands = $entityManager->getRepository('SehBundle:Brand')->findAll();
         /** @var ProgressHelper $progress */
         $progress = $this->getHelperSet()->get('progress');
         $progress->start($output, count($brands));
         /** @var Brand $brand */
         foreach ($brands as $brand) {
             $brandSlug = strtolower($brand->getArtsysID());
             $descriptorFile = sprintf('%s%s.csv', $descriptorFilesDir, $brandSlug);
             if (file_exists($descriptorFile)) {
                 $fp = fopen($descriptorFile, 'r');
                 while (($line = fgetcsv($fp, null, ';', '"')) !== false) {
                     list($reservitId, $fileName, $thumbnail) = $line;
                     $fileName = sprintf('%s%s/%s', $portfolioPath, $brandSlug, $fileName);
                     $fullFileName = $webDir . $fileName;
                     if (file_exists($fullFileName)) {
                         $newMedia = false;
                         if (!($media = $mediaRepo->findOneBy(array('file' => $fileName)))) {
                             $media = new Media();
                             $media->setFile($fileName);
                             $media->setType(pathinfo($fullFileName, PATHINFO_EXTENSION));
                             $entityManager->persist($media);
                             $entityManager->flush();
                             $newMedia = true;
                         }
                         if ($hotel = $entityManager->getRepository('SehBundle:Hotel')->findOneBy(array('reservitId' => $reservitId))) {
                             if ($newMedia or !($usageGallery = $mediaUsageRepo->findOneBy(array('mediaId' => $media, 'tableRef' => 'SehBundle:Hotel', 'columnRef' => 'gallery', 'elementId' => $hotel->getId())))) {
                                 $mediaUsage = new MediaUsage();
                                 $mediaUsage->setMediaId($media);
                                 $mediaUsage->setTableRef('SehBundle:Hotel');
                                 $mediaUsage->setColumnRef('gallery');
                                 $mediaUsage->setElementId($hotel->getId());
                                 $entityManager->persist($mediaUsage);
                             }
                             $existingGallery = $hotel->getGallery();
                             if (strpos($existingGallery, (string) $media->getId()) === false) {
                                 $hotel->setGallery($existingGallery ? $existingGallery . ';' . $media->getId() : $media->getId());
                                 $entityManager->persist($hotel);
                             }
                             if ($thumbnail and (!$hotel->getThumbnail() and ($newMedia or !($usageGallery = $mediaUsageRepo->findOneBy(array('mediaId' => $media, 'tableRef' => 'SehBundle:Hotel', 'columnRef' => 'thumbnail', 'elementId' => $hotel->getId())))))) {
                                 $mediaUsage = new MediaUsage();
                                 $mediaUsage->setMediaId($media);
                                 $mediaUsage->setTableRef('SehBundle:Hotel');
                                 $mediaUsage->setColumnRef('thumbnail');
                                 $mediaUsage->setElementId($hotel->getId());
                                 $entityManager->persist($mediaUsage);
                             }
                             if (!$hotel->getThumbnail()) {
                                 $hotel->setThumbnail($media->getId());
                                 $entityManager->persist($hotel);
                             }
                         }
                     }
                 }
             }
             $progress->advance();
         }
         $progress->finish();
         $output->writeln(' > <info>Cleaning</info>');
         $hotelRepo = $entityManager->getRepository('SehBundle:Hotel');
         /** @var Hotel $hotel */
         foreach ($hotelRepo->findAll() as $hotel) {
             $currentMediaIds = explode(';', $hotel->getGallery());
             $newMediaIds = array();
             foreach ($currentMediaIds as $mediaId) {
                 if ($mediaRepo->find($mediaId)) {
                     $newMediaIds[] = $mediaId;
                 }
             }
             if (array_diff($currentMediaIds, $newMediaIds)) {
                 $ids = null;
                 if ($newMediaIds) {
                     $ids = implode(';', $newMediaIds);
                 }
                 $hotel->setGallery($ids);
                 $entityManager->persist($hotel);
             }
         }
         $output->writeln(' > <info>Flushing</info>');
         $entityManager->flush();
         $output->writeln(' > <comment>OK</comment>');
     } catch (\Exception $e) {
         $output->writeln(' > <error>Erreur : ' . $e->getMessage() . '</error>');
     }
 }