Exemple #1
0
 public function parseTemplate($templateType, $template)
 {
     $templateDefinition = new TemplateDefinition($template, $templateType);
     $hooks = array();
     $this->walkDir($templateDefinition->getAbsolutePath(), $hooks);
     // load language message
     $locale = Lang::getDefaultLanguage()->getLocale();
     $this->loadTrans($templateType, $locale);
     $ret = array();
     foreach ($hooks as $hook) {
         try {
             $ret[] = $this->prepareHook($hook);
         } catch (\UnexpectedValueException $ex) {
             Tlog::getInstance()->warning($ex->getMessage());
         }
     }
     return $ret;
 }
 protected function renderTemplate()
 {
     // Get related strings, if all input data are here
     $itemToTranslate = $this->getRequest()->get('item_to_translate');
     $itemName = $this->getRequest()->get('item_name', '');
     if ($itemToTranslate == 'mo' && !empty($itemName)) {
         $modulePart = $this->getRequest()->get('module_part', '');
     } else {
         $modulePart = false;
     }
     $template = $directory = $i18nDirectory = false;
     $walkMode = TranslationEvent::WALK_MODE_TEMPLATE;
     $templateArguments = array('item_to_translate' => $itemToTranslate, 'item_name' => $itemName, 'module_part' => $modulePart, 'view_missing_traductions_only' => $this->getRequest()->get('view_missing_traductions_only'), 'max_input_vars_warning' => false);
     // Find the i18n directory, and the directory to examine.
     if (!empty($itemName) || $itemToTranslate == 'co' || $itemToTranslate == 'in') {
         switch ($itemToTranslate) {
             // Module core
             case 'mo':
                 $module = $this->getModule($itemName);
                 if ($modulePart == 'core') {
                     $directory = $module->getAbsoluteBaseDir();
                     $domain = $module->getTranslationDomain();
                     $i18nDirectory = $module->getAbsoluteI18nPath();
                     $walkMode = TranslationEvent::WALK_MODE_PHP;
                 } elseif ($modulePart == 'admin-includes') {
                     $directory = $module->getAbsoluteAdminIncludesPath();
                     $domain = $module->getAdminIncludesTranslationDomain();
                     $i18nDirectory = $module->getAbsoluteAdminIncludesI18nPath();
                     $walkMode = TranslationEvent::WALK_MODE_TEMPLATE;
                 } elseif (!empty($modulePart)) {
                     // Front, back, pdf or email office template,
                     // form of $module_part is [bo|fo|pdf|email].subdir-name
                     list($type, $subdir) = explode('.', $modulePart);
                     switch ($type) {
                         case 'bo':
                             $directory = $module->getAbsoluteBackOfficeTemplatePath($subdir);
                             $domain = $module->getBackOfficeTemplateTranslationDomain($subdir);
                             $i18nDirectory = $module->getAbsoluteBackOfficeI18nTemplatePath($subdir);
                             break;
                         case 'fo':
                             $directory = $module->getAbsoluteFrontOfficeTemplatePath($subdir);
                             $domain = $module->getFrontOfficeTemplateTranslationDomain($subdir);
                             $i18nDirectory = $module->getAbsoluteFrontOfficeI18nTemplatePath($subdir);
                             break;
                         case 'email':
                             $directory = $module->getAbsoluteEmailTemplatePath($subdir);
                             $domain = $module->getEmailTemplateTranslationDomain($subdir);
                             $i18nDirectory = $module->getAbsoluteEmailI18nTemplatePath($subdir);
                             break;
                         case 'pdf':
                             $directory = $module->getAbsolutePdfTemplatePath($subdir);
                             $domain = $module->getPdfTemplateTranslationDomain($subdir);
                             $i18nDirectory = $module->getAbsolutePdfI18nTemplatePath($subdir);
                             break;
                         default:
                             throw new \InvalidArgumentException("Undefined module template type: '{$type}'.");
                     }
                     $walkMode = TranslationEvent::WALK_MODE_TEMPLATE;
                 }
                 // Modules translations files are in the cache, and are not always
                 // updated. Force a reload of the files to get last changes.
                 if (!empty($domain)) {
                     $this->loadTranslation($i18nDirectory, $domain);
                 }
                 // List front and back office templates defined by this module
                 $templateArguments['back_office_templates'] = implode(',', $this->getModuleTemplateNames($module, TemplateDefinition::BACK_OFFICE));
                 $templateArguments['front_office_templates'] = implode(',', $this->getModuleTemplateNames($module, TemplateDefinition::FRONT_OFFICE));
                 $templateArguments['email_templates'] = implode(',', $this->getModuleTemplateNames($module, TemplateDefinition::EMAIL));
                 $templateArguments['pdf_templates'] = implode(',', $this->getModuleTemplateNames($module, TemplateDefinition::PDF));
                 // Check if we have admin-include files
                 try {
                     $finder = Finder::create()->files()->depth(0)->in($module->getAbsoluteAdminIncludesPath())->name('/\\.html$/i');
                     $hasAdminIncludes = $finder->count() > 0;
                 } catch (\InvalidArgumentException $ex) {
                     $hasAdminIncludes = false;
                 }
                 $templateArguments['has_admin_includes'] = $hasAdminIncludes;
                 break;
                 // Thelia Core
             // Thelia Core
             case 'co':
                 $directory = THELIA_LIB;
                 $domain = 'core';
                 $i18nDirectory = THELIA_LIB . 'Config' . DS . 'I18n';
                 $walkMode = TranslationEvent::WALK_MODE_PHP;
                 break;
                 // Thelia Install
             // Thelia Install
             case 'in':
                 $directory = THELIA_SETUP_DIRECTORY;
                 $domain = 'install';
                 $i18nDirectory = THELIA_SETUP_DIRECTORY . 'I18n';
                 $walkMode = TranslationEvent::WALK_MODE_TEMPLATE;
                 // resources not loaded by default
                 $this->loadTranslation($i18nDirectory, $domain);
                 break;
                 // Front-office template
             // Front-office template
             case 'fo':
                 $template = new TemplateDefinition($itemName, TemplateDefinition::FRONT_OFFICE);
                 break;
                 // Back-office template
             // Back-office template
             case 'bo':
                 $template = new TemplateDefinition($itemName, TemplateDefinition::BACK_OFFICE);
                 break;
                 // PDF templates
             // PDF templates
             case 'pf':
                 $template = new TemplateDefinition($itemName, TemplateDefinition::PDF);
                 break;
                 // Email templates
             // Email templates
             case 'ma':
                 $template = new TemplateDefinition($itemName, TemplateDefinition::EMAIL);
                 break;
         }
         if ($template) {
             $directory = $template->getAbsolutePath();
             $i18nDirectory = $template->getAbsoluteI18nPath();
             $domain = $template->getTranslationDomain();
             // Load translations files is this template is not the current template
             // as it is not loaded in Thelia.php
             if (!$this->getTemplateHelper()->isActive($template)) {
                 $this->loadTranslation($i18nDirectory, $domain);
             }
         }
         // Load strings to translate
         if ($directory && !empty($domain)) {
             // Save the string set, if the form was submitted
             if ($i18nDirectory) {
                 $save_mode = $this->getRequest()->get('save_mode', false);
                 if ($save_mode !== false) {
                     $texts = $this->getRequest()->get('text', array());
                     if (!empty($texts)) {
                         $event = TranslationEvent::createWriteFileEvent(sprintf("%s" . DS . "%s.php", $i18nDirectory, $this->getCurrentEditionLocale()), $texts, $this->getRequest()->get('translation', []), true);
                         $event->setDomain($domain)->setLocale($this->getCurrentEditionLocale())->setCustomFallbackStrings($this->getRequest()->get('translation_custom', []))->setGlobalFallbackStrings($this->getRequest()->get('translation_global', []));
                         $this->getDispatcher()->dispatch(TheliaEvents::TRANSLATION_WRITE_FILE, $event);
                         if ($save_mode == 'stay') {
                             return $this->generateRedirectFromRoute("admin.configuration.translations", $templateArguments);
                         } else {
                             return $this->generateRedirect(URL::getInstance()->adminViewUrl('configuration'));
                         }
                     }
                 }
             }
             // Load strings
             $event = TranslationEvent::createGetStringsEvent($directory, $walkMode, $this->getCurrentEditionLocale(), $domain);
             $this->getDispatcher()->dispatch(TheliaEvents::TRANSLATION_GET_STRINGS, $event);
             // Estimate number of fields, and compare to php ini max_input_vars
             $stringsCount = $event->getTranslatableStringCount() * 4 + 6;
             if ($stringsCount > ini_get('max_input_vars')) {
                 $templateArguments['max_input_vars_warning'] = true;
                 $templateArguments['required_max_input_vars'] = $stringsCount;
                 $templateArguments['current_max_input_vars'] = ini_get('max_input_vars');
             } else {
                 $templateArguments['all_strings'] = $event->getTranslatableStrings();
             }
             $templateArguments['is_writable'] = $this->checkWritableI18nDirectory(THELIA_LOCAL_DIR . 'I18n');
         }
     }
     return $this->render('translations', $templateArguments);
 }
 protected function renderTemplate()
 {
     // Get related strings, if all input data are here
     $item_to_translate = $this->getRequest()->get('item_to_translate');
     $item_name = $this->getRequest()->get('item_name', '');
     if ($item_to_translate == 'mo' && !empty($item_name)) {
         $module_part = $this->getRequest()->get('module_part', '');
     } else {
         $module_part = false;
     }
     $all_strings = array();
     $template = $directory = $i18n_directory = false;
     $walkMode = TemplateHelper::WALK_MODE_TEMPLATE;
     $templateArguments = array('item_to_translate' => $item_to_translate, 'item_name' => $item_name, 'module_part' => $module_part, 'view_missing_traductions_only' => $this->getRequest()->get('view_missing_traductions_only', 0), 'max_input_vars_warning' => false);
     // Find the i18n directory, and the directory to examine.
     if (!empty($item_name) || $item_to_translate == 'co') {
         switch ($item_to_translate) {
             // Module core
             case 'mo':
                 $module = $this->getModule($item_name);
                 if ($module_part == 'core') {
                     $directory = $module->getAbsoluteBaseDir();
                     $domain = $module->getTranslationDomain();
                     $i18n_directory = $module->getAbsoluteI18nPath();
                     $walkMode = TemplateHelper::WALK_MODE_PHP;
                 } elseif ($module_part == 'admin-includes') {
                     $directory = $module->getAbsoluteAdminIncludesPath();
                     $domain = $module->getAdminIncludesTranslationDomain();
                     $i18n_directory = $module->getAbsoluteAdminIncludesI18nPath();
                     $walkMode = TemplateHelper::WALK_MODE_TEMPLATE;
                 } elseif (!empty($module_part)) {
                     // Front or back office template, form of $module_part is [bo|fo].subdir-name
                     list($type, $subdir) = explode('.', $module_part);
                     if ($type == 'bo') {
                         $directory = $module->getAbsoluteBackOfficeTemplatePath($subdir);
                         $domain = $module->getBackOfficeTemplateTranslationDomain($subdir);
                         $i18n_directory = $module->getAbsoluteBackOfficeI18nTemplatePath($subdir);
                     } elseif ($type == 'fo') {
                         $directory = $module->getAbsoluteFrontOfficeTemplatePath($subdir);
                         $domain = $module->getFrontOfficeTemplateTranslationDomain($subdir);
                         $i18n_directory = $module->getAbsoluteFrontOfficeI18nTemplatePath($subdir);
                     } else {
                         throw new \InvalidArgumentException("Undefined module template type: '{$type}'.");
                     }
                     $walkMode = TemplateHelper::WALK_MODE_TEMPLATE;
                 }
                 // Modules translations files are in the cache, and are not always
                 // updated. Force a reload of the files to get last changes.
                 if (!empty($domain)) {
                     $this->loadTranslation($i18n_directory, $domain);
                 }
                 // List front and back office templates defined by this module
                 $templateArguments['back_office_templates'] = implode(',', $this->getModuleTemplateNames($module, TemplateDefinition::BACK_OFFICE));
                 $templateArguments['front_office_templates'] = implode(',', $this->getModuleTemplateNames($module, TemplateDefinition::FRONT_OFFICE));
                 // Check if we have admin-include files
                 try {
                     $finder = Finder::create()->files()->depth(0)->in($module->getAbsoluteAdminIncludesPath())->name('/\\.html$/i');
                     $hasAdminIncludes = $finder->count() > 0;
                 } catch (\InvalidArgumentException $ex) {
                     $hasAdminIncludes = false;
                 }
                 $templateArguments['has_admin_includes'] = $hasAdminIncludes;
                 break;
                 // Thelia Core
             // Thelia Core
             case 'co':
                 $directory = THELIA_ROOT . 'core/lib/Thelia';
                 $domain = 'core';
                 $i18n_directory = THELIA_ROOT . 'core/lib/Thelia/Config/I18n';
                 $walkMode = TemplateHelper::WALK_MODE_PHP;
                 break;
                 // Front-office template
             // Front-office template
             case 'fo':
                 $template = new TemplateDefinition($item_name, TemplateDefinition::FRONT_OFFICE);
                 break;
                 // Back-office template
             // Back-office template
             case 'bo':
                 $template = new TemplateDefinition($item_name, TemplateDefinition::BACK_OFFICE);
                 break;
                 // PDF templates
             // PDF templates
             case 'pf':
                 $template = new TemplateDefinition($item_name, TemplateDefinition::PDF);
                 break;
                 // Email templates
             // Email templates
             case 'ma':
                 $template = new TemplateDefinition($item_name, TemplateDefinition::EMAIL);
                 break;
         }
         if ($template) {
             $directory = $template->getAbsolutePath();
             $i18n_directory = $template->getAbsoluteI18nPath();
             $domain = $template->getTranslationDomain();
             // Load translations files is this template is not the current template
             // as it is not loaded in Thelia.php
             if (!TemplateHelper::getInstance()->isActive($template)) {
                 $this->loadTranslation($i18n_directory, $domain);
             }
         }
         // Load strings to translate
         if ($directory && !empty($domain)) {
             // Save the string set, if the form was submitted
             if ($i18n_directory) {
                 $save_mode = $this->getRequest()->get('save_mode', false);
                 if ($save_mode !== false) {
                     $texts = $this->getRequest()->get('text', array());
                     if (!empty($texts)) {
                         $file = sprintf("%s" . DS . "%s.php", $i18n_directory, $this->getCurrentEditionLocale());
                         $translations = $this->getRequest()->get('translation', array());
                         TemplateHelper::getInstance()->writeTranslation($file, $texts, $translations, true);
                         if ($save_mode == 'stay') {
                             return $this->generateRedirectFromRoute("admin.configuration.translations", $templateArguments);
                         } else {
                             return $this->generateRedirect(URL::getInstance()->adminViewUrl('configuration'));
                         }
                     }
                 }
             }
             // Load strings
             $stringsCount = TemplateHelper::getInstance()->walkDir($directory, $walkMode, $this->getTranslator(), $this->getCurrentEditionLocale(), $domain, $all_strings);
             // Estimate number of fields, and compare to php ini max_input_vars
             $stringsCount = $stringsCount * 2 + 6;
             if ($stringsCount > ini_get('max_input_vars')) {
                 $templateArguments['max_input_vars_warning'] = true;
                 $templateArguments['required_max_input_vars'] = $stringsCount;
                 $templateArguments['current_max_input_vars'] = ini_get('max_input_vars');
             } else {
                 $templateArguments['all_strings'] = $all_strings;
             }
         }
     }
     return $this->render('translations', $templateArguments);
 }