. */ /* */ /*************************************************************************************/ namespace Thelia\Module; use Symfony\Component\DependencyInjection\ContainerAware; use Thelia\Model\Map\ModuleImageTableMap; use Thelia\Tools\Image; use Thelia\Exception\ModuleException; use Thelia\Model\Module; use Thelia\Model\ModuleImage; use Thelia\Model\ModuleQuery; abstract class BaseModule extends ContainerAware { const CLASSIC_MODULE_TYPE = 1; const DELIVERY_MODULE_TYPE = 2; const PAYMENT_MODULE_TYPE = 3; public function __construct() { } protected function activate() { } public function hasContainer() { return null === $this->container; } public function getContainer() { if ($this->hasContainer() === false) { throw new \RuntimeException("Sorry, container his not available in this context"); } return $this->container; } public function deployImageFolder(Module $module, $folderPath) { try { $directoryBrowser = new \DirectoryIterator($folderPath); } catch(\UnexpectedValueException $e) { throw $e; } $con = \Propel\Runtime\Propel::getConnection( ModuleImageTableMap::DATABASE_NAME ); /* browse the directory */ $imagePosition = 1; 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(); $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("module/%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->commit(); $imagePosition++; } } } } /** * @return ChildModule * @throws \Thelia\Exception\ModuleException */ public function getModuleModel() { $moduleModel = ModuleQuery::create()->findOneByCode($this->getCode()); if(null === $moduleModel) { throw new ModuleException(sprintf("Module Code `%s` not found", $this->getCode()), ModuleException::CODE_NOT_FOUND); } return $moduleModel; } abstract public function getCode(); abstract public function install(); abstract public function destroy(); }