diff --git a/config/module.config.php b/config/module.config.php index c4a9635..e02cc31 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -178,7 +178,7 @@ 'MinifyAssets' => 'MelisFront\Service\Factory\MinifyAssetsServiceFactory', 'MelisSiteTranslationService' => 'MelisFront\Service\Factory\MelisSiteTranslationServiceFactory', 'MelisSiteConfigService' => 'MelisFront\Service\Factory\MelisSiteConfigServiceFactory', - + 'MelisTranslationService' => 'MelisFront\Service\Factory\MelisTranslationServiceFactory', 'MelisFront\Listener\MelisFront404To301Listener' => 'MelisFront\Listener\Factory\MelisFront404To301ListenerFactory', ), ), @@ -216,6 +216,7 @@ 'MelisPageLangLink' => 'MelisFront\View\Helper\Factory\MelisPageLangVersionLinkHelperFactory', 'MelisHomePageLink' => 'MelisFront\View\Helper\Factory\MelisHomePageLinkHelperFactory', 'siteTranslate' => 'MelisFront\View\Helper\Factory\MelisSiteTranslationHelperFactory', + 'boTranslate' => 'MelisFront\View\Helper\Factory\MelisTranslationHelperFactory', 'SiteConfig' => 'MelisFront\View\Helper\Factory\SiteConfigViewHelperFactory', 'MelisGdprBannerPlugin' => 'MelisFront\View\Helper\Factory\MelisGdprBannerHelperFactory', 'MelisListFromFolderPlugin' => 'MelisFront\View\Helper\Factory\MelisListFromFolderHelperFactory', diff --git a/src/Controller/MelisSiteActionController.php b/src/Controller/MelisSiteActionController.php index ada3ca5..8a6f7f8 100644 --- a/src/Controller/MelisSiteActionController.php +++ b/src/Controller/MelisSiteActionController.php @@ -32,6 +32,7 @@ class MelisSiteActionController extends AbstractActionController public function onDispatch(MvcEvent $event) { + $this->idPage = $this->params()->fromRoute('idpage'); $this->renderType = $this->params()->fromRoute('renderType'); if (empty($this->renderType)) @@ -79,23 +80,24 @@ public function onDispatch(MvcEvent $event) $siteLang = $melisEnginLangService->getSiteLanguage(); $siteLangId = $siteLang['langId']; $siteLangLocale = $siteLang['langLocale']; - + $this->layout()->setVariables(array( - 'idPage' => $this->idPage, - 'renderType' => $this->renderType, - 'renderMode' => $this->renderMode, - 'pageLangId' => $this->pageLangId, - 'pageLangLocale' => $this->pageLangLocale, - 'pageBreadCrumb' => $pageBreadCrumb, - 'mainPageId' => $mainPageId, - 'pageTemplate' => $pageTemplate, - 'siteLangId' => $siteLangId, - 'siteLangLocale' => $siteLangLocale, + 'idPage' => $this->idPage, + 'renderType' => $this->renderType, + 'renderMode' => $this->renderMode, + 'pageLangId' => $this->pageLangId, + 'pageLangLocale' => $this->pageLangLocale, + 'pageBreadCrumb' => $pageBreadCrumb, + 'mainPageId' => $mainPageId, + 'pageTemplate' => $pageTemplate, + 'siteLangId' => $siteLangId, + 'siteLangLocale' => $siteLangLocale, + 'preview' => $this->params()->fromRoute('preview') )); $this->oMelisPage = $datasPage; } - + return parent::onDispatch($event); } diff --git a/src/Listener/MelisFrontMiniTemplateConfigListener.php b/src/Listener/MelisFrontMiniTemplateConfigListener.php index 38e9691..a0e8f00 100644 --- a/src/Listener/MelisFrontMiniTemplateConfigListener.php +++ b/src/Listener/MelisFrontMiniTemplateConfigListener.php @@ -110,14 +110,31 @@ public function onLoadModulesPost(ModuleEvent $e) */ public function prepareMiniTemplateConfig($miniTplPath) { + $image_ext = ['PNG', 'png', 'JPG', 'jpg', 'JPEG', 'jpeg']; $pluginsFormat = array(); foreach($miniTplPath as $siteName => $path) { if (file_exists($path) && is_dir($path)) { + $tplImgList = []; //get the plugin config format $pluginsConfig = include __DIR__ . '/../../config/plugins/MiniTemplatePlugin.config.php'; if (!empty($pluginsConfig)) { //get all the mini template $tpls = array_diff(scandir($path), array('..', '.')); + /** + * Remove all the images + */ + foreach ($tpls as $key => $tpl){ + foreach($image_ext as $ext){ + //if image found, store the image path with the template name as the key + if(strpos($tpl, $ext) !== false) { + $fName = pathinfo($tpl, PATHINFO_FILENAME); + $tplImgList[$fName] = '/'.$siteName.'/miniTemplatesTinyMce/'.$fName.'.'.$ext; + //remove the image + unset($tpls[$key]); + } + } + } + if (!empty($tpls)) { //set the site name as sub category title $pluginsConfig['melis']['subcategory']['title'] = $siteName; @@ -135,6 +152,8 @@ public function prepareMiniTemplateConfig($miniTplPath) $pluginsConfig['front']['default'] = file_get_contents($content); //set the plugin name using the template name $pluginsConfig['melis']['name'] = $name; + //apply minitemplate thumbnail + $pluginsConfig['melis']['thumbnail'] = $tplImgList[$name] ?? '/MelisFront/plugins/images/default.jpg'; //include the mini tpl plugin config $pluginsFormat['plugins']['MelisMiniTemplate']['plugins']['MiniTemplatePlugin_' . $postName] = $pluginsConfig; } diff --git a/src/Service/Factory/MelisTranslationServiceFactory.php b/src/Service/Factory/MelisTranslationServiceFactory.php new file mode 100644 index 0000000..60826a3 --- /dev/null +++ b/src/Service/Factory/MelisTranslationServiceFactory.php @@ -0,0 +1,27 @@ +get('MelisAssetManagerModulesService'); + $melisTranslationService = new MelisTranslationService($moduleSvc); + $melisTranslationService->setServiceLocator($sl); + + return $melisTranslationService; + } + +} \ No newline at end of file diff --git a/src/Service/MelisTranslationService.php b/src/Service/MelisTranslationService.php new file mode 100644 index 0000000..9fea7b5 --- /dev/null +++ b/src/Service/MelisTranslationService.php @@ -0,0 +1,154 @@ +moduleSvc = $moduleService; + } + + /** + * + * get all module translations by locale + * + * @param string $locale + * @return array + */ + public function getTranslationsByLocale($locale = "en_EN") + { + // Event parameters prepare + $arrayParameters = $this->makeArrayFromParameters(__METHOD__, func_get_args()); + // Sending service start event + $arrayParameters = $this->sendEvent('melis_translation_get_trans_by_locale_start', $arrayParameters); + $transMessages = []; + $tmpTrans = []; + $modules = $this->moduleSvc->getAllModules(); + $locale = $arrayParameters['locale']; + $moduleFolders = []; + // get modules path + foreach ($modules as $module) + { + array_push($moduleFolders, $this->moduleSvc->getModulePath($module)); + } + + $transFiles = array( + $locale.'.interface.php', + $locale.'.forms.php', + ); + $insideDirTrans = []; + set_time_limit(0); + foreach($moduleFolders as $module) { + if(file_exists($module.'/language')) { + foreach($transFiles as $file) { + if(file_exists($module.'/language/'.$file)) { + $tmpTrans[] = include($module.'/language/'.$file); + } + } + // get the directory + $iterator = new \RecursiveDirectoryIterator($module . "/language", \RecursiveDirectoryIterator::SKIP_DOTS); + $files = new \RecursiveIteratorIterator($iterator,\RecursiveIteratorIterator::CHILD_FIRST); + /** @var \SplFileInfo $file */ + // get the files under the directory + foreach($files as $file) { + if (stristr($file->getBasename(),$locale)){ + // get the translation based on locale + $tmpTrans[]= include $file->getFileInfo()->getPathname(); + } else if (stristr($file->getBasename(),"en_EN")){ + // fall back locale + $tmpTrans[] = include $file->getFileInfo()->getPathname(); + } + } + + } + } + + if($tmpTrans) { + foreach($tmpTrans as $tmpIdx => $transKey) { + foreach($transKey as $key => $value) { + $transMessages[$key] = $value; + } + } + } + // results + $arrayParameters['results'] = $transMessages; + // send event + $arrayParameters = $this->sendEvent('melis_translation_get_trans_by_locale_end', $arrayParameters); + + return $arrayParameters['results']; + + } + + /**+ + * + * + * get translations by key and locale + * + * @param $translationKey + * @param string $locale + * @return mixed + */ + public function translateByLocale($translationKey, $locale = "en_EN") + { + // Event parameters prepare + $arrayParameters = $this->makeArrayFromParameters(__METHOD__, func_get_args()); + // Sending service start event + $arrayParameters = $this->sendEvent('trans_by_locale_start', $arrayParameters); + $text = $translationKey; + // check translation key in the translations + $translations = $this->getTranslationsByLocale($locale); + if (array_key_exists($translationKey, $translations)) { + $text = $translations[$translationKey]; + } + + // results + $arrayParameters['results'] = $text; + // send event + $arrayParameters = $this->sendEvent('trans_by_locale_end', $arrayParameters); + + return $arrayParameters['results']; + } + + /** + * translate translationkey based from Back-office locale + * @param $translationKey + * @return mixed + */ + public function boTranslate($translationKey) + { + // Event parameters prepare + $arrayParameters = $this->makeArrayFromParameters(__METHOD__, func_get_args()); + // Sending service start event + $arrayParameters = $this->sendEvent('bo_translate_start', $arrayParameters); + $text = $translationKey; + // get bo locale + $melisBoContainer = new Container('meliscore'); + // check translation key in the translations + $translations = $this->getTranslationsByLocale($melisBoContainer['melis-lang-locale']); + if (array_key_exists($translationKey, $translations)) { + $text = $translations[$translationKey]; + } + + // results + $arrayParameters['results'] = $text; + // send event + $arrayParameters = $this->sendEvent('bo_translate_end', $arrayParameters); + + return $arrayParameters['results']; + } +} diff --git a/src/View/Helper/Factory/MelisTranslationHelperFactory.php b/src/View/Helper/Factory/MelisTranslationHelperFactory.php new file mode 100644 index 0000000..8b9bc5c --- /dev/null +++ b/src/View/Helper/Factory/MelisTranslationHelperFactory.php @@ -0,0 +1,27 @@ +getServiceLocator()); + } + +} \ No newline at end of file diff --git a/src/View/Helper/MelisTranslationHelper.php b/src/View/Helper/MelisTranslationHelper.php new file mode 100644 index 0000000..1b2de8a --- /dev/null +++ b/src/View/Helper/MelisTranslationHelper.php @@ -0,0 +1,37 @@ +serviceManager = $sm; + } + + public function __invoke($translationKey , $locale = null) + { + $text = ""; + // melis translation view helper + /** @var MelisTranslationService $melisTrans */ + $melisTrans = $this->serviceManager->get('MelisTranslationService'); + if (! empty($locale)) { + $text = $melisTrans->translateByLocale($translationKey,$locale); + } else { + // get melis back office locale + $melisCoreLang = new Container('meliscore'); + // get translation + $text = $melisTrans->translateByLocale($translationKey,$melisCoreLang['melis-lang-locale']); + } + + return $text; + } + +} \ No newline at end of file