sub_language.class.php 14 KB

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