BxDolStudioLanguagesUtilsQuery.php 6.8 KB

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