BxDolLanguagesQuery.php 12 KB

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