PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/admin/trans.edit.php

https://github.com/cyrke/phpMyFAQ
PHP | 290 lines | 197 code | 30 blank | 63 comment | 45 complexity | 81b89b4eb8faab10dfd72940d93c6ada MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Handle ajax requests for the interface translation tool
  4. *
  5. * PHP Version 5.3
  6. *
  7. * This Source Code Form is subject to the terms of the Mozilla Public License,
  8. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. * obtain one at http://mozilla.org/MPL/2.0/.
  10. *
  11. * @category phpMyFAQ
  12. * @package Administration
  13. * @author Anatoliy Belsky <ab@php.net>
  14. * @copyright 2009-2012 phpMyFAQ Team
  15. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  16. * @link http://www.phpmyfaq.de
  17. * @since 2009-05-11
  18. */
  19. if (!defined('IS_VALID_PHPMYFAQ')) {
  20. header('Location: http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']));
  21. exit();
  22. }
  23. if (!$permission["edittranslation"]) {
  24. print $PMF_LANG['err_NotAuth'];
  25. return;
  26. }
  27. $translateLang = PMF_Filter::filterInput(INPUT_GET, 'translang', FILTER_SANITIZE_STRING);
  28. $page = PMF_Filter::filterInput(INPUT_GET, 'page', FILTER_VALIDATE_INT);
  29. $page = 1 > $page ? 1 : $page;
  30. if (empty($translateLang) || !file_exists(PMF_ROOT_DIR . "/lang/language_$translateLang.php")) {
  31. header("Location: ?action=translist");
  32. }
  33. $tt = new PMF_TransTool;
  34. /**
  35. * There are meanwhile over 600 language
  36. * vars and we won't to show them all
  37. * at once, so let's paginate.
  38. */
  39. $itemsPerPage = 32;
  40. if(!isset($_SESSION['trans'])) {
  41. /**
  42. * English is our exemplary language
  43. */
  44. $_SESSION['trans']['leftVarsOnly'] = $tt->getVars(PMF_ROOT_DIR . "/lang/language_en.php");
  45. $_SESSION['trans']['rightVarsOnly'] = $tt->getVars(PMF_ROOT_DIR . "/lang/language_$translateLang.php");
  46. }
  47. $leftVarsOnly = array_slice($_SESSION['trans']['leftVarsOnly'],
  48. ($page-1)*$itemsPerPage,
  49. $itemsPerPage);
  50. $rightVarsOnly = &$_SESSION['trans']['rightVarsOnly'];
  51. $options = array(
  52. 'baseUrl' => PMF_Link::getSystemRelativeUri('index.php') . '?' . str_replace('&', '&amp;', $_SERVER['QUERY_STRING']),
  53. 'total' => count($_SESSION['trans']['leftVarsOnly']),
  54. 'perPage' => $itemsPerPage
  55. );
  56. $pagination = new PMF_Pagination($faqConfig, $options);
  57. $pageBar = $pagination->render();
  58. /**
  59. * These keys always exist as they are defined when creating translation.
  60. * We use these values to add the correct number of input boxes.
  61. * Left column will always have 2 boxes, right - 1 to 6+ boxes.
  62. */
  63. $leftNPlurals = (int)$_SESSION['trans']['leftVarsOnly']['PMF_LANG[nplurals]'];
  64. $rightNPlurals = (int)$rightVarsOnly['PMF_LANG[nplurals]'];
  65. printf('<header><h2>%s</h2></header>', $PMF_LANG['ad_menu_translations']);
  66. printf('<p style="color: red;">%s</p>', $PMF_LANG['msgTransToolNoteFileSaving']);
  67. $NPluralsErrorReported = false;
  68. ?>
  69. <form id="transDiffForm">
  70. <table class="list" style="width: 100%">
  71. <tr>
  72. <th><?php print $PMF_LANG['msgVariable'] ?></th>
  73. <th>en</th>
  74. <th><?php print $translateLang ?></th>
  75. </tr>
  76. <?php while(list($key, $line) = each($leftVarsOnly)): ?>
  77. <?php
  78. // These parameters are not real translations, so don't offer to translate them
  79. if ($tt->isKeyIgnorable($key)) {
  80. print "<tr>\n";
  81. print "<td>".$key."</td>\n";
  82. print '<td><input style="width: 300px;" type="text" value="'.PMF_String::htmlspecialchars($line).'" disabled="disabled" /></td>'."\n";
  83. print '<td><input style="width: 300px;" type="text" name="'.$key.'" value="'.PMF_String::htmlspecialchars($rightVarsOnly[$key]).'" disabled="disabled" />';
  84. print '<input type="hidden" name="'.$key.'" value="'.PMF_String::htmlspecialchars($rightVarsOnly[$key]).'" /></td>'."\n";
  85. print "</tr>\n";
  86. continue;
  87. }
  88. /**
  89. * Plural form support in translation interface
  90. */
  91. // We deal with the second plural form when dealing with the first, so skip it here
  92. if ($tt->isKeyASecondPluralForm($key))
  93. continue;
  94. if ($tt->isKeyAFirstPluralForm($key)) {
  95. if ($rightNPlurals == -1) {
  96. // Report missing plural form support once.
  97. if (!$NPluralsErrorReported) {
  98. print "<tr>\n";
  99. print '<td align="center" colspan="3">'.sprintf($PMF_LANG['msgTransToolLanguagePluralNotSet'], $translateLang)."</td>\n";
  100. print "</tr>\n";
  101. $NPluralsErrorReported = true;
  102. }
  103. continue;
  104. }
  105. /**
  106. * We print one box for English and one for other language
  107. * because other language will always have at least 1 form
  108. */
  109. print "<tr>\n";
  110. print "<td>".$key."</td>\n";
  111. print '<td><input style="width: 300px;" type="text" value="'.PMF_String::htmlspecialchars($line).'" disabled="disabled" /></td>'."\n";
  112. if (array_key_exists($key, $rightVarsOnly) && ($line != $rightVarsOnly[$key] ||
  113. $tt->isKeyIgnorable($key) || $tt->isValIgnorable($line)))
  114. print '<td><input style="width: 300px;" type="text" name="'.$key.'" value="'.PMF_String::htmlspecialchars($rightVarsOnly[$key]).'" /></td>'."\n";
  115. else
  116. print '<td><input style="width: 300px;border-color: red;" type="text" name="'.$key.'" value="'.PMF_String::htmlspecialchars($line).'" /></td>'."\n";
  117. print "</tr>\n";
  118. // Add second English form and translation
  119. $key2 = str_replace('[0]', '[1]', $key);
  120. print "<tr>\n";
  121. print "<td>".$key2."</td>\n";
  122. print '<td><input style="width: 300px;" type="text" value="'.PMF_String::htmlspecialchars($leftVarsOnly[$key2]).'" disabled="disabled" /></td>'."\n";
  123. if ($rightNPlurals == 1) {
  124. // Other language has only one form
  125. print '<td><input style="width: 300px;" type="text" value="'.$PMF_LANG['msgTransToolLanguageOnePlural'].'" disabled="disabled" /></td>'."\n";
  126. } else {
  127. if (array_key_exists($key2, $rightVarsOnly))
  128. print '<td><input style="width: 300px;" type="text" name="'.$key2.'" value="'.PMF_String::htmlspecialchars($rightVarsOnly[$key2]).'" /></td>'."\n";
  129. else
  130. print '<td><input style="width: 300px;border-color: red;" type="text" name="'.$key2.'" value="'.PMF_String::htmlspecialchars($leftVarsOnly[$key2]).'" /></td>'."\n";
  131. }
  132. print "</tr>\n";
  133. // Other language has more than 2 forms
  134. for ($i = 2; $i < $rightNPlurals; $i++) {
  135. $keyI = str_replace('[0]', "[$i]", $key);
  136. print "<tr>\n";
  137. print "<td>".$keyI."</td>\n";
  138. print '<td><input style="width: 300px;" type="text" value="" disabled="disabled" /></td>'."\n";
  139. if (array_key_exists($keyI, $rightVarsOnly) && $leftVarsOnly[$key2] != $rightVarsOnly[$key])
  140. print '<td><input style="width: 300px;" type="text" name="'.$keyI.'" value="'.PMF_String::htmlspecialchars($rightVarsOnly[$keyI]).'" /></td>'."\n";
  141. else
  142. print '<td><input style="width: 300px;border-color: red;" type="text" name="'.$keyI.'" value="'.PMF_String::htmlspecialchars($leftVarsOnly[$key2]).'" /></td>'."\n";
  143. print "</tr>\n";
  144. }
  145. // We do not need to process this $key any further
  146. continue;
  147. }
  148. ?>
  149. <tr>
  150. <td><?php print $key?></td>
  151. <td><input style="width: 300px;" type="text" value="<?php print PMF_String::htmlspecialchars($line) ?>" disabled="disabled" /></td>
  152. <?php
  153. if (array_key_exists($key, $rightVarsOnly) && ($line != $rightVarsOnly[$key] ||
  154. $tt->isKeyIgnorable($key) || $tt->isValIgnorable($line))):
  155. ?>
  156. <td><input style="width: 300px;" type="text" name="<?php print $key?>" value="<?php print PMF_String::htmlspecialchars($rightVarsOnly[$key]) ?>" /></td>
  157. <?php else: ?>
  158. <td><input style="width: 300px;border-color: red;" type="text" name="<?php print $key?>" value="<?php print PMF_String::htmlspecialchars($line) ?>" /></td>
  159. <?php endif; ?>
  160. </tr>
  161. <?php endwhile; ?>
  162. <tr>
  163. <td colspan="3"><?php print $pageBar; ?></td>
  164. </tr>
  165. <tr>
  166. <td>&nbsp;</td>
  167. <td>
  168. <button class="btn btn-inverse" type="button" onclick="location.href='?action=translist'">
  169. <?php print $PMF_LANG['msgCancel'] ?>
  170. </button>
  171. </td>
  172. <td>
  173. <button class="btn btn-success" type="button"
  174. onclick="save()"<?php if (!is_writable(PMF_ROOT_DIR . "/lang/language_$translateLang.php")) { print ' disabled="disabled"'; } ?>>
  175. <?php print $PMF_LANG['msgSave'] ?>
  176. </button>
  177. </td>
  178. </tr>
  179. </table>
  180. </form>
  181. <script>
  182. /**
  183. * Gather data from the current form
  184. *
  185. * @return object
  186. */
  187. function getFormData()
  188. {
  189. var data = {};
  190. var form = document.getElementById('transDiffForm');
  191. for (var i=0; i < form.elements.length;i++) {
  192. var element = form.elements[i]
  193. if (('text' == element.type || 'hidden' == element.type) && !element.disabled) {
  194. data[element.name] = element.value
  195. }
  196. }
  197. return data;
  198. }
  199. /**
  200. * Go to some page
  201. *
  202. * @return void
  203. */
  204. function go(url)
  205. {
  206. if(savePageBuffer()) {
  207. document.location = url;
  208. }
  209. }
  210. /**
  211. * Send page buffer to save it into the session
  212. *
  213. * @return boolean
  214. */
  215. function savePageBuffer()
  216. {
  217. var result = false;
  218. $('#saving_data_indicator').html('<img src="images/indicator.gif" /> <?php printf($PMF_LANG['msgTransToolRecordingPageBuffer'], $page); ?>');
  219. $.ajax({url: 'index.php?action=ajax&ajax=trans&ajaxaction=save_page_buffer',
  220. data: getFormData(),
  221. async: false,
  222. type: 'POST',
  223. success: function (retval, status) {
  224. result = 1*retval > 0 && 'success' == status;
  225. if (result) {
  226. $('#saving_data_indicator').html('<?php printf($PMF_LANG['msgTransToolPageBufferRecorded'], $page); ?>');
  227. } else {
  228. $('#saving_data_indicator').html('<?php printf($PMF_LANG['msgTransToolErrorRecordingPageBuffer'], $page); ?>');
  229. }
  230. },
  231. error: function() {
  232. $('#saving_data_indicator').html('<?php printf($PMF_LANG['msgTransToolErrorRecordingPageBuffer'], $page); ?>');
  233. }
  234. });
  235. return result;
  236. }
  237. /**
  238. * Transparently save the translation form
  239. * @return void
  240. */
  241. function save()
  242. {
  243. $('#saving_data_indicator').html('<img src="images/indicator.gif" /> <?php print $PMF_LANG['msgSaving3Dots'] ?>');
  244. if(savePageBuffer()) {
  245. $.post('index.php?action=ajax&ajax=trans&ajaxaction=save_translated_lang',
  246. null,
  247. function (retval, status) {
  248. if (1*retval > 0 && 'success' == status) {
  249. $('#saving_data_indicator').html('<?php print $PMF_LANG['msgTransToolFileSaved'] ?>');
  250. document.location = '?action=translist'
  251. } else {
  252. $('#saving_data_indicator').html('<?php print $PMF_LANG['msgTransToolErrorSavingFile'] ?>');
  253. }
  254. }
  255. )
  256. } else {
  257. $('#saving_data_indicator').html('<?php print $PMF_LANG['msgTransToolErrorSavingFile'] ?>');
  258. }
  259. }
  260. </script>