BxDolLanguagesQuery.php 14 KB

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