sub_language_add.php 13 KB

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