sub_language.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class SubLanguageManager
  5. * @package chamilo.admin.sublanguage
  6. */
  7. class SubLanguageManager
  8. {
  9. /**
  10. * Constructor
  11. */
  12. public function __construct()
  13. {
  14. }
  15. /**
  16. * Get all files of lang folder (forum.inc.php,gradebook.inc.php,notebook.inc.php)
  17. * @param String The lang path folder (/var/www/my_lms/main/lang/spanish)
  18. * @param bool true if we only want the "subname" trad4all instead of trad4all.inc.php
  19. *
  20. * @return Array All file of lang folder
  21. */
  22. public static function get_lang_folder_files_list($path, $only_main_name = false)
  23. {
  24. $content_dir = array();
  25. if (is_dir($path)) {
  26. if ($dh = opendir($path)) {
  27. while (($file = readdir($dh)) !== false) {
  28. if ($file[0] <> '.' && substr($file, -4, strlen($file)) == '.php') {
  29. if ($only_main_name) {
  30. if ($file != '' && strpos($file, '.inc.php'))
  31. $content_dir[] = substr($file, 0, strpos($file, '.inc.php'));
  32. } else {
  33. $content_dir[] = $file;
  34. }
  35. }
  36. }
  37. }
  38. closedir($dh);
  39. return $content_dir;
  40. }
  41. }
  42. /**
  43. * Get all information of sub-language
  44. * @param Integer The parent id(Language father id)
  45. * @param Integer The sub language id
  46. * @return Array All information about sub-language
  47. */
  48. public static function get_all_information_of_sub_language($parent_id, $sub_language_id)
  49. {
  50. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  51. $sql = 'SELECT * FROM ' . $tbl_admin_languages . '
  52. WHERE parent_id= ' . intval($parent_id) . ' AND id= ' . intval($sub_language_id) . '';
  53. $rs = Database::query($sql);
  54. $all_information = array();
  55. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  56. $all_information = $row;
  57. }
  58. return $all_information;
  59. }
  60. /**
  61. * Get all information of language
  62. * @param Integer The parent id(Language father id)
  63. * @return Array All information about language
  64. */
  65. public static function get_all_information_of_language($parent_id)
  66. {
  67. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  68. $sql = 'SELECT * FROM ' . $tbl_admin_languages . ' WHERE id = "' . intval($parent_id) . '"';
  69. $rs = Database::query($sql);
  70. $all_information = array();
  71. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  72. $all_information = $row;
  73. }
  74. return $all_information;
  75. }
  76. /**
  77. * Get all information of chamilo file
  78. * @param String The chamilo path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php)
  79. * @patam Bool Whether we want to remove the '$' prefix in the results or not
  80. * @return Array Contains all information of chamilo file
  81. */
  82. public static function get_all_language_variable_in_file($system_path_file, $get_as_string_index = false)
  83. {
  84. $res_list = array();
  85. if (!is_readable($system_path_file)) {
  86. return $res_list;
  87. }
  88. $info_file = file($system_path_file);
  89. foreach ($info_file as $line) {
  90. if (substr($line, 0, 1) != '$') {
  91. continue;
  92. }
  93. list($var, $val) = split('=', $line, 2);
  94. $var = trim($var);
  95. $val = trim($val);
  96. if ($get_as_string_index) { //remove the prefix $
  97. $var = substr($var, 1);
  98. }
  99. $res_list[$var] = $val;
  100. }
  101. return $res_list;
  102. }
  103. /**
  104. * Add file in sub-language directory and add header(tag php)
  105. * @param String The chamilo path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php)
  106. * @return bool
  107. */
  108. public static function add_file_in_language_directory($system_path_file)
  109. {
  110. $return_value = @file_put_contents($system_path_file, '<?php' . PHP_EOL);
  111. return $return_value;
  112. }
  113. /**
  114. * Write in file of sub-language
  115. * @param String The path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php)
  116. * @param String The new sub-language
  117. * @param String The language variable
  118. * @return Boolean True on success, False on error
  119. */
  120. public static function write_data_in_file($path_file, $new_term, $new_variable)
  121. {
  122. $return_value = false;
  123. $new_data = $new_variable . '=' . $new_term;
  124. $resource = @fopen($path_file, "a");
  125. if (file_exists($path_file) && $resource) {
  126. if (fwrite($resource, $new_data . PHP_EOL) === false) {
  127. //not allow to write
  128. $return_value = false;
  129. } else {
  130. $return_value = true;
  131. }
  132. fclose($resource);
  133. }
  134. return $return_value;
  135. }
  136. /**
  137. * Add directory for sub-language
  138. * @param string $sub_language_dir The sub-language directory ( e.g. 'spanish_corporate' )
  139. * @return boolean True on success, false on failure
  140. */
  141. public static function add_language_directory($sub_language_dir)
  142. {
  143. if (empty($sub_language_dir)) {
  144. return false;
  145. }
  146. $dir = api_get_path(SYS_LANG_PATH) . $sub_language_dir;
  147. if (is_dir($dir)) {
  148. return true;
  149. } //even if the dir already exists, we reach the objective of having the directory there
  150. return @mkdir($dir, api_get_permissions_for_new_directories());
  151. }
  152. /**
  153. * Delete sub-language.
  154. * In order to avoid deletion of main laguages, we check the existence of a parent
  155. * @param int The parent id
  156. * @return bool True on success, false on error
  157. */
  158. public static function remove_sub_language($parent_id, $sub_language_id)
  159. {
  160. if (empty($parent_id) or (intval($parent_id) != $parent_id) or empty($sub_language_id) or (intval($sub_language_id) != $sub_language_id)) {
  161. return false;
  162. }
  163. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  164. $sql = 'SELECT dokeos_folder FROM ' . $tbl_admin_languages . '
  165. WHERE parent_id = ' . $parent_id . ' and id = ' . $sub_language_id;
  166. $res = Database::query($sql);
  167. if ($res === false or Database::num_rows($res) < 1) {
  168. return false;
  169. }
  170. $row = Database::fetch_assoc($res);
  171. $res = SubLanguageManager::remove_language_directory($row['dokeos_folder']);
  172. if ($res === false) {
  173. return false;
  174. } //can't delete dir, so do not delete language record
  175. $sql = 'DELETE FROM ' . $tbl_admin_languages . '
  176. WHERE id= ' . intval($sub_language_id) . ' ';
  177. $res = Database::query($sql);
  178. return $res;
  179. }
  180. /**
  181. * Remove directory for sub-language
  182. * @param String The sub-language path directory ( e.g. 'spanish_corporate'' )
  183. * @return boolean True on success, false on failure
  184. */
  185. public static function remove_language_directory($sub_language_dir)
  186. {
  187. if (empty($sub_language_dir)) {
  188. return false;
  189. }
  190. $dir = api_get_path(SYS_LANG_PATH) . $sub_language_dir;
  191. if (!is_dir($dir)) {
  192. return true;
  193. } //even if the dir does not exist, we reach the objective of not having the directory there
  194. $content = SubLanguageManager::get_lang_folder_files_list($dir);
  195. if (count($content) > 0) {
  196. foreach ($content as $value_content) {
  197. $path_file = $dir . '/' . $value_content;
  198. unlink($path_file);
  199. }
  200. return @rmdir($dir);
  201. } else {
  202. return @rmdir($dir);
  203. }
  204. }
  205. /**
  206. * check if language exist by id
  207. * @param int $language_id
  208. * @return bool
  209. */
  210. public static function check_if_exist_language_by_id($language_id)
  211. {
  212. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  213. $sql = 'SELECT count(*) as count
  214. FROM ' . $tbl_admin_languages . '
  215. WHERE id="' . intval($language_id) . '"';
  216. $rs = Database::query($sql);
  217. if (Database::num_rows($rs) > 0) {
  218. if (Database::result($rs, 0, 'count') == 1) {
  219. return true;
  220. } else {
  221. return false;
  222. }
  223. } else {
  224. return false;
  225. }
  226. }
  227. /**
  228. * Get name of language by id
  229. * @param Integer The language id
  230. * @return String The original name of language
  231. */
  232. public static function get_name_of_language_by_id($language_id)
  233. {
  234. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  235. $sql = 'SELECT original_name
  236. FROM ' . $tbl_admin_languages . '
  237. WHERE id= ' . intval($language_id) . '';
  238. $rs = Database::query($sql);
  239. if (Database::num_rows($rs) > 0) {
  240. return Database::result($rs, 0, 'original_name');
  241. } else {
  242. return '';
  243. }
  244. }
  245. /**
  246. * Verified if language is sub-language
  247. * @param Integer The language id
  248. * @return Boolean
  249. */
  250. public static function check_if_language_is_sub_language($language_id)
  251. {
  252. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  253. $sql = 'SELECT count(*) AS count FROM ' . $tbl_admin_languages . '
  254. WHERE id = ' . intval($language_id) . ' AND NOT ISNULL(parent_id)';
  255. $rs = Database::query($sql);
  256. if (Database::num_rows($rs) > 0 && Database::result($rs, '0', 'count') == 1) {
  257. return true;
  258. } else {
  259. return false;
  260. }
  261. }
  262. /**
  263. * @param int $language_id
  264. * @return bool
  265. */
  266. public static function check_if_language_is_used($language_id)
  267. {
  268. $language_info = self::get_all_information_of_language($language_id);
  269. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  270. $sql = 'SELECT count(*) AS count FROM ' . $user_table . '
  271. WHERE language ="' . Database::escape_string($language_info['english_name']).'"';
  272. $rs = Database::query($sql);
  273. if (Database::num_rows($rs) > 0 && Database::result($rs, '0', 'count') >= 1) {
  274. return true;
  275. } else {
  276. return false;
  277. }
  278. }
  279. /**
  280. * Verified if language is father of an sub-language
  281. * @param Integer The language id
  282. * @return Boolean
  283. */
  284. public static function check_if_language_is_father($language_id)
  285. {
  286. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  287. $sql = 'SELECT count(*) AS count FROM ' . $tbl_admin_languages . '
  288. WHERE parent_id= ' . intval($language_id) . ' AND NOT ISNULL(parent_id);';
  289. $rs = Database::query($sql);
  290. if (Database::num_rows($rs) > 0 && Database::result($rs, '0', 'count') == 1) {
  291. return true;
  292. } else {
  293. return false;
  294. }
  295. }
  296. /**
  297. * Make unavailable the language
  298. * @param Integer The language id
  299. * @return void()
  300. */
  301. public static function make_unavailable_language($language_id)
  302. {
  303. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  304. $sql = "UPDATE $tbl_admin_languages SET available='0'
  305. WHERE id = " . intval($language_id) . "";
  306. $result = Database::query($sql);
  307. return $result !== false; //only return false on sql error
  308. }
  309. /**
  310. * Make available the language
  311. * @param Integer The language id
  312. * @return void
  313. */
  314. public static function make_available_language($language_id)
  315. {
  316. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  317. $sql = "UPDATE $tbl_admin_languages SET available='1'
  318. WHERE id = " . intval($language_id) . "";
  319. $result = Database::query($sql);
  320. return $result !== false; //only return false on sql error
  321. }
  322. /**
  323. * Set platform language
  324. * @param Integer The language id
  325. * @return bool
  326. */
  327. public static function set_platform_language($language_id)
  328. {
  329. if (empty($language_id) or (intval($language_id) != $language_id)) {
  330. return false;
  331. }
  332. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  333. $tbl_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  334. $sql = "SELECT english_name FROM " . $tbl_admin_languages . "
  335. WHERE id= " . intval($language_id) . "";
  336. $result = Database::query($sql);
  337. $lang = Database::fetch_array($result);
  338. $sql_update_2 = "UPDATE " . $tbl_settings_current . " SET selected_value='" . $lang['english_name'] . "'
  339. WHERE variable='platformLanguage'";
  340. $result_2 = Database::query($sql_update_2);
  341. Event::addEvent(
  342. LOG_PLATFORM_LANGUAGE_CHANGE,
  343. LOG_PLATFORM_LANGUAGE,
  344. $lang['english_name']
  345. );
  346. return $result_2 !== false;
  347. }
  348. /**
  349. * Get platform language ID
  350. * @return int The platform language ID
  351. */
  352. public static function get_platform_language_id()
  353. {
  354. $name = api_get_setting('platformLanguage');
  355. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  356. $sql = "SELECT id FROM " . $tbl_admin_languages . " WHERE english_name ='$name'";
  357. $res = Database::query($sql);
  358. if (Database::num_rows($res) < 1) {
  359. return false;
  360. }
  361. $row = Database::fetch_array($res);
  362. return $row['id'];
  363. }
  364. /**
  365. * Get parent language path (or null if no parent)
  366. * @param string Children language path
  367. * @return string Parent language path or null
  368. */
  369. public static function get_parent_language_path($language_path)
  370. {
  371. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  372. $sql = "SELECT dokeos_folder
  373. FROM " . $tbl_admin_languages . "
  374. WHERE id = (
  375. SELECT parent_id FROM " . $tbl_admin_languages . "
  376. WHERE dokeos_folder = '" . Database::escape_string($language_path) . "'
  377. )
  378. ";
  379. $result = Database::query($sql);
  380. if (Database::num_rows($result) == 0) {
  381. return null;
  382. }
  383. $row = Database::fetch_array($result);
  384. return $row['dokeos_folder'];
  385. }
  386. /**
  387. * Get language matching isocode
  388. * @param string $isocode The language isocode (en, es, fr, zh-TW, etc)
  389. * @return mixed English name of the matching language, or false if no active language could be found
  390. */
  391. public static function getLanguageFromIsocode($isocode)
  392. {
  393. $isocode = Database::escape_string($isocode);
  394. $adminLanguagesTable = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  395. // select language - if case several languages match, get the last (more recent) one
  396. $sql = "SELECT english_name
  397. FROM " . $adminLanguagesTable . "
  398. WHERE
  399. isocode ='$isocode' AND
  400. available = 1
  401. ORDER BY id
  402. DESC LIMIT 1";
  403. $res = Database::query($sql);
  404. if (Database::num_rows($res) < 1) {
  405. return false;
  406. }
  407. $row = Database::fetch_assoc($res);
  408. return $row['english_name'];
  409. }
  410. /**
  411. * Get best language in browser preferences
  412. * @param string $preferences The browser-configured language preferences (e.g. "en,es;q=0.7;en-us;q=0.3", etc)
  413. * @return mixed English name of the matching language, or false if no active language could be found
  414. */
  415. public static function getLanguageFromBrowserPreference($preferences)
  416. {
  417. if (empty($preferences)) {
  418. return false;
  419. }
  420. $preferencesArray = explode(',', $preferences);
  421. if (count($preferencesArray) > 0) {
  422. foreach ($preferencesArray as $pref) {
  423. $s = strpos($pref, ';');
  424. if ($s >= 2) {
  425. $code = substr($pref, 0, $s);
  426. } else {
  427. $code = $pref;
  428. }
  429. $name = self::getLanguageFromIsocode($code);
  430. if ($name !== false) {
  431. return $name;
  432. }
  433. }
  434. }
  435. return false;
  436. }
  437. }