BxDolLanguagesQuery.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 getLanguageFlag($sName, $bFromCache = true)
  43. {
  44. return $this->getLanguageField($sName, 'Flag', $bFromCache);
  45. }
  46. function getLanguageDirection($sName, $bFromCache = true)
  47. {
  48. return $this->getLanguageField($sName, 'Direction', $bFromCache);
  49. }
  50. function getLangCountryCode($sName, $bFromCache = true)
  51. {
  52. return $this->getLanguageField($sName, 'LanguageCountry', $bFromCache);
  53. }
  54. protected function getLanguageField($sName, $sField, $bFromCache = true)
  55. {
  56. $sSql = $this->prepare("SELECT `" . $sField . "` FROM `sys_localization_languages` WHERE `Name`=? AND `Enabled`='1' LIMIT 1", $sName);
  57. if($bFromCache)
  58. return $this->fromCache('checkLangExists_' . $sName . '_' . $sField, 'getOne', $sSql);
  59. else
  60. return $this->getOne($sSql);
  61. }
  62. protected function getLanguageFieldById($iId, $sField, $bFromCache = true)
  63. {
  64. $sSql = $this->prepare("SELECT `" . $sField . "` FROM `sys_localization_languages` WHERE `ID`=? AND `Enabled`='1' LIMIT 1", $iId);
  65. if($bFromCache)
  66. return $this->fromCache('checkLangExists_' . $iId . '_' . $sField, 'getOne', $sSql);
  67. else
  68. return $this->getOne($sSql);
  69. }
  70. function getLanguages($bIdAsKey = false, $bActiveOnly = false)
  71. {
  72. return $this->getLanguagesWithKey($bIdAsKey ? 'ID' : 'Name', $bActiveOnly);
  73. }
  74. function getLanguagesWithKey($sKey = 'Name', $bActiveOnly = false)
  75. {
  76. $sSql = "SELECT * FROM `sys_localization_languages` WHERE 1 " . ($bActiveOnly ? " AND `Enabled`='1'" : "") . " ORDER BY `Title` ASC";
  77. return $this->getPairs($sSql, $sKey, 'Title');
  78. }
  79. function getLanguagesBy($aParams, &$aItems, $bReturnCount = true)
  80. {
  81. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  82. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  83. if(!isset($aParams['order']) || empty($aParams['order']))
  84. $sOrderClause = " `tl`.`ID` ASC ";
  85. switch($aParams['type']) {
  86. case 'by_id':
  87. $aMethod['name'] = 'getRow';
  88. $aMethod['params'][1] = array(
  89. 'id' => $aParams['value']
  90. );
  91. $sWhereClause .= " AND `tl`.`ID`=:id";
  92. break;
  93. case 'by_name':
  94. $aMethod['name'] = 'getRow';
  95. $aMethod['params'][1] = array(
  96. 'name' => $aParams['value']
  97. );
  98. $sWhereClause .= " AND `tl`.`Name`=:name";
  99. break;
  100. case 'default':
  101. $aMethod['name'] = 'getRow';
  102. $aMethod['params'][1] = array(
  103. 'name' => getParam('lang_default')
  104. );
  105. $sWhereClause .= " AND `tl`.`Name`=:name";
  106. break;
  107. case 'active':
  108. $sWhereClause = " AND `tl`.`Enabled`='1'";
  109. break;
  110. case 'all':
  111. $sOrderClause = " `tl`.`Name` ASC ";
  112. break;
  113. case 'all_by_id':
  114. $aMethod['params'][1] = array(
  115. 'id' => $aParams['value']
  116. );
  117. $sWhereClause .= " AND `tl`.`ID`=:id";
  118. $sOrderClause = " `tl`.`Name` ASC ";
  119. break;
  120. case 'all_by_name':
  121. $aMethod['params'][1] = array(
  122. 'name' => $aParams['value']
  123. );
  124. $sWhereClause .= " AND `tl`.`Name`=:name";
  125. $sOrderClause = " `tl`.`Name` ASC ";
  126. break;
  127. case 'all_key_id':
  128. $aMethod['name'] = 'getAllWithKey';
  129. $aMethod['params'][1] = 'id';
  130. $aMethod['params'][2] = array();
  131. if(!empty($aParams['language'])) {
  132. $aMethod['params'][2]['id'] = $aParams['language'];
  133. $sWhereClause .= " AND `tl`.`ID`=:id";
  134. }
  135. if(isset($aParams['enabled'])) {
  136. $aMethod['params'][2]['enabled'] = (int)$aParams['enabled'];
  137. $sWhereClause .= " AND `tl`.`Enabled`=:enabled";
  138. }
  139. break;
  140. case 'all_key_name':
  141. $aMethod['name'] = 'getAllWithKey';
  142. $aMethod['params'][1] = 'name';
  143. $aMethod['params'][2] = array();
  144. if(!empty($aParams['language'])) {
  145. $aMethod['params'][2]['name'] = $aParams['language'];
  146. $sWhereClause .= " AND `tl`.`Name`=:name";
  147. }
  148. if(isset($aParams['enabled'])) {
  149. $aMethod['params'][2]['enabled'] = (int)$aParams['enabled'];
  150. $sWhereClause .= " AND `tl`.`Enabled`=:enabled";
  151. }
  152. break;
  153. }
  154. $aMethod['params'][0] = "SELECT SQL_CALC_FOUND_ROWS
  155. `tl`.`ID` AS `id`,
  156. `tl`.`Name` AS `name`,
  157. `tl`.`Flag` AS `flag`,
  158. `tl`.`Title` AS `title`,
  159. `tl`.`Enabled` AS `enabled`" . $sSelectClause . "
  160. FROM `sys_localization_languages` AS `tl`" . $sJoinClause . "
  161. WHERE 1" . $sWhereClause . "
  162. ORDER BY" . $sOrderClause . $sLimitClause;
  163. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  164. if(!$bReturnCount)
  165. return count($aItems) > 0;
  166. return (int)$this->getOne("SELECT FOUND_ROWS()");
  167. }
  168. function getKeys()
  169. {
  170. $sSql = "SELECT `ID`, `IDCategory`, `Key` FROM `sys_localization_keys`";
  171. return $this->getAllWithKey($sSql, 'ID');
  172. }
  173. function getKeysBy($aParams, &$aItems)
  174. {
  175. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  176. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  177. $sSelectClause = "`tk`.`ID` AS `id`, `tk`.`IDCategory` AS `category_id`, `tk`.`Key` AS `key`";
  178. if(!isset($aParams['order']) || empty($aParams['order']))
  179. $sOrderClause = " `tk`.`Key` ASC ";
  180. switch($aParams['type']) {
  181. case 'by_ids':
  182. $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`";
  183. $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` ";
  184. $sWhereClause .= " AND `tk`.`ID` IN ('" . implode("','", $aParams['value']) . "')";
  185. if(isset($aParams['language']) && (int)$aParams['language'] != 0) {
  186. $aMethod['params'][1] = array(
  187. 'id' => $aParams['language']
  188. );
  189. $sWhereClause .= " AND `tl`.`ID`=:id";
  190. }
  191. break;
  192. case 'by_name':
  193. $aMethod['name'] = 'getRow';
  194. $aMethod['params'][1] = array(
  195. 'key' => $aParams['value']
  196. );
  197. $sWhereClause = " AND `tk`.`Key`=:key ";
  198. break;
  199. case 'by_language_name_key_key':
  200. $aMethod['name'] = 'getAllWithKey';
  201. $aMethod['params'][1] = 'key';
  202. $aMethod['params'][2] = array(
  203. 'name' => $aParams['value']
  204. );
  205. $sSelectClause .= ", `ts`.`String` AS `string` ";
  206. $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` ";
  207. $sWhereClause = " AND `tl`.`Name`=:name ";
  208. break;
  209. case 'by_language_id_key_key':
  210. $aMethod['name'] = 'getAllWithKey';
  211. $aMethod['params'][1] = 'key';
  212. $aMethod['params'][2] = array(
  213. 'language_id' => (int)$aParams['value']
  214. );
  215. $sSelectClause .= ", `ts`.`String` AS `string` ";
  216. $sJoinClause = " LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey` ";
  217. $sWhereClause = " AND `ts`.`IDLanguage`=:language_id ";
  218. break;
  219. case 'search':
  220. $aMethod['name'] = 'getColumn';
  221. $aMethod['params'][1] = array();
  222. $sSelectClause = "DISTINCT `tk`.`ID` AS `id`";
  223. $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` ";
  224. if(isset($aParams['category']) && (int)$aParams['category'] != 0) {
  225. $aMethod['params'][1]['category_id'] = $aParams['category'];
  226. $sWhereClause .= " AND `tc`.`ID`=:category_id";
  227. }
  228. if(isset($aParams['language']) && (int)$aParams['language'] != 0) {
  229. $aMethod['params'][1]['language_id'] = $aParams['language'];
  230. $sWhereClause .= " AND `tl`.`ID`=:language_id";
  231. }
  232. if(isset($aParams['keyword']) && $aParams['keyword'] != '' && $aParams['keyword'] != _t('_adm_pgt_txt_keyword')) {
  233. $aMethod['params'][1]['key'] = '%' . $aParams['keyword'] . '%';
  234. $aMethod['params'][1]['string'] = '%' . $aParams['keyword'] . '%';
  235. $sWhereClause .= " AND (`tk`.`Key` LIKE :key OR `ts`.`String` LIKE :string)";
  236. }
  237. $iStart = 0;
  238. if(isset($aParams['start']) && (int)$aParams['start'] != 0)
  239. $iStart = $aParams['start'];
  240. $iLength = 20;
  241. if(isset($aParams['length']) && (int)$aParams['length'] != 0)
  242. $iLength = $aParams['length'];
  243. $aMethod['params'][1]['start'] = $iStart;
  244. $aMethod['params'][1]['length'] = $iLength;
  245. $sLimitClause .= " LIMIT :start, :length";
  246. break;
  247. case 'counter_by_category':
  248. $aMethod['name'] = 'getPairs';
  249. $aMethod['params'][1] = 'category_id';
  250. $aMethod['params'][2] = 'counter';
  251. $sSelectClause .= ", COUNT(*) AS `counter`";
  252. $sGroupClause = "GROUP BY `tk`.`IDCategory`";
  253. break;
  254. }
  255. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  256. FROM `sys_localization_keys` AS `tk`" . $sJoinClause . "
  257. WHERE 1" . $sWhereClause . " " . $sGroupClause . "
  258. ORDER BY" . $sOrderClause . $sLimitClause;
  259. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  260. return $aItems !== false;
  261. }
  262. function getCategories()
  263. {
  264. $sSql = "SELECT `ID`, `Name` FROM `sys_localization_categories` ORDER BY `Name`";
  265. return $this->getPairs($sSql, 'ID', 'Name');
  266. }
  267. function getCategoriesBy($aParams, &$aItems, $bReturnCount = true)
  268. {
  269. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  270. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  271. $sSelectClause = "`tc`.`ID` AS `id`, `tc`.`Name` AS `name`";
  272. if(!isset($aParams['order']) || empty($aParams['order']))
  273. $sOrderClause = " `tc`.`ID` ASC ";
  274. switch($aParams['type']) {
  275. case 'id_by_name':
  276. $aMethod['name'] = 'getOne';
  277. $aMethod['params'][1] = array(
  278. 'name' => $aParams['value']
  279. );
  280. $sSelectClause = "`tc`.`ID` AS `id`";
  281. $sWhereClause = " AND `tc`.`Name`=:name";
  282. break;
  283. case 'by_name':
  284. $aMethod['name'] = 'getRow';
  285. $aMethod['params'][1] = array(
  286. 'name' => $aParams['value']
  287. );
  288. $sWhereClause = " AND `tc`.`Name`=:name";
  289. break;
  290. case 'all':
  291. $sOrderClause = " `tc`.`Name` ASC ";
  292. break;
  293. }
  294. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  295. " . $sSelectClause . "
  296. FROM `sys_localization_categories` AS `tc`" . $sJoinClause . "
  297. WHERE 1" . $sWhereClause . "
  298. ORDER BY" . $sOrderClause . $sLimitClause;
  299. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  300. if(!$bReturnCount)
  301. return !empty($aItems);
  302. return (int)$this->getOne("SELECT FOUND_ROWS()");
  303. }
  304. function getStringsBy($aParams, &$aItems, $bReturnCount = true)
  305. {
  306. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  307. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  308. if(!isset($aParams['order']) || empty($aParams['order']))
  309. $sOrderClause = "`ts`.`IDKey` ASC";
  310. switch($aParams['type']) {
  311. case 'by_key_language_id':
  312. $aMethod['name'] = 'getRow';
  313. $aMethod['params'][1] = array(
  314. 'key' => $aParams['key'],
  315. 'language_id' => $aParams['language_id']
  316. );
  317. $sJoinClause = "LEFT JOIN `sys_localization_keys` AS `tk` ON `ts`.`IDKey`=`tk`.`ID`";
  318. $sWhereClause .= "AND `tk`.`Key`=:key AND `ts`.`IDLanguage`=:language_id";
  319. break;
  320. case 'all_by_key_key_language_id':
  321. $aMethod['name'] = 'getAllWithKey';
  322. $aMethod['params'][1] = 'language_id';
  323. $aMethod['params'][2] = array(
  324. 'key' => $aParams['value']
  325. );
  326. $sJoinClause = "LEFT JOIN `sys_localization_keys` AS `tk` ON `ts`.`IDKey`=`tk`.`ID`";
  327. $sWhereClause .= "AND `tk`.`Key`=:key";
  328. break;
  329. }
  330. $aMethod['params'][0] = "SELECT SQL_CALC_FOUND_ROWS
  331. `ts`.`IDKey` AS `key_id`,
  332. `ts`.`IDLanguage` AS `language_id`,
  333. `ts`.`String` AS `string` " . $sSelectClause . "
  334. FROM `sys_localization_strings` AS `ts` " . $sJoinClause . "
  335. WHERE 1 " . $sWhereClause . "
  336. ORDER BY " . $sOrderClause . " " . $sLimitClause;
  337. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  338. if(!$bReturnCount)
  339. return count($aItems) > 0;
  340. return (int)$this->getOne("SELECT FOUND_ROWS()");
  341. }
  342. }
  343. /** @} */