/** * Add a module template directory to the parser environment * * @param ParserInterface $parser the parser * @param Module $module the Module. * @param string $templateType the template type (one of the TemplateDefinition type constants) * @param string $templateSubdirName the template subdirectory name (one of the TemplateDefinition::XXX_SUBDIR constants) */ protected function addModuleTemplateToParserEnvironment($parser, $module, $templateType, $templateSubdirName) { // Get template path $templateDirectory = $module->getAbsoluteTemplateDirectoryPath($templateSubdirName); try { $templateDirBrowser = new \DirectoryIterator($templateDirectory); $code = ucfirst($module->getCode()); /* browse the directory */ foreach ($templateDirBrowser as $templateDirContent) { /* is it a directory which is not . or .. ? */ if ($templateDirContent->isDir() && !$templateDirContent->isDot()) { $parser->addMethodCall('addTemplateDirectory', array($templateType, $templateDirContent->getFilename(), $templateDirContent->getPathName(), $code)); } } } catch (\UnexpectedValueException $ex) { // The directory does not exists, ignore it. } }
/** * 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++; } } } }
/** * Create a new hook if the hook definition is valid. * * @param string $class the namespace of the class * @param \Thelia\Model\Module $module the module * @param string $id the service (hook) id * @param array $attributes the hook attributes * * @throws \InvalidArgumentException */ protected function registerHook($class, $module, $id, $attributes) { if (!isset($attributes['event'])) { throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "hook.event_listener" tags.', $id)); } $active = isset($attributes['active']) ? intval($attributes['active']) : 1; $attributes['active'] = 1 === $active; $attributes['templates'] = isset($attributes['templates']) ? strval($attributes['templates']) : ''; $attributes['type'] = isset($attributes['type']) ? $this->getHookType($attributes['type']) : TemplateDefinition::FRONT_OFFICE; if (null === ($hook = $this->getHook($attributes['event'], $attributes['type']))) { return; } $attributes = $this->getMethodName($attributes); // test if method exists $validMethod = true; if (!$this->isValidHookMethod($class, $attributes['method'], $hook->getBlock())) { $validMethod = false; } // test if hook is already registered in ModuleHook $moduleHook = ModuleHookQuery::create()->filterByModuleId($module->getId())->filterByHook($hook)->filterByMethod($attributes['method'])->findOne(); if (null === $moduleHook) { if (!$validMethod) { Tlog::getInstance()->addAlert(sprintf("Module [%s] could not be registered hook [%s], method [%s] is not reachable.", $module->getCode(), $attributes['event'], $attributes['method'])); return; } // Assign the module to the hook only if it has not been "deleted" $ignoreCount = IgnoredModuleHookQuery::create()->filterByHook($hook)->filterByModuleId($module->getId())->count(); if (0 === $ignoreCount) { // hook for module doesn't exist, we add it with default registered values $moduleHook = new ModuleHook(); $moduleHook->setHook($hook)->setModuleId($module->getId())->setClassname($id)->setMethod($attributes['method'])->setActive($active)->setHookActive(true)->setModuleActive(true)->setPosition(ModuleHook::MAX_POSITION); if (isset($attributes['templates'])) { $moduleHook->setTemplates($attributes['templates']); } $moduleHook->save(); } } else { if (!$validMethod) { Tlog::getInstance()->addAlert(sprintf("Module [%s] could not use hook [%s], method [%s] is not reachable anymore.", $module->getCode(), $attributes['event'], $attributes['method'])); $moduleHook->setHookActive(false)->save(); } else { //$moduleHook->setTemplates($attributes['templates']); // Update hook if id was changed in the definition if ($moduleHook->getClassname() != $id) { $moduleHook->setClassname($id); } $moduleHook->save(); } } }