sub_language.class.php 17 KB

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