1
0

BxDolLanguagesQuery.php 14 KB

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