sub_language_add.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. // name of the language file that needs to be included
  8. $language_file = 'admin';
  9. $cidReset = true;
  10. require_once '../inc/global.inc.php';
  11. require_once 'sub_language.class.php';
  12. $this_section = SECTION_PLATFORM_ADMIN;
  13. api_protect_admin_script();
  14. /**
  15. * MAIN CODE
  16. */
  17. // setting the name of the tool
  18. $tool_name = get_lang('CreateSubLanguage');
  19. // setting breadcrumbs
  20. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  21. $interbreadcrumb[] = array ('url' => 'languages.php', 'name' => get_lang('PlatformLanguages'));
  22. /**
  23. * Add sub-language
  24. * @param string Original language name (Occitan, Wallon, Vlaams)
  25. * @param string English language name (occitan, wallon, flanders)
  26. * @param string ISO code (fr_FR, ...)
  27. * @param int Whether the sublanguage is published (0=unpublished, 1=published)
  28. * @param int ID del idioma padre
  29. * @return int New sub language ID or false on error
  30. */
  31. function add_sub_language ($original_name,$english_name,$isocode,$sublanguage_available,$parent_id) {
  32. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  33. $original_name = Database::escape_string($original_name);
  34. $english_name = Database::escape_string($english_name);
  35. $isocode = Database::escape_string($isocode);
  36. $sublanguage_available = Database::escape_string($sublanguage_available);
  37. $parent_id = intval($parent_id);
  38. $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.'")';
  39. $res = Database::query($sql);
  40. if ($res === false) {
  41. return false;
  42. }
  43. return Database::insert_id();
  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. $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. return SubLanguageManager::check_if_exist_language_by_id($language_id);
  103. }
  104. /**
  105. * Check if the given language is a parent of any sub-language
  106. * @param int Language ID of the presumed parent
  107. * @return bool True if this language has children, false otherwise
  108. */
  109. function ckeck_if_is_parent_of_sub_language ($parent_id) {
  110. $sql='SELECT count(*) AS count FROM language WHERE parent_id= '.intval($parent_id).'';
  111. $rs=Database::query($sql);
  112. if (Database::num_rows($rs)>0 && Database::result($rs,0,'count')==1) {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. /**
  119. * Get all information of sub-language
  120. * @param int Parent language ID
  121. * @param int Child language ID
  122. * @return array
  123. */
  124. function allow_get_all_information_of_sub_language ($parent_id,$sub_language_id) {
  125. return SubLanguageManager::get_all_information_of_sub_language($parent_id,$sub_language_id);
  126. }
  127. /*end declare functions*/
  128. //add data
  129. if (isset($_GET['sub_language_id']) && $_GET['sub_language_id']==strval(intval($_GET['sub_language_id']))) {
  130. $language_name=SubLanguageManager::get_name_of_language_by_id($_GET['sub_language_id']);
  131. if (check_if_exist_language_by_id ($_GET['sub_language_id'])===true) {
  132. $sub_language_id=$_GET['sub_language_id'];
  133. $sub_language_id_exist=true;
  134. } else {
  135. $sub_language_id_exist=false;
  136. }
  137. }
  138. $language_details = array();
  139. $language_name='';
  140. if (isset($_GET['id']) && $_GET['id']==strval(intval($_GET['id']))) {
  141. $language_details = SubLanguageManager::get_all_information_of_language($_GET['id']);
  142. $language_name = $language_details['original_name'];
  143. if (check_if_exist_language_by_id ($_GET['id'])===true) {
  144. $parent_id=$_GET['id'];
  145. $language_id_exist=true;
  146. } else {
  147. $language_id_exist=false;
  148. }
  149. } else {
  150. $language_id_exist=false;
  151. }
  152. //removed and register
  153. 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'])))) {
  154. if (check_if_exist_language_by_id($_GET['id'])===true && check_if_exist_language_by_id($_GET['sub_language_id'])===true) {
  155. $get_all_information=allow_get_all_information_of_sub_language ($_GET['id'],$_GET['sub_language_id']);
  156. $original_name=$get_all_information['original_name'];
  157. $english_name=$get_all_information['english_name'];
  158. $isocode=$get_all_information['isocode'];
  159. }
  160. }
  161. $language_name=get_lang('CreateSubLanguageForLanguage').' ( '.strtolower($language_name).' )';
  162. if (ckeck_if_is_parent_of_sub_language($parent_id)===true && isset($_GET['action']) && $_GET['action']=='deletesublanguage') {
  163. $language_name=get_lang('DeleteSubLanguage');
  164. }
  165. $msg = '';
  166. if (isset($_POST['SubmitAddNewLanguage'])) {
  167. $original_name=$_POST['original_name'];
  168. $english_name=$_POST['english_name'];
  169. $isocode=$_POST['isocode'];
  170. $english_name=str_replace(' ','_',$english_name);
  171. $isocode=str_replace(' ','_',$isocode);
  172. $sublanguage_available=$_POST['sub_language_is_visible'];
  173. $check_information=array();
  174. $check_information=check_if_language_exist($original_name,$english_name,$isocode,$sublanguage_available);
  175. foreach ($check_information as $index_information => $value_information) {
  176. $allow_insert_info=false;
  177. if ($index_information=='original_name') {
  178. $msg .= Display::return_message(get_lang('AlreadyExists').' "'.get_lang('OriginalName').'" '.'('.$original_name.')','error');
  179. }
  180. if ($index_information=='english_name') {
  181. $msg .= Display::return_message(get_lang('AlreadyExists').' "'.get_lang('EnglishName').'" '.'('.$english_name.')','error');
  182. }
  183. if ($index_information=='isocode') {
  184. $msg .= Display::return_message(get_lang('CodeDoesNotExists').': '.$isocode.'','error');
  185. }
  186. if ($index_information=='execute_add' && $value_information===true) {
  187. $allow_insert_info=true;
  188. }
  189. }
  190. if (strlen($original_name)>0 && strlen($english_name)>0 && strlen($isocode)>0) {
  191. if ($allow_insert_info===true && $language_id_exist===true) {
  192. $english_name=str_replace(' ','_',$english_name);
  193. //Fixes BT#1636
  194. $english_name=api_strtolower($english_name);
  195. $isocode=str_replace(' ','_',$isocode);
  196. $str_info='<br/>'.get_lang('OriginalName').' : '.$original_name.'<br/>'.get_lang('EnglishName').' : '.$english_name.'<br/>'.get_lang('PlatformCharsetTitle').' : '.$isocode;
  197. $mkdir_result=SubLanguageManager::add_language_directory($english_name);
  198. if ($mkdir_result) {
  199. $sl_id = add_sub_language($original_name,$english_name,$isocode,$sublanguage_available,$parent_id);
  200. if ($sl_id === false) {
  201. SubLanguageManager::remove_language_directory($english_name);
  202. $msg .= Display::return_message(get_lang('LanguageDirectoryNotWriteableContactAdmin'),'error');
  203. } else {
  204. // Here we build the confirmation message and we send the user to the sub language terms definition page, using a little hack - see #3712
  205. $_SESSION['msg'] = Display::return_message(get_lang('TheNewSubLanguageHasBeenAdded').$str_info.$link,'confirm',false);
  206. unset($interbreadcrumb);
  207. $_GET['sub_language_id'] = $_REQUEST['sub_language_id'] = $sl_id;
  208. require 'sub_language.php';
  209. exit();
  210. }
  211. } else {
  212. $msg .= Display::return_message(get_lang('LanguageDirectoryNotWriteableContactAdmin'),'error');
  213. }
  214. } else {
  215. if ($language_id_exist===false) {
  216. $msg .= Display::return_message(get_lang('LanguageParentNotExist'),'error');
  217. }
  218. }
  219. } else {
  220. $msg .= Display::return_message(get_lang('FormHasErrorsPleaseComplete'),'error');
  221. }
  222. }
  223. Display :: display_header($language_name);
  224. echo $msg;
  225. if (isset($_POST['SubmitAddDeleteLanguage'])) {
  226. $rs = SubLanguageManager::remove_sub_language($_GET['id'], $_GET['sub_language_id']);
  227. if ($rs===true) {
  228. Display::display_confirmation_message(get_lang('TheSubLanguageHasBeenRemoved'));
  229. } else {
  230. Display::display_error_message(get_lang('TheSubLanguageHasNotBeenRemoved'));
  231. }
  232. }
  233. // ckeck_if_is_parent_of_sub_language($parent_id)===false
  234. //
  235. if (isset($_GET['action']) && $_GET['action']=='definenewsublanguage') {
  236. $text = $language_name;
  237. $form = new FormValidator('addsublanguage', 'post', 'sub_language_add.php?id='.Security::remove_XSS($_GET['id']).'&action=definenewsublanguage');
  238. $class='add';
  239. $form->addElement('header', '', $text);
  240. $form->addElement('text', 'original_name', get_lang('OriginalName'),'class="input_titles"');
  241. $form->addRule('original_name', get_lang('ThisFieldIsRequired'), 'required');
  242. $form->addElement('text', 'english_name', get_lang('EnglishName'),'class="input_titles"');
  243. $form->addRule('english_name', get_lang('ThisFieldIsRequired'), 'required');
  244. $form->addElement('text', 'isocode', get_lang('ISOCode'), 'class="input_titles"');
  245. $form->addRule('isocode', get_lang('ThisFieldIsRequired'), 'required');
  246. $form->addElement('static', null, '&nbsp;', '<i>en, es, fr</i>');
  247. $form->addElement('checkbox', 'sub_language_is_visible', '', get_lang('Visibility'));
  248. $form->addElement('style_submit_button', 'SubmitAddNewLanguage', get_lang('CreateSubLanguage'), 'class="'.$class.'"');
  249. //$values['original_name'] = $language_details['original_name'].'...'; -> cannot be used because of quickform filtering (freeze)
  250. $values['english_name'] = $language_details['english_name'].'2';
  251. $values['isocode'] = $language_details['isocode'];
  252. $form->setDefaults($values);
  253. $form->display();
  254. } else {
  255. if (isset($_GET['action']) && $_GET['action']=='deletesublanguage') {
  256. $text=$language_name;
  257. $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']));
  258. $class='minus';
  259. $form->addElement('header', '', $text);
  260. $form->addElement('static', '', get_lang('OriginalName'),$original_name);
  261. $form->addElement('static', '', get_lang('EnglishName'),$english_name);
  262. $form->addElement('static', '', get_lang('PlatformCharsetTitle'),$isocode);
  263. $form->addElement('style_submit_button', 'SubmitAddDeleteLanguage', get_lang('DeleteSubLanguage'), 'class="'.$class.'"');
  264. $form->display();
  265. }
  266. if (isset($_GET['action']) && $_GET['action']=='definenewsublanguage') {
  267. Display::display_normal_message(get_lang('TheSubLanguageForThisLanguageHasBeenAdded'));
  268. }
  269. }
  270. /**
  271. * Footer
  272. */
  273. Display :: display_footer();