sub_language.class.php 17 KB

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