sub_language_add.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. * This script allows for the addition of sub-languages
  5. * @package chamilo.admin
  6. */
  7. /**
  8. * Initialization section
  9. */
  10. // name of the language file that needs to be included
  11. $language_file = 'admin';
  12. $cidReset = true;
  13. require_once '../inc/global.inc.php';
  14. require_once 'sub_language.class.php';
  15. $this_section = SECTION_PLATFORM_ADMIN;
  16. api_protect_admin_script();
  17. /**
  18. * MAIN CODE
  19. */
  20. // setting the name of the tool
  21. $tool_name = get_lang('CreateSubLanguage');
  22. // setting breadcrumbs
  23. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  24. $interbreadcrumb[] = array ('url' => 'languages.php', 'name' => get_lang('PlatformLanguages'));
  25. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  26. /**
  27. * Add sub-language
  28. * @param string Original language name (Occitan, Wallon, Vlaams)
  29. * @param string English language name (occitan, wallon, flanders)
  30. * @param string ISO code (fr_FR, ...)
  31. * @param int Whether the sublanguage is published (0=unpublished, 1=published)
  32. * @param int ID del idioma padre
  33. * @return void
  34. */
  35. function add_sub_language ($original_name,$english_name,$isocode,$sublanguage_available,$parent_id) {
  36. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  37. $original_name = Database::escape_string($original_name);
  38. $english_name = Database::escape_string($english_name);
  39. $isocode = Database::escape_string($isocode);
  40. $sublanguage_available = Database::escape_string($sublanguage_available);
  41. $parent_id = Database::escape_string($parent_id);
  42. $sql='INSERT INTO '.$tbl_admin_languages.'(original_name,english_name,isocode,dokeos_folder,available,parent_id) VALUES ("'.$original_name.'","'.$english_name.'","'.$isocode.'","'.$english_name.'","'.$sublanguage_available.'","'.$parent_id.'")';
  43. Database::query($sql);
  44. }
  45. /**
  46. * Check if language exists
  47. * @param string Original language name (Occitan, Wallon, Vlaams)
  48. * @param string English language name (occitan, wallon, flanders)
  49. * @param string ISO code (fr_FR, ...)
  50. * @param int Whether the sublanguage is published (0=unpublished, 1=published)
  51. * @return array Array describing the number of items found that match the
  52. * current language insert attempt (original_name => true,
  53. * english_name => true, isocode => true,
  54. * execute_add => true/false). If execute_add is true, then we
  55. * can proceed.
  56. * @todo This function is not transaction-safe and should probably be included
  57. * inside the add_sub_language function.
  58. */
  59. function check_if_language_exist ($original_name,$english_name,$isocode,$sublanguage_available) {
  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. if ($count_isocode==1) {
  81. $has_error=true;
  82. $message_information['isocode']=true;
  83. }
  84. if ($has_error===true) {
  85. $message_information['execute_add']=false;
  86. }
  87. if ($has_error===false) {
  88. $message_information['execute_add']=true;
  89. }
  90. return $message_information;
  91. }
  92. /**
  93. * Get the name of a language by id. This is just a wrapper for the
  94. * SubLanguageManager::get_name_of_language_by_id() method and should not exist
  95. * @param int Language ID
  96. * @return string Language name
  97. * @todo deprecate this function and use the static method directly
  98. */
  99. function get_name_of_language_by_id ($language_id) {
  100. return SubLanguageManager::get_name_of_language_by_id($language_id);
  101. }
  102. /**
  103. * Check if language exist, given its ID. This is just a wrapper for the
  104. * SubLanguageManager::check_if_exist_language_by_id() method and should not exist
  105. * @param int Language ID
  106. * @return bool
  107. * @todo deprecate this function and use the static method directly
  108. */
  109. function check_if_exist_language_by_id ($language_id) {
  110. return SubLanguageManager::check_if_exist_language_by_id($language_id);
  111. }
  112. /**
  113. * Check if the given language is a parent of any sub-language
  114. * @param int Language ID of the presumed parent
  115. * @return bool True if this language has children, false otherwise
  116. */
  117. function ckeck_if_is_parent_of_sub_language ($parent_id) {
  118. $sql='SELECT count(*) AS count FROM language WHERE parent_id="'.Database::escape_string($parent_id).'"';
  119. $rs=Database::query($sql);
  120. if (Database::num_rows($rs)>0 && Database::result($rs,0,'count')==1) {
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. }
  126. /**
  127. * Get all information of sub-language
  128. * @param int Parent language ID
  129. * @param int Child language ID
  130. * @return array
  131. */
  132. function allow_get_all_information_of_sub_language ($parent_id,$sub_language_id) {
  133. return SubLanguageManager::get_all_information_of_sub_language($parent_id,$sub_language_id);
  134. }
  135. /**
  136. * Add directory for sub-language
  137. * @param string Path of new sub-language
  138. * @return
  139. */
  140. function add_directory_of_sub_language ($path_sub_language) {
  141. return SubLanguageManager::add_directory_of_sub_language($path_sub_language);
  142. }
  143. /**
  144. * Remove directory of sub-language
  145. * @param string Path of new sub_language
  146. * @return bool True on success, false otherwise
  147. */
  148. function remove_directory_of_sub_language ($path) {
  149. $content = SubLanguageManager::get_all_data_of_dokeos_folder($path);
  150. if (count($content)>0) {
  151. foreach ($content as $value_content) {
  152. $path_file = $path.'/'.$value_content;
  153. unlink($path_file);
  154. }
  155. $rs = @rmdir($path);
  156. if ($rs === true) {
  157. return true;
  158. } else {
  159. return false;
  160. }
  161. } else {
  162. $rs = @rmdir($path);
  163. if ($rs === true) {
  164. return true;
  165. } else {
  166. return false;
  167. }
  168. }
  169. }
  170. /*end declare functions*/
  171. //add data
  172. if (isset($_GET['sub_language_id']) && $_GET['sub_language_id']==strval(intval($_GET['sub_language_id']))) {
  173. $language_name=get_name_of_language_by_id($_GET['sub_language_id']);
  174. if (check_if_exist_language_by_id ($_GET['sub_language_id'])===true) {
  175. $sub_language_id=$_GET['sub_language_id'];
  176. $sub_language_id_exist=true;
  177. } else {
  178. $sub_language_id_exist=false;
  179. }
  180. }
  181. if (isset($_GET['id']) && $_GET['id']==strval(intval($_GET['id']))) {
  182. $language_name=get_name_of_language_by_id($_GET['id']);
  183. if (check_if_exist_language_by_id ($_GET['id'])===true) {
  184. $parent_id=$_GET['id'];
  185. $language_id_exist=true;
  186. } else {
  187. $language_id_exist=false;
  188. }
  189. } else {
  190. $language_name='';
  191. $language_id_exist=false;
  192. }
  193. //removed and register
  194. 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'])))) {
  195. if (check_if_exist_language_by_id($_GET['id'])===true && check_if_exist_language_by_id($_GET['sub_language_id'])===true) {
  196. $get_all_information=allow_get_all_information_of_sub_language ($_GET['id'],$_GET['sub_language_id']);
  197. $original_name=$get_all_information['original_name'];
  198. $english_name=$get_all_information['english_name'];
  199. $isocode=$get_all_information['isocode'];
  200. }
  201. }
  202. $language_name=get_lang('CreateSubLanguageForLanguage').' ( '.strtolower($language_name).' )';
  203. if (ckeck_if_is_parent_of_sub_language ($parent_id)===true && isset($_GET['action']) && $_GET['action']=='deletesublanguage') {
  204. $language_name=get_lang('DeleteSubLanguage');
  205. }
  206. Display :: display_header($language_name);
  207. if (isset($_POST['SubmitAddNewLanguage'])) {
  208. $original_name=$_POST['original_name'];
  209. $english_name=$_POST['english_name'];
  210. $isocode=$_POST['isocode'];
  211. $english_name=str_replace(' ','_',$english_name);
  212. $isocode=str_replace(' ','_',$isocode);
  213. $sublanguage_available=$_POST['sub_language_is_visible'];
  214. $check_information=array();
  215. $check_information=check_if_language_exist($original_name,$english_name,$isocode,$sublanguage_available);
  216. foreach ($check_information as $index_information => $value_information) {
  217. $allow_insert_info=false;
  218. if ($index_information=='original_name') {
  219. Display::display_error_message(get_lang('AlreadyExists').' "'.get_lang('OriginalName').'" '.'('.$original_name.')');
  220. }
  221. if ($index_information=='english_name') {
  222. Display::display_error_message(get_lang('AlreadyExists').' "'.get_lang('EnglishName').'" '.'('.$english_name.')');
  223. }
  224. if ($index_information=='isocode') {
  225. Display::display_error_message(get_lang('AlreadyExists').' "'.get_lang('PlatformCharsetTitle').'" '.'('.$isocode.')');
  226. }
  227. if ($index_information=='execute_add' && $value_information===true) {
  228. $allow_insert_info=true;
  229. }
  230. }
  231. if (strlen($original_name)>0 && strlen($english_name)>0 && strlen($isocode)>0) {
  232. if ($allow_insert_info===true && $language_id_exist===true) {
  233. $english_name=str_replace(' ','_',$english_name);
  234. //Fixes BT#1636
  235. $english_name=api_strtolower($english_name);
  236. $isocode=str_replace(' ','_',$isocode);
  237. $str_info='<br/>'.get_lang('OriginalName').' : '.$original_name.'<br/>'.get_lang('EnglishName').' : '.$english_name.'<br/>'.get_lang('PlatformCharsetTitle').' : '.$isocode;
  238. $path=api_get_path(SYS_LANG_PATH).$english_name;
  239. $mkdir_result=add_directory_of_sub_language($path);
  240. if ($mkdir_result) {
  241. add_sub_language($original_name,$english_name,$isocode,$sublanguage_available,$parent_id);
  242. $link = '<br /><br /><a href="languages.php">'.get_lang('ReturnToLanguagesList').'</a>';
  243. Display::display_confirmation_message(get_lang('TheNewSubLanguageHasBeenAdded').$str_info.$link,false);
  244. $succeeded = true;
  245. } else {
  246. Display::display_error_message(get_lang('LanguageDirectoryNotWriteableContactAdmin'));
  247. }
  248. } else {
  249. if ($language_id_exist===false) {
  250. Display::display_error_message(get_lang('LanguageParentNotExist'));
  251. }
  252. }
  253. } else {
  254. Display::display_error_message(get_lang('FormHasErrorsPleaseComplete'));
  255. }
  256. }
  257. if (isset($_POST['SubmitAddDeleteLanguage'])) {
  258. $path=api_get_path(SYS_LANG_PATH).$english_name;
  259. if (is_dir($path)) {
  260. $rs=remove_directory_of_sub_language($path);
  261. if ($rs===true) {
  262. SubLanguageManager::removed_sub_language($parent_id,$sub_language_id);
  263. Display::display_confirmation_message(get_lang('TheSubLanguageHasBeenRemoved'));
  264. }
  265. }
  266. }
  267. // ckeck_if_is_parent_of_sub_language($parent_id)===false
  268. //
  269. if (isset($_GET['action']) && $_GET['action']=='definenewsublanguage') {
  270. $text = $language_name;
  271. $form = new FormValidator('addsublanguage', 'post', 'sub_language_add.php?id='.Security::remove_XSS($_GET['id']).'&action=definenewsublanguage');
  272. $class='add';
  273. $form->addElement('header', '', $text);
  274. $form->addElement('text', 'original_name', get_lang('OriginalName'),'class="input_titles"');
  275. $form->addRule('original_name', get_lang('ThisFieldIsRequired'), 'required');
  276. $form->addElement('text', 'english_name', get_lang('EnglishName'),'class="input_titles"');
  277. $form->addRule('english_name', get_lang('ThisFieldIsRequired'), 'required');
  278. $form->addElement('text', 'isocode', get_lang('ISOCode'),'class="input_titles"');
  279. $form->addRule('isocode', get_lang('ThisFieldIsRequired'), 'required');
  280. $form->addElement('checkbox', 'sub_language_is_visible', '', get_lang('Visibility'));
  281. $form->addElement('style_submit_button', 'SubmitAddNewLanguage', get_lang('CreateSubLanguage'), 'class="'.$class.'"');
  282. $form->display();
  283. } else {
  284. if (isset($_GET['action']) && $_GET['action']=='deletesublanguage') {
  285. $text=$language_name;
  286. $form = new FormValidator('deletesublanguage', 'post', 'sub_language_add.php?id='.Security::remove_XSS($_GET['id']).'&sub_language_id='.Security::remove_XSS($_GET['sub_language_id']));
  287. $class='minus';
  288. $form->addElement('header', '', $text);
  289. $form->addElement('static', '', get_lang('OriginalName'),$original_name);
  290. $form->addElement('static', '', get_lang('EnglishName'),$english_name);
  291. $form->addElement('static', '', get_lang('PlatformCharsetTitle'),$isocode);
  292. $form->addElement('style_submit_button', 'SubmitAddDeleteLanguage', get_lang('DeleteSubLanguage'), 'class="'.$class.'"');
  293. $form->display();
  294. }
  295. if (isset($_GET['action']) && $_GET['action']=='definenewsublanguage') {
  296. Display::display_normal_message(get_lang('TheSubLanguageForThisLanguageHasBeenAdded'));
  297. }
  298. }
  299. /**
  300. * Footer
  301. */
  302. Display :: display_footer();