BxDolStudioLanguagesUtils.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 {
  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. 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 compileLanguage($mixedLang = 0, $bForce = false) {
  55. $sType = 'all';
  56. if(!empty($mixedLang)) {
  57. if(is_string($mixedLang) && !is_numeric($mixedLang))
  58. $sType = 'all_by_name';
  59. else if(is_numeric($mixedLang) || is_int($mixedLang)) {
  60. $sType = 'all_by_id';
  61. $mixedLang = (int)$mixedLang;
  62. }
  63. }
  64. $aLanguages = array();
  65. $iLanguages = $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages);
  66. if($iLanguages == 0)
  67. return false;
  68. bx_import('BxDolFile');
  69. $oFile = BxDolFile::getInstance();
  70. $sNewLine = "\r\n";
  71. foreach($aLanguages as $aLanguage) {
  72. if(!$bForce && (int)$aLanguage['enabled'] != 1)
  73. continue;
  74. $aKeys = array();
  75. $this->oDb->getKeysBy(array('type' => 'by_language_id', 'value' => $aLanguage['id']), $aKeys);
  76. $sLanguageFile = "lang-" . $aLanguage['name'] . ".php";
  77. $oFile->delete('cache/' . $sLanguageFile);
  78. $rHandle = fopen( BX_DIRECTORY_PATH_CACHE . $sLanguageFile, 'w');
  79. if($rHandle === false)
  80. return false;
  81. $sContent = "<?{$sNewLine}\$GLOBALS['LANG'] = array(";
  82. foreach($aKeys as $aKey) {
  83. list($sKey, $sString) = str_replace(array("\\", "'"), array("\\\\", "\\'"), array($aKey['key'], $aKey['string']));
  84. $sContent .= "{$sNewLine}\t'$sKey' => '$sString',";
  85. }
  86. $sContent = trim($sContent, ',');
  87. if(fwrite($rHandle, $sContent."{$sNewLine});?>") === false)
  88. return false;
  89. if(fclose($rHandle) === false)
  90. return false;
  91. @chmod( BX_DIRECTORY_PATH_CACHE . "lang-{$aLanguage['name']}.php", 0666);
  92. }
  93. $this->init();
  94. return true;
  95. }
  96. function installLanguage($aLanguageModule, $bRecompile = true) {
  97. $aLanguage = array();
  98. $this->oDb->getLanguagesBy(array('type' => 'by_name', 'value' => $aLanguageModule['uri']), $aLanguage, false);
  99. if(empty($aLanguage) || !is_array($aLanguage))
  100. return false;
  101. $sPath = BX_DIRECTORY_PATH_MODULES . $aLanguageModule['path'] . 'data/langs/';
  102. if(!($rHandler = opendir($sPath)))
  103. return false;
  104. $bResult = false;
  105. while(($sFileName = readdir($rHandler)) !== false) {
  106. if(substr($sFileName, -3) != 'xml')
  107. continue;
  108. $sModule = substr($sFileName, 0, -4);
  109. $bResult |= $this->_restoreLanguageByModule($aLanguage, $sPath, $sModule);
  110. }
  111. closedir($rHandler);
  112. $bResult |= $this->_restoreLanguageByModule($aLanguage, $sPath, $aLanguageModule);
  113. return $bResult && ($bRecompile ? $this->compileLanguage($aLanguage['id']) : true);
  114. }
  115. function restoreLanguage($mixedLang = 0, $mixedModule = 0, $bRecompile = true) {
  116. $aLanguages = array();
  117. if(!empty($mixedLang)) {
  118. $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
  119. $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
  120. }
  121. else
  122. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  123. $bResult = true;
  124. foreach($aLanguages as $aLanguage) {
  125. if(empty($aLanguage) || !is_array($aLanguage))
  126. continue;
  127. bx_import('BxDolModuleQuery');
  128. $aLanguageModule = BxDolModuleQuery::getInstance()->getModuleByUri($aLanguage['name']);
  129. if(empty($aLanguageModule) || !is_array($aLanguageModule))
  130. continue;
  131. $sPath = BX_DIRECTORY_PATH_MODULES . $aLanguageModule['path'] . 'data/langs/';
  132. if(!empty($mixedModule))
  133. $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $mixedModule);
  134. else {
  135. if(!($rHandler = opendir($sPath)))
  136. continue;
  137. while(($sFileName = readdir($rHandler)) !== false) {
  138. if(substr($sFileName, -3) != 'xml')
  139. continue;
  140. $sModule = substr($sFileName, 0, -4);
  141. $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $sModule);
  142. }
  143. closedir($rHandler);
  144. }
  145. if($bRecompile)
  146. $bResult &= $this->compileLanguage($aLanguage['id']);
  147. }
  148. return $bResult;
  149. }
  150. function removeLanguageById($mixedLang = 0, $bRecompile = true) {
  151. $aLanguages = array();
  152. if(!empty($mixedLang)) {
  153. $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
  154. $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
  155. }
  156. else
  157. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  158. $bResult = true;
  159. foreach($aLanguages as $aLanguage) {
  160. if(empty($aLanguage) || !is_array($aLanguage))
  161. continue;
  162. $bResult &= (int)$this->oDb->deleteStringsBy(array('type' => 'by_lang', 'language_id' => $aLanguage['id'])) >= 0;
  163. if($bRecompile)
  164. $bResult &= $this->compileLanguage($aLanguage['id']);
  165. }
  166. return $bResult;
  167. }
  168. function removeLanguageByModule($mixedModule, $bRecompile = true) {
  169. $aModule = $mixedModule;
  170. if(!is_array($mixedModule)) {
  171. $sMethod = is_string($mixedModule) && !is_numeric($mixedModule) ? 'getModuleByUri' : 'getModuleById';
  172. $aModule = BxDolModuleQuery::getInstance()->$sMethod($mixedModule);
  173. }
  174. if(empty($aModule) || !is_array($aModule) || !$aModule['uri'] || !$aModule['lang_category'])
  175. return false;
  176. $aCategory = array();
  177. $this->oDb->getCategoriesBy(array('type' => 'by_name', 'value' => $aModule['lang_category']), $aCategory, false);
  178. if(empty($aCategory) || !is_array($aCategory))
  179. return false;
  180. $bResult = (int)$this->oDb->deleteKeysBy(array('type' => 'by_cat_id', 'value' => $aCategory['id'])) >= 0;
  181. return $bResult && ($bRecompile ? $this->compileLanguage() : true);
  182. }
  183. function getLanguageString($sKey, $iLangId = 0) {
  184. if($iLangId == 0) {
  185. $aStrings = array();
  186. $this->oDb->getStringsBy(array('type' => 'all_by_key_key_language_id', 'value' => $sKey), $aStrings, false);
  187. return $aStrings;
  188. }
  189. $aString = array();
  190. $this->oDb->getStringsBy(array('type' => 'by_key_language_id', 'key' => $sKey, 'language_id' => $iLangId), $aString, false);
  191. return $aString;
  192. }
  193. function addLanguageString($sKey, $mixedString, $iLangId = 0, $iCategoryId = 0, $bRecompile = true) {
  194. $iLangId = (int)$iLangId;
  195. $iCategoryId = (int)$iCategoryId;
  196. if($iCategoryId == 0)
  197. $iCategoryId = BX_DOL_LANGUAGE_CATEGORY_CUSTOM;
  198. $sKey = bx_process_input($sKey);
  199. $mixedString = bx_process_input($mixedString);
  200. $aLanguages = array();
  201. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  202. if(($iKeyId = $this->oDb->addKey($iCategoryId, $sKey)) === false)
  203. return false;
  204. foreach($aLanguages as $aLanguage) {
  205. $sString = '';
  206. if(is_string($mixedString))
  207. $sString = $mixedString;
  208. else if(is_array($mixedString))
  209. $sString = $mixedString[$aLanguage['id']];
  210. if(!$this->oDb->addString($iKeyId, $aLanguage['id'], $sString))
  211. return false;
  212. if($bRecompile)
  213. $this->compileLanguage($aLanguage['id']);
  214. }
  215. return $iKeyId;
  216. }
  217. function updateLanguageString($sKey, $sString, $iLangId = 0, $bRecompile = true) {
  218. $sKey = bx_process_input($sKey);
  219. $sString = bx_process_input($sString);
  220. $iLangId = (int)$iLangId;
  221. $aKey = array();
  222. $this->oDb->getKeysBy(array('type' => 'by_name', 'value' => $sKey), $aKey);
  223. if(empty($aKey))
  224. return $this->addLanguageString($sKey, $sString, $iLangId, BX_DOL_LANGUAGE_CATEGORY_CUSTOM, $bRecompile);
  225. return $this->updateLanguageStringById($aKey['id'], $sString, $iLangId, $bRecompile);
  226. }
  227. function updateLanguageStringById($iKeyId, $mixedString, $iLangId = 0, $bRecompile = true) {
  228. $iKeyId = (int)$iKeyId;
  229. $mixedString = bx_process_input($mixedString);
  230. $iLangId = (int)$iLangId;
  231. $aLanguages = array();
  232. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  233. foreach($aLanguages as $aLanguage) {
  234. $sString = '';
  235. if(is_string($mixedString))
  236. $sString = $mixedString;
  237. else if(is_array($mixedString))
  238. $sString = $mixedString[$aLanguage['id']];
  239. if($this->oDb->updateString($iKeyId, $aLanguage['id'], $sString) && $bRecompile)
  240. $this->compileLanguage($aLanguage['id']);
  241. }
  242. return true;
  243. }
  244. function deleteLanguageString($sKey, $iLangId = 0, $bRecompile = true) {
  245. $iLangId = (int)$iLangId;
  246. $sKey = bx_process_input($sKey);
  247. $aKey = array();
  248. $this->oDb->getKeysBy(array('type' => 'by_name', 'value' => $sKey), $aKey);
  249. if(empty($aKey))
  250. return false;
  251. return $this->deleteLanguageStringById($aKey['id'], $iLangId, $bRecompile);
  252. }
  253. function deleteLanguageStringById($iKeyId, $iLangId = 0, $bRecompile = true) {
  254. $iKeyId = (int)$iKeyId;
  255. $iLangId = (int)$iLangId;
  256. $aLanguages = array();
  257. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  258. foreach($aLanguages as $aLanguage) {
  259. if(!$this->oDb->deleteString($iKeyId, $aLanguage['id']))
  260. continue;
  261. if($bRecompile)
  262. $this->compileLanguage($aLanguage['id']);
  263. }
  264. return $this->oDb->deleteKey($iKeyId);
  265. }
  266. function addLanguageKeys($iLanguageId, $iCategoryId, &$aKeys) {
  267. $this->oDb->addKeys($iLanguageId, $iCategoryId, $aKeys);
  268. }
  269. function deleteLanguageKeys($aKeys) {
  270. $this->oDb->deleteKeys($aKeys);
  271. }
  272. function addLanguageCategory($sName) {
  273. return $this->oDb->addCategory($sName);
  274. }
  275. function deleteLanguageCategory($sName) {
  276. return $this->oDb->deleteCategory($sName);
  277. }
  278. protected function _restoreLanguageByModule($aLanguage, $sPath, $mixedModule) {
  279. $oXmlParser = BxDolXmlParser::getInstance();
  280. $aModule = $mixedModule;
  281. if(!is_array($mixedModule)) {
  282. $sMethod = is_string($mixedModule) && !is_numeric($mixedModule) ? 'getModuleByUri' : 'getModuleById';
  283. $aModule = BxDolModuleQuery::getInstance()->$sMethod($mixedModule);
  284. }
  285. if(empty($aModule) || !is_array($aModule) || !$aModule['uri'] || !$aModule['lang_category'])
  286. return true;
  287. $sPath = $sPath . $aModule['uri'] . '.xml';
  288. if(!file_exists($sPath) && isset($aModule['path'])) {
  289. $sPath = BX_DIRECTORY_PATH_MODULES . $aModule['path'] . 'install/langs/' . $aLanguage['name'] . '.xml';
  290. if(!file_exists($sPath))
  291. return true;
  292. }
  293. $sXmlContent = file_get_contents($sPath);
  294. $sName = $oXmlParser->getAttribute($sXmlContent, 'resources', 'name');
  295. $aStrings = $oXmlParser->getValues($sXmlContent, 'string');
  296. if(empty($sName) || empty($aStrings) || $sName != $aLanguage['name'])
  297. return false;
  298. $aCategory = array();
  299. $this->oDb->getCategoriesBy(array('type' => 'by_name', 'value' => $aModule['lang_category']), $aCategory, false);
  300. if(empty($aCategory) || !is_array($aCategory))
  301. return false;
  302. $this->oDb->deleteStringsBy(array('type' => 'by_cat_and_lang', 'category_id' => $aCategory['id'], 'language_id' => $aLanguage['id']));
  303. $bResult = true;
  304. foreach($aStrings as $sKey => $sValue)
  305. if($sKey != '')
  306. $bResult &= $this->addLanguageString($sKey, $sValue, $aLanguage['id'], $aCategory['id'], false) > 0;
  307. return $bResult;
  308. }
  309. }
  310. /** @} */