BxDolLanguagesQuery.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolLanguagesQuery extends BxDolDb implements iBxDolSingleton
  10. {
  11. protected function __construct()
  12. {
  13. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  14. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  15. parent::__construct();
  16. }
  17. /**
  18. * Prevent cloning the instance
  19. */
  20. public function __clone()
  21. {
  22. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  23. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  24. }
  25. /**
  26. * Get singleton instance of the class
  27. */
  28. static function getInstance()
  29. {
  30. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  31. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolLanguagesQuery();
  32. return $GLOBALS['bxDolClasses'][__CLASS__];
  33. }
  34. function getLanguageId($sName, $bFromCache = true)
  35. {
  36. return (int)$this->getLanguageField($sName, 'ID', $bFromCache);
  37. }
  38. function getLanguageName($iId, $bFromCache = true)
  39. {
  40. return $this->getLanguageFieldById($iId, 'Name', $bFromCache);
  41. }
  42. function getLanguageTitle($iId, $bFromCache = true)
  43. {
  44. return $this->getLanguageFieldById($iId, 'Title', $bFromCache);
  45. }
  46. function getLanguageFlag($sName, $bFromCache = true)
  47. {
  48. return $this->getLanguageField($sName, 'Flag', $bFromCache);
  49. }
  50. function getLanguageDirection($sName, $bFromCache = true)
  51. {
  52. return $this->getLanguageField($sName, 'Direction', $bFromCache);
  53. }
  54. function getLangCountryCode($sName, $bFromCache = true)
  55. {
  56. return $this->getLanguageField($sName, 'LanguageCountry', $bFromCache);
  57. }
  58. protected function getLanguageField($sName, $sField, $bFromCache = true)
  59. {
  60. $sSql = $this->prepare("SELECT `" . $sField . "` FROM `sys_localization_languages` WHERE `Name`=? AND `Enabled`='1' LIMIT 1", $sName);
  61. if($bFromCache)
  62. return $this->fromCache('checkLangExists_' . $sName . '_' . $sField, 'getOne', $sSql);
  63. else
  64. return $this->getOne($sSql);
  65. }
  66. protected function getLanguageFieldById($iId, $sField, $bFromCache = true)
  67. {
  68. $sSql = $this->prepare("SELECT `" . $sField . "` FROM `sys_localization_languages` WHERE `ID`=? AND `Enabled`='1' LIMIT 1", $iId);
  69. if($bFromCache)
  70. return $this->fromCache('checkLangExists_' . $iId . '_' . $sField, 'getOne', $sSql);
  71. else
  72. return $this->getOne($sSql);
  73. }
  74. function getLanguages($bIdAsKey = false, $bActiveOnly = false)
  75. {
  76. return $this->getLanguagesWithKey($bIdAsKey ? 'ID' : 'Name', $bActiveOnly);
  77. }
  78. function getLanguagesWithKey($sKey = 'Name', $bActiveOnly = false)
  79. {
  80. $sSql = "SELECT * FROM `sys_localization_languages` WHERE 1 " . ($bActiveOnly ? " AND `Enabled`='1'" : "") . " ORDER BY `Title` ASC";
  81. return $this->getPairs($sSql, $sKey, 'Title');
  82. }
  83. function getLanguagesBy($aParams, &$aItems, $bReturnCount = true)
  84. {
  85. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  86. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  87. if(!isset($aParams['order']) || empty($aParams['order']))
  88. $sOrderClause = " `tl`.`ID` ASC ";
  89. switch($aParams['type']) {
  90. case 'by_id':
  91. $aMethod['name'] = 'getRow';
  92. $aMethod['params'][1] = array(
  93. 'id' => $aParams['value']
  94. );
  95. $sWhereClause .= " AND `tl`.`ID`=:id";
  96. break;
  97. case 'by_name':
  98. $aMethod['name'] = 'getRow';
  99. $aMethod['params'][1] = array(
  100. 'name' => $aParams['value']
  101. );
  102. $sWhereClause .= " AND `tl`.`Name`=:name";
  103. break;
  104. case 'default':
  105. $aMethod['name'] = 'getRow';
  106. $aMethod['params'][1] = array(
  107. 'name' => getParam('lang_default')
  108. );
  109. $sWhereClause .= " AND `tl`.`Name`=:name";
  110. break;
  111. case 'active':
  112. $sWhereClause = " AND `tl`.`Enabled`='1'";
  113. break;
  114. case 'all':
  115. $sOrderClause = " `tl`.`Name` ASC ";
  116. break;
  117. case 'all_by_id':
  118. $aMethod['params'][1] = array(
  119. 'id' => $aParams['value']
  120. );
  121. $sWhereClause .= " AND `tl`.`ID`=:id";
  122. $sOrderClause = " `tl`.`Name` ASC ";
  123. break;
  124. case 'all_by_name':
  125. $aMethod['params'][1] = array(
  126. 'name' => $aParams['value']
  127. );
  128. $sWhereClause .= " AND `tl`.`Name`=:name";
  129. $sOrderClause = " `tl`.`Name` ASC ";
  130. break;
  131. case 'all_key_id':
  132. $aMethod['name'] = 'getAllWithKey';
  133. $aMethod['params'][1] = 'id';
  134. $aMethod['params'][2] = array();
  135. if(!empty($aParams['language'])) {
  136. $aMethod['params'][2]['id'] = $aParams['language'];
  137. $sWhereClause .= " AND `tl`.`ID`=:id";
  138. }
  139. if(isset($aParams['enabled'])) {
  140. $aMethod['params'][2]['enabled'] = (int)$aParams['enabled'];
  141. $sWhereClause .= " AND `tl`.`Enabled`=:enabled";
  142. }
  143. break;
  144. case 'all_key_name':
  145. $aMethod['name'] = 'getAllWithKey';
  146. $aMethod['params'][1] = 'name';
  147. $aMethod['params'][2] = array();
  148. if(!empty($aParams['language'])) {
  149. $aMethod['params'][2]['name'] = $aParams['language'];
  150. $sWhereClause .= " AND `tl`.`Name`=:name";
  151. }
  152. if(isset($aParams['enabled'])) {
  153. $aMethod['params'][2]['enabled'] = (int)$aParams['enabled'];
  154. $sWhereClause .= " AND `tl`.`Enabled`=:enabled";
  155. }
  156. break;
  157. }
  158. $aMethod['params'][0] = "SELECT SQL_CALC_FOUND_ROWS
  159. `tl`.`ID` AS `id`,
  160. `tl`.`Name` AS `name`,
  161. `tl`.`Flag` AS `flag`,
  162. `tl`.`Title` AS `title`,
  163. `tl`.`Enabled` AS `enabled`" . $sSelectClause . "
  164. FROM `sys_localization_languages` AS `tl`" . $sJoinClause . "
  165. WHERE 1" . $sWhereClause . "
  166. ORDER BY" . $sOrderClause . $sLimitClause;
  167. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  168. if(!$bReturnCount)
  169. return count($aItems) > 0;
  170. return (int)$this->getOne("SELECT FOUND_ROWS()");
  171. }
  172. function getKeys()
  173. {
  174. $sSql = "SELECT `ID`, `IDCategory`, `Key` FROM `sys_localization_keys`";
  175. return $this->getAllWithKey($sSql, 'ID');
  176. }
  177. function getKeysBy($aParams, &$aItems)
  178. {
  179. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  180. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  181. $sSelectClause = "`tk`.`ID` AS `id`, `tk`.`IDCategory` AS `category_id`, `tk`.`Key` AS `key`";
  182. if(!isset($aParams['order']) || empty($aParams['order']))
  183. $sOrderClause = " `tk`.`Key` ASC ";
  184. switch($aParams['type']) {
  185. case 'by_ids':
  186. $sSelectClause .= ", `tc`.`Name` AS `category_name`, `ts`.`IDLanguage` AS `language_id`, `tl`.`Name` AS `language_name`, `tl`.`Title` AS `language_title`, `ts`.`String` AS `string`";
  187. $sJoinClause = " LEFT JOIN `sys_localization_categories` AS `tc` ON `tk`.`IDCategory`=`tc`.`ID` LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey` LEFT JOIN `sys_localization_languages` AS `tl` ON `ts`.`IDLanguage`=`tl`.`ID` ";
  188. $sWhereClause .= " AND `tk`.`ID` IN ('" . implode("','", $aParams['value']) . "')";
  189. if(isset($aParams['language']) && (int)$aParams['language'] != 0) {
  190. $aMethod['params'][1] = array(
  191. 'id' => $aParams['language']
  192. );
  193. $sWhereClause .= " AND `tl`.`ID`=:id";
  194. }
  195. break;
  196. case 'by_name':
  197. $aMethod['name'] = 'getRow';
  198. $aMethod['params'][1] = array(
  199. 'key' => $aParams['value']
  200. );
  201. $sWhereClause = " AND `tk`.`Key`=:key ";
  202. break;
  203. case 'by_language_name_key_key':
  204. $aMethod['name'] = 'getAllWithKey';
  205. $aMethod['params'][1] = 'key';
  206. $aMethod['params'][2] = array(
  207. 'name' => $aParams['value']
  208. );
  209. $sSelectClause .= ", `ts`.`String` AS `string` ";
  210. $sJoinClause = " LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey` LEFT JOIN `sys_localization_languages` AS `tl` ON `ts`.`IDLanguage`=`tl`.`ID` ";
  211. $sWhereClause = " AND `tl`.`Name`=:name ";
  212. break;
  213. case 'by_language_id_key_key':
  214. $aMethod['name'] = 'getAllWithKey';
  215. $aMethod['params'][1] = 'key';
  216. $aMethod['params'][2] = array(
  217. 'language_id' => (int)$aParams['value']
  218. );
  219. $sSelectClause .= ", `ts`.`String` AS `string` ";
  220. $sJoinClause = " LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey` ";
  221. $sWhereClause = " AND `ts`.`IDLanguage`=:language_id ";
  222. break;
  223. case 'autogenerated':
  224. $sWhereClause = " AND `tk`.`Key` REGEXP '.+_[0-9]{8,10}$' ";
  225. if(isset($aParams['empty']) && $aParams['empty'] === true) {
  226. $aMethod['params'][1] = array(
  227. 'language_id' => (int)$aParams['language_id']
  228. );
  229. $sJoinClause = " LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey` AND `ts`.`IDLanguage`=:language_id ";
  230. $sWhereClause .= " AND ISNULL(`ts`.`String`)";
  231. }
  232. break;
  233. case 'search':
  234. $aMethod['name'] = 'getColumn';
  235. $aMethod['params'][1] = array();
  236. $sSelectClause = "DISTINCT `tk`.`ID` AS `id`";
  237. $sJoinClause = " LEFT JOIN `sys_localization_categories` AS `tc` ON `tk`.`IDCategory`=`tc`.`ID` LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey` LEFT JOIN `sys_localization_languages` AS `tl` ON `ts`.`IDLanguage`=`tl`.`ID` ";
  238. if(isset($aParams['category']) && (int)$aParams['category'] != 0) {
  239. $aMethod['params'][1]['category_id'] = $aParams['category'];
  240. $sWhereClause .= " AND `tc`.`ID`=:category_id";
  241. }
  242. if(isset($aParams['language']) && (int)$aParams['language'] != 0) {
  243. $aMethod['params'][1]['language_id'] = $aParams['language'];
  244. $sWhereClause .= " AND `tl`.`ID`=:language_id";
  245. }
  246. if(isset($aParams['keyword']) && $aParams['keyword'] != '' && $aParams['keyword'] != _t('_adm_pgt_txt_keyword')) {
  247. $aMethod['params'][1]['key'] = '%' . $aParams['keyword'] . '%';
  248. $aMethod['params'][1]['string'] = '%' . $aParams['keyword'] . '%';
  249. $sWhereClause .= " AND (`tk`.`Key` LIKE :key OR `ts`.`String` LIKE :string)";
  250. }
  251. $iStart = 0;
  252. if(isset($aParams['start']) && (int)$aParams['start'] != 0)
  253. $iStart = $aParams['start'];
  254. $iLength = 20;
  255. if(isset($aParams['length']) && (int)$aParams['length'] != 0)
  256. $iLength = $aParams['length'];
  257. $aMethod['params'][1]['start'] = $iStart;
  258. $aMethod['params'][1]['length'] = $iLength;
  259. $sLimitClause .= " LIMIT :start, :length";
  260. break;
  261. case 'counter_by_category':
  262. $aMethod['name'] = 'getPairs';
  263. $aMethod['params'][1] = 'category_id';
  264. $aMethod['params'][2] = 'counter';
  265. $sSelectClause .= ", COUNT(*) AS `counter`";
  266. $sGroupClause = "GROUP BY `tk`.`IDCategory`";
  267. break;
  268. }
  269. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  270. FROM `sys_localization_keys` AS `tk`" . $sJoinClause . "
  271. WHERE 1" . $sWhereClause . " " . $sGroupClause . "
  272. ORDER BY" . $sOrderClause . $sLimitClause;
  273. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  274. return $aItems !== false;
  275. }
  276. function getCategories()
  277. {
  278. $sSql = "SELECT `ID`, `Name` FROM `sys_localization_categories` ORDER BY `Name`";
  279. return $this->getPairs($sSql, 'ID', 'Name');
  280. }
  281. function getCategoriesBy($aParams, &$aItems, $bReturnCount = true)
  282. {
  283. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  284. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  285. $sSelectClause = "`tc`.`ID` AS `id`, `tc`.`Name` AS `name`";
  286. if(!isset($aParams['order']) || empty($aParams['order']))
  287. $sOrderClause = " `tc`.`ID` ASC ";
  288. switch($aParams['type']) {
  289. case 'id_by_name':
  290. $aMethod['name'] = 'getOne';
  291. $aMethod['params'][1] = array(
  292. 'name' => $aParams['value']
  293. );
  294. $sSelectClause = "`tc`.`ID` AS `id`";
  295. $sWhereClause = " AND `tc`.`Name`=:name";
  296. break;
  297. case 'by_name':
  298. $aMethod['name'] = 'getRow';
  299. $aMethod['params'][1] = array(
  300. 'name' => $aParams['value']
  301. );
  302. $sWhereClause = " AND `tc`.`Name`=:name";
  303. break;
  304. case 'all':
  305. $sOrderClause = " `tc`.`Name` ASC ";
  306. break;
  307. }
  308. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  309. " . $sSelectClause . "
  310. FROM `sys_localization_categories` AS `tc`" . $sJoinClause . "
  311. WHERE 1" . $sWhereClause . "
  312. ORDER BY" . $sOrderClause . $sLimitClause;
  313. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  314. if(!$bReturnCount)
  315. return !empty($aItems);
  316. return (int)$this->getOne("SELECT FOUND_ROWS()");
  317. }
  318. function getStringsBy($aParams, &$aItems, $bReturnCount = true)
  319. {
  320. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  321. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  322. if(!isset($aParams['order']) || empty($aParams['order']))
  323. $sOrderClause = "`ts`.`IDKey` ASC";
  324. switch($aParams['type']) {
  325. case 'by_key_language_id':
  326. $aMethod['name'] = 'getRow';
  327. $aMethod['params'][1] = array(
  328. 'key' => $aParams['key'],
  329. 'language_id' => $aParams['language_id']
  330. );
  331. $sJoinClause = "LEFT JOIN `sys_localization_keys` AS `tk` ON `ts`.`IDKey`=`tk`.`ID`";
  332. $sWhereClause .= "AND `tk`.`Key`=:key AND `ts`.`IDLanguage`=:language_id";
  333. break;
  334. case 'all_by_key_key_language_id':
  335. $aMethod['name'] = 'getAllWithKey';
  336. $aMethod['params'][1] = 'language_id';
  337. $aMethod['params'][2] = array(
  338. 'key' => $aParams['value']
  339. );
  340. $sJoinClause = "LEFT JOIN `sys_localization_keys` AS `tk` ON `ts`.`IDKey`=`tk`.`ID`";
  341. $sWhereClause .= "AND `tk`.`Key`=:key";
  342. break;
  343. }
  344. $aMethod['params'][0] = "SELECT SQL_CALC_FOUND_ROWS
  345. `ts`.`IDKey` AS `key_id`,
  346. `ts`.`IDLanguage` AS `language_id`,
  347. `ts`.`String` AS `string` " . $sSelectClause . "
  348. FROM `sys_localization_strings` AS `ts` " . $sJoinClause . "
  349. WHERE 1 " . $sWhereClause . "
  350. ORDER BY " . $sOrderClause . " " . $sLimitClause;
  351. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  352. if(!$bReturnCount)
  353. return count($aItems) > 0;
  354. return (int)$this->getOne("SELECT FOUND_ROWS()");
  355. }
  356. }
  357. /** @} */