sub_language.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script for sub-language administration.
  5. *
  6. * @package chamilo.admin.sub_language
  7. */
  8. $cidReset = true;
  9. $this_script = 'sub_language';
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $this_section = SECTION_PLATFORM_ADMIN;
  12. api_protect_admin_script();
  13. $htmlHeadXtra[] = '<script>
  14. $(function () {
  15. $(".save").click(function() {
  16. var button_name=$(this).attr("name");
  17. var button_array=button_name.split("|");
  18. var button_name=button_array[1];
  19. var file_id=button_array[2];
  20. var is_variable_language="$"+button_name;
  21. var is_new_language = $("#txtid_"+file_id+"_"+button_name).val();
  22. if (is_new_language == undefined) {
  23. is_new_language="_";
  24. }
  25. if (is_new_language.length>0 && is_new_language!="_" && file_id!="" && button_name!="") {
  26. $.ajax({
  27. contentType: "application/x-www-form-urlencoded",
  28. beforeSend: function(myObject) {
  29. $("#div_message_information_id").html("<div class=\"alert alert-info\"><img src=\'../inc/lib/javascript/indicator.gif\' /></div>");
  30. },
  31. type: "POST",
  32. url: "../admin/sub_language_ajax.inc.php",
  33. data: {
  34. \'new_language\': is_new_language,
  35. \'variable_language\': is_variable_language,
  36. \'file_id\': file_id,
  37. \'id\': '.intval($_REQUEST['id']).',
  38. \'sub\': '.intval($_REQUEST['sub_language_id']).',
  39. \'sub_language_id\': '.intval($_REQUEST['sub_language_id']).'
  40. },
  41. success: function(datos) {
  42. if (datos == "1") {
  43. $("#div_message_information_id").html(\''.Display::return_message(get_lang('The new word has been added'), 'success').'\');
  44. } else {
  45. $("#div_message_information_id").html("<div class=\"alert alert-warning\">" + datos +"</div>");
  46. }
  47. }
  48. });
  49. } else {
  50. $("#div_message_information_id").html(\''.Display::return_message(get_lang('The form contains incorrect or incomplete data. Please check your input.'), 'error').'\');
  51. }
  52. });
  53. });
  54. </script>';
  55. /**
  56. * Main code.
  57. */
  58. // setting the name of the tool
  59. $tool_name = get_lang('Create sub-language');
  60. // setting breadcrumbs
  61. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  62. $interbreadcrumb[] = ['url' => 'languages.php', 'name' => get_lang('Chamilo Portal Languages')];
  63. $sublanguage_folder_error = false;
  64. if (isset($_GET['id']) && $_GET['id'] == strval(intval($_GET['id']))) {
  65. $language_name = SubLanguageManager::get_name_of_language_by_id($_GET['id']);
  66. $sub_language_name = SubLanguageManager::get_name_of_language_by_id($_GET['sub_language_id']);
  67. $all_data_of_language = SubLanguageManager::get_all_information_of_language($_GET['id']);
  68. $all_data_of_sublanguage = SubLanguageManager::get_all_information_of_language($_GET['sub_language_id']);
  69. $sub_language_file = api_get_path(SYS_LANG_PATH).$all_data_of_sublanguage['dokeos_folder'];
  70. if (!file_exists($sub_language_file) || !is_writable($sub_language_file)) {
  71. $sublanguage_folder_error = $sub_language_file.' '.get_lang('is not writeable');
  72. }
  73. if (SubLanguageManager::check_if_exist_language_by_id($_GET['id']) === true) {
  74. $language_id_exist = true;
  75. } else {
  76. $language_id_exist = false;
  77. }
  78. } else {
  79. $language_name = '';
  80. $language_id_exist = false;
  81. }
  82. $intro = sprintf(get_lang('Define new terms for sub-language %s by searching some term, then save each translation by clicking the save button. You will then have to switch your own user language to see the new terms appear.'), strtolower($sub_language_name));
  83. $path_folder = api_get_path(SYS_LANG_PATH).$all_data_of_language['dokeos_folder'];
  84. if (!is_dir($path_folder) || strlen($all_data_of_language['dokeos_folder']) == 0) {
  85. api_not_allowed(true);
  86. }
  87. Display :: display_header($language_name);
  88. echo '<div class="actions-message" >';
  89. echo $intro;
  90. echo '<br />';
  91. printf(get_lang('Parent language: %s'), $language_name);
  92. echo '</div>';
  93. echo '<br />';
  94. $txt_search_word = (!empty($_REQUEST['txt_search_word']) ? Security::remove_XSS($_REQUEST['txt_search_word']) : '');
  95. $html = '<div style="float:left" class="actions">';
  96. $html .= '<form style="float:left" id="Searchlanguage" name="Searchlanguage" method="GET" action="sub_language.php">';
  97. $html .= '&nbsp;'.get_lang('Original name').'&nbsp; :&nbsp;';
  98. $html .= '<input name="id" type="hidden" id="id" value="'.Security::remove_XSS($_REQUEST['id']).'" />';
  99. $html .= '<input name="sub_language_id" type="hidden" id="id" value="'.Security::remove_XSS($_REQUEST['sub_language_id']).'" />';
  100. $html .= '<input name="txt_search_word" type="text" size="50" id="txt_search_word" value="'.$txt_search_word.'" />';
  101. $html .= "&nbsp;".'<button name="SubmitSearchLanguage" class="search" type="submit">'.get_lang('Search').'</button>';
  102. $html .= '</form>';
  103. $html .= '</div>';
  104. echo $html;
  105. echo '<br /><br /><br />';
  106. if (!empty($sublanguage_folder_error)) {
  107. echo Display::return_message($sublanguage_folder_error, 'warning');
  108. }
  109. echo '<div id="div_message_information_id">&nbsp;</div>';
  110. /**
  111. * @param $term The term to search
  112. * @param bool $search_in_variable The search will include the variable definition of the term
  113. * @param bool $search_in_english The search will include the english language variables
  114. * @param bool $search_in_parent The search will include the parent language variables of the sub language
  115. * @param bool $search_in_sub_language The search will include the sub language variables
  116. *
  117. * @author Julio Montoya
  118. *
  119. * @return array
  120. */
  121. function search_language_term(
  122. $term,
  123. $search_in_variable = true,
  124. $search_in_english = true,
  125. $search_in_parent = true,
  126. $search_in_sub_language = true
  127. ) {
  128. //These the $_REQUEST['id'] and the $_REQUEST['sub_language_id'] variables are process in global.inc.php (LOAD LANGUAGE FILES SECTION)
  129. /*
  130. These 4 arrays are set in global.inc.php with the condition that will be load from sub_language.php or sub_language_ajax.inc.php
  131. $english_language_array
  132. $parent_language_array
  133. $sub_language_array
  134. $language_files_to_load
  135. */
  136. global $language_files_to_load, $sub_language_array, $english_language_array, $parent_language_array;
  137. $language_files_to_load_keys = array_flip($language_files_to_load);
  138. $array_to_search = $parent_language_array;
  139. $list_info = [];
  140. $term = '/'.Security::remove_XSS(trim($_REQUEST['txt_search_word'])).'/i';
  141. //@todo optimize this foreach
  142. foreach ($language_files_to_load as $lang_file) {
  143. //searching in parent language of the sub language
  144. if ($search_in_parent) {
  145. $variables = $parent_language_array[$lang_file];
  146. foreach ($variables as $parent_name_variable => $parent_variable_value) {
  147. //arrays are avoided
  148. if (is_array($parent_variable_value)) {
  149. continue;
  150. }
  151. $founded = false;
  152. // searching the item in the parent tool
  153. if (preg_match($term, $parent_variable_value) !== 0) {
  154. $founded = true;
  155. }
  156. if ($founded) {
  157. //loading variable from the english array
  158. $sub_language_name_variable = isset($sub_language_array[$lang_file][$parent_name_variable])
  159. ? $sub_language_array[$lang_file][$parent_name_variable]
  160. : '';
  161. //loading variable from the english array
  162. $english_name_variable = $english_language_array[$lang_file][$parent_name_variable];
  163. //config buttons
  164. /*if (strlen($english_name_variable)>1500) {
  165. $size =20;
  166. } else {
  167. $size =4;
  168. }*/
  169. $obj_text = Display::tag(
  170. 'textarea',
  171. $sub_language_name_variable,
  172. [
  173. 'rows' => 10,
  174. 'cols' => 40,
  175. 'name' => 'txt|'.$parent_name_variable.'|'.$language_files_to_load_keys[$lang_file],
  176. 'id' => 'txtid_'.$language_files_to_load_keys[$lang_file].'_'.$parent_name_variable,
  177. ]
  178. );
  179. $obj_button = Display::button(
  180. 'btn|'.$parent_name_variable.'|'.$language_files_to_load_keys[$lang_file],
  181. get_lang('Save'),
  182. [
  183. 'class' => 'save btn btn-default btn-sm',
  184. 'type' => 'button',
  185. 'id' => 'btnid_'.$parent_name_variable,
  186. ]
  187. );
  188. $list_info[$parent_name_variable] = [
  189. $lang_file.'.inc.php',
  190. $parent_name_variable,
  191. htmlentities($english_name_variable),
  192. htmlentities($parent_variable_value),
  193. $obj_text,
  194. $obj_button,
  195. ];
  196. }
  197. }
  198. }
  199. //search in english
  200. if ($search_in_english || $search_in_variable) {
  201. $variables = $english_language_array[$lang_file];
  202. foreach ($variables as $name_variable => $variable_value) {
  203. if (isset($list_info[$name_variable])) {
  204. continue;
  205. }
  206. if (is_array($variable_value)) {
  207. continue;
  208. }
  209. if (is_array($variable_value)) {
  210. echo $lang_file;
  211. }
  212. $founded = false;
  213. if ($search_in_english && $search_in_variable) {
  214. // searching the item in the parent tool
  215. if (preg_match($term, $variable_value) !== 0 || preg_match($term, $name_variable) !== 0) {
  216. $founded = true;
  217. }
  218. } else {
  219. if ($search_in_english) {
  220. if (preg_match($term, $variable_value) !== 0) {
  221. $founded = true;
  222. }
  223. } else {
  224. if (preg_match($term, $name_variable) !== 0) {
  225. $founded = true;
  226. }
  227. }
  228. }
  229. if ($founded) {
  230. //loading variable from the english array
  231. $sub_language_name_variable = null;
  232. if (isset($sub_language_array[$lang_file][$name_variable])) {
  233. $sub_language_name_variable = $sub_language_array[$lang_file][$name_variable];
  234. }
  235. $parent_variable_value = null;
  236. if (isset($parent_language_array[$lang_file][$name_variable])) {
  237. $parent_variable_value = $parent_language_array[$lang_file][$name_variable];
  238. }
  239. //config buttons
  240. $obj_text = Display::tag(
  241. 'textarea',
  242. $sub_language_name_variable,
  243. [
  244. 'rows' => 10,
  245. 'cols' => 40,
  246. 'name' => 'txt|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file],
  247. 'id' => 'txtid_'.$language_files_to_load_keys[$lang_file].'_'.$name_variable,
  248. ]
  249. );
  250. $obj_button = Display::button(
  251. 'btn|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file],
  252. get_lang('Save'),
  253. [
  254. 'class' => 'save btn btn-default btn-sm',
  255. 'type' => 'button',
  256. 'id' => 'btnid_'.$name_variable,
  257. ]
  258. );
  259. //loading variable from the english array
  260. $english_name_variable = $english_language_array[$lang_file][$name_variable];
  261. $list_info[] = [
  262. $lang_file.'.inc.php',
  263. $name_variable,
  264. htmlentities($english_name_variable),
  265. htmlentities($parent_variable_value),
  266. $obj_text,
  267. $obj_button,
  268. ];
  269. }
  270. }
  271. }
  272. // Search in sub language
  273. if ($search_in_sub_language) {
  274. $variables = $sub_language_array[$lang_file];
  275. foreach ($variables as $name_variable => $variable_value) {
  276. if (is_array($parent_variable_value)) {
  277. continue;
  278. }
  279. if (is_array($variable_value)) {
  280. continue;
  281. }
  282. $founded = false;
  283. // searching the item in the parent tool
  284. if (preg_match($term, $variable_value) !== 0) {
  285. $founded = true;
  286. }
  287. if ($founded) {
  288. //loading variable from the english array
  289. $sub_language_name_variable = isset($sub_language_array[$lang_file][$name_variable])
  290. ? $sub_language_array[$lang_file][$name_variable]
  291. : '';
  292. $parent_variable_value = isset($parent_language_array[$lang_file][$name_variable])
  293. ? $parent_language_array[$lang_file][$name_variable]
  294. : '';
  295. //config buttons
  296. $obj_text = Display::tag(
  297. 'textarea',
  298. $sub_language_name_variable,
  299. [
  300. 'rows' => 10,
  301. 'cols' => 40,
  302. 'name' => 'txt|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file],
  303. 'id' => 'txtid_'.$language_files_to_load_keys[$lang_file].'_'.$name_variable,
  304. ]
  305. );
  306. $obj_button = Display::button(
  307. 'btn|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file],
  308. get_lang('Save'),
  309. [
  310. 'class' => 'save btn btn-default btn-sm',
  311. 'type' => 'button',
  312. 'id' => 'btnid_'.$name_variable,
  313. ]
  314. );
  315. //loading variable from the english array
  316. $english_name_variable = $english_language_array[$lang_file][$name_variable];
  317. $list_info[] = [$lang_file.'.inc.php',
  318. $name_variable,
  319. $english_name_variable,
  320. $parent_variable_value, $obj_text, $obj_button, ];
  321. }
  322. }
  323. }
  324. }
  325. $list_info = array_unique_dimensional($list_info);
  326. return $list_info;
  327. }
  328. // Allow see data in sort table
  329. $list_info = [];
  330. if (isset($_REQUEST['txt_search_word'])) {
  331. //@todo fix to accept a char with 1 char
  332. if (strlen(trim($_REQUEST['txt_search_word'])) > 2) {
  333. $list_info = search_language_term(
  334. $_REQUEST['txt_search_word'],
  335. true,
  336. true,
  337. true,
  338. true
  339. );
  340. }
  341. }
  342. $parameters = [
  343. 'id' => intval($_GET['id']),
  344. 'sub_language_id' => intval($_GET['sub_language_id']),
  345. 'txt_search_word' => $txt_search_word,
  346. ];
  347. $table = new SortableTableFromArrayConfig($list_info, 1, 20, 'data_info');
  348. $table->set_additional_parameters($parameters);
  349. $table->set_header(0, get_lang('Language file'));
  350. $table->set_header(1, get_lang('Language variable'));
  351. $table->set_header(2, get_lang('English name'));
  352. $table->set_header(3, get_lang('Original name'));
  353. $table->set_header(4, get_lang('Translation'), false);
  354. $table->set_header(5, get_lang('Action'), false);
  355. $table->setHideColumn(0);
  356. $table->display();
  357. Display :: display_footer();