sub_language.class.php 17 KB

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