From 13d823cced0acce8939ad1398c1af78def6077a3 Mon Sep 17 00:00:00 2001 From: Thibaud Chauviere Date: Tue, 5 Apr 2016 15:30:45 +0200 Subject: [PATCH 1/2] // Fix lang pack generator --- .../admin/AdminTranslatoolsController.php | 97 ++++++++++--------- 1 file changed, 50 insertions(+), 47 deletions(-) mode change 100644 => 100755 controllers/admin/AdminTranslatoolsController.php diff --git a/controllers/admin/AdminTranslatoolsController.php b/controllers/admin/AdminTranslatoolsController.php old mode 100644 new mode 100755 index a040623..d8e9c01 --- a/controllers/admin/AdminTranslatoolsController.php +++ b/controllers/admin/AdminTranslatoolsController.php @@ -7,6 +7,7 @@ class AdminTranslatoolsController extends ModuleAdminController { public function __construct($standalone=false) { + set_time_limit(0); $this->bootstrap = true; parent::__construct(); @@ -470,88 +471,90 @@ public function processCopyTabs() public function processBuild() { - $specific_language = Tools::getValue('build_code'); - $published = array(); - - if (!$specific_language) { - $tmp = Tools::jsonDecode(Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/get_each_language_pack.php?version='.$this->module->getPackVersion()), true); - if (is_array($tmp)) - foreach ($tmp as $lang) - $published[$lang['iso_code']] = true; + $specific_iso_lang = Tools::strtolower(Tools::getValue('build_code')); + $toPublish = array(); + + if (!$specific_iso_lang) { + $tmpLangs = Tools::jsonDecode(Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/get_each_language_pack.php?version='.$this->module->getPackVersion()), true); + if (is_array($tmpLangs)) { + foreach ($tmpLangs as $lang) { + $toPublish[$lang['iso_code']] = true; + } + } } else { - $published[$specific_language] = true; + $toPublish[$specific_iso_lang] = true; } - require_once dirname(__FILE__).'/../../../../tools/tar/Archive_Tar.php'; + require_once _PS_ROOT_DIR_.'/tools/tar/Archive_Tar.php'; - $dir = realpath(dirname(__FILE__).'/../../packs'); - - if (!$dir) - die("Packs directory not found, aborting."); - - FilesLister::rmDir($dir); - if (!@mkdir($dir, 0777, true)) - die('Could not create directory: '.$dir); - - $te = $this->module->exportNativePack(); + $packs_dir = $this->module->getLocalPath().'packs'; + if (!$packs_dir) { + die("Packs directory not found, aborting."); + } + FilesLister::rmDir($packs_dir); + if (!@mkdir($packs_dir, 0777, true)) { + die('Could not create directory: ' . $packs_dir); + } + $preSetExtractor = $this->module->exportNativePack(); $created = array(); - //$dl = $this->ajaxDownloadTranslationsAction(array()); foreach (scandir(_PS_ROOT_DIR_.'/translations') as $lc) { - if (!preg_match('/^\./', $lc) && is_dir(_PS_ROOT_DIR_.'/translations/'.$lc) && (isset($published[$lc]) || count($published) === 0)) - { - $te->save(); - $te->setLanguage($lc); - $te->fill(); + $isDottedFile = (bool)preg_match('/^\./', $lc); + $isDir = is_dir(_PS_ROOT_DIR_.'/translations/'.$lc); + $hasToBuildThisPack = isset($toPublish[$lc]); + + if (!$isDottedFile && $isDir && $hasToBuildThisPack) { + $preSetExtractor->save(); - $wrote = $te->write($dir); + $preSetExtractor->setLanguage($lc); + $preSetExtractor->fill(); + $preSetExtractor->write($packs_dir); $cwd = getcwd(); - chdir(FilesLister::join($dir, $lc)); + chdir(FilesLister::join($packs_dir, $lc)); - $archname = $lc.'.gzip'; - $archpath = '../'.$archname; + $archiveName = $lc.'.gzip'; + $archivePath = '../'.$archiveName; - if (file_exists($archpath)) - unlink($archpath); + if (file_exists($archivePath)) { + unlink($archivePath); + } - $arch = new Archive_Tar($archpath, 'gz'); + $newArchiveBuilt = new Archive_Tar($archivePath, 'gz'); $add = array(); - foreach (FilesLister::listFiles('.', null, null, true) as $path) - $add[] = preg_replace('#^\./#', '', $path); - - $arch->add($add); - - $created[] = $archname; + foreach (FilesLister::listFiles('.', null, null, true) as $path) { + $add[] = preg_replace('#^\./#', '', $path); + } + $newArchiveBuilt->add($add); + $created[] = $archiveName; chdir($cwd); - - $te->load(); + $preSetExtractor->load(); } } // Put it back - $this->module->exportAsInCodeLanguage(); + //$this->module->exportAsInCodeLanguage(); // ?????? - chdir($dir); + chdir($packs_dir); $allpath = 'all_packs.tar.gz'; - if (file_exists($allpath)) - unlink($allpath); + if (file_exists($allpath)) { + unlink($allpath); + } $all = new Archive_Tar($allpath, 'gz'); $all->add($created); - - ob_end_clean(); header('Content-Description: File Transfer'); header('Content-Type: application/x-gzip'); header('Content-Disposition: attachment; filename='.$allpath); header('Content-Transfer-Encoding: binary'); + header('Content-Length: ' . filesize($allpath)); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); From 2398769cc1af4710b5a289d625b2e850ffea7c5e Mon Sep 17 00:00:00 2001 From: Thibaud Chauviere Date: Tue, 5 Apr 2016 15:34:49 +0200 Subject: [PATCH 2/2] // Fix PSR2 in AdminTranslation --- .../admin/AdminTranslatoolsController.php | 1059 +++++++++-------- 1 file changed, 531 insertions(+), 528 deletions(-) diff --git a/controllers/admin/AdminTranslatoolsController.php b/controllers/admin/AdminTranslatoolsController.php index d8e9c01..28b1551 100755 --- a/controllers/admin/AdminTranslatoolsController.php +++ b/controllers/admin/AdminTranslatoolsController.php @@ -5,552 +5,553 @@ class AdminTranslatoolsController extends ModuleAdminController { - public function __construct($standalone=false) - { + public function __construct($standalone=false) + { set_time_limit(0); - $this->bootstrap = true; - - parent::__construct(); - - $this->crowdin = $this->module->crowdin; - - if (!$standalone) - { - if (!$this->module->active) - Tools::redirectAdmin($this->context->link->getAdminLink('AdminHome')); - } - } - - public function init() - { - $action = Tools::getValue('action'); - $this->action = $action ? $action : 'default'; - $this->template = basename($this->action).'.tpl'; - - parent::init(); - } - - public function postProcess() - { - if ($this->action && !$this->ajax && !method_exists($this, 'process'.Tools::ucfirst($this->action))) - { - $method = $this->action.'Action'; - - foreach (array($this, $this->module) as $object) - { - if (is_callable(array($object, $method))) - { - $data = $object->$method(); - if (is_array($data)) - $this->context->smarty->assign($data); - break; - } - } - - } - else if ($this->action && $this->ajax && !method_exists($this, 'ajaxProcess'.Tools::ucfirst($this->action))) - { - $method = 'ajax'.Tools::ucfirst($this->action).'Action'; - foreach (array($this, $this->module) as $object) - { - if (is_callable(array($object, $method))) - { - $data = $object->$method(Tools::jsonDecode(Tools::file_get_contents('php://input'), true)); - die(Tools::jsonEncode($data)); - } - } - } - else - { - parent::postProcess(); - } - } - - public function getCrowdinPath($version, $ps_path) - { - $m = array(); - if (basename($ps_path) === 'lang_content.php') - $path = $version.'/email_contents'; - // Module file => modules/name - else if (preg_match('#^modules/([^/]+)/#', $ps_path, $m)) - $path = $version.'/modules/'.$m[1]; - // Theme module => modules/name::theme - else if (preg_match('#^themes/([^/]+)/modules/([^/]+)/#', $ps_path, $m)) - $path = $version.'/modules/'.$m[2].'::'.$m[1]; - // Theme language file => theme::name - else if (preg_match('#^themes/([^/]+)/lang#', $ps_path, $m)) - $path = $version.'/theme::'.$m[1]; - // Anything else in theme => name::theme - else if (preg_match('#^themes/([^/]+)/#', $ps_path, $m)) - $path = $version.'/'.basename($ps_path,'.php').'::'.$m[1]; - else if (preg_match('#^mails/.*?/lang.php$#', $ps_path)) - $path = $version.'/email_subjects'; - // Admin, pdf, etc. => name - else - $path = $version.'/'.basename($ps_path,'.php'); - - $path .= '.php'; - - return $path; - } - - public function getCrowdinExportPattern($path) - { - // Get interesting part of path, - // i.e. as if we were at the root of the target - // PrestaShop installation - $path_parts = explode('/translatools/packs/en/', $path); - $path = end($path_parts); - - $export_pattern = str_replace( - array('/en.php', '/en/'), - array('/%locale%.php', '/%locale%/'), - $path - ); - - // Prepend version - $export_pattern = '/'.$this->module->getPackVersion().'/'.$export_pattern; - - return $export_pattern; - } - - public function ajaxExportSourcesAction($payload) - { - $info = $this->crowdin->info(); - - $this->module->exportAsInCodeLanguage(); - $path_to_sources = dirname(__FILE__).'/../../packs/en'; - - $files_to_export = array(); - $dirs_to_create = array(); - - if (is_dir($path_to_sources)) - { - foreach (FilesLister::listFiles($path_to_sources, null, null, true) as $path) - { - if (preg_match('/\.php$/', $path)) - { - $ps_path = Tools::substr($path, Tools::strlen($path_to_sources)+1); - - $dest = $this->getCrowdinPath($this->module->getPackVersion(), $ps_path); - - $files_to_export[] = array( - 'real_relative_path' => FilesLister::cleanPath(Tools::substr(realpath($path), Tools::strlen(_PS_ROOT_DIR_)+1)), - 'dest' => $dest, - 'add_or_update' => isset($info['files'][$dest]) ? 'update' : 'add' - ); - - $dirs_to_create[dirname($dest)] = true; - } - } - - // Determine the new directories - $dirs_to_create = array_diff( - array_keys($dirs_to_create), - $info['directories'] - ); - - // Sort them in ascending order so that - // we don't risk creating a dir twice - // (they are created with parents) - sort($dirs_to_create); - - // List what we need to do - $tasks = array(); - - // Need to create the directories - // before putting files in them - foreach ($dirs_to_create as $dir) - { - $tasks[] = array('action' => 'createDirectory', 'path' => $dir); - } - - // Then we do the file! - foreach ($files_to_export as $data) - { - $tasks[] = array('action' => 'exportSourceFile', 'data' => $data); - } - - return array( - 'success' => true, - 'message' => 'Found sources...', - 'next-payload' => $tasks, - 'next-action' => 'dequeue' - ); - } - else - { - return array( - 'success' => false, - 'message' => 'Could not find sources, please try exporting the "As in code" language!' - ); - } - } - - public function ajaxDequeueAction($payload) - { - $action = array_shift($payload); - - $ok = true; - $message = $action['action']; - - if ($action['action'] === 'createDirectory') - { - $res = $this->crowdin->createDirectory($action['path']); - - if ($res['success']) - $message = 'Created directory: '.$action['path']; - else - { - $ok = false; - $message = $res['error']['message']; - } - } - else if ($action['action'] === 'exportSourceFile') - { - $data = array(); - - $data['src'] = _PS_ROOT_DIR_.'/'.$action['data']['real_relative_path']; - $data['dest'] = $action['data']['dest']; - $data['title'] = basename($data['dest'], '.php'); - $data['export_pattern'] = $this->getCrowdinExportPattern($action['data']['real_relative_path']); - - $res = $this->crowdin->addOrUpdateFile($action['data']['add_or_update'], $data); - - if ($res['success']) - $message = 'Exported file: '.$action['data']['dest']; - else - { - $ok = false; - $message = $res['error']['message']; - } - } - else if ($action['action'] === 'exportTranslationFile') - { - $res = $this->crowdin->uploadTranslations( - $action['language'], - _PS_ROOT_DIR_.'/'.$action['relsrc'], - $action['dest'] - ); - if ($res['success']) - $message = 'Exported translations ('.$action['language'].') for: '.$action['dest']; - else - { - $ok = false; - $message = $res['error']['message']; - } - } - - if (count($payload) > 0) - return array( - 'success' => $ok, - 'message' => $message, - 'next-action' => 'dequeue', - 'next-payload' => $payload - ); - else - return array( - 'success' => $ok, - 'message' => $ok ? 'Done :)' : $message, - ); - } - - public function getTranslationsFromCrowdin($prestashop_language_code) - { - $files = array(); - - $lc = $this->module->getCrowdinLanguageCode($prestashop_language_code); - $data = $this->crowdin->downloadTranslations($lc); - $file = tempnam(null, 'translatools'); - file_put_contents($file, $data); - $za = new ZipArchive(); - $za->open($file); - - $te = new TranslationsExtractor(); - - for ($i=0; $i<$za->numFiles; $i++) - { - $stat = $za->statIndex($i); - $name = $stat['name']; - $m = array(); - $exp = '#^'.preg_quote($this->module->getPackVersion()).'/(.*?\.php)$#'; - if (preg_match($exp, $name, $m)) - { - $target_path = $this->module->getPrestaShopPathFromCrowdinPath($m[1]); - - if ($target_path !== false) - { - $contents = $za->getFromIndex($i); - $files[$target_path] = $te::parseDictionaryFromString($contents); - } - } - } - - return $files; - } - - public function ajaxTestAction() - { - ddd($this->getTranslationsFromCrowdin('fr')); - } - - public function ajaxDownloadTranslationsAction($payload) - { - if (!empty($payload['only_virtual'])) - $language = 'an'; - else if (!empty($payload['language'])) - $language = $payload['language']; - else - $language = 'all'; - - return $this->module->downloadTranslations($language); - } - - public function ajaxExportTranslationsAction($payload) - { - $te = new TranslationsExtractor(); - $te->setRootDir(_PS_ROOT_DIR_); - - $built = $te->buildFromTranslationFiles(dirname(__FILE__).'/../../packs/en'); - - if ($built !== true) - { - return array('success' => false, 'message' => $built); - } - - $tasks = array(); - - $languages = array(); - if ($payload['language'] === '*') - { - foreach (Language::getLanguages() as $lang) - { - $languages[] = $lang['iso_code']; - } - } - else - { - $languages[] = $payload['language']; - } - - // Build the necessary languages - foreach ($languages as $code) - { - // Don't export English or Aragonese to Crowdin! - if ($code === 'en' || $code === 'an') - continue; - - $packs_root = realpath(dirname(__FILE__).'/../../packs/'); - $te->save(); - $te->setLanguage($code); - $te->fill(); - // Remove identical translations - $te->diffFromArrayOfDictionaries($code, $this->getTranslationsFromCrowdin($code)); - $wrote = $te->write($packs_root); - $te->load(); - foreach ($wrote as $file) - { - $relpath = Tools::substr($file, Tools::strlen(_PS_ROOT_DIR_)+1); - $tasks[] = array( - 'action' => 'exportTranslationFile', - 'language' => $this->module->getCrowdinShortCode($code), - 'relsrc' => $relpath, - 'dest' => $this->getCrowdinPath( - $this->module->getPackVersion(), - Tools::substr($file, Tools::strlen($packs_root.'/'.$code)+1) - ) - ); - } - } - - return array( - 'success' => true, - 'message' => 'Built packs, exporting now.', - 'next-action' => 'dequeue', - 'next-payload' => $tasks - ); - } - - public function ajaxSwitchVirtualLanguageAction($payload) - { - $jipt_id_lang = Language::getIdByIso('an'); - - if ($payload['value']) - { - if ($jipt_id_lang) - { - $this->context->cookie->JIPT_PREVIOUS_ID_LANG = $this->context->employee->id_lang; - - - $this->context->employee->id_lang = $jipt_id_lang; - $this->context->employee->save(); - - return array('success' => true, 'language' => $jipt_id_lang); - } - - return array('success' => false, 'language' => $this->context->employee->id_lang); - } - else - { - $language_to_set = $this->context->cookie->JIPT_PREVIOUS_ID_LANG; - - if ($language_to_set == $jipt_id_lang || !$language_to_set) - $language_to_set = Configuration::get('PS_LANG_DEFAULT'); - - $this->context->employee->id_lang = $language_to_set; - $this->context->employee->save(); - unset($this->context->cookie->JIPT_PREVIOUS_ID_LANG); - return array('success' => true, 'language' => $language_to_set); - } - } - - public function processCopyTabs() - { - $installer_dir = null; - foreach (array('install-dev', 'install') as $dir) - { - $path = FilesLister::join(_PS_ROOT_DIR_, $dir); - if (is_dir($path)) - { - $installer_dir = $path; - break; - } - } - - if ($installer_dir === null) - { - die("Could not find installer dir!"); - } - - $langs_dir = FilesLister::join($installer_dir, 'langs'); - - $en_tabs_xml = simplexml_load_string(file_get_contents(FilesLister::join($langs_dir, 'en/data/tab.xml'))); - - $tabs_mapping_xml = simplexml_load_string(file_get_contents(FilesLister::join($installer_dir, 'data/xml/tab.xml'))); - $id_to_class_name = array(); - - foreach ($tabs_mapping_xml->entities->tab as $tab) { - $id_to_class_name[(string)$tab['id']] = (string)$tab->class_name; - } - - foreach (scandir($langs_dir) as $lang) - { - if ($lang[0] === '.') { - continue; - } - - $tabs_xml_path = FilesLister::join($langs_dir, $lang.'/data/tab.xml'); - - if (!file_exists($tabs_xml_path)) { - continue; - } - - $tabs_php_path = FilesLister::join(_PS_ROOT_DIR_, 'translations/' . $lang . '/tabs.php'); - if (!file_exists($tabs_php_path)) { - continue; - } - - $translations = require $tabs_php_path; - - $xml = clone $en_tabs_xml; - $xml->addAttribute('encoding', 'UTF-8'); - - foreach ($xml->tab as $tab) { - $id = (string)$tab['id']; - if (array_key_exists($id, $id_to_class_name)) { - $class_name = $id_to_class_name[$id]; - if (array_key_exists($class_name, $translations)) { - $tab['name'] = $translations[$class_name]; - } - } - } - - file_put_contents($tabs_xml_path, $xml->asXML()); - } - } - - public function processBuild() - { - $specific_iso_lang = Tools::strtolower(Tools::getValue('build_code')); - $toPublish = array(); - - if (!$specific_iso_lang) { - $tmpLangs = Tools::jsonDecode(Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/get_each_language_pack.php?version='.$this->module->getPackVersion()), true); - if (is_array($tmpLangs)) { + $this->bootstrap = true; + + parent::__construct(); + + $this->crowdin = $this->module->crowdin; + + if (!$standalone) { + if (!$this->module->active) { + Tools::redirectAdmin($this->context->link->getAdminLink('AdminHome')); + } + } + } + + public function init() + { + $action = Tools::getValue('action'); + $this->action = $action ? $action : 'default'; + $this->template = basename($this->action).'.tpl'; + + parent::init(); + } + + public function postProcess() + { + if ($this->action && !$this->ajax && !method_exists($this, 'process'.Tools::ucfirst($this->action))) { + $method = $this->action.'Action'; + + foreach (array($this, $this->module) as $object) + { + if (is_callable(array($object, $method))) { + $data = $object->$method(); + if (is_array($data)) { + $this->context->smarty->assign($data); + } + break; + } + } + + } + else if ($this->action && $this->ajax && !method_exists($this, 'ajaxProcess'.Tools::ucfirst($this->action))) { + $method = 'ajax'.Tools::ucfirst($this->action).'Action'; + foreach (array($this, $this->module) as $object) + { + if (is_callable(array($object, $method))) { + $data = $object->$method(Tools::jsonDecode(Tools::file_get_contents('php://input'), true)); + die(Tools::jsonEncode($data)); + } + } + } + else + { + parent::postProcess(); + } + } + + public function getCrowdinPath($version, $ps_path) + { + $m = array(); + if (basename($ps_path) === 'lang_content.php') { + $path = $version.'/email_contents'; + } + // Module file => modules/name + else if (preg_match('#^modules/([^/]+)/#', $ps_path, $m)) { + $path = $version.'/modules/'.$m[1]; + } + // Theme module => modules/name::theme + else if (preg_match('#^themes/([^/]+)/modules/([^/]+)/#', $ps_path, $m)) { + $path = $version.'/modules/'.$m[2].'::'.$m[1]; + } + // Theme language file => theme::name + else if (preg_match('#^themes/([^/]+)/lang#', $ps_path, $m)) { + $path = $version.'/theme::'.$m[1]; + } + // Anything else in theme => name::theme + else if (preg_match('#^themes/([^/]+)/#', $ps_path, $m)) { + $path = $version.'/'.basename($ps_path, '.php').'::'.$m[1]; + } + else if (preg_match('#^mails/.*?/lang.php$#', $ps_path)) { + $path = $version.'/email_subjects'; + } + // Admin, pdf, etc. => name + else { + $path = $version.'/'.basename($ps_path, '.php'); + } + + $path .= '.php'; + + return $path; + } + + public function getCrowdinExportPattern($path) + { + // Get interesting part of path, + // i.e. as if we were at the root of the target + // PrestaShop installation + $path_parts = explode('/translatools/packs/en/', $path); + $path = end($path_parts); + + $export_pattern = str_replace( + array('/en.php', '/en/'), + array('/%locale%.php', '/%locale%/'), + $path + ); + + // Prepend version + $export_pattern = '/'.$this->module->getPackVersion().'/'.$export_pattern; + + return $export_pattern; + } + + public function ajaxExportSourcesAction($payload) + { + $info = $this->crowdin->info(); + + $this->module->exportAsInCodeLanguage(); + $path_to_sources = dirname(__FILE__).'/../../packs/en'; + + $files_to_export = array(); + $dirs_to_create = array(); + + if (is_dir($path_to_sources)) { + foreach (FilesLister::listFiles($path_to_sources, null, null, true) as $path) + { + if (preg_match('/\.php$/', $path)) { + $ps_path = Tools::substr($path, Tools::strlen($path_to_sources)+1); + + $dest = $this->getCrowdinPath($this->module->getPackVersion(), $ps_path); + + $files_to_export[] = array( + 'real_relative_path' => FilesLister::cleanPath(Tools::substr(realpath($path), Tools::strlen(_PS_ROOT_DIR_)+1)), + 'dest' => $dest, + 'add_or_update' => isset($info['files'][$dest]) ? 'update' : 'add' + ); + + $dirs_to_create[dirname($dest)] = true; + } + } + + // Determine the new directories + $dirs_to_create = array_diff( + array_keys($dirs_to_create), + $info['directories'] + ); + + // Sort them in ascending order so that + // we don't risk creating a dir twice + // (they are created with parents) + sort($dirs_to_create); + + // List what we need to do + $tasks = array(); + + // Need to create the directories + // before putting files in them + foreach ($dirs_to_create as $dir) + { + $tasks[] = array('action' => 'createDirectory', 'path' => $dir); + } + + // Then we do the file! + foreach ($files_to_export as $data) + { + $tasks[] = array('action' => 'exportSourceFile', 'data' => $data); + } + + return array( + 'success' => true, + 'message' => 'Found sources...', + 'next-payload' => $tasks, + 'next-action' => 'dequeue' + ); + } + else + { + return array( + 'success' => false, + 'message' => 'Could not find sources, please try exporting the "As in code" language!' + ); + } + } + + public function ajaxDequeueAction($payload) + { + $action = array_shift($payload); + + $ok = true; + $message = $action['action']; + + if ($action['action'] === 'createDirectory') { + $res = $this->crowdin->createDirectory($action['path']); + + if ($res['success']) { + $message = 'Created directory: '.$action['path']; + } + else + { + $ok = false; + $message = $res['error']['message']; + } + } + else if ($action['action'] === 'exportSourceFile') { + $data = array(); + + $data['src'] = _PS_ROOT_DIR_.'/'.$action['data']['real_relative_path']; + $data['dest'] = $action['data']['dest']; + $data['title'] = basename($data['dest'], '.php'); + $data['export_pattern'] = $this->getCrowdinExportPattern($action['data']['real_relative_path']); + + $res = $this->crowdin->addOrUpdateFile($action['data']['add_or_update'], $data); + + if ($res['success']) { + $message = 'Exported file: '.$action['data']['dest']; + } + else + { + $ok = false; + $message = $res['error']['message']; + } + } + else if ($action['action'] === 'exportTranslationFile') { + $res = $this->crowdin->uploadTranslations( + $action['language'], + _PS_ROOT_DIR_.'/'.$action['relsrc'], + $action['dest'] + ); + if ($res['success']) { + $message = 'Exported translations ('.$action['language'].') for: '.$action['dest']; + } + else + { + $ok = false; + $message = $res['error']['message']; + } + } + + if (count($payload) > 0) { + return array( + 'success' => $ok, + 'message' => $message, + 'next-action' => 'dequeue', + 'next-payload' => $payload + ); + } + else { + return array( + 'success' => $ok, + 'message' => $ok ? 'Done :)' : $message, + ); + } + } + + public function getTranslationsFromCrowdin($prestashop_language_code) + { + $files = array(); + + $lc = $this->module->getCrowdinLanguageCode($prestashop_language_code); + $data = $this->crowdin->downloadTranslations($lc); + $file = tempnam(null, 'translatools'); + file_put_contents($file, $data); + $za = new ZipArchive(); + $za->open($file); + + $te = new TranslationsExtractor(); + + for ($i=0; $i<$za->numFiles; $i++) + { + $stat = $za->statIndex($i); + $name = $stat['name']; + $m = array(); + $exp = '#^'.preg_quote($this->module->getPackVersion()).'/(.*?\.php)$#'; + if (preg_match($exp, $name, $m)) { + $target_path = $this->module->getPrestaShopPathFromCrowdinPath($m[1]); + + if ($target_path !== false) { + $contents = $za->getFromIndex($i); + $files[$target_path] = $te::parseDictionaryFromString($contents); + } + } + } + + return $files; + } + + public function ajaxTestAction() + { + ddd($this->getTranslationsFromCrowdin('fr')); + } + + public function ajaxDownloadTranslationsAction($payload) + { + if (!empty($payload['only_virtual'])) { + $language = 'an'; + } + else if (!empty($payload['language'])) { + $language = $payload['language']; + } + else { + $language = 'all'; + } + + return $this->module->downloadTranslations($language); + } + + public function ajaxExportTranslationsAction($payload) + { + $te = new TranslationsExtractor(); + $te->setRootDir(_PS_ROOT_DIR_); + + $built = $te->buildFromTranslationFiles(dirname(__FILE__).'/../../packs/en'); + + if ($built !== true) { + return array('success' => false, 'message' => $built); + } + + $tasks = array(); + + $languages = array(); + if ($payload['language'] === '*') { + foreach (Language::getLanguages() as $lang) + { + $languages[] = $lang['iso_code']; + } + } + else + { + $languages[] = $payload['language']; + } + + // Build the necessary languages + foreach ($languages as $code) + { + // Don't export English or Aragonese to Crowdin! + if ($code === 'en' || $code === 'an') { + continue; + } + + $packs_root = realpath(dirname(__FILE__).'/../../packs/'); + $te->save(); + $te->setLanguage($code); + $te->fill(); + // Remove identical translations + $te->diffFromArrayOfDictionaries($code, $this->getTranslationsFromCrowdin($code)); + $wrote = $te->write($packs_root); + $te->load(); + foreach ($wrote as $file) + { + $relpath = Tools::substr($file, Tools::strlen(_PS_ROOT_DIR_)+1); + $tasks[] = array( + 'action' => 'exportTranslationFile', + 'language' => $this->module->getCrowdinShortCode($code), + 'relsrc' => $relpath, + 'dest' => $this->getCrowdinPath( + $this->module->getPackVersion(), + Tools::substr($file, Tools::strlen($packs_root.'/'.$code)+1) + ) + ); + } + } + + return array( + 'success' => true, + 'message' => 'Built packs, exporting now.', + 'next-action' => 'dequeue', + 'next-payload' => $tasks + ); + } + + public function ajaxSwitchVirtualLanguageAction($payload) + { + $jipt_id_lang = Language::getIdByIso('an'); + + if ($payload['value']) { + if ($jipt_id_lang) { + $this->context->cookie->JIPT_PREVIOUS_ID_LANG = $this->context->employee->id_lang; + + + $this->context->employee->id_lang = $jipt_id_lang; + $this->context->employee->save(); + + return array('success' => true, 'language' => $jipt_id_lang); + } + + return array('success' => false, 'language' => $this->context->employee->id_lang); + } + else + { + $language_to_set = $this->context->cookie->JIPT_PREVIOUS_ID_LANG; + + if ($language_to_set == $jipt_id_lang || !$language_to_set) { + $language_to_set = Configuration::get('PS_LANG_DEFAULT'); + } + + $this->context->employee->id_lang = $language_to_set; + $this->context->employee->save(); + unset($this->context->cookie->JIPT_PREVIOUS_ID_LANG); + return array('success' => true, 'language' => $language_to_set); + } + } + + public function processCopyTabs() + { + $installer_dir = null; + foreach (array('install-dev', 'install') as $dir) + { + $path = FilesLister::join(_PS_ROOT_DIR_, $dir); + if (is_dir($path)) { + $installer_dir = $path; + break; + } + } + + if ($installer_dir === null) { + die("Could not find installer dir!"); + } + + $langs_dir = FilesLister::join($installer_dir, 'langs'); + + $en_tabs_xml = simplexml_load_string(file_get_contents(FilesLister::join($langs_dir, 'en/data/tab.xml'))); + + $tabs_mapping_xml = simplexml_load_string(file_get_contents(FilesLister::join($installer_dir, 'data/xml/tab.xml'))); + $id_to_class_name = array(); + + foreach ($tabs_mapping_xml->entities->tab as $tab) { + $id_to_class_name[(string)$tab['id']] = (string)$tab->class_name; + } + + foreach (scandir($langs_dir) as $lang) + { + if ($lang[0] === '.') { + continue; + } + + $tabs_xml_path = FilesLister::join($langs_dir, $lang.'/data/tab.xml'); + + if (!file_exists($tabs_xml_path)) { + continue; + } + + $tabs_php_path = FilesLister::join(_PS_ROOT_DIR_, 'translations/' . $lang . '/tabs.php'); + if (!file_exists($tabs_php_path)) { + continue; + } + + $translations = include $tabs_php_path; + + $xml = clone $en_tabs_xml; + $xml->addAttribute('encoding', 'UTF-8'); + + foreach ($xml->tab as $tab) { + $id = (string)$tab['id']; + if (array_key_exists($id, $id_to_class_name)) { + $class_name = $id_to_class_name[$id]; + if (array_key_exists($class_name, $translations)) { + $tab['name'] = $translations[$class_name]; + } + } + } + + file_put_contents($tabs_xml_path, $xml->asXML()); + } + } + + public function processBuild() + { + $specific_iso_lang = Tools::strtolower(Tools::getValue('build_code')); + $toPublish = array(); + + if (!$specific_iso_lang) { + $tmpLangs = Tools::jsonDecode(Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/get_each_language_pack.php?version='.$this->module->getPackVersion()), true); + if (is_array($tmpLangs)) { foreach ($tmpLangs as $lang) { $toPublish[$lang['iso_code']] = true; } } - } else { + } else { $toPublish[$specific_iso_lang] = true; - } + } - require_once _PS_ROOT_DIR_.'/tools/tar/Archive_Tar.php'; + include_once _PS_ROOT_DIR_.'/tools/tar/Archive_Tar.php'; - $packs_dir = $this->module->getLocalPath().'packs'; - if (!$packs_dir) { + $packs_dir = $this->module->getLocalPath().'packs'; + if (!$packs_dir) { die("Packs directory not found, aborting."); } - FilesLister::rmDir($packs_dir); - if (!@mkdir($packs_dir, 0777, true)) { + FilesLister::rmDir($packs_dir); + if (!@mkdir($packs_dir, 0777, true)) { die('Could not create directory: ' . $packs_dir); } - $preSetExtractor = $this->module->exportNativePack(); - $created = array(); + $preSetExtractor = $this->module->exportNativePack(); + $created = array(); - foreach (scandir(_PS_ROOT_DIR_.'/translations') as $lc) - { + foreach (scandir(_PS_ROOT_DIR_.'/translations') as $lc) + { $isDottedFile = (bool)preg_match('/^\./', $lc); $isDir = is_dir(_PS_ROOT_DIR_.'/translations/'.$lc); $hasToBuildThisPack = isset($toPublish[$lc]); - if (!$isDottedFile && $isDir && $hasToBuildThisPack) { + if (!$isDottedFile && $isDir && $hasToBuildThisPack) { $preSetExtractor->save(); $preSetExtractor->setLanguage($lc); $preSetExtractor->fill(); - $preSetExtractor->write($packs_dir); + $preSetExtractor->write($packs_dir); - $cwd = getcwd(); - chdir(FilesLister::join($packs_dir, $lc)); + $cwd = getcwd(); + chdir(FilesLister::join($packs_dir, $lc)); - $archiveName = $lc.'.gzip'; - $archivePath = '../'.$archiveName; + $archiveName = $lc.'.gzip'; + $archivePath = '../'.$archiveName; - if (file_exists($archivePath)) { + if (file_exists($archivePath)) { unlink($archivePath); } - $newArchiveBuilt = new Archive_Tar($archivePath, 'gz'); + $newArchiveBuilt = new Archive_Tar($archivePath, 'gz'); - $add = array(); + $add = array(); - foreach (FilesLister::listFiles('.', null, null, true) as $path) { + foreach (FilesLister::listFiles('.', null, null, true) as $path) { $add[] = preg_replace('#^\./#', '', $path); } $newArchiveBuilt->add($add); - $created[] = $archiveName; - chdir($cwd); + $created[] = $archiveName; + chdir($cwd); $preSetExtractor->load(); - } - } + } + } - // Put it back - //$this->module->exportAsInCodeLanguage(); // ?????? + // Put it back + //$this->module->exportAsInCodeLanguage(); // ?????? - chdir($packs_dir); + chdir($packs_dir); - $allpath = 'all_packs.tar.gz'; - if (file_exists($allpath)) { + $allpath = 'all_packs.tar.gz'; + if (file_exists($allpath)) { unlink($allpath); } - $all = new Archive_Tar($allpath, 'gz'); - $all->add($created); - header('Content-Description: File Transfer'); + $all = new Archive_Tar($allpath, 'gz'); + $all->add($created); + header('Content-Description: File Transfer'); header('Content-Type: application/x-gzip'); header('Content-Disposition: attachment; filename='.$allpath); header('Content-Transfer-Encoding: binary'); @@ -560,28 +561,30 @@ public function processBuild() header('Pragma: public'); readfile($allpath); exit; - } - - public function checkQualityAction() - { - $with_tags = array(); - - $extractor = $this->module->exportAsInCodeLanguage(); - foreach ($extractor->getFiles() as $name => $data) - foreach ($data as $key => $details) - { - if ($details['type'] === 'installer') - continue; - else if ($details['type'] === 'mailContent') - continue; - - $message = $details['message']; - if ($message !== strip_tags($message)) - { - $with_tags[] = $message; - } - } - - return array('with_tags' => $with_tags); - } + } + + public function checkQualityAction() + { + $with_tags = array(); + + $extractor = $this->module->exportAsInCodeLanguage(); + foreach ($extractor->getFiles() as $name => $data) { + foreach ($data as $key => $details) + { + if ($details['type'] === 'installer') { + continue; + } + else if ($details['type'] === 'mailContent') { + continue; + } + + $message = $details['message']; + if ($message !== strip_tags($message)) { + $with_tags[] = $message; + } + } + } + + return array('with_tags' => $with_tags); + } }