sub_language_add.php 13 KB

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