BxDolLanguagesQuery.php 15 KB

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