sub_language.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. $(document).ready(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(objeto) {
  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('TheNewWordHasBeenAdded'), '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('FormHasErrorsPleaseComplete'), 'error').'\');
  51. }
  52. });
  53. });
  54. </script>';
  55. /**
  56. * Main code.
  57. */
  58. // setting the name of the tool
  59. $tool_name = get_lang('CreateSubLanguage');
  60. // setting breadcrumbs
  61. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  62. $interbreadcrumb[] = ['url' => 'languages.php', 'name' => get_lang('PlatformLanguages')];
  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('IsNotWritable');
  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('RegisterTermsOfSubLanguageForX'), 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('ParentLanguageX'), $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('OriginalName').'&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 = $sub_language_array[$lang_file][$parent_name_variable];
  159. //loading variable from the english array
  160. $english_name_variable = $english_language_array[$lang_file][$parent_name_variable];
  161. //config buttons
  162. /*if (strlen($english_name_variable)>1500) {
  163. $size =20;
  164. } else {
  165. $size =4;
  166. }*/
  167. $obj_text = '<textarea rows="10" cols="40" name="txt|'.$parent_name_variable.'|'.$language_files_to_load_keys[$lang_file].'" id="txtid_'.$language_files_to_load_keys[$lang_file].'_'.$parent_name_variable.'" >'.$sub_language_name_variable.'</textarea>';
  168. $obj_button = '<button class="save" type="button" name="btn|'.$parent_name_variable.'|'.$language_files_to_load_keys[$lang_file].'" id="btnid_'.$parent_name_variable.'" />'.get_lang('Save').'</button>';
  169. $list_info[] = [
  170. $lang_file.'.inc.php',
  171. $parent_name_variable,
  172. $english_name_variable,
  173. $parent_variable_value,
  174. $obj_text,
  175. $obj_button,
  176. ];
  177. }
  178. }
  179. }
  180. //search in english
  181. if ($search_in_english || $search_in_variable) {
  182. $variables = $english_language_array[$lang_file];
  183. foreach ($variables as $name_variable => $variable_value) {
  184. if (is_array($variable_value)) {
  185. continue;
  186. }
  187. if (is_array($variable_value)) {
  188. echo $lang_file;
  189. }
  190. $founded = false;
  191. if ($search_in_english && $search_in_variable) {
  192. // searching the item in the parent tool
  193. if (preg_match($term, $variable_value) !== 0 || preg_match($term, $name_variable) !== 0) {
  194. $founded = true;
  195. }
  196. } else {
  197. if ($search_in_english) {
  198. if (preg_match($term, $variable_value) !== 0) {
  199. $founded = true;
  200. }
  201. } else {
  202. if (preg_match($term, $name_variable) !== 0) {
  203. $founded = true;
  204. }
  205. }
  206. }
  207. if ($founded) {
  208. //loading variable from the english array
  209. $sub_language_name_variable = null;
  210. if (isset($sub_language_array[$lang_file][$name_variable])) {
  211. $sub_language_name_variable = $sub_language_array[$lang_file][$name_variable];
  212. }
  213. $parent_variable_value = null;
  214. if (isset($parent_language_array[$lang_file][$name_variable])) {
  215. $parent_variable_value = $parent_language_array[$lang_file][$name_variable];
  216. }
  217. //config buttons
  218. $obj_text = '<textarea rows="10" cols="40" name="txt|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file].'" id="txtid_'.$language_files_to_load_keys[$lang_file].'_'.$name_variable.'" >'.
  219. $sub_language_name_variable.'
  220. </textarea>';
  221. $obj_button = '<button class="save" type="button" name="btn|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file].'" id="btnid_'.$name_variable.'" />'.get_lang('Save').'</button>';
  222. //loading variable from the english array
  223. $english_name_variable = $english_language_array[$lang_file][$name_variable];
  224. $list_info[] = [
  225. $lang_file.'.inc.php',
  226. $name_variable,
  227. $english_name_variable,
  228. $parent_variable_value,
  229. $obj_text,
  230. $obj_button,
  231. ];
  232. }
  233. }
  234. }
  235. // Search in sub language
  236. if ($search_in_sub_language) {
  237. $variables = $sub_language_array[$lang_file];
  238. foreach ($variables as $name_variable => $variable_value) {
  239. if (is_array($parent_variable_value)) {
  240. continue;
  241. }
  242. if (is_array($variable_value)) {
  243. continue;
  244. }
  245. $founded = false;
  246. // searching the item in the parent tool
  247. if (preg_match($term, $variable_value) !== 0) {
  248. $founded = true;
  249. }
  250. if ($founded) {
  251. //loading variable from the english array
  252. $sub_language_name_variable = $sub_language_array[$lang_file][$name_variable];
  253. $parent_variable_value = $parent_language_array[$lang_file][$name_variable];
  254. //config buttons
  255. $obj_text = '<textarea rows="10" cols="40" name="txt|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file].'" id="txtid_'.$language_files_to_load_keys[$lang_file].'_'.$name_variable.'" >'.$sub_language_name_variable.'</textarea>';
  256. $obj_button = '<button class="save" type="button" name="btn|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file].'" id="btnid_'.$name_variable.'" />'.get_lang('Save').'</button>';
  257. //loading variable from the english array
  258. $english_name_variable = $english_language_array[$lang_file][$name_variable];
  259. $list_info[] = [$lang_file.'.inc.php',
  260. $name_variable,
  261. $english_name_variable,
  262. $parent_variable_value, $obj_text, $obj_button, ];
  263. }
  264. }
  265. }
  266. }
  267. $list_info = array_unique_dimensional($list_info);
  268. return $list_info;
  269. }
  270. // Allow see data in sort table
  271. $list_info = [];
  272. if (isset($_REQUEST['txt_search_word'])) {
  273. //@todo fix to accept a char with 1 char
  274. if (strlen(trim($_REQUEST['txt_search_word'])) > 2) {
  275. $list_info = search_language_term(
  276. $_REQUEST['txt_search_word'],
  277. true,
  278. true,
  279. true,
  280. true
  281. );
  282. }
  283. }
  284. $parameters = [
  285. 'id' => intval($_GET['id']),
  286. 'sub_language_id' => intval($_GET['sub_language_id']),
  287. 'txt_search_word' => $txt_search_word,
  288. ];
  289. $table = new SortableTableFromArrayConfig($list_info, 1, 20, 'data_info');
  290. $table->set_additional_parameters($parameters);
  291. $table->set_header(0, get_lang('LanguageFile'));
  292. $table->set_header(1, get_lang('LanguageVariable'));
  293. $table->set_header(2, get_lang('EnglishName'));
  294. $table->set_header(3, get_lang('OriginalName'));
  295. $table->set_header(4, get_lang('Translation'), false);
  296. $table->set_header(5, get_lang('Action'), false);
  297. $table->display();
  298. Display :: display_footer();