sub_language_add.php 13 KB

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