PageRenderTime 70ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/controllers/admin/AdminTranslationsController.php

https://bitbucket.org/yhjohn/ayanapure.com
PHP | 2591 lines | 1835 code | 302 blank | 454 comment | 358 complexity | 843b8fbac240f4a7e92a45a2b1eabce9 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @version Release: $Revision: 7310 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. define ('TEXTAREA_SIZED', 70);
  28. class AdminTranslationsControllerCore extends AdminController
  29. {
  30. /** Name of theme by default */
  31. const DEFAULT_THEME_NAME = 'default';
  32. /** @var string : Link which list all pack of language */
  33. protected $link_lang_pack = 'http://www.prestashop.com/download/lang_packs/get_each_language_pack.php';
  34. /** @var int : number of sentence which can be translated */
  35. protected $total_expression = 0;
  36. /** @var int : number of sentence which aren't translated */
  37. protected $missing_translations = 0;
  38. /** @var array : List of ISO code for all languages */
  39. protected $all_iso_lang = array();
  40. /** @var array */
  41. protected $modules_translations = array();
  42. /** @var array : List of folder which must be ignored */
  43. protected static $ignore_folder = array('.', '..', '.svn', '.htaccess', 'index.php');
  44. /** @var array : List of theme by translation type : FRONT, BACK, ERRORS... */
  45. protected $translations_informations = array();
  46. /** @var array : List of theme by translation type : FRONT, BACK, ERRORS... */
  47. protected $translations_type_for_theme = array('front', 'modules', 'pdf', 'mails');
  48. /** @var array : List of all languages */
  49. protected $languages;
  50. /** @var array : List of all themes */
  51. protected $themes;
  52. /** @var string : Directory of selected theme */
  53. protected $theme_selected;
  54. /** @var string : Name of translations type */
  55. protected $type_selected;
  56. /** @var object : Language for the selected language */
  57. protected $lang_selected;
  58. /** @var boolean : Is true if number of var exceed the suhosin request or post limit */
  59. protected $post_limit_exceed = false;
  60. public function __construct()
  61. {
  62. $this->multishop_context = Shop::CONTEXT_ALL;
  63. parent::__construct();
  64. $this->table = 'translations';
  65. // Include all file for create or read an archive
  66. include_once(_PS_ADMIN_DIR_.'/../tools/tar/Archive_Tar.php');
  67. include_once(_PS_ADMIN_DIR_.'/../tools/pear/PEAR.php');
  68. }
  69. /*
  70. * Set the type which is selected
  71. */
  72. public function setTypeSelected($type_selected)
  73. {
  74. $this->type_selected = $type_selected;
  75. }
  76. /**
  77. * AdminController::initContent() override
  78. * @see AdminController::initContent()
  79. */
  80. public function initContent()
  81. {
  82. if (!is_null($this->type_selected))
  83. {
  84. $method_name = 'initForm'.$this->type_selected;
  85. if (method_exists($this, $method_name))
  86. $this->content = $this->initForm($method_name);
  87. else
  88. {
  89. $this->errors[] = sprintf(Tools::displayError('"%s" does not exist. Maybe you typed the URL manually.'), $this->type_selected);
  90. $this->content = $this->initMain();
  91. }
  92. }
  93. else
  94. $this->content = $this->initMain();
  95. $this->context->smarty->assign(array('content' => $this->content));
  96. }
  97. /**
  98. * This function create vars by default and call the good method for generate form
  99. *
  100. * @param $method_name
  101. * @return call the method $this->method_name()
  102. */
  103. public function initForm($method_name)
  104. {
  105. // Create a title for each translation page
  106. $title = sprintf(
  107. $this->l('%1$s (Language: %2$s, Theme: %3$s)'),
  108. $this->translations_informations[$this->type_selected]['name'],
  109. $this->lang_selected->name,
  110. $this->theme_selected
  111. );
  112. // Set vars for all forms
  113. $this->tpl_view_vars = array(
  114. 'lang' => $this->lang_selected->iso_code,
  115. 'title' => $title,
  116. 'type' => $this->type_selected,
  117. 'theme' => $this->theme_selected,
  118. 'post_limit_exceeded' => $this->post_limit_exceed,
  119. 'url_submit' => self::$currentIndex.'&submitTranslations'.ucfirst($this->type_selected).'=1&token='.$this->token,
  120. 'toggle_button' => $this->displayToggleButton(),
  121. 'textarea_sized' => TEXTAREA_SIZED,
  122. 'auto_translate' => ''
  123. );
  124. // Call method initForm for a type
  125. return $this->{$method_name}();
  126. }
  127. /**
  128. * AdminController::initToolbar() override
  129. * @see AdminController::initToolbar()
  130. */
  131. public function initToolbar()
  132. {
  133. $this->toolbar_btn['save-and-stay'] = array(
  134. 'short' => 'SaveAndStay',
  135. 'href' => '#',
  136. 'desc' => $this->l('Save and stay'),
  137. );
  138. $this->toolbar_btn['save'] = array(
  139. 'href' => '#',
  140. 'desc' => $this->l('Update translations')
  141. );
  142. $this->toolbar_btn['cancel'] = array(
  143. 'href' => self::$currentIndex.'&token='.$this->token,
  144. 'desc' => $this->l('Cancel')
  145. );
  146. }
  147. /**
  148. * Generate the Main page
  149. */
  150. public function initMain()
  151. {
  152. // Block add/update a language
  153. $packs_to_install = array();
  154. $packs_to_update = array();
  155. $token = Tools::getAdminToken('AdminLanguages'.(int)Tab::getIdFromClassName('AdminLanguages').(int)$this->context->employee->id);
  156. $file_name = $this->link_lang_pack.'?version='._PS_VERSION_;
  157. $array_stream_context = array('http' => array('method' => 'GET', 'timeout' => 5));
  158. if ($lang_packs = Tools::file_get_contents($file_name, false, @stream_context_create($array_stream_context)))
  159. // Notice : for php < 5.2 compatibility, Tools::jsonDecode. The second parameter to true will set us
  160. if ($lang_packs != '' && $lang_packs = Tools::jsonDecode($lang_packs, true))
  161. foreach ($lang_packs as $key => $lang_pack)
  162. {
  163. if (!Language::isInstalled($lang_pack['iso_code']))
  164. $packs_to_install[$key] = $lang_pack;
  165. else
  166. $packs_to_update[$key] = $lang_pack;
  167. }
  168. $this->tpl_view_vars = array(
  169. 'theme_default' => self::DEFAULT_THEME_NAME,
  170. 'theme_lang_dir' =>_THEME_LANG_DIR_,
  171. 'token' => $this->token,
  172. 'languages' => $this->languages,
  173. 'translations_type' => $this->translations_informations,
  174. 'translations_type_for_theme' => $this->translations_type_for_theme,
  175. 'packs_to_install' => $packs_to_install,
  176. 'packs_to_update' => $packs_to_update,
  177. 'url_submit' => self::$currentIndex.'&token='.$this->token,
  178. 'themes' => $this->themes,
  179. 'id_theme_current' => $this->context->shop->id_theme,
  180. 'url_create_language' => 'index.php?controller=AdminLanguages&addlang&token='.$token,
  181. );
  182. $this->toolbar_scroll = false;
  183. $this->base_tpl_view = 'main.tpl';
  184. return parent::renderView();
  185. }
  186. /**
  187. * This method merge each arrays of modules translation in the array of modules translations
  188. */
  189. protected function getModuleTranslations()
  190. {
  191. global $_MODULE;
  192. $name_var = $this->translations_informations[$this->type_selected]['var'];
  193. if (!isset($_MODULE) && !isset($GLOBALS[$name_var]))
  194. $GLOBALS[$name_var] = array();
  195. else if (isset($_MODULE))
  196. if (is_array($GLOBALS[$name_var]) && is_array($_MODULE))
  197. $GLOBALS[$name_var] = array_merge($GLOBALS[$name_var], $_MODULE);
  198. else
  199. $GLOBALS[$name_var] = $_MODULE;
  200. }
  201. /**
  202. * This method is only used by AdminTranslations::submitCopyLang().
  203. *
  204. * It try to create folder in new theme.
  205. *
  206. * When a translation file is copied for a module, its translation key is wrong.
  207. * We have to change the translation key and rewrite the file.
  208. *
  209. * @param string $dest file name
  210. * @return bool
  211. */
  212. protected function checkDirAndCreate($dest)
  213. {
  214. $bool = true;
  215. // To get only folder path
  216. $path = dirname($dest);
  217. // If folder wasn't already added
  218. if (!Tools::file_exists_cache($path))
  219. if (!mkdir($path, 0777, true))
  220. {
  221. $bool &= false;
  222. $this->errors[] = sprintf($this->l('Cannot create the folder "%s". Check directory writing permisions.'), $path);
  223. }
  224. return $bool;
  225. }
  226. /**
  227. * Read the Post var and write the translation file.
  228. * This method overwrites the old translation file.
  229. *
  230. * @param bool $override_file : set true if this file is a override
  231. */
  232. protected function writeTranslationFile($override_file = false)
  233. {
  234. $type = Tools::toCamelCase($this->type_selected, true);
  235. $translation_informations = $this->translations_informations[$this->type_selected];
  236. if ($override_file)
  237. $file_path = $translation_informations['override']['dir'].$translation_informations['override']['file'];
  238. else
  239. $file_path = $translation_informations['dir'].$translation_informations['file'];
  240. if (!file_exists($file_path))
  241. throw new PrestaShopException(sprintf(Tools::displayError('This file doesn\'t exists: "%s". Please create this file.'), $file_path));
  242. if ($fd = fopen($file_path, 'w'))
  243. {
  244. // Get value of button save and stay
  245. $save_and_stay = Tools::getValue('submitTranslations'.$type.'AndStay');
  246. // Get language
  247. $lang = strtolower(Tools::getValue('lang'));
  248. // Unset all POST which are not translations
  249. unset(
  250. $_POST['submitTranslations'.$type],
  251. $_POST['submitTranslations'.$type.'AndStay'],
  252. $_POST['lang'],
  253. $_POST['token'],
  254. $_POST['theme'],
  255. $_POST['type']
  256. );
  257. // Get all POST which aren't empty
  258. $to_insert = array();
  259. foreach ($_POST as $key => $value)
  260. if (!empty($value))
  261. $to_insert[$key] = $value;
  262. // translations array is ordered by key (easy merge)
  263. ksort($to_insert);
  264. $tab = $translation_informations['var'];
  265. fwrite($fd, "<?php\n\nglobal \$".$tab.";\n\$".$tab." = array();\n");
  266. foreach ($to_insert as $key => $value)
  267. fwrite($fd, '$'.$tab.'[\''.pSQL($key, true).'\'] = \''.pSQL($value, true).'\';'."\n");
  268. fwrite($fd, "\n?>");
  269. fclose($fd);
  270. // Redirect
  271. if ($save_and_stay)
  272. $this->redirect(true);
  273. else
  274. $this->redirect();
  275. }
  276. else
  277. throw new PrestaShopException(sprintf(Tools::displayError('Cannot write this file: "%s"'), $file_path));
  278. }
  279. public function submitCopyLang()
  280. {
  281. if (!($from_lang = strval(Tools::getValue('fromLang'))) || !($to_lang = strval(Tools::getValue('toLang'))))
  282. $this->errors[] = $this->l('You must select 2 languages in order to copy data from one to another');
  283. else if (!($from_theme = strval(Tools::getValue('fromTheme'))) || !($to_theme = strval(Tools::getValue('toTheme'))))
  284. $this->errors[] = $this->l('You must select 2 themes in order to copy data from one to another');
  285. else if (!Language::copyLanguageData(Language::getIdByIso($from_lang), Language::getIdByIso($to_lang)))
  286. $this->errors[] = $this->l('An error occurred while copying data');
  287. else if ($from_lang == $to_lang && $from_theme == $to_theme)
  288. $this->errors[] = $this->l('Nothing to copy! (same language and theme)');
  289. if (count($this->errors))
  290. return;
  291. $bool = true;
  292. $items = Language::getFilesList($from_lang, $from_theme, $to_lang, $to_theme, false, false, true);
  293. foreach ($items as $source => $dest)
  294. {
  295. $bool &= $this->checkDirAndCreate($dest);
  296. $bool &= @copy($source, $dest);
  297. if (strpos($dest, 'modules') && basename($source) === $from_lang.'.php' && $bool !== false)
  298. $bool &= $this->changeModulesKeyTranslation($dest, $from_theme, $to_theme);
  299. }
  300. if ($bool)
  301. $this->redirect(false, 14);
  302. $this->errors[] = $this->l('A part of the data has been copied but some language files could not be found or copied');
  303. }
  304. /**
  305. * Change the key translation to according it to theme name.
  306. *
  307. * @param string $path
  308. * @param string $theme_from
  309. * @param string $theme_to
  310. * @return boolean
  311. */
  312. public function changeModulesKeyTranslation($path, $theme_from, $theme_to)
  313. {
  314. $content = file_get_contents($path);
  315. $arr_replace = array();
  316. $bool_flag = true;
  317. if (preg_match_all('#\$_MODULE\[\'([^\']+)\'\]#Ui', $content, $matches))
  318. {
  319. foreach ($matches[1] as $key => $value)
  320. $arr_replace[$value] = str_replace($theme_from, $theme_to, $value);
  321. $content = str_replace(array_keys($arr_replace), array_values($arr_replace), $content);
  322. $bool_flag = (file_put_contents($path, $content) === false) ? false : true;
  323. }
  324. return $bool_flag;
  325. }
  326. public function exportTabs()
  327. {
  328. // Get name tabs by iso code
  329. $tabs = Tab::getTabs($this->lang_selected->id);
  330. // Get name of the default tabs
  331. $tabs_default_lang = Tab::getTabs(1);
  332. $tabs_default = array();
  333. foreach ($tabs_default_lang as $tab)
  334. $tabs_default[$tab['class_name']] = pSQL($tab['name']);
  335. // Create content
  336. $content = "<?php\n\n\$tabs = array();";
  337. if (!empty($tabs))
  338. foreach ($tabs as $tab)
  339. if ($tabs_default[$tab['class_name']] != pSQL($tab['name']))
  340. $content .= "\n\$tabs['".$tab['class_name']."'] = '".pSQL($tab['name'])."';";
  341. $content .= "\n\nreturn \$tabs;";
  342. $dir = _PS_TRANSLATIONS_DIR_.$this->lang_selected->iso_code.DIRECTORY_SEPARATOR;
  343. $path = $dir.'tabs.php';
  344. // Check if tabs.php exists for the selected Iso Code
  345. if (!Tools::file_exists_cache($dir))
  346. if (!mkdir($dir, 0777, true))
  347. throw new PrestaShopException('The file '.$dir.' cannot be created.');
  348. if (!file_put_contents($path, $content))
  349. throw new PrestaShopException('File "'.$path.'" doesn\'t exists and cannot be created in '.$dir);
  350. if (!is_writable($path))
  351. $this->displayWarning(sprintf(Tools::displayError('This file must be writable: %s'), $path));
  352. }
  353. public function submitExportLang()
  354. {
  355. if ($this->lang_selected->iso_code && $this->theme_selected)
  356. {
  357. $this->exportTabs();
  358. $items = array_flip(Language::getFilesList($this->lang_selected->iso_code, $this->theme_selected, false, false, false, false, true));
  359. $gz = new Archive_Tar(_PS_TRANSLATIONS_DIR_.'/export/'.$this->lang_selected->iso_code.'.gzip', true);
  360. $file_name = Tools::getCurrentUrlProtocolPrefix().Tools::getShopDomain().__PS_BASE_URI__.'translations/export/'.$this->lang_selected->iso_code.'.gzip';
  361. if ($gz->createModify($items, null, _PS_ROOT_DIR_));
  362. {
  363. ob_start();
  364. header('Pragma: public');
  365. header('Expires: 0');
  366. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  367. header('Cache-Control: public');
  368. header('Content-Description: File Transfer');
  369. header('Content-type: application/octet-stream');
  370. header('Content-Disposition: attachment; filename="'.$this->lang_selected->iso_code.'.gzip'.'"');
  371. header('Content-Transfer-Encoding: binary');
  372. ob_end_flush();
  373. @readfile($file_name);
  374. }
  375. $this->errors[] = Tools::displayError('An error occurred while creating archive.');
  376. }
  377. $this->errors[] = Tools::displayError('Please choose a language and a theme.');
  378. }
  379. public static function checkAndAddMailsFiles($iso_code, $files_list)
  380. {
  381. // 1 - Scan mails files
  382. $mails = scandir(_PS_MAIL_DIR_.'en/');
  383. $mails_new_lang = array();
  384. // Get all email files
  385. foreach ($files_list as $file)
  386. {
  387. if (preg_match('#^mails\/([a-z0-9]+)\/#Ui', $file['filename'], $matches))
  388. {
  389. $slash_pos = strrpos($file['filename'], '/');
  390. $mails_new_lang[] = substr($file['filename'], -(strlen($file['filename']) - $slash_pos - 1));
  391. }
  392. }
  393. // Get the difference
  394. $arr_mails_needed = array_diff($mails, $mails_new_lang);
  395. // Add mails files
  396. foreach ($arr_mails_needed as $mail_to_add)
  397. if (!in_array($mail_to_add, self::$ignore_folder))
  398. @copy(_PS_MAIL_DIR_.'en/'.$mail_to_add, _PS_MAIL_DIR_.$iso_code.'/'.$mail_to_add);
  399. // 2 - Scan modules files
  400. $modules = scandir(_PS_MODULE_DIR_);
  401. $module_mail_en = array();
  402. $module_mail_iso_code = array();
  403. foreach ($modules as $module)
  404. {
  405. if (!in_array($module, self::$ignore_folder) && Tools::file_exists_cache(_PS_MODULE_DIR_.$module.'/mails/en/'))
  406. {
  407. $arr_files = scandir(_PS_MODULE_DIR_.$module.'/mails/en/');
  408. foreach ($arr_files as $file)
  409. {
  410. if (!in_array($file, self::$ignore_folder))
  411. {
  412. if (Tools::file_exists_cache(_PS_MODULE_DIR_.$module.'/mails/en/'.$file))
  413. $module_mail_en[] = _PS_MODULE_DIR_.$module.'/mails/ISO_CODE/'.$file;
  414. if (Tools::file_exists_cache(_PS_MODULE_DIR_.$module.'/mails/'.$iso_code.'/'.$file))
  415. $module_mail_iso_code[] = _PS_MODULE_DIR_.$module.'/mails/ISO_CODE/'.$file;
  416. }
  417. }
  418. }
  419. }
  420. // Get the difference in this modules
  421. $arr_modules_mails_needed = array_diff($module_mail_en, $module_mail_iso_code);
  422. // Add mails files for this modules
  423. foreach ($arr_modules_mails_needed as $file)
  424. {
  425. $file_en = str_replace('ISO_CODE', 'en', $file);
  426. $file_iso_code = str_replace('ISO_CODE', $iso_code, $file);
  427. $dir_iso_code = substr($file_iso_code, 0, -(strlen($file_iso_code) - strrpos($file_iso_code, '/') - 1));
  428. if (!file_exists($dir_iso_code))
  429. mkdir($dir_iso_code);
  430. if (Tools::file_exists_cache($file_en))
  431. copy($file_en, $file_iso_code);
  432. }
  433. }
  434. /**
  435. * Move theme translations in selected themes
  436. *
  437. * @param array $files
  438. * @param array $themes_selected
  439. */
  440. public function checkAndAddThemesFiles($files, $themes_selected)
  441. {
  442. foreach ($files as $file)
  443. {
  444. // Check if file is a file theme
  445. if (preg_match('#^themes\/([a-z0-9]+)\/lang\/#Ui', $file['filename'], $matches))
  446. {
  447. $slash_pos = strrpos($file['filename'], '/');
  448. $name_file = substr($file['filename'], -(strlen($file['filename']) - $slash_pos - 1));
  449. $name_default_theme = $matches[1];
  450. $deleted_old_theme = false;
  451. // Get the old file theme
  452. if (file_exists(_PS_THEME_DIR_.'lang/'.$name_file))
  453. $theme_file_old = _PS_THEME_DIR_.'lang/'.$name_file;
  454. else
  455. {
  456. $deleted_old_theme = true;
  457. $theme_file_old = str_replace(self::DEFAULT_THEME_NAME, $name_default_theme, _PS_THEME_DIR_.'lang/'.$name_file);
  458. }
  459. // Move the old file theme in the new folder
  460. foreach ($themes_selected as $theme_name)
  461. if (file_exists($theme_file_old))
  462. copy($theme_file_old, str_replace($name_default_theme, $theme_name, $theme_file_old));
  463. if ($deleted_old_theme)
  464. @unlink($theme_file_old);
  465. }
  466. }
  467. }
  468. /**
  469. * Add new translations tabs by code ISO
  470. *
  471. * @param array $iso_code
  472. * @param array $files
  473. */
  474. public static function addNewTabs($iso_code, $files)
  475. {
  476. foreach ($files as $file)
  477. {
  478. // Check if file is a file theme
  479. if (preg_match('#^translations\/'.$iso_code.'\/tabs.php#Ui', $file['filename'], $matches) && Validate::isLanguageIsoCode($iso_code))
  480. {
  481. // Include array width new translations tabs
  482. $tabs = include _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.$file['filename'];
  483. foreach ($tabs as $class_name => $translations)
  484. {
  485. // Get instance of this tab by class name
  486. $tab = Tab::getInstanceFromClassName($class_name);
  487. //Check if class name exists
  488. if (isset($tab->class_name) && !empty($tab->class_name))
  489. {
  490. $id_lang = Language::getIdByIso($iso_code);
  491. $tab->name[(int)$id_lang] = pSQL($translations);
  492. // Update this tab
  493. $tab->update();
  494. }
  495. }
  496. }
  497. }
  498. }
  499. public function submitImportLang()
  500. {
  501. if (!isset($_FILES['file']['tmp_name']) || !$_FILES['file']['tmp_name'])
  502. $this->errors[] = Tools::displayError('No file selected');
  503. else
  504. {
  505. $gz = new Archive_Tar($_FILES['file']['tmp_name'], true);
  506. $filename = $_FILES['file']['name'];
  507. $iso_code = str_replace(array('.tar.gz', '.gzip'), '', $filename);
  508. if (Validate::isLangIsoCode($iso_code))
  509. {
  510. $themes_selected = Tools::getValue('theme', array(self::DEFAULT_THEME_NAME));
  511. $files_list = $gz->listContent();
  512. if ($gz->extract(_PS_TRANSLATIONS_DIR_.'../', false))
  513. {
  514. AdminTranslationsController::checkAndAddMailsFiles($iso_code, $files_list);
  515. $this->checkAndAddThemesFiles($files_list, $themes_selected);
  516. AdminTranslationsController::addNewTabs($iso_code, $files_list);
  517. if (Validate::isLanguageFileName($filename))
  518. {
  519. if (!Language::checkAndAddLanguage($iso_code))
  520. $conf = 20;
  521. }
  522. $this->redirect(false, (isset($conf) ? $conf : '15'));
  523. }
  524. $this->errors[] = Tools::displayError('Archive cannot be extracted.');
  525. }
  526. else
  527. $this->errors[] = sprintf(Tools::displayError('ISO CODE invalid "%1$s" for the following file: "%2$s"'), $iso_code, $filename);
  528. }
  529. }
  530. public function submitAddLang()
  531. {
  532. $arr_import_lang = explode('|', Tools::getValue('params_import_language')); /* 0 = Language ISO code, 1 = PS version */
  533. if (Validate::isLangIsoCode($arr_import_lang[0]))
  534. {
  535. if ($content = Tools::file_get_contents(
  536. 'http://www.prestashop.com/download/lang_packs/gzip/'.$arr_import_lang[1].'/'.$arr_import_lang[0].'.gzip', false,
  537. @stream_context_create(array('http' => array('method' => 'GET', 'timeout' => 5)))))
  538. {
  539. $file = _PS_TRANSLATIONS_DIR_.$arr_import_lang[0].'.gzip';
  540. if (file_put_contents($file, $content))
  541. {
  542. $gz = new Archive_Tar($file, true);
  543. $files_list = $gz->listContent();
  544. if ($gz->extract(_PS_TRANSLATIONS_DIR_.'../', false))
  545. {
  546. AdminTranslationsController::checkAndAddMailsFiles($arr_import_lang[0], $files_list);
  547. AdminTranslationsController::addNewTabs($arr_import_lang[0], $files_list);
  548. if (!Language::checkAndAddLanguage($arr_import_lang[0]))
  549. $conf = 20;
  550. if (!unlink($file))
  551. $this->errors[] = Tools::displayError('Cannot delete archive');
  552. $this->redirect(false, (isset($conf) ? $conf : '15'));
  553. }
  554. $this->errors[] = Tools::displayError('Archive cannot be extracted.');
  555. if (!unlink($file))
  556. $this->errors[] = Tools::displayError('Cannot delete archive');
  557. }
  558. else
  559. $this->errors[] = Tools::displayError('Server does not have permissions for writing.');
  560. }
  561. else
  562. $this->errors[] = Tools::displayError('Language not found');
  563. }
  564. else
  565. $this->errors[] = Tools::displayError('Invalid parameter');
  566. }
  567. /**
  568. * This method check each file (tpl or php file), get its sentences to translate,
  569. * compare with posted values and write in iso code translation file.
  570. *
  571. * @param string $file_name
  572. * @param array $files
  573. * @param string $theme_name
  574. * @param string $module_name
  575. * @param string|boolean $dir
  576. * @return void
  577. */
  578. protected function findAndWriteTranslationsIntoFile($file_name, $files, $theme_name, $module_name, $dir = false)
  579. {
  580. // These static vars allow to use file to write just one time.
  581. static $cache_file = array();
  582. static $str_write = '';
  583. static $array_check_duplicate = array();
  584. // Default translations and Prestashop overriding themes are distinguish
  585. $is_default = $theme_name === self::DEFAULT_THEME_NAME ? true : false;
  586. // Set file_name in static var, this allow to open and wright the file just one time
  587. if (!isset($cache_file[$theme_name.'-'.$file_name]))
  588. {
  589. $str_write = '';
  590. $cache_file[$theme_name.'-'.$file_name] = true;
  591. if (!Tools::file_exists_cache($file_name))
  592. file_put_contents($file_name, '');
  593. if (!is_writable($file_name))
  594. throw new PrestaShopException(sprintf(
  595. Tools::displayError('Cannot write to the theme\'s language file (%s). Please check write permissions.'),
  596. $file_name
  597. ));
  598. // this string is initialized one time for a file
  599. $str_write .= "<?php\n\nglobal \$_MODULE;\n\$_MODULE = array();\n";
  600. $array_check_duplicate = array();
  601. }
  602. foreach ($files as $file)
  603. {
  604. if (preg_match('/^(.*).(tpl|php)$/', $file) && Tools::file_exists_cache($file_path = $dir.$file) && !in_array($file, self::$ignore_folder))
  605. {
  606. // Get content for this file
  607. $content = file_get_contents($file_path);
  608. // Get file type
  609. $type_file = substr($file, -4) == '.tpl' ? 'tpl' : 'php';
  610. // Parse this content
  611. $matches = $this->userParseFile($content, $this->type_selected, $type_file);
  612. // Write each translation on its module file
  613. $template_name = substr(basename($file), 0, -4);
  614. foreach ($matches as $key)
  615. {
  616. if ($is_default)
  617. {
  618. $post_key = md5(strtolower($module_name).'_'.self::DEFAULT_THEME_NAME.'_'.strtolower($template_name).'_'.md5($key));
  619. $pattern = '\'<{'.strtolower($module_name).'}prestashop>'.strtolower($template_name).'_'.md5($key).'\'';
  620. }
  621. else
  622. {
  623. $post_key = md5(strtolower($module_name).'_'.strtolower($theme_name).'_'.strtolower($template_name).'_'.md5($key));
  624. $pattern = '\'<{'.strtolower($module_name).'}'.strtolower($theme_name).'>'.strtolower($template_name).'_'.md5($key).'\'';
  625. }
  626. if (array_key_exists($post_key, $_POST) && !empty($_POST[$post_key]) && !in_array($pattern, $array_check_duplicate))
  627. {
  628. $array_check_duplicate[] = $pattern;
  629. $str_write .= '$_MODULE['.$pattern.'] = \''.pSQL(str_replace(array("\r\n", "\r", "\n"), ' ', $_POST[$post_key])).'\';'."\n";
  630. $this->total_expression++;
  631. }
  632. }
  633. }
  634. }
  635. if (isset($cache_file[$theme_name.'-'.$file_name]) && $str_write != "<?php\n\nglobal \$_MODULE;\n\$_MODULE = array();\n")
  636. file_put_contents($file_name, $str_write);
  637. }
  638. /**
  639. * Clear the list of module file by type (file or directory)
  640. *
  641. * @param $files : list of files
  642. * @param string $type_clear (file|directory)
  643. * @param string $path
  644. * @return array : list of a good files
  645. */
  646. public function clearModuleFiles($files, $type_clear = 'file', $path = '')
  647. {
  648. // List of directory which not must be parsed
  649. $arr_exclude = array('img', 'js', 'mails');
  650. // List of good extention files
  651. $arr_good_ext = array('.tpl', '.php');
  652. foreach ($files as $key => $file)
  653. {
  654. if ($file{0} === '.' || in_array(substr($file, 0, strrpos($file, '.')), $this->all_iso_lang))
  655. unset($files[$key]);
  656. else if ($type_clear === 'file' && !in_array(substr($file, strrpos($file, '.')), $arr_good_ext))
  657. unset($files[$key]);
  658. else if ($type_clear === 'directory' && (!is_dir($path.$file) || in_array($file, $arr_exclude)))
  659. unset($files[$key]);
  660. }
  661. return $files;
  662. }
  663. /**
  664. * This method get translation for each files of a module,
  665. * compare with global $_MODULES array and fill AdminTranslations::modules_translations array
  666. * With key as English sentences and values as their iso code translations.
  667. *
  668. * @param array $files
  669. * @param string $theme_name
  670. * @param string $module_name
  671. * @param string|boolean $dir
  672. * @param string $iso_code
  673. * @return void
  674. */
  675. protected function findAndFillTranslations($files, $theme_name, $module_name, $dir = false)
  676. {
  677. $name_var = $this->translations_informations[$this->type_selected]['var'];
  678. // added for compatibility
  679. $GLOBALS[$name_var] = array_change_key_case($GLOBALS[$name_var]);
  680. // Default translations and Prestashop overriding themes are distinguish
  681. $is_default = $theme_name === self::DEFAULT_THEME_NAME ? true : false;
  682. // Thank to this var similar keys are not duplicate
  683. // in AndminTranslation::modules_translations array
  684. // see below
  685. $array_check_duplicate = array();
  686. foreach ($files as $file)
  687. {
  688. if ((preg_match('/^(.*).tpl$/', $file) || preg_match('/^(.*).php$/', $file)) && Tools::file_exists_cache($file_path = $dir.$file))
  689. {
  690. // Get content for this file
  691. $content = file_get_contents($file_path);
  692. // Module files can now be ignored by adding this string in a file
  693. if (strpos($content, 'IGNORE_THIS_FILE_FOR_TRANSLATION') !== false)
  694. continue;
  695. // Get file type
  696. $type_file = substr($file, -4) == '.tpl' ? 'tpl' : 'php';
  697. // Parse this content
  698. $matches = $this->userParseFile($content, $this->type_selected, $type_file);
  699. // Write each translation on its module file
  700. $template_name = substr(basename($file), 0, -4);
  701. foreach ($matches as $key)
  702. {
  703. $module_key = '<{'.Tools::strtolower($module_name).'}'.
  704. strtolower($is_default ? 'prestashop' : $theme_name).'>'.Tools::strtolower($template_name).'_'.md5($key);
  705. // to avoid duplicate entry
  706. if (!in_array($module_key, $array_check_duplicate))
  707. {
  708. $array_check_duplicate[] = $module_key;
  709. if (!isset($this->modules_translations[$theme_name][$module_name][$template_name][$key]['trad']))
  710. $this->total_expression++;
  711. if (array_key_exists($module_key, $GLOBALS[$name_var]))
  712. $this->modules_translations[$theme_name][$module_name][$template_name][$key]['trad'] = html_entity_decode($GLOBALS[$name_var][$module_key], ENT_COMPAT, 'UTF-8');
  713. else
  714. {
  715. $this->modules_translations[$theme_name][$module_name][$template_name][$key]['trad'] = '';
  716. $this->missing_translations++;
  717. }
  718. $this->modules_translations[$theme_name][$module_name][$template_name][$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  719. }
  720. }
  721. }
  722. }
  723. }
  724. /**
  725. * Get list of files which must be parsed by directory and by type of translations
  726. *
  727. * @return array : list of files by directory
  728. */
  729. public function getFileToParseByTypeTranslation()
  730. {
  731. $directories = array();
  732. switch ($this->type_selected)
  733. {
  734. case 'front':
  735. $directories['tpl'] = array(_PS_ALL_THEMES_DIR_.'/' => scandir(_PS_ALL_THEMES_DIR_));
  736. self::$ignore_folder[] = 'modules';
  737. $directories['tpl'] = array_merge($directories['tpl'], $this->listFiles(_PS_THEME_SELECTED_DIR_));
  738. if (Tools::file_exists_cache(_PS_THEME_OVERRIDE_DIR_))
  739. $directories['tpl'] = array_merge($directories['tpl'], $this->listFiles(_PS_THEME_OVERRIDE_DIR_));
  740. break;
  741. case 'back':
  742. $directories = array(
  743. 'php' => array(
  744. _PS_ADMIN_CONTROLLER_DIR_.'/' => scandir(_PS_ADMIN_CONTROLLER_DIR_),
  745. _PS_OVERRIDE_DIR_.'controllers/admin/' => scandir(_PS_OVERRIDE_DIR_.'controllers/admin/'),
  746. _PS_CLASS_DIR_.'helper/' => scandir(_PS_CLASS_DIR_.'helper/'),
  747. _PS_CLASS_DIR_.'controller/' => array('AdminController.php'),
  748. _PS_CLASS_DIR_ => array('PaymentModule.php')
  749. ),
  750. 'tpl' => $this->listFiles(_PS_ADMIN_DIR_.'/themes/'),
  751. 'specific' => array(
  752. _PS_ADMIN_DIR_.'/' => array(
  753. 'header.inc.php',
  754. 'footer.inc.php',
  755. 'index.php',
  756. 'login.php',
  757. 'password.php',
  758. 'functions.php'
  759. )
  760. )
  761. );
  762. // For translate the template which are overridden
  763. if (file_exists(_PS_OVERRIDE_DIR_.'controllers'.DIRECTORY_SEPARATOR.'admin'.DIRECTORY_SEPARATOR.'templates'))
  764. $directories['tpl'] = array_merge($directories['tpl'], $this->listFiles(_PS_OVERRIDE_DIR_.'controllers'.DIRECTORY_SEPARATOR.'admin'.DIRECTORY_SEPARATOR.'templates'));
  765. break;
  766. case 'errors':
  767. $directories['php'] = array(
  768. _PS_ROOT_DIR_.'/' => scandir(_PS_ROOT_DIR_),
  769. _PS_ADMIN_DIR_.'/' => scandir(_PS_ADMIN_DIR_),
  770. _PS_FRONT_CONTROLLER_DIR_ => scandir(_PS_FRONT_CONTROLLER_DIR_),
  771. _PS_ADMIN_CONTROLLER_DIR_ => scandir(_PS_ADMIN_CONTROLLER_DIR_),
  772. _PS_OVERRIDE_DIR_.'controllers/front/' => scandir(_PS_OVERRIDE_DIR_.'controllers/front/'),
  773. _PS_OVERRIDE_DIR_.'controllers/admin/' => scandir(_PS_OVERRIDE_DIR_.'controllers/admin/')
  774. );
  775. // Get all files for folders classes/ and override/classes/ recursively
  776. $directories['php'] = array_merge($directories['php'], $this->listFiles(_PS_CLASS_DIR_, array(), 'php'));
  777. $directories['php'] = array_merge($directories['php'], $this->listFiles(_PS_OVERRIDE_DIR_.'classes/', array(), 'php'));
  778. break;
  779. case 'fields':
  780. $directories['php'] = $this->listFiles(_PS_CLASS_DIR_, array(), 'php');
  781. break;
  782. case 'pdf':
  783. $tpl_theme = Tools::file_exists_cache(_PS_THEME_SELECTED_DIR_.'pdf/') ? scandir(_PS_THEME_SELECTED_DIR_.'pdf/') : array();
  784. $directories = array(
  785. 'php' => array(
  786. _PS_CLASS_DIR_.'pdf/' => scandir(_PS_CLASS_DIR_.'pdf/'),
  787. _PS_OVERRIDE_DIR_.'classes/pdf/' => scandir(_PS_OVERRIDE_DIR_.'classes/pdf/')
  788. ),
  789. 'tpl' => array(
  790. _PS_PDF_DIR_ => scandir(_PS_PDF_DIR_),
  791. _PS_THEME_SELECTED_DIR_.'pdf/' => $tpl_theme
  792. )
  793. );
  794. break;
  795. case 'mails':
  796. $directories['php'] = array(
  797. _PS_FRONT_CONTROLLER_DIR_ => scandir(_PS_FRONT_CONTROLLER_DIR_),
  798. _PS_ADMIN_CONTROLLER_DIR_ => scandir(_PS_ADMIN_CONTROLLER_DIR_),
  799. _PS_OVERRIDE_DIR_.'controllers/front/' => scandir(_PS_OVERRIDE_DIR_.'controllers/front/'),
  800. _PS_OVERRIDE_DIR_.'controllers/admin/' => scandir(_PS_OVERRIDE_DIR_.'controllers/admin/'),
  801. _PS_ADMIN_DIR_.'/' => scandir(_PS_ADMIN_DIR_),
  802. _PS_ADMIN_DIR_.'/tabs/' => scandir(_PS_ADMIN_DIR_.'/tabs')
  803. );
  804. // Get all files for folders classes/ and override/classes/ recursively
  805. $directories['php'] = array_merge($directories['php'], $this->listFiles(_PS_CLASS_DIR_, array(), 'php'));
  806. $directories['php'] = array_merge($directories['php'], $this->listFiles(_PS_OVERRIDE_DIR_.'classes/', array(), 'php'));
  807. $directories['php'] = array_merge($directories['php'], $this->getModulesHasMails());
  808. break;
  809. }
  810. return $directories;
  811. }
  812. /**
  813. * This method parse a file by type of translation and type file
  814. *
  815. * @param $content
  816. * @param $type_translation : front, back, errors, modules...
  817. * @param string|bool $type_file : (tpl|php)
  818. * @return return $matches
  819. */
  820. protected function userParseFile($content, $type_translation, $type_file = false)
  821. {
  822. switch ($type_translation)
  823. {
  824. case 'front':
  825. // Parsing file in Front office
  826. $regex = '/\{l\s*s=\''._PS_TRANS_PATTERN_.'\'(\s*sprintf=.*)?(\s*js=1)?\s*\}/U';
  827. break;
  828. case 'back':
  829. // Parsing file in Back office
  830. if ($type_file == 'php')
  831. $regex = '/this->l\(\''._PS_TRANS_PATTERN_.'\'[\)|\,]/U';
  832. else if ($type_file == 'specific')
  833. $regex = '/translate\(\''._PS_TRANS_PATTERN_.'\'\)/U';
  834. else
  835. $regex = '/\{l\s*s\s*=\''._PS_TRANS_PATTERN_.'\'(\s*sprintf=.*)?(\s*js=1)?(\s*slashes=1)?\s*\}/U';
  836. break;
  837. case 'errors':
  838. // Parsing file for all errors syntax
  839. $regex = '/Tools::displayError\(\''._PS_TRANS_PATTERN_.'\'(,\s*(true|false))?\)/U';
  840. break;
  841. case 'modules':
  842. // Parsing modules file
  843. if ($type_file == 'php')
  844. $regex = '/->l\(\''._PS_TRANS_PATTERN_.'\'(, ?\'(.+)\')?(, ?(.+))?\)/U';
  845. else
  846. $regex = '/\{l\s*s=\''._PS_TRANS_PATTERN_.'\'(\s*sprintf=.*)?(\s*mod=\'.+\')?(\s*js=1)?\s*\}/U';
  847. break;
  848. case 'pdf':
  849. // Parsing PDF file
  850. if ($type_file == 'php')
  851. $regex = '/HTMLTemplate.*::l\(\''._PS_TRANS_PATTERN_.'\'[\)|\,]/U';
  852. else
  853. $regex = '/\{l\s*s=\''._PS_TRANS_PATTERN_.'\'(\s*sprintf=.*)?(\s*js=1)?(\s*pdf=\'true\')?\s*\}/U';
  854. break;
  855. }
  856. preg_match_all($regex, $content, $matches);
  857. return $matches[1];
  858. }
  859. /**
  860. * Get all translations informations for all type of translations
  861. *
  862. * array(
  863. * 'type' => array(
  864. * 'name' => string : title for the translation type,
  865. * 'var' => string : name of var for the translation file,
  866. * 'dir' => string : dir of translation file
  867. * 'file' => string : file name of translation file
  868. * )
  869. * )
  870. */
  871. public function getTranslationsInformations()
  872. {
  873. $this->translations_informations = array(
  874. 'front' => array(
  875. 'name' => $this->l('Front Office translations'),
  876. 'var' => '_LANG',
  877. 'dir' => _PS_THEME_SELECTED_DIR_.'lang/',
  878. 'file' => $this->lang_selected->iso_code.'.php'
  879. ),
  880. 'back' => array(
  881. 'name' => $this->l('Back Office translations'),
  882. 'var' => '_LANGADM',
  883. 'dir' => _PS_TRANSLATIONS_DIR_.$this->lang_selected->iso_code.'/',
  884. 'file' => 'admin.php'
  885. ),
  886. 'errors' => array(
  887. 'name' => $this->l('Error message translations'),
  888. 'var' => '_ERRORS',
  889. 'dir' => _PS_TRANSLATIONS_DIR_.$this->lang_selected->iso_code.'/',
  890. 'file' => 'errors.php'
  891. ),
  892. 'fields' => array(
  893. 'name' => $this->l('Field name translations'),
  894. 'var' => '_FIELDS',
  895. 'dir' => _PS_TRANSLATIONS_DIR_.$this->lang_selected->iso_code.'/',
  896. 'file' => 'fields.php'
  897. ),
  898. 'modules' => array(
  899. 'name' => $this->l('Installed module translations'),
  900. 'var' => '_MODULES',
  901. 'dir' => _PS_MODULE_DIR_,
  902. 'file' => '',
  903. 'override' => array(
  904. 'dir' => _PS_THEME_SELECTED_DIR_.'modules/',
  905. 'file' => ''
  906. )
  907. ),
  908. 'pdf' => array(
  909. 'name' => $this->l('PDF translations'),
  910. 'var' => '_LANGPDF',
  911. 'dir' => _PS_TRANSLATIONS_DIR_.$this->lang_selected->iso_code.'/',
  912. 'file' => 'pdf.php',
  913. 'override' => array(
  914. 'dir' => _PS_THEME_SELECTED_DIR_.'pdf/lang/',
  915. 'file' => $this->lang_selected->iso_code.'.php'
  916. )
  917. ),
  918. 'mails' => array(
  919. 'name' => $this->l('E-mail template translations'),
  920. 'var' => '_LANGMAIL',
  921. 'dir' => _PS_MAIL_DIR_.$this->lang_selected->iso_code.'/',
  922. 'file' => 'lang.php',
  923. 'override' => array(
  924. 'dir' => _PS_THEME_SELECTED_DIR_.'mails/'.$this->lang_selected->iso_code.'/',
  925. 'file' => 'lang.php'
  926. )
  927. )
  928. );
  929. }
  930. /**
  931. * Get all informations on : languages, theme and the translation type.
  932. */
  933. public function getInformations()
  934. {
  935. // Get all Languages
  936. $this->languages = Language::getLanguages(false);
  937. // Get all iso_code of languages
  938. foreach ($this->languages as $language)
  939. $this->all_iso_lang[] = $language['iso_code'];
  940. // Get all themes
  941. $this->themes = Theme::getThemes();
  942. // Get folder name of theme
  943. if (($theme = Tools::getValue('theme')) && !is_array($theme))
  944. $this->theme_selected = Tools::safeOutput($theme);
  945. else
  946. $this->theme_selected = self::DEFAULT_THEME_NAME;
  947. // Set the path of selected theme
  948. define('_PS_THEME_SELECTED_DIR_', _PS_ROOT_DIR_.'/themes/'.$this->theme_selected.'/');
  949. // Get type of translation
  950. if (($type = Tools::getValue('type')) && !is_array($type))
  951. $this->type_selected = strtolower(Tools::safeOutput($type));
  952. // Get selected language
  953. if (Tools::getValue('lang') || Tools::getValue('iso_code'))
  954. {
  955. $iso_code = Tools::getValue('lang') ? Tools::getValue('lang') : Tools::getValue('iso_code');
  956. if (!Validate::isLangIsoCode($iso_code) || !in_array($iso_code, $this->all_iso_lang))
  957. throw new PrestaShopException(sprintf(Tools::displayError('Invalid iso code "%s"'), $iso_code));
  958. $this->lang_selected = new Language((int)Language::getIdByIso($iso_code));
  959. }
  960. else
  961. $this->lang_selected = new Language((int)Language::getIdByIso('en'));
  962. // Get all information for translations
  963. $this->getTranslationsInformations();
  964. }
  965. /**
  966. * AdminController::postProcess() override
  967. * @see AdminController::postProcess()
  968. */
  969. public function postProcess()
  970. {
  971. $this->getInformations();
  972. /* PrestaShop demo mode */
  973. if (_PS_MODE_DEMO_)
  974. {
  975. $this->errors[] = Tools::displayError('This functionality has been disabled.');
  976. return;
  977. }
  978. /* PrestaShop demo mode */
  979. if (Tools::isSubmit('submitCopyLang'))
  980. {
  981. if ($this->tabAccess['add'] === '1')
  982. $this->submitCopyLang();
  983. else
  984. $this->errors[] = Tools::displayError('You do not have permission to add here.');
  985. }
  986. else if (Tools::isSubmit('submitExport'))
  987. {
  988. if ($this->tabAccess['add'] === '1')
  989. $this->submitExportLang();
  990. else
  991. $this->errors[] = Tools::displayError('You do not have permission to add here.');
  992. }
  993. else if (Tools::isSubmit('submitImport'))
  994. {
  995. if ($this->tabAccess['add'] === '1')
  996. $this->submitImportLang();
  997. else
  998. $this->errors[] = Tools::displayError('You do not have permission to add here.');
  999. }
  1000. else if (Tools::isSubmit('submitAddLanguage'))
  1001. {
  1002. if ($this->tabAccess['add'] === '1')
  1003. $this->submitAddLang();
  1004. else
  1005. $this->errors[] = Tools::displayError('You do not have permission to add here.');
  1006. }
  1007. else if (Tools::isSubmit('submitTranslationsFront'))
  1008. {
  1009. if ($this->tabAccess['edit'] === '1')
  1010. $this->writeTranslationFile();
  1011. else
  1012. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1013. }
  1014. else if (Tools::isSubmit('submitTranslationsPdf'))
  1015. {
  1016. if ($this->tabAccess['edit'] === '1')
  1017. // Only the PrestaShop team should write the translations into the _PS_TRANSLATIONS_DIR_
  1018. if (($this->theme_selected == self::DEFAULT_THEME_NAME) && _PS_MODE_DEV_)
  1019. $this->writeTranslationFile();
  1020. else
  1021. $this->writeTranslationFile(true);
  1022. else
  1023. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1024. }
  1025. else if (Tools::isSubmit('submitTranslationsBack'))
  1026. {
  1027. if ($this->tabAccess['edit'] === '1')
  1028. $this->writeTranslationFile();
  1029. else
  1030. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1031. }
  1032. else if (Tools::isSubmit('submitTranslationsErrors'))
  1033. {
  1034. if ($this->tabAccess['edit'] === '1')
  1035. $this->writeTranslationFile();
  1036. else
  1037. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1038. }
  1039. else if (Tools::isSubmit('submitTranslationsFields'))
  1040. {
  1041. if ($this->tabAccess['edit'] === '1')
  1042. $this->writeTranslationFile();
  1043. else
  1044. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1045. }
  1046. else if (Tools::isSubmit('submitTranslationsMails') || Tools::isSubmit('submitTranslationsMailsAndStay'))
  1047. {
  1048. if ($this->tabAccess['edit'] === '1')
  1049. $this->submitTranslationsMails();
  1050. else
  1051. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1052. }
  1053. else if (Tools::isSubmit('submitTranslationsModules'))
  1054. {
  1055. if ($this->tabAccess['edit'] === '1')
  1056. {
  1057. // Get a good path for module directory
  1058. if ($this->theme_selected == self::DEFAULT_THEME_NAME)
  1059. $i18n_dir = $this->translations_informations[$this->type_selected]['dir'];
  1060. else
  1061. $i18n_dir = $this->translations_informations[$this->type_selected]['override']['dir'];
  1062. // Get list of modules
  1063. if ($modules = $this->getListModules())
  1064. {
  1065. // Get files of all modules
  1066. $arr_files = $this->getAllModuleFiles($modules, $i18n_dir, $this->lang_selected->iso_code, true);
  1067. // Find and write all translation modules files
  1068. foreach ($arr_files as $value)
  1069. $this->findAndWriteTranslationsIntoFile($value['file_name'], $value['files'], $value['theme'], $value['module'], $value['dir']);
  1070. // Redirect
  1071. if (Tools::getValue('submitTranslationsModulesAndStay'))
  1072. $this->redirect(true);
  1073. else
  1074. $this->redirect();
  1075. }
  1076. }
  1077. else
  1078. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1079. }
  1080. }
  1081. /**
  1082. * This method redirect in the translation main page or in the translation page
  1083. *
  1084. * @param bool $save_and_stay : true if the user has clicked on the button "save and stay"
  1085. * @param bool $conf : id of confirmation message
  1086. */
  1087. protected function redirect($save_and_stay = false, $conf = false)
  1088. {
  1089. $conf = !$conf ? 4 : $conf;
  1090. $url_base = self::$currentIndex.'&token='.$this->token.'&conf='.$conf;
  1091. if ($save_and_stay)
  1092. Tools::redirectAdmin($url_base.'&lang='.$this->lang_selected->iso_code.'&type='.$this->type_selected.'&theme='.$this->theme_selected);
  1093. else
  1094. Tools::redirectAdmin($url_base);
  1095. }
  1096. protected function getMailPattern()
  1097. {
  1098. // Let the indentation like it.
  1099. return '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">
  1100. <html>
  1101. <head>
  1102. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  1103. <title>#title</title>
  1104. </head>
  1105. <body>
  1106. #content
  1107. </body>
  1108. </html>';
  1109. }
  1110. /**
  1111. * This method is used to wright translation for mails.
  1112. * This wrights subject translation files
  1113. * (in root/mails/lang_choosen/lang.php or root/_PS_THEMES_DIR_/mails/lang_choosen/lang.php)
  1114. * and mails files.
  1115. */
  1116. protected function submitTranslationsMails()
  1117. {
  1118. $arr_mail_content = array();
  1119. $arr_mail_path = array();
  1120. if (Tools::getValue('core_mail'))
  1121. {
  1122. $arr_mail_content['core_mail'] = Tools::getValue('core_mail');
  1123. // Get path of directory for find a good path of translation file
  1124. if ($this->theme_selected != self::DEFAULT_THEME_NAME)
  1125. $arr_mail_path['core_mail'] = $this->translations_informations[$this->type_selected]['override']['dir'];
  1126. else
  1127. $arr_mail_path['core_mail'] = $this->translations_informations[$this->type_selected]['dir'];
  1128. }
  1129. if (Tools::getValue('module_mail'))
  1130. {
  1131. $arr_mail_content['module_mail'] = Tools::getValue('module_mail');
  1132. // Get path of directory for find a good path of translation file
  1133. if ($this->theme_selected != self::DEFAULT_THEME_NAME)
  1134. $arr_mail_path['module_mail'] = $this->translations_informations['modules']['override']['dir'].'{module}/mails/'.$this->lang_selected->iso_code.'/';
  1135. else
  1136. $arr_mail_path['module_mail'] = $this->translations_informations['modules']['dir'].'{module}/mails/'.$this->lang_selected->iso_code.'/';
  1137. }
  1138. // Save each mail content
  1139. foreach ($arr_mail_content as $group_name => $all_content)
  1140. {
  1141. foreach ($all_content as $type_content => $mails)
  1142. {
  1143. foreach ($mails as $mail_name => $content)
  1144. {
  1145. $module_name = false;
  1146. $module_name_pipe_pos = stripos($mail_name, '|');
  1147. if ($module_name_pipe_pos)
  1148. {
  1149. $module_name = substr($mail_name, 0, $module_name_pipe_pos);
  1150. if (!Validate::isModuleName($module_name))
  1151. throw new PrestaShopException(sprinf(Tools::displayError('Invalid module name "%s"'), $module_name));
  1152. $mail_name = substr($mail_name, $module_name_pipe_pos + 1);
  1153. if (!Validate::isTplName($mail_name))
  1154. throw new PrestaShopException(sprintf(Tools::displayError('Invalid mail name "%s"'), $mail_name));
  1155. }
  1156. if ($type_content == 'html')
  1157. {
  1158. $content = Tools::htmlentitiesUTF8($content);
  1159. $content = htmlspecialchars_decode($content);
  1160. // replace correct end of line
  1161. $content = str_replace("\r\n", PHP_EOL, $content);
  1162. $title = '';
  1163. if (Tools::getValue('title_'.$group_name.'_'.$mail_name))
  1164. $title = Tools::getValue('title_'.$group_name.'_'.$mail_name);
  1165. $string_mail = $this->getMailPattern();
  1166. $content = str_replace(array('#title', '#content'), array($title, $content), $string_mail);
  1167. // Magic Quotes shall... not.. PASS!
  1168. if (_PS_MAGIC_QUOTES_GPC_)
  1169. $content = stripslashes($content);
  1170. }
  1171. if (Validate::isCleanHTML($content))
  1172. {
  1173. $path = $arr_mail_path[$group_name];
  1174. if ($module_name)
  1175. $path = str_replace('{module}', $module_name, $path);
  1176. file_put_contents($path.$mail_name.'.'.$type_content, $content);
  1177. }
  1178. else
  1179. throw new PrestaShopException(Tools::displayError('HTML e-mail templates cannot contain JavaScript code.'));
  1180. }
  1181. }
  1182. }
  1183. // Update subjects
  1184. $array_subjects = array();
  1185. if (($subjects = Tools::getValue('subject')) && is_array($subjects))
  1186. {
  1187. $array_subjects['core_and_modules'] = array('translations'=>array(), 'path'=>$arr_mail_path['core_mail'].'lang.php');
  1188. foreach ($subjects as $subject_translation)
  1189. $array_subjects['core_and_modules']['translations'] = array_merge($array_subjects['core_and_modules']['translations'], $subject_translation);
  1190. }
  1191. if (!empty($array_subjects))
  1192. foreach ($array_subjects as $infos)
  1193. $this->writeSubjectTranslationFile($infos['translations'], $infos['path']);
  1194. if (Tools::isSubmit('submitTranslationsMailsAndStay'))
  1195. $this->redirect(true);
  1196. else
  1197. $this->redirect();
  1198. }
  1199. /**
  1200. * Include file $dir/$file and return the var $var declared in it.
  1201. * This create the file if not exists
  1202. *
  1203. * return array : translations
  1204. */
  1205. public function fileExists()
  1206. {
  1207. $var = $this->translations_informations[$this->type_selected]['var'];
  1208. $dir = $this->translations_informations[$this->type_selected]['dir'];
  1209. $file = $this->translations_informations[$this->type_selected]['file'];
  1210. $$var = array();
  1211. if (!Tools::file_exists_cache($dir))
  1212. if (!mkdir($dir, 0700))
  1213. throw new PrestaShopException('Directory '.$dir.' cannot be created.');
  1214. if (!Tools::file_exists_cache($dir.DIRECTORY_SEPARATOR.$file))
  1215. if (!file_put_contents($dir.'/'.$file, "<?php\n\nglobal \$".$var.";\n\$".$var." = array();\n\n?>"))
  1216. throw new PrestaShopException('File "'.$file.'" doesn\'t exists and cannot be created in '.$dir);
  1217. if (!is_writable($dir.DIRECTORY_SEPARATOR.$file))
  1218. $this->displayWarning(Tools::displayError('This file must be writable:').' '.$dir.'/'.$file);
  1219. include($dir.DIRECTORY_SEPARATOR.$file);
  1220. return $$var;
  1221. }
  1222. public function displayToggleButton($closed = false)
  1223. {
  1224. $str_output = '
  1225. <script type="text/javascript">';
  1226. if (Tools::getValue('type') == 'mails')
  1227. $str_output .= '$(document).ready(function(){
  1228. openCloseAllDiv(\''.$this->type_selected.'_div\', this.value == openAll); toggleElemValue(this.id, openAll, closeAll);
  1229. });';
  1230. $str_output .= '
  1231. var openAll = \''.html_entity_decode($this->l('Expand all fieldsets'), ENT_NOQUOTES, 'UTF-8').'\';
  1232. var closeAll = \''.html_entity_decode($this->l('Close all fieldsets'), ENT_NOQUOTES, 'UTF-8').'\';
  1233. </script>
  1234. <input type="button" class="button" id="buttonall" onclick="openCloseAllDiv(\''.$this->type_selected.'_div\', this.value == openAll); toggleElemValue(this.id, openAll, closeAll);" />
  1235. <script type="text/javascript">toggleElemValue(\'buttonall\', '.($closed ? 'openAll' : 'closeAll').', '.($closed ? 'closeAll' : 'openAll').');</script>';
  1236. return $str_output;
  1237. }
  1238. protected function displaySubmitButtons($name)
  1239. {
  1240. return '
  1241. <input type="submit" name="submitTranslations'.ucfirst($name).'" value="'.$this->l('Update translations').'" class="button" />
  1242. <input type="submit" name="submitTranslations'.ucfirst($name).'AndStay" value="'.$this->l('Update and stay').'" class="button" />';
  1243. }
  1244. /**
  1245. * Init js variables for translation with google
  1246. *
  1247. * @return array of variables to assign to the smarty template
  1248. */
  1249. public function initAutoTranslate()
  1250. {
  1251. $this->addJS('http://www.google.com/jsapi');
  1252. $this->addJS(_PS_JS_DIR_.'gg-translate.js');
  1253. $this->addJS(_PS_JS_DIR_.'admin-translations.js');
  1254. $language_code = Tools::htmlentitiesUTF8(Language::getLanguageCodeByIso(Tools::getValue('lang')));
  1255. return array('language_code' => $language_code,
  1256. 'not_available' => addslashes(html_entity_decode($this->l('this language is not available in Google Translate\'s API'), ENT_QUOTES, 'utf-8')),
  1257. 'tooltip_title' => addslashes(html_entity_decode($this->l('Google Translate suggests:'), ENT_QUOTES, 'utf-8'))
  1258. );
  1259. }
  1260. public function displayLimitPostWarning($count)
  1261. {
  1262. $return = array();
  1263. if ((ini_get('suhosin.post.max_vars') && ini_get('suhosin.post.max_vars') < $count) || (ini_get('suhosin.request.max_vars') && ini_get('suhosin.request.max_vars') < $count))
  1264. {
  1265. $return['error_type'] = 'suhosin';
  1266. $return['post.max_vars'] = ini_get('suhosin.post.max_vars');
  1267. $return['request.max_vars'] = ini_get('suhosin.request.max_vars');
  1268. $return['needed_limit'] = $count + 100;
  1269. }
  1270. elseif (ini_get('max_input_vars') && ini_get('max_input_vars') < $count)
  1271. {
  1272. $return['error_type'] = 'conf';
  1273. $return['max_input_vars'] = ini_get('max_input_vars');
  1274. $return['needed_limit'] = $count + 100;
  1275. }
  1276. return $return;
  1277. }
  1278. /**
  1279. * Find sentence which use %d, %s, %%, %1$d, %1$s...
  1280. *
  1281. * @param $key : english sentence
  1282. * @return array|bool return list of matches
  1283. */
  1284. public function checkIfKeyUseSprintf($key)
  1285. {
  1286. if (preg_match_all('#(?:%%|%(?:[0-9]+\$)?[+-]?(?:[ 0]|\'.)?-?[0-9]*(?:\.[0-9]+)?[bcdeufFosxX])#', $key, $matches))
  1287. return implode(', ', $matches[0]);
  1288. return false;
  1289. }
  1290. /**
  1291. * This method generate the form for front translations
  1292. */
  1293. public function initFormFront()
  1294. {
  1295. $missing_translations_front = array();
  1296. $name_var = $this->translations_informations[$this->type_selected]['var'];
  1297. $GLOBALS[$name_var] = $this->fileExists();
  1298. /* List templates to parse */
  1299. $files_by_directory = $this->getFileToParseByTypeTranslation();
  1300. $count = 0;
  1301. $tabs_array = array();
  1302. foreach ($files_by_directory['tpl'] as $dir => $files)
  1303. {
  1304. $prefix = '';
  1305. if ($dir == _PS_THEME_OVERRIDE_DIR_)
  1306. $prefix = 'override_';
  1307. foreach ($files as $file)
  1308. {
  1309. if (preg_match('/^(.*).tpl$/', $file) && (Tools::file_exists_cache($file_path = $dir.$file)))
  1310. {
  1311. $prefix_key = $prefix.substr(basename($file), 0, -4);
  1312. $new_lang = array();
  1313. // Get content for this file
  1314. $content = file_get_contents($file_path);
  1315. // Parse this content
  1316. $matches = $this->userParseFile($content, $this->type_selected);
  1317. /* Get string translation */
  1318. foreach ($matches as $key)
  1319. {
  1320. if (empty($key))
  1321. {
  1322. $this->errors[] = sprintf($this->l('Empty string found, please edit: "%s"'), $file_path);
  1323. $new_lang[$key] = '';
  1324. }
  1325. else
  1326. {
  1327. // Caution ! front has underscore between prefix key and md5, back has not
  1328. if (isset($GLOBALS[$name_var][$prefix_key.'_'.md5($key)]))
  1329. $new_lang[$key]['trad'] = stripslashes(html_entity_decode($GLOBALS[$name_var][$prefix_key.'_'.md5($key)], ENT_COMPAT, 'UTF-8'));
  1330. else
  1331. {
  1332. if (!isset($new_lang[$key]['trad']))
  1333. {
  1334. $new_lang[$key]['trad'] = '';
  1335. if (!isset($missing_translations_front[$prefix_key]))
  1336. $missing_translations_front[$prefix_key] = 1;
  1337. else
  1338. $missing_translations_front[$prefix_key]++;
  1339. }
  1340. }
  1341. $new_lang[$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  1342. }
  1343. }
  1344. if (isset($tabs_array[$prefix_key]))
  1345. $tabs_array[$prefix_key] = array_merge($tabs_array[$prefix_key], $new_lang);
  1346. else
  1347. $tabs_array[$prefix_key] = $new_lang;
  1348. $count += count($new_lang);
  1349. }
  1350. }
  1351. }
  1352. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  1353. 'missing_translations' => $missing_translations_front,
  1354. 'count' => $count,
  1355. 'limit_warning' => $this->displayLimitPostWarning($count),
  1356. 'tabsArray' => $tabs_array,
  1357. ));
  1358. // Add js variables needed for autotranslate
  1359. //$this->tpl_view_vars = array_merge($this->tpl_view_vars, $this->initAutoTranslate());
  1360. $this->initToolbar();
  1361. $this->base_tpl_view = 'translation_form.tpl';
  1362. return parent::renderView();
  1363. }
  1364. /**
  1365. * This method generate the form for back translations
  1366. */
  1367. public function initFormBack()
  1368. {
  1369. $name_var = $this->translations_informations[$this->type_selected]['var'];
  1370. $GLOBALS[$name_var] = $this->fileExists();
  1371. $missing_translations_back = array();
  1372. // Get all types of file (PHP, TPL...) and a list of files to parse by folder
  1373. $files_per_directory = $this->getFileToParseByTypeTranslation();
  1374. foreach ($files_per_directory['php'] as $dir => $files)
  1375. foreach ($files as $file)
  1376. // Check if is a PHP file and if the override file exists
  1377. if (preg_match('/^(.*)\.php$/', $file) && Tools::file_exists_cache($file_path = $dir.$file) && !in_array($file, self::$ignore_folder))
  1378. {
  1379. $prefix_key = basename($file);
  1380. // -4 becomes -14 to remove the ending "Controller.php" from the filename
  1381. if (strpos($file, 'Controller.php') !== false)
  1382. $prefix_key = basename(substr($file, 0, -14));
  1383. else if (strpos($file, 'Helper') !== false)
  1384. $prefix_key = 'Helper';
  1385. if ($prefix_key == 'Admin')
  1386. $prefix_key = 'AdminController';
  1387. if ($prefix_key == 'PaymentModule.php')
  1388. $prefix_key = 'PaymentModule';
  1389. // Get content for this file
  1390. $content = file_get_contents($file_path);
  1391. // Parse this content
  1392. $matches = $this->userParseFile($content, $this->type_selected, 'php');
  1393. foreach ($matches as $key)
  1394. {
  1395. // Caution ! front has underscore between prefix key and md5, back has not
  1396. if (isset($GLOBALS[$name_var][$prefix_key.md5($key)]))
  1397. $tabs_array[$prefix_key][$key]['trad'] = stripslashes(html_entity_decode($GLOBALS[$name_var][$prefix_key.md5($key)], ENT_COMPAT, 'UTF-8'));
  1398. else
  1399. {
  1400. if (!isset($tabs_array[$prefix_key][$key]['trad']))
  1401. {
  1402. $tabs_array[$prefix_key][$key]['trad'] = '';
  1403. if (!isset($missing_translations_back[$prefix_key]))
  1404. $missing_translations_back[$prefix_key] = 1;
  1405. else
  1406. $missing_translations_back[$prefix_key]++;
  1407. }
  1408. }
  1409. $tabs_array[$prefix_key][$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  1410. }
  1411. }
  1412. foreach ($files_per_directory['specific'] as $dir => $files)
  1413. foreach ($files as $file)
  1414. if (Tools::file_exists_cache($file_path = $dir.$file) && !in_array($file, self::$ignore_folder))
  1415. {
  1416. $prefix_key = 'index';
  1417. // Get content for this file
  1418. $content = file_get_contents($file_path);
  1419. // Parse this content
  1420. $matches = $this->userParseFile($content, $this->type_selected, 'specific');
  1421. foreach ($matches as $key)
  1422. {
  1423. // Caution ! front has underscore between prefix key and md5, back has not
  1424. if (isset($GLOBALS[$name_var][$prefix_key.md5($key)]))
  1425. $tabs_array[$prefix_key][$key]['trad'] = stripslashes(html_entity_decode($GLOBALS[$name_var][$prefix_key.md5($key)], ENT_COMPAT, 'UTF-8'));
  1426. else
  1427. {
  1428. if (!isset($tabs_array[$prefix_key][$key]['trad']))
  1429. {
  1430. $tabs_array[$prefix_key][$key]['trad'] = '';
  1431. if (!isset($missing_translations_back[$prefix_key]))
  1432. $missing_translations_back[$prefix_key] = 1;
  1433. else
  1434. $missing_translations_back[$prefix_key]++;
  1435. }
  1436. }
  1437. $tabs_array[$prefix_key][$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  1438. }
  1439. }
  1440. foreach ($files_per_directory['tpl'] as $dir => $files)
  1441. foreach ($files as $file)
  1442. if (preg_match('/^(.*).tpl$/', $file) && Tools::file_exists_cache($file_path = $dir.$file))
  1443. {
  1444. // get controller name instead of file name
  1445. $prefix_key = Tools::toCamelCase(str_replace(_PS_ADMIN_DIR_.'/themes', '', $file_path), true);
  1446. $pos = strrpos($prefix_key, DIRECTORY_SEPARATOR);
  1447. $tmp = substr($prefix_key, 0, $pos);
  1448. if (preg_match('#controllers#', $tmp))
  1449. {
  1450. $parent_class = explode(DIRECTORY_SEPARATOR, $tmp);
  1451. $key = array_search('controllers', $parent_class);
  1452. $prefix_key = 'Admin'.ucfirst($parent_class[$key + 1]);
  1453. }
  1454. else
  1455. $prefix_key = 'Admin'.ucfirst(substr($tmp, strrpos($tmp, DIRECTORY_SEPARATOR) + 1, $pos));
  1456. // Adding list, form, option in Helper Translations
  1457. $list_prefix_key = array('AdminHelpers', 'AdminList', 'AdminView', 'AdminOptions', 'AdminForm', 'AdminHelpAccess');
  1458. if (in_array($prefix_key, $list_prefix_key))
  1459. $prefix_key = 'Helper';
  1460. // Adding the folder backup/download/ in AdminBackup Translations
  1461. if ($prefix_key == 'AdminDownload')
  1462. $prefix_key = 'AdminBackup';
  1463. // use the prefix "AdminController" (like old php files 'header', 'footer.inc', 'index', 'login', 'password', 'functions'
  1464. if ($prefix_key == 'Admin' || $prefix_key == 'AdminTemplate')
  1465. $prefix_key = 'AdminController';
  1466. $new_lang = array();
  1467. // Get content for this file
  1468. $content = file_get_contents($file_path);
  1469. // Parse this content
  1470. $matches = $this->userParseFile($content, $this->type_selected, 'tpl');
  1471. /* Get string translation for each tpl file */
  1472. foreach ($matches as $english_string)
  1473. {
  1474. if (empty($english_string))
  1475. {
  1476. $this->errors[] = sprintf($this->l('Error in template - Empty string found, please edit: "%s"'), $file_path);
  1477. $new_lang[$english_string] = '';
  1478. }
  1479. else
  1480. {
  1481. $trans_key = $prefix_key.md5($english_string);
  1482. if (isset($GLOBALS[$name_var][$trans_key]))
  1483. $new_lang[$english_string]['trad'] = html_entity_decode($GLOBALS[$name_var][$trans_key], ENT_COMPAT, 'UTF-8');
  1484. else
  1485. {
  1486. if (!isset($new_lang[$english_string]['trad']))
  1487. {
  1488. $new_lang[$english_string]['trad'] = '';
  1489. if (!isset($missing_translations_back[$prefix_key]))
  1490. $missing_translations_back[$prefix_key] = 1;
  1491. else
  1492. $missing_translations_back[$prefix_key]++;
  1493. }
  1494. }
  1495. $new_lang[$english_string]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  1496. }
  1497. }
  1498. if (isset($tabs_array[$prefix_key]))
  1499. $tabs_array[$prefix_key] = array_merge($tabs_array[$prefix_key], $new_lang);
  1500. else
  1501. $tabs_array[$prefix_key] = $new_lang;
  1502. }
  1503. // count will contain the number of expressions of the page
  1504. $count = 0;
  1505. foreach ($tabs_array as $array)
  1506. $count += count($array);
  1507. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  1508. 'count' => $count,
  1509. 'limit_warning' => $this->displayLimitPostWarning($count),
  1510. 'tabsArray' => $tabs_array,
  1511. 'missing_translations' => $missing_translations_back
  1512. ));
  1513. // Add js variables needed for autotranslate
  1514. //$this->tpl_view_vars = array_merge($this->tpl_view_vars, $this->initAutoTranslate());
  1515. $this->initToolbar();
  1516. $this->base_tpl_view = 'translation_form.tpl';
  1517. return parent::renderView();
  1518. }
  1519. /**
  1520. * Check if directory and file exist and return an list of modules
  1521. *
  1522. * @return array : list of modules
  1523. */
  1524. public function getListModules()
  1525. {
  1526. if (!Tools::file_exists_cache($this->translations_informations['modules']['dir']))
  1527. throw new PrestaShopException(Tools::displayError('Fatal error: Module directory does not exist').'('.$this->translations_informations['modules']['dir'].')');
  1528. if (!is_writable($this->translations_informations['modules']['dir']))
  1529. throw new PrestaShopException(Tools::displayError('The module directory must be writable'));
  1530. $modules = array();
  1531. if (!_PS_MODE_DEV_ && $this->theme_selected == self::DEFAULT_THEME_NAME)
  1532. {
  1533. // Get all module which are installed for to have a minimum of POST
  1534. $modules = Module::getModulesInstalled();
  1535. foreach ($modules as &$module)
  1536. $module = $module['name'];
  1537. }
  1538. else if ($this->theme_selected == self::DEFAULT_THEME_NAME)
  1539. if (Tools::file_exists_cache($this->translations_informations['modules']['dir']))
  1540. $modules = scandir($this->translations_informations['modules']['dir']);
  1541. else
  1542. $this->displayWarning(Tools::displayError('There are no modules in your copy of PrestaShop. Use the Modules page to activate them or go to our Website to download additional Modules.'));
  1543. else
  1544. if (Tools::file_exists_cache($this->translations_informations['modules']['override']['dir']))
  1545. $modules = scandir($this->translations_informations['modules']['override']['dir']);
  1546. else
  1547. $this->displayWarning(Tools::displayError('There are no modules in your copy of PrestaShop. Use the Modules page to activate them or go to our Website to download additional Modules.'));
  1548. return $modules;
  1549. }
  1550. /**
  1551. * This method generate the form for errors translations
  1552. */
  1553. public function initFormErrors()
  1554. {
  1555. $name_var = $this->translations_informations[$this->type_selected]['var'];
  1556. $GLOBALS[$name_var] = $this->fileExists();
  1557. $count_empty = array();
  1558. /* List files to parse */
  1559. $string_to_translate = array();
  1560. $file_by_directory = $this->getFileToParseByTypeTranslation();
  1561. if ($modules = $this->getListModules())
  1562. {
  1563. foreach ($modules as $module)
  1564. if (is_dir(_PS_MODULE_DIR_.$module) && !in_array($module, self::$ignore_folder))
  1565. $file_by_directory['php'] = array_merge($file_by_directory['php'], $this->listFiles(_PS_MODULE_DIR_.$module.'/', array(), 'php'));
  1566. }
  1567. foreach ($file_by_directory['php'] as $dir => $files)
  1568. foreach ($files as $file)
  1569. if (preg_match('/\.php$/', $file) && Tools::file_exists_cache($file_path = $dir.$file) && !in_array($file, self::$ignore_folder))
  1570. {
  1571. if (!filesize($file_path))
  1572. continue;
  1573. // Get content for this file
  1574. $content = file_get_contents($file_path);
  1575. // Parse this content
  1576. $matches = $this->userParseFile($content, $this->type_selected);
  1577. foreach ($matches as $key)
  1578. {
  1579. if (array_key_exists(md5($key), $GLOBALS[$name_var]))
  1580. $string_to_translate[$key]['trad'] = html_entity_decode($GLOBALS[$name_var][md5($key)], ENT_COMPAT, 'UTF-8');
  1581. else
  1582. {
  1583. $string_to_translate[$key]['trad'] = '';
  1584. if (!isset($count_empty[$key]))
  1585. $count_empty[$key] = 1;
  1586. }
  1587. $string_to_translate[$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  1588. }
  1589. }
  1590. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  1591. 'count' => count($string_to_translate),
  1592. 'limit_warning' => $this->displayLimitPostWarning(count($string_to_translate)),
  1593. 'errorsArray' => $string_to_translate,
  1594. 'missing_translations' => $count_empty
  1595. ));
  1596. $this->initToolbar();
  1597. $this->base_tpl_view = 'translation_errors.tpl';
  1598. return parent::renderView();
  1599. }
  1600. /**
  1601. * This method generate the form for fields translations
  1602. */
  1603. public function initFormFields()
  1604. {
  1605. $name_var = $this->translations_informations[$this->type_selected]['var'];
  1606. $GLOBALS[$name_var] = $this->fileExists();
  1607. $missing_translations_fields = array();
  1608. $class_array = array();
  1609. $tabs_array = array();
  1610. $count = 0;
  1611. $files_by_directory = $this->getFileToParseByTypeTranslation();
  1612. foreach ($files_by_directory['php'] as $dir => $files)
  1613. foreach ($files as $file)
  1614. {
  1615. if (!preg_match('/\.php$/', $file) || $file == 'index.php')
  1616. continue;
  1617. include_once($dir.$file);
  1618. $prefix_key = substr($file, 0, -4);
  1619. if (!class_exists($prefix_key))
  1620. continue;
  1621. if (!is_subclass_of($prefix_key, 'ObjectModel'))
  1622. continue;
  1623. $class_array[$prefix_key] = call_user_func(array($prefix_key, 'getValidationRules'), $prefix_key);
  1624. }
  1625. foreach ($class_array as $prefix_key => $rules)
  1626. {
  1627. if (isset($rules['validate']))
  1628. foreach ($rules['validate'] as $key => $value)
  1629. {
  1630. if (isset($GLOBALS[$name_var][$prefix_key.'_'.md5($key)]))
  1631. {
  1632. $tabs_array[$prefix_key][$key]['trad'] = html_entity_decode($GLOBALS[$name_var][$prefix_key.'_'.md5($key)], ENT_COMPAT, 'UTF-8');
  1633. $count++;
  1634. }
  1635. else
  1636. {
  1637. if (!isset($tabs_array[$prefix_key][$key]['trad']))
  1638. {
  1639. $tabs_array[$prefix_key][$key]['trad'] = '';
  1640. if (!isset($missing_translations_fields[$prefix_key]))
  1641. $missing_translations_fields[$prefix_key] = 1;
  1642. else
  1643. $missing_translations_fields[$prefix_key]++;
  1644. $count++;
  1645. }
  1646. }
  1647. }
  1648. if (isset($rules['validateLang']))
  1649. foreach ($rules['validateLang'] as $key => $value)
  1650. {
  1651. if (isset($GLOBALS[$name_var][$prefix_key.'_'.md5($key)]))
  1652. {
  1653. $tabs_array[$prefix_key][$key]['trad'] = '';
  1654. if (array_key_exists($prefix_key.'_'.md5(addslashes($key)), $GLOBALS[$name_var]))
  1655. $tabs_array[$prefix_key][$key]['trad'] = html_entity_decode($GLOBALS[$name_var][$prefix_key.'_'.md5(addslashes($key))], ENT_COMPAT, 'UTF-8');
  1656. $count++;
  1657. }
  1658. else
  1659. {
  1660. if (!isset($tabs_array[$prefix_key][$key]['trad']))
  1661. {
  1662. $tabs_array[$prefix_key][$key]['trad'] = '';
  1663. if (!isset($missing_translations_fields[$prefix_key]))
  1664. $missing_translations_fields[$prefix_key] = 1;
  1665. else
  1666. $missing_translations_fields[$prefix_key]++;
  1667. $count++;
  1668. }
  1669. }
  1670. }
  1671. }
  1672. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  1673. 'count' => $count,
  1674. 'limit_warning' => $this->displayLimitPostWarning($count),
  1675. 'tabsArray' => $tabs_array,
  1676. 'missing_translations' => $missing_translations_fields
  1677. ));
  1678. $this->initToolbar();
  1679. $this->base_tpl_view = 'translation_form.tpl';
  1680. return parent::renderView();
  1681. }
  1682. /**
  1683. * Get each informations for each mails founded in the folder $dir.
  1684. *
  1685. * @since 1.4.0.14
  1686. * @param string $dir
  1687. * @param string $group_name
  1688. * @return array : list of mails
  1689. */
  1690. public function getMailFiles($dir, $group_name = 'mail')
  1691. {
  1692. $arr_return = array();
  1693. // Very usefull to name input and textarea fields
  1694. $arr_return['group_name'] = $group_name;
  1695. $arr_return['empty_values'] = 0;
  1696. $arr_return['total_filled'] = 0;
  1697. $arr_return['directory'] = $dir;
  1698. // Get path for english mail directory
  1699. $dir_en = str_replace('/'.$this->lang_selected->iso_code.'/', '/en/', $dir);
  1700. if (Tools::file_exists_cache($dir_en))
  1701. {
  1702. // Get all english files to compare with the language to translate
  1703. foreach (scandir($dir_en) as $email_file)
  1704. {
  1705. if (strripos($email_file, '.html') > 0 || strripos($email_file, '.txt') > 0)
  1706. {
  1707. $email_name = substr($email_file, 0, strripos($email_file, '.'));
  1708. $type = substr($email_file, strripos($email_file, '.') + 1);
  1709. if (!isset($arr_return['files'][$email_name]))
  1710. $arr_return['files'][$email_name] = array();
  1711. // $email_file is from scandir ($dir), so we already know that file exists
  1712. $arr_return['files'][$email_name][$type]['en'] = $this->getMailContent($dir_en, $email_file);
  1713. // check if the file exists in the language to translate
  1714. if (Tools::file_exists_cache($dir.'/'.$email_file))
  1715. {
  1716. $arr_return['files'][$email_name][$type][$this->lang_selected->iso_code] = $this->getMailContent($dir, $email_file);
  1717. $this->total_expression++;
  1718. }
  1719. else
  1720. $arr_return['files'][$email_name][$type][$this->lang_selected->iso_code] = '';
  1721. if ($arr_return['files'][$email_name][$type][$this->lang_selected->iso_code] == '')
  1722. $arr_return['empty_values']++;
  1723. else
  1724. $arr_return['total_filled']++;
  1725. }
  1726. }
  1727. }
  1728. else
  1729. $this->warnings[] = sprintf(Tools::displayError('mail directory exists for %1$s but not for english in %2$s'),
  1730. $this->lang_selected->iso_code, str_replace(_PS_ROOT_DIR_, '', $dir));
  1731. return $arr_return;
  1732. }
  1733. /**
  1734. * Get content of the mail file.
  1735. *
  1736. * @since 1.4.0.14
  1737. * @param string $dir
  1738. * @param string $file
  1739. * @return array : content of file
  1740. */
  1741. protected function getMailContent($dir, $file)
  1742. {
  1743. $content = file_get_contents($dir.'/'.$file);
  1744. if (Tools::strlen($content) === 0)
  1745. $content = '';
  1746. return $content;
  1747. }
  1748. /**
  1749. * Display mails in html format.
  1750. * This was create for factorize the html displaying
  1751. *
  1752. * @since 1.4.0.14
  1753. * @param array $mails
  1754. * @param array $all_subject_mail
  1755. * @param Language $obj_lang
  1756. * @param string $id_html use for set html id attribute for the block
  1757. * @param string $title Set the title for the block
  1758. * @param string|boolean $name_for_module is not false define add a name for disntiguish mails module
  1759. */
  1760. protected function displayMailContent($mails, $all_subject_mail, $obj_lang, $id_html, $title, $name_for_module = false)
  1761. {
  1762. $str_return = '';
  1763. $group_name = 'mail';
  1764. if (array_key_exists('group_name', $mails))
  1765. $group_name = $mails['group_name'];
  1766. $str_return .= '
  1767. <div class="mails_field" >
  1768. <h3 style="cursor : pointer" onclick="$(\'#'.$id_html.'\').slideToggle();">'.$title.' - <font color="red">'.$mails['empty_values'].'</font> '
  1769. .sprintf($this->l('missing translation(s) on %1$s template(s) for %2$s'),
  1770. '<font color="blue">'.((int)$mails['empty_values'] + (int)$mails['total_filled']).'</font>',
  1771. $obj_lang->name)
  1772. .':</h3>
  1773. <div name="mails_div" id="'.$id_html.'">';
  1774. if (!empty($mails['files']))
  1775. {
  1776. foreach ($mails['files'] as $mail_name => $mail_files)
  1777. {
  1778. if (array_key_exists('html', $mail_files) || array_key_exists('txt', $mail_files))
  1779. {
  1780. if (array_key_exists($mail_name, $all_subject_mail))
  1781. {
  1782. $subject_mail = $all_subject_mail[$mail_name];
  1783. $value_subject_mail = isset($mails['subject'][$subject_mail]) ? $mails['subject'][$subject_mail] : '';
  1784. $str_return .= '
  1785. <div class="label-subject" style="text-align:center;">
  1786. <label style="text-align:right">'.sprintf($this->l('Subject for %s:'), '<em>'.$mail_name.'</em>').'</label>
  1787. <div class="mail-form" style="text-align:left">
  1788. <b>'.$subject_mail.'</b><br />';
  1789. if (isset($value_subject_mail['trad']) && $value_subject_mail['trad'])
  1790. $str_return .= '<input type="text" name="subject['.$group_name.']['.$subject_mail.']" value="'.$value_subject_mail['trad'].'" />';
  1791. else
  1792. $str_return .= '<input type="text" name="subject['.$group_name.']['.$subject_mail.']" value="" />';
  1793. if (isset($value_subject_mail['use_sprintf']) && $value_subject_mail['use_sprintf'])
  1794. {
  1795. $str_return .= '<a class="useSpecialSyntax" title="'.$this->l('This expression uses a special syntax:').' '.$value_subject_mail['use_sprintf'].'" style="cursor:pointer">
  1796. <img src="'._PS_IMG_.'admin/error.png" alt="'.$value_subject_mail['use_sprintf'].'" />
  1797. </a>';
  1798. }
  1799. $str_return .= '</div>
  1800. </div>';
  1801. }
  1802. else
  1803. {
  1804. $str_return .= '
  1805. <div class="label-subject">
  1806. <b>'.sprintf($this->l('No Subject was found for %s, or subject is generated in database.'), '<em>'.$mail_name.'</em>').'</b>
  1807. </div>';
  1808. }
  1809. if (array_key_exists('html', $mail_files))
  1810. {
  1811. $base_uri = str_replace(_PS_ROOT_DIR_, __PS_BASE_URI__, $mails['directory']);
  1812. $base_uri = str_replace('//', '/', $base_uri);
  1813. $url_mail = $base_uri.$mail_name.'.html';
  1814. $str_return .= $this->displayMailBlockHtml($mail_files['html'], $obj_lang->iso_code, $url_mail, $mail_name, $group_name, $name_for_module);
  1815. }
  1816. if (array_key_exists('txt', $mail_files))
  1817. $str_return .= $this->displayMailBlockTxt($mail_files['txt'], $obj_lang->iso_code, $mail_name, $group_name, $name_for_module);
  1818. }
  1819. }
  1820. }
  1821. else
  1822. {
  1823. $str_return .= '
  1824. <p class="error">'.$this->l('There is a problem getting the Mail files.').'<br />'
  1825. .sprintf($this->l('Please ensure that English files exist in %s folder'), '<em>'.$mails['directory'].'en</em>')
  1826. .'</p>';
  1827. }
  1828. $str_return .= '
  1829. </div><!-- #'.$id_html.' -->
  1830. <div class="clear"></div>
  1831. </div>';
  1832. return $str_return;
  1833. }
  1834. /**
  1835. * Just build the html structure for display txt mails
  1836. *
  1837. * @since 1.4.0.14
  1838. * @param array $content with english and language needed contents
  1839. * @param string $lang iso code of the needed language
  1840. * @param string $mail_name name of the file to translate (same for txt and html files)
  1841. * @param string $group_name group name allow to distinguish each block of mail.
  1842. * @param string|boolean $name_for_module is not false define add a name for disntiguish mails module
  1843. */
  1844. protected function displayMailBlockTxt($content, $lang, $mail_name, $group_name, $name_for_module = false)
  1845. {
  1846. return '
  1847. <div class="block-mail" >
  1848. <label>'.$mail_name.'.txt</label>
  1849. <div class="mail-form">
  1850. <div><textarea class="rte mailrte noEditor" cols="80" rows="30" name="'.$group_name.'[txt]['.($name_for_module ? $name_for_module.'|' : '' ).$mail_name.']" style="width:560px;margin=0;">'.Tools::htmlentitiesUTF8(stripslashes(strip_tags($content[$lang]))).'</textarea></div>
  1851. </div><!-- .mail-form -->
  1852. </div><!-- .block-mail -->';
  1853. }
  1854. /**
  1855. * Just build the html structure for display html mails.
  1856. *
  1857. * @since 1.4.0.14
  1858. * @param array $content with english and language needed contents
  1859. * @param string $lang iso code of the needed language
  1860. * @param string $url for the html page and displaying an outline
  1861. * @param string $mail_name name of the file to translate (same for txt and html files)
  1862. * @param string $group_name group name allow to distinguish each block of mail.
  1863. * @param string|boolean $name_for_module is not false define add a name for disntiguish mails module
  1864. */
  1865. protected function displayMailBlockHtml($content, $lang, $url, $mail_name, $group_name, $name_for_module = false)
  1866. {
  1867. $title = array();
  1868. // Because TinyMCE don't work correctly with <DOCTYPE>, <html> and <body> tags
  1869. if (stripos($content[$lang], '<body'))
  1870. {
  1871. $array_lang = $lang != 'en' ? array('en', $lang) : array($lang);
  1872. foreach ($array_lang as $language)
  1873. {
  1874. $title[$language] = substr($content[$language], 0, stripos($content[$language], '<body'));
  1875. preg_match('#<title>([^<]+)</title>#Ui', $title[$language], $matches);
  1876. $title[$language] = empty($matches[1])?'':$matches[1];
  1877. // The 2 lines below allow to exlude <body> tag from the content.
  1878. // This allow to exclude body tag even if attributs are setted.
  1879. $content[$language] = substr($content[$language], stripos($content[$language], '<body') + 5);
  1880. $content[$language] = substr($content[$language], stripos($content[$language], '>') + 1);
  1881. $content[$language] = substr($content[$language], 0, stripos($content[$language], '</body>'));
  1882. }
  1883. }
  1884. $str_return = '';
  1885. $name_for_module = $name_for_module ? $name_for_module.'|' : '';
  1886. $content[$lang] = (isset($content[$lang]) ? Tools::htmlentitiesUTF8(stripslashes($content[$lang])) : '');
  1887. $str_return .= '
  1888. <div class="block-mail" >
  1889. <label>'.$mail_name.'.html</label>
  1890. <div class="mail-form">
  1891. <div>';
  1892. $str_return .= '
  1893. <div class="label-subject">
  1894. <b>'.$this->l('"title" tag:').'</b>&nbsp;'.(isset($title['en']) ? $title['en'] : '').'<br />
  1895. <input type="text" name="title_'.$group_name.'_'.$mail_name.'" value="'.(isset($title[$lang]) ? $title[$lang] : '').'" />
  1896. </div><!-- .label-subject -->';
  1897. $str_return .= '
  1898. <iframe style="background:white;border:1px solid #DFD5C3;" border="0" src ="'.$url.'?'.(rand(0, 1000000000)).'" width="565" height="497"></iframe>
  1899. <a style="display:block;margin-top:5px;width:130px;" href="#" onclick="$(this).parent().hide(); displayTiny($(this).parent().next()); return false;" class="button">'.$this->l('Edit this e-mail template').'</a>
  1900. </div>
  1901. <textarea style="display:none;" class="rte mailrte" cols="80" rows="30" name="'.$group_name.'[html]['.$name_for_module.$mail_name.']">'.$content[$lang].'</textarea>
  1902. </div><!-- .mail-form -->
  1903. </div><!-- .block-mail -->';
  1904. return $str_return;
  1905. }
  1906. /**
  1907. * Check in each module if contains mails folder.
  1908. *
  1909. * @return array of module which has mails
  1910. */
  1911. public function getModulesHasMails($with_module_name = false)
  1912. {
  1913. if ($this->theme_selected != self::DEFAULT_THEME_NAME)
  1914. $i18n_dir = $this->translations_informations['modules']['override']['dir'];
  1915. else
  1916. $i18n_dir = $this->translations_informations['modules']['dir'];
  1917. $arr_modules = array();
  1918. foreach (scandir($i18n_dir) as $module_dir)
  1919. {
  1920. $dir = $i18n_dir.$module_dir.'/';
  1921. if (!in_array($module_dir, self::$ignore_folder) && Tools::file_exists_cache($dir.'mails/'))
  1922. if ($with_module_name)
  1923. $arr_modules[$module_dir] = $dir;
  1924. else
  1925. $arr_modules[$dir] = scandir($dir);
  1926. }
  1927. return $arr_modules;
  1928. }
  1929. protected function getTinyMCEForMails($iso_lang)
  1930. {
  1931. // TinyMCE
  1932. $iso_tiny_mce = (Tools::file_exists_cache(_PS_ROOT_DIR_.'/js/tiny_mce/langs/'.$iso_lang.'.js') ? $iso_lang : 'en');
  1933. $ad = dirname($_SERVER['PHP_SELF']);
  1934. return '
  1935. <script type="text/javascript">
  1936. var iso = \''.$iso_tiny_mce.'\' ;
  1937. var pathCSS = \''._THEME_CSS_DIR_.'\' ;
  1938. var ad = \''.$ad.'\' ;
  1939. </script>
  1940. <script type="text/javascript" src="'.__PS_BASE_URI__.'js/tiny_mce/tiny_mce.js"></script>
  1941. <script type="text/javascript" src="'.__PS_BASE_URI__.'js/tinymce.inc.js"></script>
  1942. <script type="text/javascript">
  1943. $(document).ready(function () {
  1944. tinySetup();
  1945. });
  1946. function displayTiny(obj) {
  1947. tinyMCE.get(obj.attr(\'name\')).show();
  1948. }
  1949. </script>';
  1950. }
  1951. /**
  1952. * This method generate the form for mails translations
  1953. */
  1954. public function initFormMails($no_display = false)
  1955. {
  1956. $module_mails = array();
  1957. // get all mail subjects, this method parse each files in Prestashop !!
  1958. $subject_mail = array();
  1959. $modules_has_mails = $this->getModulesHasMails(true);
  1960. $files_by_directiories = $this->getFileToParseByTypeTranslation();
  1961. foreach ($files_by_directiories['php'] as $dir => $files)
  1962. foreach ($files as $file)
  1963. if (Tools::file_exists_cache($dir.$file) && is_file($dir.$file) && !in_array($file, self::$ignore_folder) && preg_match('/\.php$/', $file))
  1964. $subject_mail = $this->getSubjectMail($dir, $file, $subject_mail);
  1965. // Get path of directory for find a good path of translation file
  1966. if ($this->theme_selected != self::DEFAULT_THEME_NAME && @filemtime($this->translations_informations[$this->type_selected]['override']['dir']))
  1967. $i18n_dir = $this->translations_informations[$this->type_selected]['override']['dir'];
  1968. else
  1969. $i18n_dir = $this->translations_informations[$this->type_selected]['dir'];
  1970. $core_mails = $this->getMailFiles($i18n_dir, 'core_mail');
  1971. $core_mails['subject'] = $this->getSubjectMailContent($i18n_dir);
  1972. foreach ($modules_has_mails as $module_name => $module_path)
  1973. {
  1974. $module_mails[$module_name] = $this->getMailFiles($module_path.'mails/'.$this->lang_selected->iso_code.'/', 'module_mail');
  1975. $module_mails[$module_name]['subject'] = $core_mails['subject'];
  1976. $module_mails[$module_name]['display'] = $this->displayMailContent($module_mails[$module_name], $subject_mail, $this->lang_selected, Tools::strtolower($module_name), sprintf($this->l('E-mails for %s module'), '<em>'.$module_name.'</em>'), $module_name);
  1977. }
  1978. if ($no_display)
  1979. {
  1980. $empty = 0;
  1981. $total = 0;
  1982. $total += (int)$core_mails['total_filled'];
  1983. $empty += (int)$core_mails['empty_values'];
  1984. foreach ($module_mails as $mod_infos)
  1985. {
  1986. $total += (int)$mod_infos['total_filled'];
  1987. $empty += (int)$mod_infos['empty_values'];
  1988. }
  1989. return array('total' => $total, 'empty' => $empty);
  1990. }
  1991. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  1992. 'limit_warning' => $this->displayLimitPostWarning($this->total_expression),
  1993. 'tinyMCE' => $this->getTinyMCEForMails($this->lang_selected->iso_code),
  1994. 'mail_content' => $this->displayMailContent($core_mails, $subject_mail, $this->lang_selected, 'core', $this->l('Core e-mails')),
  1995. 'module_mails' => $module_mails,
  1996. 'theme_name' => $this->theme_selected
  1997. ));
  1998. $this->initToolbar();
  1999. $this->base_tpl_view = 'translation_mails.tpl';
  2000. return parent::renderView();
  2001. }
  2002. /**
  2003. * Get list of subjects of mails
  2004. *
  2005. * @param $dir
  2006. * @param $file
  2007. * @param $subject_mail
  2008. * @return array : list of subjects of mails
  2009. */
  2010. protected function getSubjectMail($dir, $file, $subject_mail)
  2011. {
  2012. $content = file_get_contents($dir.'/'.$file);
  2013. $content = str_replace("\n", ' ', $content);
  2014. if (preg_match_all('/Mail::Send([^;]*);/si', $content, $tab))
  2015. for ($i = 0; isset($tab[1][$i]); $i++)
  2016. {
  2017. $tab2 = explode(',', $tab[1][$i]);
  2018. if (is_array($tab2))
  2019. if ($tab2 && isset($tab2[1]))
  2020. {
  2021. $tab2[1] = trim(str_replace('\'', '', $tab2[1]));
  2022. if (preg_match('/Mail::l\(\''._PS_TRANS_PATTERN_.'\'/s', $tab2[2], $matches))
  2023. $subject_mail[$tab2[1]] = $matches[1];
  2024. }
  2025. }
  2026. if (!in_array($file, self::$ignore_folder) && is_dir($dir.'/'.$file))
  2027. $subject_mail = $this->getSubjectMail($dir, $file, $subject_mail);
  2028. return $subject_mail;
  2029. }
  2030. /**
  2031. * @param $directory : name of directory
  2032. * @return array
  2033. */
  2034. protected function getSubjectMailContent($directory)
  2035. {
  2036. $subject_mail_content = array();
  2037. if (Tools::file_exists_cache($directory.'/lang.php'))
  2038. {
  2039. // we need to include this even if already included (no include once)
  2040. include($directory.'/lang.php');
  2041. foreach ($GLOBALS[$this->translations_informations[$this->type_selected]['var']] as $key => $subject)
  2042. {
  2043. $this->total_expression++;
  2044. $subject = str_replace('\n', ' ', $subject);
  2045. $subject = str_replace("\\'", "\'", $subject);
  2046. $subject_mail_content[$key]['trad'] = htmlentities($subject, ENT_QUOTES, 'UTF-8');
  2047. $subject_mail_content[$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  2048. }
  2049. }
  2050. else
  2051. $this->errors[] = sprintf($this->l('Subject mail translation file not found in "%s"'), $directory);
  2052. return $subject_mail_content;
  2053. }
  2054. protected function writeSubjectTranslationFile($sub, $path)
  2055. {
  2056. if ($fd = @fopen($path, 'w'))
  2057. {
  2058. $tab = 'LANGMAIL';
  2059. fwrite($fd, "<?php\n\nglobal \$_".$tab.";\n\$_".$tab." = array();\n");
  2060. foreach ($sub as $key => $value)
  2061. {
  2062. // Magic Quotes shall... not.. PASS!
  2063. if (_PS_MAGIC_QUOTES_GPC_)
  2064. $value = stripslashes($value);
  2065. fwrite($fd, '$_'.$tab.'[\''.pSQL($key).'\'] = \''.pSQL($value).'\';'."\n");
  2066. }
  2067. fwrite($fd, "\n?>");
  2068. fclose($fd);
  2069. }
  2070. else
  2071. throw new PrestaShopException(sprintf(Tools::displayError('Cannot write language file for e-mail subjects, path is: %s'), $path));
  2072. }
  2073. /**
  2074. * This get files to translate in module directory.
  2075. * Recursive method allow to get each files for a module no matter his depth.
  2076. *
  2077. * @param string $path directory path to scan
  2078. * @param array $array_files by reference - array which saved files to parse.
  2079. * @param string $module_name module name
  2080. * @param string $lang_file full path of translation file
  2081. * @param boolean $is_default
  2082. */
  2083. protected function recursiveGetModuleFiles($path, &$array_files, $module_name, $lang_file, $is_default = false)
  2084. {
  2085. $files_module = array();
  2086. if (Tools::file_exists_cache($path))
  2087. $files_module = scandir($path);
  2088. $files_for_module = $this->clearModuleFiles($files_module, 'file');
  2089. if (!empty($files_for_module))
  2090. $array_files[] = array(
  2091. 'file_name' => $lang_file,
  2092. 'dir' => $path,
  2093. 'files' => $files_for_module,
  2094. 'module' => $module_name,
  2095. 'is_default' => $is_default,
  2096. 'theme' => $this->theme_selected,
  2097. );
  2098. $dir_module = $this->clearModuleFiles($files_module, 'directory', $path);
  2099. if (!empty($dir_module))
  2100. foreach ($dir_module as $folder)
  2101. $this->recursiveGetModuleFiles($path.$folder.'/', $array_files, $module_name, $lang_file, $is_default);
  2102. }
  2103. /**
  2104. * This method get translation in each translations file.
  2105. * The file depend on $lang param.
  2106. *
  2107. * @param array $modules list of modules
  2108. * @param string $root_dir path where it get each modules
  2109. * @param string $lang iso code of choosen language to translate
  2110. * @param boolean $is_default set it if modules are located in root/prestashop/modules folder
  2111. * This allow to distinguish overrided prestashop theme and original module
  2112. */
  2113. protected function getAllModuleFiles($modules, $root_dir, $lang, $is_default = false)
  2114. {
  2115. $array_files = array();
  2116. foreach ($modules as $module)
  2117. {
  2118. if ($module{0} != '.' && is_dir($root_dir.$module))
  2119. {
  2120. if (Tools::file_exists_cache($root_dir.$module.'/translations/'.$lang.'.php'))
  2121. $lang_file = $root_dir.$module.'/translations/'.$lang.'.php';
  2122. else
  2123. $lang_file = $root_dir.$module.'/'.$lang.'.php';
  2124. @include($lang_file);
  2125. $this->getModuleTranslations();
  2126. $this->recursiveGetModuleFiles($root_dir.$module.'/', $array_files, $module, $lang_file, $is_default);
  2127. }
  2128. }
  2129. return $array_files;
  2130. }
  2131. /**
  2132. * This method generate the form for modules translations
  2133. */
  2134. public function initFormModules()
  2135. {
  2136. // Get path of directory for find a good path of translation file
  2137. if ($this->theme_selected != self::DEFAULT_THEME_NAME)
  2138. $i18n_dir = $this->translations_informations[$this->type_selected]['override']['dir'];
  2139. else
  2140. $i18n_dir = $this->translations_informations[$this->type_selected]['dir'];
  2141. // Get list of modules
  2142. $modules = $this->getListModules();
  2143. if (!empty($modules))
  2144. {
  2145. // Get all modules files and include all translation files
  2146. $arr_files = $this->getAllModuleFiles($modules, $i18n_dir, $this->lang_selected->iso_code, true);
  2147. foreach ($arr_files as $value)
  2148. $this->findAndFillTranslations($value['files'], $value['theme'], $value['module'], $value['dir']);
  2149. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  2150. 'default_theme_name' => self::DEFAULT_THEME_NAME,
  2151. 'count' => $this->total_expression,
  2152. 'limit_warning' => $this->displayLimitPostWarning($this->total_expression),
  2153. 'textarea_sized' => TEXTAREA_SIZED,
  2154. 'modules_translations' => isset($this->modules_translations) ? $this->modules_translations : array(),
  2155. 'missing_translations' => $this->missing_translations
  2156. ));
  2157. $this->initToolbar();
  2158. $this->base_tpl_view = 'translation_modules.tpl';
  2159. return parent::renderView();
  2160. }
  2161. }
  2162. /** Parse PDF class
  2163. *
  2164. * @param string $file_path file to parse
  2165. * @param string $file_type type of file
  2166. * @param array $langArray contains expression in the chosen language
  2167. * @param string $tab name to use with the md5 key
  2168. * @param array $tabs_array
  2169. * @return array containing all datas needed for building the translation form
  2170. * @since 1.4.5.0
  2171. */
  2172. protected function parsePdfClass($file_path, $file_type, $lang_array, $tab, $tabs_array, &$count_missing)
  2173. {
  2174. // Get content for this file
  2175. $content = file_get_contents($file_path);
  2176. // Parse this content
  2177. $matches = $this->userParseFile($content, $this->type_selected, $file_type);
  2178. foreach ($matches as $key)
  2179. {
  2180. if (stripslashes(array_key_exists($tab.md5(addslashes($key)), $lang_array)))
  2181. $tabs_array[$tab][$key]['trad'] = html_entity_decode($lang_array[$tab.md5(addslashes($key))], ENT_COMPAT, 'UTF-8');
  2182. else
  2183. {
  2184. $tabs_array[$tab][$key]['trad'] = '';
  2185. if (!isset($count_missing[$tab]))
  2186. $count_missing[$tab] = 1;
  2187. else
  2188. $count_missing[$tab]++;
  2189. }
  2190. $tabs_array[$tab][$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  2191. }
  2192. return $tabs_array;
  2193. }
  2194. /**
  2195. * This method generate the form for PDF translations
  2196. */
  2197. public function initFormPDF()
  2198. {
  2199. $name_var = $this->translations_informations[$this->type_selected]['var'];
  2200. $GLOBALS[$name_var] = array();
  2201. $missing_translations_pdf = array();
  2202. $i18n_dir = $this->translations_informations[$this->type_selected]['dir'];
  2203. $default_i18n_file = $i18n_dir.$this->translations_informations[$this->type_selected]['file'];
  2204. if (($this->theme_selected == self::DEFAULT_THEME_NAME) || _PS_MODE_DEV_)
  2205. $i18n_file = $default_i18n_file;
  2206. else
  2207. {
  2208. $i18n_dir = $this->translations_informations[$this->type_selected]['override']['dir'];
  2209. $i18n_file = $i18n_dir.$this->translations_informations[$this->type_selected]['override']['file'];
  2210. }
  2211. $this->checkDirAndCreate($i18n_file);
  2212. if (!file_exists($i18n_file))
  2213. $this->errors[] = sprintf(Tools::displayError('Please create a "%1$s.php" file in "%2$s"'), $this->lang_selected->iso_code, $i18n_dir);
  2214. if (!is_writable($i18n_file))
  2215. $this->errors[] = sprintf(Tools::displayError('Cannot write into the "%s"'), $i18n_file);
  2216. @include($i18n_file);
  2217. // if the override's translation file is empty load the default file
  2218. if (!isset($GLOBALS[$name_var]) || count($GLOBALS[$name_var]) == 0)
  2219. @include($default_i18n_file);
  2220. $prefix_key = 'PDF';
  2221. $tabs_array = array($prefix_key => array());
  2222. $files_by_directory = $this->getFileToParseByTypeTranslation();
  2223. foreach ($files_by_directory as $type => $directories)
  2224. foreach ($directories as $dir => $files)
  2225. foreach ($files as $file)
  2226. if (!in_array($file, self::$ignore_folder) && Tools::file_exists_cache($file_path = $dir.$file))
  2227. {
  2228. if ($type == 'tpl')
  2229. {
  2230. if (Tools::file_exists_cache($file_path) && is_file($file_path))
  2231. {
  2232. // Get content for this file
  2233. $content = file_get_contents($file_path);
  2234. // Parse this content
  2235. $matches = $this->userParseFile($content, $this->type_selected, 'tpl');
  2236. foreach ($matches as $key)
  2237. {
  2238. if (isset($GLOBALS[$name_var][$prefix_key.md5($key)]))
  2239. $tabs_array[$prefix_key][$key]['trad'] = (html_entity_decode($GLOBALS[$name_var][$prefix_key.md5($key)], ENT_COMPAT, 'UTF-8'));
  2240. else
  2241. {
  2242. if (!isset($tabs_array[$prefix_key][$key]['trad']))
  2243. {
  2244. $tabs_array[$prefix_key][$key]['trad'] = '';
  2245. if (!isset($missing_translations_pdf[$prefix_key]))
  2246. $missing_translations_pdf[$prefix_key] = 1;
  2247. else
  2248. $missing_translations_pdf[$prefix_key]++;
  2249. }
  2250. }
  2251. $tabs_array[$prefix_key][$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  2252. }
  2253. }
  2254. }
  2255. else
  2256. if (Tools::file_exists_cache($file_path))
  2257. $tabs_array = $this->parsePdfClass($file_path, 'php', $GLOBALS[$name_var], $prefix_key, $tabs_array, $missing_translations_pdf);
  2258. }
  2259. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  2260. 'count' => count($tabs_array['PDF']),
  2261. 'limit_warning' => $this->displayLimitPostWarning(count($tabs_array['PDF'])),
  2262. 'tabsArray' => $tabs_array,
  2263. 'missing_translations' => $missing_translations_pdf
  2264. ));
  2265. $this->initToolbar();
  2266. $this->base_tpl_view = 'translation_form.tpl';
  2267. return parent::renderView();
  2268. }
  2269. /**
  2270. * recursively list files in directory $dir
  2271. */
  2272. public function listFiles($dir, $list = array(), $file_ext = 'tpl')
  2273. {
  2274. $dir = rtrim($dir, '/').DIRECTORY_SEPARATOR;
  2275. $to_parse = scandir($dir);
  2276. // copied (and kind of) adapted from AdminImages.php
  2277. foreach ($to_parse as $file)
  2278. {
  2279. if (!in_array($file, self::$ignore_folder))
  2280. {
  2281. if (preg_match('#'.preg_quote($file_ext, '#').'$#i', $file))
  2282. $list[$dir][] = $file;
  2283. else if (is_dir($dir.$file))
  2284. $list = $this->listFiles($dir.$file, $list, $file_ext);
  2285. }
  2286. }
  2287. return $list;
  2288. }
  2289. }