BxDolStudioLanguagesUtils.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinStudio Dolphin Studio
  7. * @{
  8. */
  9. defined('BX_DOL') or die('hack attempt');
  10. bx_import('BxDolLanguages');
  11. bx_import('BxDolXmlParser');
  12. bx_import('BxDolStudioLanguagesUtilsQuery');
  13. class BxDolStudioLanguagesUtils extends BxDolLanguages implements iBxDolSingleton {
  14. function __construct() {
  15. if(isset($GLOBALS['bxDolClasses'][get_class($this)]))
  16. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  17. parent::__construct();
  18. $this->oDb = new BxDolStudioLanguagesUtilsQuery();
  19. }
  20. /**
  21. * Prevent cloning the instance
  22. */
  23. public function __clone() {
  24. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  25. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  26. }
  27. /**
  28. * Get singleton instance of the class
  29. */
  30. public static function getInstance() {
  31. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  32. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolStudioLanguagesUtils();
  33. return $GLOBALS['bxDolClasses'][__CLASS__];
  34. }
  35. function getLanguageInfo($sName) {
  36. $aLanguage = array();
  37. $this->oDb->getLanguagesBy(array('type' => 'by_name', 'value' => $sName), $aLanguage, false);
  38. bx_import('BxDolStudioTemplate');
  39. if(!empty($aLanguage))
  40. $aLanguage['icon'] = BxDolStudioTemplate::getInstance()->getIconUrl('sys_fl_' . $aLanguage['flag'] . '.gif');
  41. return $aLanguage;
  42. }
  43. function getLanguagesInfo($bIdAsKey = false, $bActiveOnly = false) {
  44. $aLanguages = array();
  45. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  46. if(!is_array($aLanguages) || empty($aLanguages))
  47. return $aLanguages;
  48. bx_import('BxDolStudioTemplate');
  49. $oTemplate = BxDolStudioTemplate::getInstance();
  50. foreach($aLanguages as $iKey => $aLanguage)
  51. $aLanguages[$iKey]['icon'] = $oTemplate->getIconUrl('sys_fl_' . $aLanguage['flag'] . '.gif');
  52. return $aLanguages;
  53. }
  54. function readLanguage($sPath, $sType = 'restore') {
  55. if(!file_exists($sPath))
  56. return array();
  57. $oXmlParser = BxDolXmlParser::getInstance();
  58. $sXmlContent = file_get_contents($sPath);
  59. $aResult = array();
  60. $aResult['name'] = $oXmlParser->getAttribute($sXmlContent, 'resources', 'name');
  61. switch($sType) {
  62. case 'restore':
  63. $aResult['strings'] = $oXmlParser->getValues($sXmlContent, 'string');
  64. break;
  65. case 'update':
  66. $aResult['strings_del'] = $oXmlParser->getValues($sXmlContent, 'string_del');
  67. $aResult['strings_add'] = $oXmlParser->getValues($sXmlContent, 'string_add');
  68. $aResult['strings_upd'] = $oXmlParser->getValues($sXmlContent, 'string_upd');
  69. break;
  70. }
  71. return $aResult;
  72. }
  73. function compileLanguage($mixedLang = 0, $bForce = false) {
  74. $sType = 'all';
  75. if(!empty($mixedLang)) {
  76. if(is_string($mixedLang) && !is_numeric($mixedLang))
  77. $sType = 'all_by_name';
  78. else if(is_numeric($mixedLang) || is_int($mixedLang)) {
  79. $sType = 'all_by_id';
  80. $mixedLang = (int)$mixedLang;
  81. }
  82. }
  83. $aLanguages = array();
  84. $iLanguages = $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages);
  85. if($iLanguages == 0)
  86. return false;
  87. bx_import('BxDolFile');
  88. $oFile = BxDolFile::getInstance();
  89. $sNewLine = "\r\n";
  90. foreach($aLanguages as $aLanguage) {
  91. if(!$bForce && (int)$aLanguage['enabled'] != 1)
  92. continue;
  93. $aKeys = array();
  94. $this->oDb->getKeysBy(array('type' => 'by_language_id', 'value' => $aLanguage['id']), $aKeys);
  95. $sLanguageFile = "lang-" . $aLanguage['name'] . ".php";
  96. $oFile->delete('cache/' . $sLanguageFile);
  97. $rHandle = fopen( BX_DIRECTORY_PATH_CACHE . $sLanguageFile, 'w');
  98. if($rHandle === false)
  99. return false;
  100. $sContent = "<?php{$sNewLine}\$GLOBALS['LANG'] = array(";
  101. foreach($aKeys as $aKey) {
  102. list($sKey, $sString) = str_replace(array("\\", "'"), array("\\\\", "\\'"), array($aKey['key'], $aKey['string']));
  103. $sContent .= "{$sNewLine}\t'$sKey' => '$sString',";
  104. }
  105. $sContent = trim($sContent, ',');
  106. if(fwrite($rHandle, $sContent."{$sNewLine});?>") === false)
  107. return false;
  108. if(fclose($rHandle) === false)
  109. return false;
  110. @chmod( BX_DIRECTORY_PATH_CACHE . "lang-{$aLanguage['name']}.php", 0666);
  111. }
  112. $this->init();
  113. return true;
  114. }
  115. function installLanguage($aLanguageModule, $bRecompile = true) {
  116. $aLanguage = array();
  117. $this->oDb->getLanguagesBy(array('type' => 'by_name', 'value' => $aLanguageModule['uri']), $aLanguage, false);
  118. if(empty($aLanguage) || !is_array($aLanguage))
  119. return false;
  120. $sPath = BX_DIRECTORY_PATH_MODULES . $aLanguageModule['path'] . 'data/langs/';
  121. if(!($rHandler = opendir($sPath)))
  122. return false;
  123. $bResult = false;
  124. while(($sFileName = readdir($rHandler)) !== false) {
  125. if(substr($sFileName, -3) != 'xml')
  126. continue;
  127. $sModule = substr($sFileName, 0, -4);
  128. $bResult |= $this->_restoreLanguageByModule($aLanguage, $sPath, $sModule);
  129. }
  130. closedir($rHandler);
  131. $bResult |= $this->_restoreLanguageByModule($aLanguage, $sPath, $aLanguageModule);
  132. return $bResult && ($bRecompile ? $this->compileLanguage($aLanguage['id']) : true);
  133. }
  134. function restoreLanguage($mixedLang = 0, $mixedModule = 0, $bRecompile = true) {
  135. $aLanguages = array();
  136. if(!empty($mixedLang)) {
  137. $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
  138. $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
  139. }
  140. else
  141. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  142. $bResult = true;
  143. foreach($aLanguages as $aLanguage) {
  144. if(empty($aLanguage) || !is_array($aLanguage))
  145. continue;
  146. bx_import('BxDolModuleQuery');
  147. $aLanguageModule = BxDolModuleQuery::getInstance()->getModuleByUri($aLanguage['name']);
  148. if(empty($aLanguageModule) || !is_array($aLanguageModule))
  149. continue;
  150. $sPath = BX_DIRECTORY_PATH_MODULES . $aLanguageModule['path'] . 'data/langs/';
  151. if(!empty($mixedModule))
  152. $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $mixedModule);
  153. else {
  154. if(!($rHandler = opendir($sPath)))
  155. continue;
  156. while(($sFileName = readdir($rHandler)) !== false) {
  157. if(substr($sFileName, -3) != 'xml')
  158. continue;
  159. $sModule = substr($sFileName, 0, -4);
  160. $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $sModule);
  161. }
  162. closedir($rHandler);
  163. }
  164. if($bRecompile)
  165. $bResult &= $this->compileLanguage($aLanguage['id']);
  166. }
  167. return $bResult;
  168. }
  169. function removeLanguageById($mixedLang = 0, $bRecompile = true) {
  170. $aLanguages = array();
  171. if(!empty($mixedLang)) {
  172. $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
  173. $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
  174. }
  175. else
  176. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  177. $bResult = true;
  178. foreach($aLanguages as $aLanguage) {
  179. if(empty($aLanguage) || !is_array($aLanguage))
  180. continue;
  181. $bResult &= (int)$this->oDb->deleteStringsBy(array('type' => 'by_lang', 'language_id' => $aLanguage['id'])) >= 0;
  182. if($bRecompile)
  183. $bResult &= $this->compileLanguage($aLanguage['id']);
  184. }
  185. return $bResult;
  186. }
  187. function removeLanguageByModule($mixedModule, $bRecompile = true) {
  188. $aModule = $mixedModule;
  189. if(!is_array($mixedModule)) {
  190. $sMethod = is_string($mixedModule) && !is_numeric($mixedModule) ? 'getModuleByUri' : 'getModuleById';
  191. $aModule = BxDolModuleQuery::getInstance()->$sMethod($mixedModule);
  192. }
  193. if(empty($aModule) || !is_array($aModule) || !$aModule['uri'] || !$aModule['lang_category'])
  194. return false;
  195. $aCategory = array();
  196. $this->oDb->getCategoriesBy(array('type' => 'by_name', 'value' => $aModule['lang_category']), $aCategory, false);
  197. if(empty($aCategory) || !is_array($aCategory))
  198. return false;
  199. $bResult = (int)$this->oDb->deleteKeysBy(array('type' => 'by_cat_id', 'value' => $aCategory['id'])) >= 0;
  200. return $bResult && ($bRecompile ? $this->compileLanguage() : true);
  201. }
  202. function getLanguageString($sKey, $iLangId = 0) {
  203. if($iLangId == 0) {
  204. $aStrings = array();
  205. $this->oDb->getStringsBy(array('type' => 'all_by_key_key_language_id', 'value' => $sKey), $aStrings, false);
  206. return $aStrings;
  207. }
  208. $aString = array();
  209. $this->oDb->getStringsBy(array('type' => 'by_key_language_id', 'key' => $sKey, 'language_id' => $iLangId), $aString, false);
  210. return $aString;
  211. }
  212. function addLanguageString($sKey, $mixedString, $iLangId = 0, $iCategoryId = 0, $bRecompile = true) {
  213. $iLangId = (int)$iLangId;
  214. $iCategoryId = (int)$iCategoryId;
  215. if($iCategoryId == 0)
  216. $iCategoryId = BX_DOL_LANGUAGE_CATEGORY_CUSTOM;
  217. $sKey = bx_process_input($sKey);
  218. $mixedString = bx_process_input($mixedString);
  219. $aLanguages = array();
  220. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  221. if(($iKeyId = $this->oDb->addKey($iCategoryId, $sKey)) === false)
  222. return false;
  223. foreach($aLanguages as $aLanguage) {
  224. $sString = '';
  225. if(is_string($mixedString))
  226. $sString = $mixedString;
  227. else if(is_array($mixedString))
  228. $sString = $mixedString[$aLanguage['id']];
  229. if(!$this->oDb->addString($iKeyId, $aLanguage['id'], $sString))
  230. return false;
  231. if($bRecompile)
  232. $this->compileLanguage($aLanguage['id']);
  233. }
  234. return $iKeyId;
  235. }
  236. function updateLanguageString($sKey, $sString, $iLangId = 0, $bRecompile = true) {
  237. $sKey = bx_process_input($sKey);
  238. $sString = bx_process_input($sString);
  239. $iLangId = (int)$iLangId;
  240. $aKey = array();
  241. $this->oDb->getKeysBy(array('type' => 'by_name', 'value' => $sKey), $aKey);
  242. if(empty($aKey))
  243. return $this->addLanguageString($sKey, $sString, $iLangId, BX_DOL_LANGUAGE_CATEGORY_CUSTOM, $bRecompile);
  244. return $this->updateLanguageStringById($aKey['id'], $sString, $iLangId, $bRecompile);
  245. }
  246. function updateLanguageStringById($iKeyId, $mixedString, $iLangId = 0, $bRecompile = true) {
  247. $iKeyId = (int)$iKeyId;
  248. $mixedString = bx_process_input($mixedString);
  249. $iLangId = (int)$iLangId;
  250. $aLanguages = array();
  251. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  252. foreach($aLanguages as $aLanguage) {
  253. $sString = '';
  254. if(is_string($mixedString))
  255. $sString = $mixedString;
  256. else if(is_array($mixedString))
  257. $sString = $mixedString[$aLanguage['id']];
  258. if($this->oDb->updateString($iKeyId, $aLanguage['id'], $sString) && $bRecompile)
  259. $this->compileLanguage($aLanguage['id']);
  260. }
  261. return true;
  262. }
  263. function deleteLanguageString($sKey, $iLangId = 0, $bRecompile = true) {
  264. $iLangId = (int)$iLangId;
  265. $sKey = bx_process_input($sKey);
  266. $aKey = array();
  267. $this->oDb->getKeysBy(array('type' => 'by_name', 'value' => $sKey), $aKey);
  268. if(empty($aKey))
  269. return false;
  270. return $this->deleteLanguageStringById($aKey['id'], $iLangId, $bRecompile);
  271. }
  272. function deleteLanguageStringById($iKeyId, $iLangId = 0, $bRecompile = true) {
  273. $iKeyId = (int)$iKeyId;
  274. $iLangId = (int)$iLangId;
  275. $aLanguages = array();
  276. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  277. foreach($aLanguages as $aLanguage) {
  278. if(!$this->oDb->deleteString($iKeyId, $aLanguage['id']))
  279. continue;
  280. if($bRecompile)
  281. $this->compileLanguage($aLanguage['id']);
  282. }
  283. return $this->oDb->deleteKey($iKeyId);
  284. }
  285. function addLanguageKeys($iLanguageId, $iCategoryId, &$aKeys) {
  286. $this->oDb->addKeys($iLanguageId, $iCategoryId, $aKeys);
  287. }
  288. function deleteLanguageKeys($aKeys) {
  289. $this->oDb->deleteKeys($aKeys);
  290. }
  291. function addLanguageCategory($sName) {
  292. return $this->oDb->addCategory($sName);
  293. }
  294. function deleteLanguageCategory($sName) {
  295. return $this->oDb->deleteCategory($sName);
  296. }
  297. protected function _restoreLanguageByModule($aLanguage, $sPath, $mixedModule) {
  298. $oXmlParser = BxDolXmlParser::getInstance();
  299. $aModule = $mixedModule;
  300. if(!is_array($mixedModule)) {
  301. $sMethod = is_string($mixedModule) && !is_numeric($mixedModule) ? 'getModuleByUri' : 'getModuleById';
  302. $aModule = BxDolModuleQuery::getInstance()->$sMethod($mixedModule);
  303. }
  304. if(empty($aModule) || !is_array($aModule) || !$aModule['uri'] || !$aModule['lang_category'])
  305. return true;
  306. $sPath = $sPath . $aModule['uri'] . '.xml';
  307. if(!file_exists($sPath) && isset($aModule['path'])) {
  308. $sPath = BX_DIRECTORY_PATH_MODULES . $aModule['path'] . 'install/langs/' . $aLanguage['name'] . '.xml';
  309. if(!file_exists($sPath))
  310. return true;
  311. }
  312. $aLanguageInfo = $this->readLanguage($sPath);
  313. if(empty($aLanguageInfo['name']) || empty($aLanguageInfo['strings']) || $aLanguageInfo['name'] != $aLanguage['name'])
  314. return false;
  315. $aCategory = array();
  316. $this->oDb->getCategoriesBy(array('type' => 'by_name', 'value' => $aModule['lang_category']), $aCategory, false);
  317. if(empty($aCategory) || !is_array($aCategory))
  318. return false;
  319. $this->oDb->deleteStringsBy(array('type' => 'by_cat_and_lang', 'category_id' => $aCategory['id'], 'language_id' => $aLanguage['id']));
  320. $bResult = true;
  321. foreach($aLanguageInfo['strings'] as $sKey => $sValue)
  322. if($sKey != '')
  323. $bResult &= $this->addLanguageString($sKey, $sValue, $aLanguage['id'], $aCategory['id'], false) > 0;
  324. return $bResult;
  325. }
  326. }
  327. /** @} */