sub_language.class.php 16 KB

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