BxDolStudioLanguagesUtilsQuery.php 6.8 KB

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