sub_language_add.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script allows for the addition of sub-languages.
  5. *
  6. * @package chamilo.admin
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $this_section = SECTION_PLATFORM_ADMIN;
  11. api_protect_admin_script();
  12. /**
  13. * MAIN CODE.
  14. */
  15. // setting the name of the tool
  16. $tool_name = get_lang('Create sub-language');
  17. // setting breadcrumbs
  18. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  19. $interbreadcrumb[] = ['url' => 'languages.php', 'name' => get_lang('Chamilo Portal Languages')];
  20. /**
  21. * Add sub-language.
  22. *
  23. * @param string Original language name (Occitan, Wallon, Vlaams)
  24. * @param string English language name (occitan, wallon, flanders)
  25. * @param string ISO code (fr_FR, ...)
  26. * @param int Whether the sublanguage is published (0=unpublished, 1=published)
  27. * @param int ID del idioma padre
  28. *
  29. * @return false|string New sub language ID or false on error
  30. */
  31. function add_sub_language($original_name, $english_name, $isocode, $sublanguage_available, $parent_id)
  32. {
  33. $tbl_admin_languages = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  34. $original_name = Database::escape_string($original_name);
  35. $english_name = Database::escape_string($english_name);
  36. $isocode = Database::escape_string($isocode);
  37. $sublanguage_available = Database::escape_string($sublanguage_available);
  38. $parent_id = intval($parent_id);
  39. $sql = 'INSERT INTO '.$tbl_admin_languages.'(original_name,english_name,isocode,dokeos_folder,available,parent_id)
  40. VALUES ("'.$original_name.'","'.$english_name.'","'.$isocode.'","'.$english_name.'","'.$sublanguage_available.'","'.$parent_id.'")';
  41. $res = Database::query($sql);
  42. if ($res === false) {
  43. return false;
  44. }
  45. return Database::insert_id();
  46. }
  47. /**
  48. * Check if language exists.
  49. *
  50. * @param string Original language name (Occitan, Wallon, Vlaams)
  51. * @param string English language name (occitan, wallon, flanders)
  52. * @param string ISO code (fr_FR, ...)
  53. * @param int Whether the sublanguage is published (0=unpublished, 1=published)
  54. *
  55. * @return array Array describing the number of items found that match the
  56. * current language insert attempt (original_name => true,
  57. * english_name => true, isocode => true,
  58. * execute_add => true/false). If execute_add is true, then we
  59. * can proceed.
  60. *
  61. * @todo This function is not transaction-safe and should probably be included
  62. * inside the add_sub_language function.
  63. */
  64. function check_if_language_exist($original_name, $english_name, $isocode, $sublanguage_available)
  65. {
  66. $tbl_admin_languages = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  67. $sql_original_name = 'SELECT count(*) AS count_original_name FROM '.$tbl_admin_languages.'
  68. WHERE original_name="'.Database::escape_string($original_name).'" ';
  69. $sql_english_name = 'SELECT count(*) AS count_english_name FROM '.$tbl_admin_languages.'
  70. WHERE english_name="'.Database::escape_string($english_name).'" ';
  71. $rs_original_name = Database::query($sql_original_name);
  72. $rs_english_name = Database::query($sql_english_name);
  73. $count_original_name = Database::result($rs_original_name, 0, 'count_original_name');
  74. $count_english_name = Database::result($rs_english_name, 0, 'count_english_name');
  75. $has_error = false;
  76. $message_information = [];
  77. if ($count_original_name == 1) {
  78. $has_error = true;
  79. $message_information['original_name'] = true;
  80. }
  81. if ($count_english_name == 1) {
  82. $has_error = true;
  83. $message_information['english_name'] = true;
  84. }
  85. $iso_list = api_get_platform_isocodes();
  86. $iso_list = array_values($iso_list);
  87. if (!in_array($isocode, $iso_list)) {
  88. $has_error = true;
  89. $message_information['isocode'] = true;
  90. }
  91. if ($has_error === true) {
  92. $message_information['execute_add'] = false;
  93. }
  94. if ($has_error === false) {
  95. $message_information['execute_add'] = true;
  96. }
  97. return $message_information;
  98. }
  99. /**
  100. * Check if language exist, given its ID. This is just a wrapper for the
  101. * SubLanguageManager::check_if_exist_language_by_id() method and should not exist.
  102. *
  103. * @param int Language ID
  104. *
  105. * @return bool
  106. *
  107. * @todo deprecate this function and use the static method directly
  108. */
  109. function check_if_exist_language_by_id($language_id)
  110. {
  111. return SubLanguageManager::check_if_exist_language_by_id($language_id);
  112. }
  113. /**
  114. * Check if the given language is a parent of any sub-language.
  115. *
  116. * @param int Language ID of the presumed parent
  117. *
  118. * @return bool True if this language has children, false otherwise
  119. */
  120. function ckeck_if_is_parent_of_sub_language($parent_id)
  121. {
  122. $sql = 'SELECT count(*) AS count FROM language WHERE parent_id= '.intval($parent_id);
  123. $rs = Database::query($sql);
  124. if (Database::num_rows($rs) > 0 && Database::result($rs, 0, 'count') == 1) {
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. /**
  131. * Get all information of sub-language.
  132. *
  133. * @param int Parent language ID
  134. * @param int Child language ID
  135. *
  136. * @return array
  137. */
  138. function allow_get_all_information_of_sub_language($parent_id, $sub_language_id)
  139. {
  140. return SubLanguageManager::get_all_information_of_sub_language($parent_id, $sub_language_id);
  141. }
  142. /*end declare functions*/
  143. //add data
  144. if (isset($_GET['sub_language_id']) && $_GET['sub_language_id'] == strval(intval($_GET['sub_language_id']))) {
  145. $language_name = SubLanguageManager::get_name_of_language_by_id($_GET['sub_language_id']);
  146. if (check_if_exist_language_by_id($_GET['sub_language_id']) === true) {
  147. $sub_language_id = $_GET['sub_language_id'];
  148. $sub_language_id_exist = true;
  149. } else {
  150. $sub_language_id_exist = false;
  151. }
  152. }
  153. $language_details = [];
  154. $language_name = '';
  155. if (isset($_GET['id']) && $_GET['id'] == strval(intval($_GET['id']))) {
  156. $language_details = SubLanguageManager::get_all_information_of_language($_GET['id']);
  157. $language_name = $language_details['original_name'];
  158. if (check_if_exist_language_by_id($_GET['id']) === true) {
  159. $parent_id = $_GET['id'];
  160. $language_id_exist = true;
  161. } else {
  162. $language_id_exist = false;
  163. }
  164. } else {
  165. $language_id_exist = false;
  166. }
  167. //removed and register
  168. if ((isset($_GET['id']) && $_GET['id'] == strval(intval($_GET['id']))) &&
  169. (isset($_GET['sub_language_id']) && $_GET['sub_language_id'] == strval(intval($_GET['sub_language_id'])))
  170. ) {
  171. if (check_if_exist_language_by_id($_GET['id']) === true && check_if_exist_language_by_id($_GET['sub_language_id']) === true) {
  172. $get_all_information = allow_get_all_information_of_sub_language($_GET['id'], $_GET['sub_language_id']);
  173. $original_name = $get_all_information['original_name'];
  174. $english_name = $get_all_information['english_name'];
  175. $isocode = $get_all_information['isocode'];
  176. }
  177. }
  178. $language_name = get_lang('Create sub-languageForLanguage').' ( '.strtolower($language_name).' )';
  179. if (ckeck_if_is_parent_of_sub_language($parent_id) === true &&
  180. isset($_GET['action']) && $_GET['action'] == 'deletesublanguage'
  181. ) {
  182. $language_name = get_lang('Delete sub-language');
  183. }
  184. $msg = '';
  185. if (isset($_POST['SubmitAddNewLanguage'])) {
  186. $original_name = $_POST['original_name'];
  187. $english_name = $_POST['english_name'];
  188. $isocode = $_POST['isocode'];
  189. $english_name = str_replace(' ', '_', $english_name);
  190. $isocode = str_replace(' ', '_', $isocode);
  191. $sublanguage_available = $_POST['sub_language_is_visible'];
  192. $check_information = [];
  193. $check_information = check_if_language_exist($original_name, $english_name, $isocode, $sublanguage_available);
  194. foreach ($check_information as $index_information => $value_information) {
  195. $allow_insert_info = false;
  196. if ($index_information == 'original_name') {
  197. $msg .= Display::return_message(
  198. get_lang('Already exists').' "'.get_lang('Original name').'" '.'('.$original_name.')',
  199. 'error'
  200. );
  201. }
  202. if ($index_information == 'english_name') {
  203. $msg .= Display::return_message(
  204. get_lang('Already exists').' "'.get_lang('English name').'" '.'('.$english_name.')',
  205. 'error'
  206. );
  207. }
  208. if ($index_information == 'isocode') {
  209. $msg .= Display::return_message(get_lang('This code does not exist').': '.$isocode.'', 'error');
  210. }
  211. if ($index_information == 'execute_add' && $value_information === true) {
  212. $allow_insert_info = true;
  213. }
  214. }
  215. if (strlen($original_name) > 0 && strlen($english_name) > 0 && strlen($isocode) > 0) {
  216. if ($allow_insert_info === true && $language_id_exist === true) {
  217. $english_name = str_replace(' ', '_', $english_name);
  218. //Fixes BT#1636
  219. $english_name = api_strtolower($english_name);
  220. $isocode = str_replace(' ', '_', $isocode);
  221. $str_info = '<br/>'.get_lang('Original name').' : '.$original_name.'<br/>'.get_lang('English name').' : '.$english_name.'<br/>'.get_lang('Character set').' : '.$isocode;
  222. $mkdir_result = SubLanguageManager::add_language_directory($english_name);
  223. if ($mkdir_result) {
  224. $sl_id = add_sub_language($original_name, $english_name, $isocode, $sublanguage_available, $parent_id);
  225. if ($sl_id === false) {
  226. SubLanguageManager::remove_language_directory($english_name);
  227. $msg .= Display::return_message(get_lang('The /main/lang directory, used on this portal to store the languages, is not writable. Please contact your platform administrator and report this message.'), 'error');
  228. } else {
  229. Display::addFlash(
  230. Display::return_message(get_lang('The new sub-language has been added').$str_info, null, false)
  231. );
  232. unset($interbreadcrumb);
  233. $_GET['sub_language_id'] = $_REQUEST['sub_language_id'] = $sl_id;
  234. require 'sub_language.php';
  235. exit();
  236. }
  237. } else {
  238. $msg .= Display::return_message(get_lang('The /main/lang directory, used on this portal to store the languages, is not writable. Please contact your platform administrator and report this message.'), 'error');
  239. }
  240. } else {
  241. if ($language_id_exist === false) {
  242. $msg .= Display::return_message(get_lang('The parent language does not exist.'), 'error');
  243. }
  244. }
  245. } else {
  246. $msg .= Display::return_message(get_lang('The form contains incorrect or incomplete data. Please check your input.'), 'error');
  247. }
  248. }
  249. Display:: display_header($language_name);
  250. echo $msg;
  251. if (isset($_POST['SubmitAddDeleteLanguage'])) {
  252. $rs = SubLanguageManager::remove_sub_language($_GET['id'], $_GET['sub_language_id']);
  253. if ($rs === true) {
  254. echo Display::return_message(get_lang('The sub language has been removed'), 'confirm');
  255. } else {
  256. echo Display::return_message(get_lang('The sub-language has not been removed.'), 'error');
  257. }
  258. }
  259. // ckeck_if_is_parent_of_sub_language($parent_id)===false
  260. if (isset($_GET['action']) && $_GET['action'] == 'definenewsublanguage') {
  261. $text = $language_name;
  262. $form = new FormValidator(
  263. 'addsublanguage',
  264. 'post',
  265. 'sub_language_add.php?id='.Security::remove_XSS($_GET['id']).'&action=definenewsublanguage'
  266. );
  267. $class = 'add';
  268. $form->addElement('header', '', $text);
  269. $form->addElement('text', 'original_name', get_lang('Original name'), 'class="input_titles"');
  270. $form->addRule('original_name', get_lang('Required field'), 'required');
  271. $form->addElement('text', 'english_name', get_lang('English name'), 'class="input_titles"');
  272. $form->addRule('english_name', get_lang('Required field'), 'required');
  273. $form->addElement('text', 'isocode', get_lang('ISO code'), 'class="input_titles"');
  274. $form->addRule('isocode', get_lang('Required field'), 'required');
  275. $form->addElement('static', null, '&nbsp;', '<i>en, es, fr</i>');
  276. $form->addElement('checkbox', 'sub_language_is_visible', '', get_lang('Visibility'));
  277. $form->addButtonCreate(get_lang('Create sub-language'), 'SubmitAddNewLanguage');
  278. //$values['original_name'] = $language_details['original_name'].'...'; -> cannot be used because of quickform filtering (freeze)
  279. $values['english_name'] = $language_details['english_name'].'2';
  280. $values['isocode'] = $language_details['isocode'];
  281. $form->setDefaults($values);
  282. $form->display();
  283. } else {
  284. if (isset($_GET['action']) && $_GET['action'] == 'deletesublanguage') {
  285. $text = $language_name;
  286. $form = new FormValidator(
  287. 'deletesublanguage',
  288. 'post',
  289. 'sub_language_add.php?id='.Security::remove_XSS($_GET['id']).'&sub_language_id='.Security::remove_XSS($_GET['sub_language_id'])
  290. );
  291. $class = 'minus';
  292. $form->addElement('header', '', $text);
  293. $form->addElement('static', '', get_lang('Original name'), $original_name);
  294. $form->addElement('static', '', get_lang('English name'), $english_name);
  295. $form->addElement('static', '', get_lang('Character set'), $isocode);
  296. $form->addButtonCreate(get_lang('Delete sub-language'), 'SubmitAddDeleteLanguage');
  297. $form->display();
  298. }
  299. if (isset($_GET['action']) && $_GET['action'] == 'definenewsublanguage') {
  300. echo Display::return_message(get_lang('The sub-language of this language has been added'));
  301. }
  302. }
  303. /**
  304. * Footer.
  305. */
  306. Display:: display_footer();