BxDolLanguagesQuery.php 12 KB

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