sub_language_add.php 13 KB

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