BxDolStudioLanguagesUtilsQuery.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 TridentStudio Trident 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. $sWhereClause = "";
  85. switch($aParams['type']) {
  86. case 'by_key_id':
  87. $sWhereClause = $this->prepare(" AND `tk`.`ID`=?", $aParams['value']);
  88. break;
  89. case 'by_cat_id':
  90. $sWhereClause .= $this->prepare(" AND `tc`.`ID`=?", $aParams['value']);
  91. break;
  92. }
  93. $sSql = "DELETE FROM `tk`, `ts`
  94. USING
  95. `sys_localization_categories` AS `tc`,
  96. `sys_localization_keys` AS `tk`,
  97. `sys_localization_strings` AS `ts`
  98. WHERE `tk`.`ID`=`ts`.`IDKey` AND `tk`.`IDCategory`=`tc`.`ID`" . $sWhereClause;
  99. return (int)$this->query($sSql);
  100. }
  101. function updateKeys($aParamsSet, $aParamsWhere)
  102. {
  103. if(empty($aParamsSet) || empty($aParamsWhere))
  104. return false;
  105. $sSql = "UPDATE `sys_localization_keys` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  106. return $this->query($sSql);
  107. }
  108. function addString($iKeyId, $iLanguageId, $sString)
  109. {
  110. $sSql = $this->prepare("INSERT IGNORE INTO `sys_localization_strings` SET `IDKey`=?, `IDLanguage`=?, `String`=?", $iKeyId, $iLanguageId, $sString);
  111. return (int)$this->query($sSql) > 0;
  112. }
  113. function updateString($iKeyId, $iLanguageId, $sString)
  114. {
  115. $sSql = $this->prepare("SELECT `IDKey` FROM `sys_localization_strings` WHERE `IDKey`=? AND `IDLanguage`=?", $iKeyId, $iLanguageId);
  116. $iKeyIdDb = (int)$this->getOne($sSql);
  117. if($iKeyIdDb != 0)
  118. $sSql = $this->prepare("UPDATE `sys_localization_strings` SET `String`=? WHERE `IDKey`=? AND `IDLanguage`=?", $sString, $iKeyId, $iLanguageId);
  119. else
  120. $sSql = $this->prepare("INSERT INTO `sys_localization_strings` SET `IDKey`=?, `IDLanguage`=?, `String`=?", $iKeyId, $iLanguageId, $sString);
  121. return (int)$this->query($sSql) > 0;
  122. }
  123. function deleteString($iKeyId, $iLanguageId)
  124. {
  125. $sSql = $this->prepare("DELETE FROM `sys_localization_strings` WHERE `IDKey`=? AND `IDLanguage`=?", $iKeyId, $iLanguageId);
  126. return (int)$this->query($sSql) > 0;
  127. }
  128. function deleteStringsBy($aParams = array())
  129. {
  130. $sWhereClause = "";
  131. switch($aParams['type']) {
  132. case 'by_lang':
  133. $sWhereClause .= $this->prepare(" AND `tl`.`ID`=?", $aParams['language_id']);
  134. break;
  135. case 'by_cat_and_lang':
  136. $sWhereClause .= $this->prepare(" AND `tc`.`ID`=? AND `tl`.`ID`=?", $aParams['category_id'], $aParams['language_id']);
  137. break;
  138. case 'by_key_and_lang':
  139. $sWhereClause .= $this->prepare(" AND `tk`.`Key`=? AND `tl`.`ID`=?", $aParams['key'], $aParams['language_id']);
  140. break;
  141. }
  142. $sSql = "DELETE FROM `ts`
  143. USING `sys_localization_keys` AS `tk`
  144. LEFT JOIN `sys_localization_categories` AS `tc` ON `tk`.`IDCategory`=`tc`.`ID`
  145. LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey`
  146. LEFT JOIN `sys_localization_languages` AS `tl` ON `ts`.`IDLanguage`=`tl`.`ID`
  147. WHERE 1" . $sWhereClause;
  148. return (int)$this->query($sSql);
  149. }
  150. function addCategory($sName)
  151. {
  152. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_localization_categories` SET `Name`=?", $sName);
  153. if((int)$this->query($sQuery) > 0)
  154. return (int)$this->lastId();
  155. $iCategoryId = 0;
  156. $this->getCategoriesBy(array('type' => 'id_by_name', 'value' => $sName), $iCategoryId);
  157. return $iCategoryId;
  158. }
  159. function deleteCategory($sName)
  160. {
  161. $sQuery = $this->prepare("DELETE FROM `sys_localization_categories` WHERE `Name`=?", $sName);
  162. $this->query($sQuery);
  163. }
  164. }
  165. /** @} */