示例#1
0
 /**
  * Ensure the proper deployment of the module's images.
  *
  * TODO : this method does not take care of internationalization. This is a bug.
  *
  * @param Module              $module     the module
  * @param string              $folderPath the image folder path
  * @param ConnectionInterface $con
  *
  * @throws \Thelia\Exception\ModuleException
  * @throws \Exception
  * @throws \UnexpectedValueException
  */
 public function deployImageFolder(Module $module, $folderPath, ConnectionInterface $con = null)
 {
     try {
         $directoryBrowser = new \DirectoryIterator($folderPath);
     } catch (\UnexpectedValueException $e) {
         throw $e;
     }
     if (null === $con) {
         $con = Propel::getConnection(ModuleImageTableMap::DATABASE_NAME);
     }
     /* browse the directory */
     $imagePosition = 1;
     /** @var \DirectoryIterator $directoryContent */
     foreach ($directoryBrowser as $directoryContent) {
         /* is it a file ? */
         if ($directoryContent->isFile()) {
             $fileName = $directoryContent->getFilename();
             $filePath = $directoryContent->getPathName();
             /* is it a picture ? */
             if (Image::isImage($filePath)) {
                 $con->beginTransaction();
                 $image = new ModuleImage();
                 $image->setModuleId($module->getId());
                 $image->setPosition($imagePosition);
                 $image->save($con);
                 $imageDirectory = sprintf("%s/../../../../local/media/images/module", __DIR__);
                 $imageFileName = sprintf("%s-%d-%s", $module->getCode(), $image->getId(), $fileName);
                 $increment = 0;
                 while (file_exists($imageDirectory . '/' . $imageFileName)) {
                     $imageFileName = sprintf("%s-%d-%d-%s", $module->getCode(), $image->getId(), $increment, $fileName);
                     $increment++;
                 }
                 $imagePath = sprintf('%s/%s', $imageDirectory, $imageFileName);
                 if (!is_dir($imageDirectory)) {
                     if (!@mkdir($imageDirectory, 0777, true)) {
                         $con->rollBack();
                         throw new ModuleException(sprintf("Cannot create directory : %s", $imageDirectory), ModuleException::CODE_NOT_FOUND);
                     }
                 }
                 if (!@copy($filePath, $imagePath)) {
                     $con->rollBack();
                     throw new ModuleException(sprintf("Cannot copy file : %s to : %s", $filePath, $imagePath), ModuleException::CODE_NOT_FOUND);
                 }
                 $image->setFile($imageFileName);
                 $image->save($con);
                 $con->commit();
                 $imagePosition++;
             }
         }
     }
 }