BxDolLanguagesQuery.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 'search':
  224. $aMethod['name'] = 'getColumn';
  225. $aMethod['params'][1] = array();
  226. $sSelectClause = "DISTINCT `tk`.`ID` AS `id`";
  227. $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` ";
  228. if(isset($aParams['category']) && (int)$aParams['category'] != 0) {
  229. $aMethod['params'][1]['category_id'] = $aParams['category'];
  230. $sWhereClause .= " AND `tc`.`ID`=:category_id";
  231. }
  232. if(isset($aParams['language']) && (int)$aParams['language'] != 0) {
  233. $aMethod['params'][1]['language_id'] = $aParams['language'];
  234. $sWhereClause .= " AND `tl`.`ID`=:language_id";
  235. }
  236. if(isset($aParams['keyword']) && $aParams['keyword'] != '' && $aParams['keyword'] != _t('_adm_pgt_txt_keyword')) {
  237. $aMethod['params'][1]['key'] = '%' . $aParams['keyword'] . '%';
  238. $aMethod['params'][1]['string'] = '%' . $aParams['keyword'] . '%';
  239. $sWhereClause .= " AND (`tk`.`Key` LIKE :key OR `ts`.`String` LIKE :string)";
  240. }
  241. $iStart = 0;
  242. if(isset($aParams['start']) && (int)$aParams['start'] != 0)
  243. $iStart = $aParams['start'];
  244. $iLength = 20;
  245. if(isset($aParams['length']) && (int)$aParams['length'] != 0)
  246. $iLength = $aParams['length'];
  247. $aMethod['params'][1]['start'] = $iStart;
  248. $aMethod['params'][1]['length'] = $iLength;
  249. $sLimitClause .= " LIMIT :start, :length";
  250. break;
  251. case 'counter_by_category':
  252. $aMethod['name'] = 'getPairs';
  253. $aMethod['params'][1] = 'category_id';
  254. $aMethod['params'][2] = 'counter';
  255. $sSelectClause .= ", COUNT(*) AS `counter`";
  256. $sGroupClause = "GROUP BY `tk`.`IDCategory`";
  257. break;
  258. }
  259. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  260. FROM `sys_localization_keys` AS `tk`" . $sJoinClause . "
  261. WHERE 1" . $sWhereClause . " " . $sGroupClause . "
  262. ORDER BY" . $sOrderClause . $sLimitClause;
  263. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  264. return $aItems !== false;
  265. }
  266. function getCategories()
  267. {
  268. $sSql = "SELECT `ID`, `Name` FROM `sys_localization_categories` ORDER BY `Name`";
  269. return $this->getPairs($sSql, 'ID', 'Name');
  270. }
  271. function getCategoriesBy($aParams, &$aItems, $bReturnCount = true)
  272. {
  273. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  274. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  275. $sSelectClause = "`tc`.`ID` AS `id`, `tc`.`Name` AS `name`";
  276. if(!isset($aParams['order']) || empty($aParams['order']))
  277. $sOrderClause = " `tc`.`ID` ASC ";
  278. switch($aParams['type']) {
  279. case 'id_by_name':
  280. $aMethod['name'] = 'getOne';
  281. $aMethod['params'][1] = array(
  282. 'name' => $aParams['value']
  283. );
  284. $sSelectClause = "`tc`.`ID` AS `id`";
  285. $sWhereClause = " AND `tc`.`Name`=:name";
  286. break;
  287. case 'by_name':
  288. $aMethod['name'] = 'getRow';
  289. $aMethod['params'][1] = array(
  290. 'name' => $aParams['value']
  291. );
  292. $sWhereClause = " AND `tc`.`Name`=:name";
  293. break;
  294. case 'all':
  295. $sOrderClause = " `tc`.`Name` ASC ";
  296. break;
  297. }
  298. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  299. " . $sSelectClause . "
  300. FROM `sys_localization_categories` AS `tc`" . $sJoinClause . "
  301. WHERE 1" . $sWhereClause . "
  302. ORDER BY" . $sOrderClause . $sLimitClause;
  303. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  304. if(!$bReturnCount)
  305. return !empty($aItems);
  306. return (int)$this->getOne("SELECT FOUND_ROWS()");
  307. }
  308. function getStringsBy($aParams, &$aItems, $bReturnCount = true)
  309. {
  310. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  311. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  312. if(!isset($aParams['order']) || empty($aParams['order']))
  313. $sOrderClause = "`ts`.`IDKey` ASC";
  314. switch($aParams['type']) {
  315. case 'by_key_language_id':
  316. $aMethod['name'] = 'getRow';
  317. $aMethod['params'][1] = array(
  318. 'key' => $aParams['key'],
  319. 'language_id' => $aParams['language_id']
  320. );
  321. $sJoinClause = "LEFT JOIN `sys_localization_keys` AS `tk` ON `ts`.`IDKey`=`tk`.`ID`";
  322. $sWhereClause .= "AND `tk`.`Key`=:key AND `ts`.`IDLanguage`=:language_id";
  323. break;
  324. case 'all_by_key_key_language_id':
  325. $aMethod['name'] = 'getAllWithKey';
  326. $aMethod['params'][1] = 'language_id';
  327. $aMethod['params'][2] = array(
  328. 'key' => $aParams['value']
  329. );
  330. $sJoinClause = "LEFT JOIN `sys_localization_keys` AS `tk` ON `ts`.`IDKey`=`tk`.`ID`";
  331. $sWhereClause .= "AND `tk`.`Key`=:key";
  332. break;
  333. }
  334. $aMethod['params'][0] = "SELECT SQL_CALC_FOUND_ROWS
  335. `ts`.`IDKey` AS `key_id`,
  336. `ts`.`IDLanguage` AS `language_id`,
  337. `ts`.`String` AS `string` " . $sSelectClause . "
  338. FROM `sys_localization_strings` AS `ts` " . $sJoinClause . "
  339. WHERE 1 " . $sWhereClause . "
  340. ORDER BY " . $sOrderClause . " " . $sLimitClause;
  341. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  342. if(!$bReturnCount)
  343. return count($aItems) > 0;
  344. return (int)$this->getOne("SELECT FOUND_ROWS()");
  345. }
  346. }
  347. /** @} */