BxDolLanguagesQuery.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. class BxDolLanguagesQuery extends BxDolDb implements iBxDolSingleton
  10. {
  11. 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. $sWhereClause .= $this->prepare(" AND `tl`.`ID`=?", $aParams['value']);
  69. break;
  70. case 'by_name':
  71. $aMethod['name'] = 'getRow';
  72. $sWhereClause .= $this->prepare(" AND `tl`.`Name`=?", $aParams['value']);
  73. break;
  74. case 'default':
  75. $aMethod['name'] = 'getRow';
  76. $sWhereClause .= $this->prepare(" AND `tl`.`Name`=?", getParam('lang_default'));
  77. break;
  78. case 'active':
  79. $sWhereClause = " AND `tl`.`Enabled`='1'";
  80. break;
  81. case 'all':
  82. $sOrderClause = " `tl`.`Name` ASC ";
  83. break;
  84. case 'all_by_id':
  85. $sWhereClause .= $this->prepare(" AND `tl`.`ID`=?", $aParams['value']);
  86. $sOrderClause = " `tl`.`Name` ASC ";
  87. break;
  88. case 'all_by_name':
  89. $sWhereClause .= $this->prepare(" AND `tl`.`Name`=?", $aParams['value']);
  90. $sOrderClause = " `tl`.`Name` ASC ";
  91. break;
  92. case 'all_key_id':
  93. $aMethod['name'] = 'getAllWithKey';
  94. $aMethod['params'][1] = 'id';
  95. if(isset($aParams['language']) && (int)$aParams['language'] != 0)
  96. $sWhereClause .= $this->prepare(" AND `tl`.`ID`=?", $aParams['language']);
  97. break;
  98. }
  99. $aMethod['params'][0] = "SELECT SQL_CALC_FOUND_ROWS
  100. `tl`.`ID` AS `id`,
  101. `tl`.`Name` AS `name`,
  102. `tl`.`Flag` AS `flag`,
  103. `tl`.`Title` AS `title`,
  104. `tl`.`Enabled` AS `enabled`" . $sSelectClause . "
  105. FROM `sys_localization_languages` AS `tl`" . $sJoinClause . "
  106. WHERE 1" . $sWhereClause . "
  107. ORDER BY" . $sOrderClause . $sLimitClause;
  108. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  109. if(!$bReturnCount)
  110. return count($aItems) > 0;
  111. return (int)$this->getOne("SELECT FOUND_ROWS()");
  112. }
  113. function getKeys()
  114. {
  115. $sSql = "SELECT `ID`, `IDCategory`, `Key` FROM `sys_localization_keys`";
  116. return $this->getAllWithKey($sSql, 'ID');
  117. }
  118. function getKeysBy($aParams, &$aItems)
  119. {
  120. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  121. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  122. $sSelectClause = "`tk`.`ID` AS `id`, `tk`.`IDCategory` AS `category_id`, `tk`.`Key` AS `key`";
  123. if(!isset($aParams['order']) || empty($aParams['order']))
  124. $sOrderClause = " `tk`.`Key` ASC ";
  125. switch($aParams['type']) {
  126. case 'by_ids':
  127. $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`";
  128. $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` ";
  129. $sWhereClause .= " AND `tk`.`ID` IN ('" . implode("','", $aParams['value']) . "')";
  130. if(isset($aParams['language']) && (int)$aParams['language'] != 0)
  131. $sWhereClause .= $this->prepare(" AND `tl`.`ID`=?", $aParams['language']);
  132. break;
  133. case 'by_name':
  134. $aMethod['name'] = 'getRow';
  135. $sWhereClause = $this->prepare(" AND `tk`.`Key`=? ", $aParams['value']);
  136. break;
  137. case 'by_language_name_key_key':
  138. $aMethod['name'] = 'getAllWithKey';
  139. $aMethod['params'][1] = 'key';
  140. $sSelectClause .= ", `ts`.`String` AS `string` ";
  141. $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` ";
  142. $sWhereClause = $this->prepare(" AND `tl`.`Name`=? ", $aParams['value']);
  143. break;
  144. case 'by_language_id_key_key':
  145. $aMethod['name'] = 'getAllWithKey';
  146. $aMethod['params'][1] = 'key';
  147. $sSelectClause .= ", `ts`.`String` AS `string` ";
  148. $sJoinClause = " LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey` ";
  149. $sWhereClause = $this->prepare(" AND `ts`.`IDLanguage`=? ", (int)$aParams['value']);
  150. break;
  151. case 'search':
  152. $aMethod['name'] = 'getColumn';
  153. $sSelectClause = "DISTINCT `tk`.`ID` AS `id`";
  154. $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` ";
  155. if(isset($aParams['category']) && (int)$aParams['category'] != 0)
  156. $sWhereClause .= $this->prepare(" AND `tc`.`ID`=?", $aParams['category']);
  157. if(isset($aParams['language']) && (int)$aParams['language'] != 0)
  158. $sWhereClause .= $this->prepare(" AND `tl`.`ID`=?", $aParams['language']);
  159. if(isset($aParams['keyword']) && $aParams['keyword'] != '' && $aParams['keyword'] != _t('_adm_pgt_txt_keyword'))
  160. $sWhereClause .= $this->prepare(" AND (`tk`.`Key` LIKE ? OR `ts`.`String` LIKE ?)", '%' . $aParams['keyword'] . '%', '%' . $aParams['keyword'] . '%');
  161. $iStart = 0;
  162. if(isset($aParams['start']) && (int)$aParams['start'] != 0)
  163. $iStart = $aParams['start'];
  164. $iLength = 20;
  165. if(isset($aParams['length']) && (int)$aParams['length'] != 0)
  166. $iLength = $aParams['length'];
  167. $sLimitClause .= $this->prepare(" LIMIT ?, ?", $iStart, $iLength);
  168. break;
  169. case 'counter_by_category':
  170. $aMethod['name'] = 'getPairs';
  171. $aMethod['params'][1] = 'category_id';
  172. $aMethod['params'][2] = 'counter';
  173. $sSelectClause .= ", COUNT(*) AS `counter`";
  174. $sGroupClause = "GROUP BY `tk`.`IDCategory`";
  175. break;
  176. }
  177. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  178. FROM `sys_localization_keys` AS `tk`" . $sJoinClause . "
  179. WHERE 1" . $sWhereClause . " " . $sGroupClause . "
  180. ORDER BY" . $sOrderClause . $sLimitClause;
  181. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  182. return $aItems !== false;
  183. }
  184. function getCategories()
  185. {
  186. $sSql = "SELECT `ID`, `Name` FROM `sys_localization_categories` ORDER BY `Name`";
  187. return $this->getPairs($sSql, 'ID', 'Name');
  188. }
  189. function getCategoriesBy($aParams, &$aItems, $bReturnCount = true)
  190. {
  191. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  192. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  193. $sSelectClause = "`tc`.`ID` AS `id`, `tc`.`Name` AS `name`";
  194. if(!isset($aParams['order']) || empty($aParams['order']))
  195. $sOrderClause = " `tc`.`ID` ASC ";
  196. switch($aParams['type']) {
  197. case 'id_by_name':
  198. $aMethod['name'] = 'getOne';
  199. $sSelectClause = "`tc`.`ID` AS `id`";
  200. $sWhereClause = $this->prepare(" AND `tc`.`Name`=?", $aParams['value']);
  201. break;
  202. case 'by_name':
  203. $aMethod['name'] = 'getRow';
  204. $sWhereClause = $this->prepare(" AND `tc`.`Name`=?", $aParams['value']);
  205. break;
  206. case 'all':
  207. $sOrderClause = " `tc`.`Name` ASC ";
  208. break;
  209. }
  210. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  211. " . $sSelectClause . "
  212. FROM `sys_localization_categories` AS `tc`" . $sJoinClause . "
  213. WHERE 1" . $sWhereClause . "
  214. ORDER BY" . $sOrderClause . $sLimitClause;
  215. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  216. if(!$bReturnCount)
  217. return !empty($aItems);
  218. return (int)$this->getOne("SELECT FOUND_ROWS()");
  219. }
  220. function getStringsBy($aParams, &$aItems, $bReturnCount = true)
  221. {
  222. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  223. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  224. if(!isset($aParams['order']) || empty($aParams['order']))
  225. $sOrderClause = "`ts`.`IDKey` ASC";
  226. switch($aParams['type']) {
  227. case 'by_key_language_id':
  228. $aMethod['name'] = 'getRow';
  229. $sJoinClause = "LEFT JOIN `sys_localization_keys` AS `tk` ON `ts`.`IDKey`=`tk`.`ID`";
  230. $sWhereClause .= $this->prepare("AND `tk`.`Key`=? AND `ts`.`IDLanguage`=?", $aParams['key'], $aParams['language_id']);
  231. break;
  232. case 'all_by_key_key_language_id':
  233. $aMethod['name'] = 'getAllWithKey';
  234. $aMethod['params'][1] = 'language_id';
  235. $sJoinClause = "LEFT JOIN `sys_localization_keys` AS `tk` ON `ts`.`IDKey`=`tk`.`ID`";
  236. $sWhereClause .= $this->prepare("AND `tk`.`Key`=?", $aParams['value']);
  237. break;
  238. }
  239. $aMethod['params'][0] = "SELECT SQL_CALC_FOUND_ROWS
  240. `ts`.`IDKey` AS `key_id`,
  241. `ts`.`IDLanguage` AS `language_id`,
  242. `ts`.`String` AS `string` " . $sSelectClause . "
  243. FROM `sys_localization_strings` AS `ts` " . $sJoinClause . "
  244. WHERE 1 " . $sWhereClause . "
  245. ORDER BY " . $sOrderClause . " " . $sLimitClause;
  246. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  247. if(!$bReturnCount)
  248. return count($aItems) > 0;
  249. return (int)$this->getOne("SELECT FOUND_ROWS()");
  250. }
  251. }
  252. /** @} */