BxDolLanguagesQuery.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. if(isset($aParams['language']) && (int)$aParams['language'] != 0) {
  131. $aMethod['params'][2] = array(
  132. 'id' => $aParams['language']
  133. );
  134. $sWhereClause .= " AND `tl`.`ID`=:id";
  135. }
  136. break;
  137. }
  138. $aMethod['params'][0] = "SELECT SQL_CALC_FOUND_ROWS
  139. `tl`.`ID` AS `id`,
  140. `tl`.`Name` AS `name`,
  141. `tl`.`Flag` AS `flag`,
  142. `tl`.`Title` AS `title`,
  143. `tl`.`Enabled` AS `enabled`" . $sSelectClause . "
  144. FROM `sys_localization_languages` AS `tl`" . $sJoinClause . "
  145. WHERE 1" . $sWhereClause . "
  146. ORDER BY" . $sOrderClause . $sLimitClause;
  147. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  148. if(!$bReturnCount)
  149. return count($aItems) > 0;
  150. return (int)$this->getOne("SELECT FOUND_ROWS()");
  151. }
  152. function getKeys()
  153. {
  154. $sSql = "SELECT `ID`, `IDCategory`, `Key` FROM `sys_localization_keys`";
  155. return $this->getAllWithKey($sSql, 'ID');
  156. }
  157. function getKeysBy($aParams, &$aItems)
  158. {
  159. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  160. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  161. $sSelectClause = "`tk`.`ID` AS `id`, `tk`.`IDCategory` AS `category_id`, `tk`.`Key` AS `key`";
  162. if(!isset($aParams['order']) || empty($aParams['order']))
  163. $sOrderClause = " `tk`.`Key` ASC ";
  164. switch($aParams['type']) {
  165. case 'by_ids':
  166. $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`";
  167. $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` ";
  168. $sWhereClause .= " AND `tk`.`ID` IN ('" . implode("','", $aParams['value']) . "')";
  169. if(isset($aParams['language']) && (int)$aParams['language'] != 0) {
  170. $aMethod['params'][1] = array(
  171. 'id' => $aParams['language']
  172. );
  173. $sWhereClause .= " AND `tl`.`ID`=:id";
  174. }
  175. break;
  176. case 'by_name':
  177. $aMethod['name'] = 'getRow';
  178. $aMethod['params'][1] = array(
  179. 'key' => $aParams['value']
  180. );
  181. $sWhereClause = " AND `tk`.`Key`=:key ";
  182. break;
  183. case 'by_language_name_key_key':
  184. $aMethod['name'] = 'getAllWithKey';
  185. $aMethod['params'][1] = 'key';
  186. $aMethod['params'][2] = array(
  187. 'name' => $aParams['value']
  188. );
  189. $sSelectClause .= ", `ts`.`String` AS `string` ";
  190. $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` ";
  191. $sWhereClause = " AND `tl`.`Name`=:name ";
  192. break;
  193. case 'by_language_id_key_key':
  194. $aMethod['name'] = 'getAllWithKey';
  195. $aMethod['params'][1] = 'key';
  196. $aMethod['params'][2] = array(
  197. 'language_id' => (int)$aParams['value']
  198. );
  199. $sSelectClause .= ", `ts`.`String` AS `string` ";
  200. $sJoinClause = " LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey` ";
  201. $sWhereClause = " AND `ts`.`IDLanguage`=:language_id ";
  202. break;
  203. case 'search':
  204. $aMethod['name'] = 'getColumn';
  205. $aMethod['params'][1] = array();
  206. $sSelectClause = "DISTINCT `tk`.`ID` AS `id`";
  207. $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` ";
  208. if(isset($aParams['category']) && (int)$aParams['category'] != 0) {
  209. $aMethod['params'][1]['category_id'] = $aParams['category'];
  210. $sWhereClause .= " AND `tc`.`ID`=:category_id";
  211. }
  212. if(isset($aParams['language']) && (int)$aParams['language'] != 0) {
  213. $aMethod['params'][1]['language_id'] = $aParams['language'];
  214. $sWhereClause .= " AND `tl`.`ID`=:language_id";
  215. }
  216. if(isset($aParams['keyword']) && $aParams['keyword'] != '' && $aParams['keyword'] != _t('_adm_pgt_txt_keyword')) {
  217. $aMethod['params'][1]['key'] = '%' . $aParams['keyword'] . '%';
  218. $aMethod['params'][1]['string'] = '%' . $aParams['keyword'] . '%';
  219. $sWhereClause .= " AND (`tk`.`Key` LIKE :key OR `ts`.`String` LIKE :string)";
  220. }
  221. $iStart = 0;
  222. if(isset($aParams['start']) && (int)$aParams['start'] != 0)
  223. $iStart = $aParams['start'];
  224. $iLength = 20;
  225. if(isset($aParams['length']) && (int)$aParams['length'] != 0)
  226. $iLength = $aParams['length'];
  227. $aMethod['params'][1]['start'] = $iStart;
  228. $aMethod['params'][1]['length'] = $iLength;
  229. $sLimitClause .= " LIMIT :start, :length";
  230. break;
  231. case 'counter_by_category':
  232. $aMethod['name'] = 'getPairs';
  233. $aMethod['params'][1] = 'category_id';
  234. $aMethod['params'][2] = 'counter';
  235. $sSelectClause .= ", COUNT(*) AS `counter`";
  236. $sGroupClause = "GROUP BY `tk`.`IDCategory`";
  237. break;
  238. }
  239. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  240. FROM `sys_localization_keys` AS `tk`" . $sJoinClause . "
  241. WHERE 1" . $sWhereClause . " " . $sGroupClause . "
  242. ORDER BY" . $sOrderClause . $sLimitClause;
  243. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  244. return $aItems !== false;
  245. }
  246. function getCategories()
  247. {
  248. $sSql = "SELECT `ID`, `Name` FROM `sys_localization_categories` ORDER BY `Name`";
  249. return $this->getPairs($sSql, 'ID', 'Name');
  250. }
  251. function getCategoriesBy($aParams, &$aItems, $bReturnCount = true)
  252. {
  253. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  254. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  255. $sSelectClause = "`tc`.`ID` AS `id`, `tc`.`Name` AS `name`";
  256. if(!isset($aParams['order']) || empty($aParams['order']))
  257. $sOrderClause = " `tc`.`ID` ASC ";
  258. switch($aParams['type']) {
  259. case 'id_by_name':
  260. $aMethod['name'] = 'getOne';
  261. $aMethod['params'][1] = array(
  262. 'name' => $aParams['value']
  263. );
  264. $sSelectClause = "`tc`.`ID` AS `id`";
  265. $sWhereClause = " AND `tc`.`Name`=:name";
  266. break;
  267. case 'by_name':
  268. $aMethod['name'] = 'getRow';
  269. $aMethod['params'][1] = array(
  270. 'name' => $aParams['value']
  271. );
  272. $sWhereClause = " AND `tc`.`Name`=:name";
  273. break;
  274. case 'all':
  275. $sOrderClause = " `tc`.`Name` ASC ";
  276. break;
  277. }
  278. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  279. " . $sSelectClause . "
  280. FROM `sys_localization_categories` AS `tc`" . $sJoinClause . "
  281. WHERE 1" . $sWhereClause . "
  282. ORDER BY" . $sOrderClause . $sLimitClause;
  283. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  284. if(!$bReturnCount)
  285. return !empty($aItems);
  286. return (int)$this->getOne("SELECT FOUND_ROWS()");
  287. }
  288. function getStringsBy($aParams, &$aItems, $bReturnCount = true)
  289. {
  290. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  291. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  292. if(!isset($aParams['order']) || empty($aParams['order']))
  293. $sOrderClause = "`ts`.`IDKey` ASC";
  294. switch($aParams['type']) {
  295. case 'by_key_language_id':
  296. $aMethod['name'] = 'getRow';
  297. $aMethod['params'][1] = array(
  298. 'key' => $aParams['key'],
  299. 'language_id' => $aParams['language_id']
  300. );
  301. $sJoinClause = "LEFT JOIN `sys_localization_keys` AS `tk` ON `ts`.`IDKey`=`tk`.`ID`";
  302. $sWhereClause .= "AND `tk`.`Key`=:key AND `ts`.`IDLanguage`=:language_id";
  303. break;
  304. case 'all_by_key_key_language_id':
  305. $aMethod['name'] = 'getAllWithKey';
  306. $aMethod['params'][1] = 'language_id';
  307. $aMethod['params'][2] = array(
  308. 'key' => $aParams['value']
  309. );
  310. $sJoinClause = "LEFT JOIN `sys_localization_keys` AS `tk` ON `ts`.`IDKey`=`tk`.`ID`";
  311. $sWhereClause .= "AND `tk`.`Key`=:key";
  312. break;
  313. }
  314. $aMethod['params'][0] = "SELECT SQL_CALC_FOUND_ROWS
  315. `ts`.`IDKey` AS `key_id`,
  316. `ts`.`IDLanguage` AS `language_id`,
  317. `ts`.`String` AS `string` " . $sSelectClause . "
  318. FROM `sys_localization_strings` AS `ts` " . $sJoinClause . "
  319. WHERE 1 " . $sWhereClause . "
  320. ORDER BY " . $sOrderClause . " " . $sLimitClause;
  321. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  322. if(!$bReturnCount)
  323. return count($aItems) > 0;
  324. return (int)$this->getOne("SELECT FOUND_ROWS()");
  325. }
  326. }
  327. /** @} */