1
0

BxDolStudioLanguagesUtilsQuery.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 UnaStudio UNA Studio
  7. * @{
  8. */
  9. class BxDolStudioLanguagesUtilsQuery extends BxDolLanguagesQuery
  10. {
  11. function __construct()
  12. {
  13. parent::__construct();
  14. }
  15. function deleteLanguage($iId = 0)
  16. {
  17. $iId = (int)$iId;
  18. if($iId <= 0)
  19. return false;
  20. $sSql = $this->prepare("SELECT `ID`, `Name` FROM `sys_localization_languages` WHERE `ID`=?", $iId);
  21. $aLanguage = $this->getRow($sSql);
  22. if(empty($aLanguage))
  23. return false;
  24. $sSql = $this->prepare("SELECT COUNT(`IDKey`) FROM `sys_localization_strings` WHERE `IDLanguage`=?", $iId);
  25. $iStrings = (int)$this->getOne();
  26. $sSql = $this->prepare("DELETE FROM `sys_localization_strings` WHERE `IDLanguage`=?", $iId);
  27. if((int)$this->query($sSql) < $iStrings)
  28. return false;
  29. $sSql = $this->prepare("DELETE FROM `sys_localization_languages` WHERE `ID`=?", $iId);
  30. if((int)$this->query($sSql) <= 0)
  31. return false;
  32. @unlink( BX_DIRECTORY_PATH_ROOT . 'langs/lang-' . $aLanguage['Name'] . '.php');
  33. $sSql = $this->prepare("DELETE FROM `sys_email_templates` WHERE `LangID`=?", $iId);
  34. $this->query($sSql);
  35. return true;
  36. }
  37. function addKey($iCategoryId, $sKey)
  38. {
  39. $sSql = $this->prepare("SELECT `ID`, `IDCategory` FROM `sys_localization_keys` WHERE `Key`=? LIMIT 1", $sKey);
  40. $aKey = $this->getRow($sSql);
  41. if(!empty($aKey) && is_array($aKey)) {
  42. if((int)$aKey['IDCategory'] != $iCategoryId)
  43. $this->updateKeys(array('IDCategory' => $iCategoryId), array('ID' => $aKey['ID']));
  44. return $aKey['ID'];
  45. }
  46. $sSql = $this->prepare("INSERT INTO `sys_localization_keys` SET `IDCategory`=?, `Key`=?", $iCategoryId, $sKey);
  47. return (int)$this->query($sSql) > 0 ? $this->lastId() : false;
  48. }
  49. function addKeys($iLanguageId, $iCategoryId, &$aKeys)
  50. {
  51. foreach($aKeys as $sKey => $sValue) {
  52. $sQuery = $this->prepare("SELECT `ID` FROM `sys_localization_keys` WHERE `IDCategory`=? AND `Key`=? LIMIT 1", $iCategoryId, $sKey);
  53. $iKeyId = (int)$this->getOne($sQuery);
  54. if($iKeyId == 0) {
  55. $sQuery = $this->prepare("INSERT INTO `sys_localization_keys`(`IDCategory`, `Key`) VALUES(?, ?)", $iCategoryId, $sKey);
  56. if((int)$this->query($sQuery) <= 0)
  57. continue;
  58. $iKeyId = (int)$this->lastId();
  59. }
  60. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_localization_strings`(`IDKey`, `IDLanguage`, `String`) VALUES(?, ?, ?)", $iKeyId, $iLanguageId, $sValue);
  61. $this->query($sQuery);
  62. }
  63. }
  64. /**
  65. * Remove language key ONLY. Language strings should be already removed.
  66. * @param integer $iKeyId language key ID
  67. */
  68. function deleteKey($iKeyId)
  69. {
  70. $sQuery = $this->prepare("DELETE FROM `sys_localization_keys` WHERE `ID`=? LIMIT 1", $iKeyId);
  71. return (int)$this->query($sQuery) > 0;
  72. }
  73. function deleteKeys($aKeys)
  74. {
  75. $iResult = 0;
  76. foreach($aKeys as $sKey => $sValue) {
  77. $sQuery = $this->prepare("DELETE FROM `tk`, `ts` USING `sys_localization_keys` AS `tk` LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey` WHERE `tk`.`Key`=?", $sKey);
  78. $iResult += (int)$this->query($sQuery);
  79. }
  80. return $iResult;
  81. }
  82. function deleteKeysBy($aParams = array())
  83. {
  84. $aBindings = array();
  85. $sWhereClause = "";
  86. switch($aParams['type']) {
  87. case 'by_key_id':
  88. $aBindings['id'] = $aParams['value'];
  89. $sWhereClause = " AND `tk`.`ID`=:id";
  90. break;
  91. case 'by_cat_id':
  92. $aBindings['id'] = $aParams['value'];
  93. $sWhereClause .= " AND `tc`.`ID`=:id";
  94. break;
  95. }
  96. $sSql = "DELETE FROM `tk`, `ts`
  97. USING
  98. `sys_localization_categories` AS `tc`,
  99. `sys_localization_keys` AS `tk`,
  100. `sys_localization_strings` AS `ts`
  101. WHERE `tk`.`ID`=`ts`.`IDKey` AND `tk`.`IDCategory`=`tc`.`ID`" . $sWhereClause;
  102. return (int)$this->query($sSql, $aBindings);
  103. }
  104. function updateKeys($aParamsSet, $aParamsWhere)
  105. {
  106. if(empty($aParamsSet) || empty($aParamsWhere))
  107. return false;
  108. $sSql = "UPDATE `sys_localization_keys` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  109. return $this->query($sSql);
  110. }
  111. function addString($iKeyId, $iLanguageId, $sString)
  112. {
  113. $sSql = $this->prepare("INSERT IGNORE INTO `sys_localization_strings` SET `IDKey`=?, `IDLanguage`=?, `String`=?", $iKeyId, $iLanguageId, $sString);
  114. return (int)$this->query($sSql) > 0;
  115. }
  116. function updateString($iKeyId, $iLanguageId, $sString)
  117. {
  118. $sSql = $this->prepare("SELECT `IDKey` FROM `sys_localization_strings` WHERE `IDKey`=? AND `IDLanguage`=?", $iKeyId, $iLanguageId);
  119. $iKeyIdDb = (int)$this->getOne($sSql);
  120. if($iKeyIdDb != 0)
  121. $sSql = $this->prepare("UPDATE `sys_localization_strings` SET `String`=? WHERE `IDKey`=? AND `IDLanguage`=?", $sString, $iKeyId, $iLanguageId);
  122. else
  123. $sSql = $this->prepare("INSERT INTO `sys_localization_strings` SET `IDKey`=?, `IDLanguage`=?, `String`=?", $iKeyId, $iLanguageId, $sString);
  124. return (int)$this->query($sSql) > 0;
  125. }
  126. function deleteString($iKeyId, $iLanguageId)
  127. {
  128. $sSql = $this->prepare("DELETE FROM `sys_localization_strings` WHERE `IDKey`=? AND `IDLanguage`=?", $iKeyId, $iLanguageId);
  129. return (int)$this->query($sSql) > 0;
  130. }
  131. function deleteStringsBy($aParams = array())
  132. {
  133. $aBindings = array();
  134. $sWhereClause = "";
  135. switch($aParams['type']) {
  136. case 'by_lang':
  137. $aBindings = array(
  138. 'language_id' => $aParams['language_id']
  139. );
  140. $sWhereClause .= " AND `tl`.`ID`=:language_id";
  141. break;
  142. case 'by_cat_and_lang':
  143. $aBindings = array(
  144. 'category_id' => $aParams['category_id'],
  145. 'language_id' => $aParams['language_id']
  146. );
  147. $sWhereClause .= " AND `tc`.`ID`=:category_id AND `tl`.`ID`=:language_id";
  148. break;
  149. case 'by_key_and_lang':
  150. $aBindings = array(
  151. 'key' => $aParams['key'],
  152. 'language_id' => $aParams['language_id']
  153. );
  154. $sWhereClause .= " AND `tk`.`Key`=:key AND `tl`.`ID`=:language_id";
  155. break;
  156. }
  157. $sSql = "DELETE FROM `ts`
  158. USING `sys_localization_keys` AS `tk`
  159. LEFT JOIN `sys_localization_categories` AS `tc` ON `tk`.`IDCategory`=`tc`.`ID`
  160. LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey`
  161. LEFT JOIN `sys_localization_languages` AS `tl` ON `ts`.`IDLanguage`=`tl`.`ID`
  162. WHERE 1" . $sWhereClause;
  163. return (int)$this->query($sSql, $aBindings);
  164. }
  165. function addCategory($sName)
  166. {
  167. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_localization_categories` SET `Name`=?", $sName);
  168. if((int)$this->query($sQuery) > 0)
  169. return (int)$this->lastId();
  170. $iCategoryId = 0;
  171. $this->getCategoriesBy(array('type' => 'id_by_name', 'value' => $sName), $iCategoryId);
  172. return $iCategoryId;
  173. }
  174. function deleteCategory($sName)
  175. {
  176. $sQuery = $this->prepare("DELETE FROM `sys_localization_categories` WHERE `Name`=?", $sName);
  177. $this->query($sQuery);
  178. }
  179. }
  180. /** @} */