BxDolStudioLanguagesUtilsQuery.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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`, `IDCategory` FROM `sys_localization_keys` WHERE `Key`=? LIMIT 1", $sKey);
  41. $aKey = $this->getRow($sSql);
  42. if(!empty($aKey) && is_array($aKey)) {
  43. if((int)$aKey['IDCategory'] != $iCategoryId)
  44. $this->updateKeys(array('IDCategory' => $iCategoryId), array('ID' => $aKey['ID']));
  45. return $aKey['ID'];
  46. }
  47. $sSql = $this->prepare("INSERT INTO `sys_localization_keys` SET `IDCategory`=?, `Key`=?", $iCategoryId, $sKey);
  48. return (int)$this->query($sSql) > 0 ? $this->lastId() : false;
  49. }
  50. function addKeys($iLanguageId, $iCategoryId, &$aKeys)
  51. {
  52. foreach($aKeys as $sKey => $sValue) {
  53. $sQuery = $this->prepare("SELECT `ID` FROM `sys_localization_keys` WHERE `IDCategory`=? AND `Key`=? LIMIT 1", $iCategoryId, $sKey);
  54. $iKeyId = (int)$this->getOne($sQuery);
  55. if($iKeyId == 0) {
  56. $sQuery = $this->prepare("INSERT INTO `sys_localization_keys`(`IDCategory`, `Key`) VALUES(?, ?)", $iCategoryId, $sKey);
  57. if((int)$this->query($sQuery) <= 0)
  58. continue;
  59. $iKeyId = (int)$this->lastId();
  60. }
  61. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_localization_strings`(`IDKey`, `IDLanguage`, `String`) VALUES(?, ?, ?)", $iKeyId, $iLanguageId, $sValue);
  62. $this->query($sQuery);
  63. }
  64. }
  65. /**
  66. * Remove language key ONLY. Language strings should be already removed.
  67. * @param integer $iKeyId language key ID
  68. */
  69. function deleteKey($iKeyId)
  70. {
  71. $sQuery = $this->prepare("DELETE FROM `sys_localization_keys` WHERE `ID`=? LIMIT 1", $iKeyId);
  72. return (int)$this->query($sQuery) > 0;
  73. }
  74. function deleteKeys($aKeys)
  75. {
  76. $iResult = 0;
  77. foreach($aKeys as $sKey => $sValue) {
  78. $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);
  79. $iResult += (int)$this->query($sQuery);
  80. }
  81. return $iResult;
  82. }
  83. function deleteKeysBy($aParams = array())
  84. {
  85. $sWhereClause = "";
  86. switch($aParams['type']) {
  87. case 'by_key_id':
  88. $sWhereClause = $this->prepare(" AND `tk`.`ID`=?", $aParams['value']);
  89. break;
  90. case 'by_cat_id':
  91. $sWhereClause .= $this->prepare(" AND `tc`.`ID`=?", $aParams['value']);
  92. break;
  93. }
  94. $sSql = "DELETE FROM `tk`, `ts`
  95. USING
  96. `sys_localization_categories` AS `tc`,
  97. `sys_localization_keys` AS `tk`,
  98. `sys_localization_strings` AS `ts`
  99. WHERE `tk`.`ID`=`ts`.`IDKey` AND `tk`.`IDCategory`=`tc`.`ID`" . $sWhereClause;
  100. return (int)$this->query($sSql);
  101. }
  102. function updateKeys($aParamsSet, $aParamsWhere)
  103. {
  104. if(empty($aParamsSet) || empty($aParamsWhere))
  105. return false;
  106. $sSql = "UPDATE `sys_localization_keys` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  107. return $this->query($sSql);
  108. }
  109. function addString($iKeyId, $iLanguageId, $sString)
  110. {
  111. $sSql = $this->prepare("INSERT INTO `sys_localization_strings` SET `IDKey`=?, `IDLanguage`=?, `String`=?", $iKeyId, $iLanguageId, $sString);
  112. return (int)$this->query($sSql) > 0;
  113. }
  114. function updateString($iKeyId, $iLanguageId, $sString)
  115. {
  116. $sSql = $this->prepare("SELECT `IDKey` FROM `sys_localization_strings` WHERE `IDKey`=? AND `IDLanguage`=?", $iKeyId, $iLanguageId);
  117. $iKeyIdDb = (int)$this->getOne($sSql);
  118. if($iKeyIdDb != 0)
  119. $sSql = $this->prepare("UPDATE `sys_localization_strings` SET `String`=? WHERE `IDKey`=? AND `IDLanguage`=?", $sString, $iKeyId, $iLanguageId);
  120. else
  121. $sSql = $this->prepare("INSERT INTO `sys_localization_strings` SET `IDKey`=?, `IDLanguage`=?, `String`=?", $iKeyId, $iLanguageId, $sString);
  122. return (int)$this->query($sSql) > 0;
  123. }
  124. function deleteString($iKeyId, $iLanguageId)
  125. {
  126. $sSql = $this->prepare("DELETE FROM `sys_localization_strings` WHERE `IDKey`=? AND `IDLanguage`=?", $iKeyId, $iLanguageId);
  127. return (int)$this->query($sSql) > 0;
  128. }
  129. function deleteStringsBy($aParams = array())
  130. {
  131. $sWhereClause = "";
  132. switch($aParams['type']) {
  133. case 'by_lang':
  134. $sWhereClause .= $this->prepare(" AND `tl`.`ID`=?", $aParams['language_id']);
  135. break;
  136. case 'by_cat_and_lang':
  137. $sWhereClause .= $this->prepare(" AND `tc`.`ID`=? AND `tl`.`ID`=?", $aParams['category_id'], $aParams['language_id']);
  138. break;
  139. case 'by_key_and_lang':
  140. $sWhereClause .= $this->prepare(" AND `tk`.`Key`=? AND `tl`.`ID`=?", $aParams['key'], $aParams['language_id']);
  141. break;
  142. }
  143. $sSql = "DELETE FROM `ts`
  144. USING `sys_localization_keys` AS `tk`
  145. LEFT JOIN `sys_localization_categories` AS `tc` ON `tk`.`IDCategory`=`tc`.`ID`
  146. LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey`
  147. LEFT JOIN `sys_localization_languages` AS `tl` ON `ts`.`IDLanguage`=`tl`.`ID`
  148. WHERE 1" . $sWhereClause;
  149. return (int)$this->query($sSql);
  150. }
  151. function addCategory($sName)
  152. {
  153. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_localization_categories` SET `Name`=?", $sName);
  154. if((int)$this->query($sQuery) > 0)
  155. return (int)$this->lastId();
  156. $iCategoryId = 0;
  157. $this->getCategoriesBy(array('type' => 'id_by_name', 'value' => $sName), $iCategoryId);
  158. return $iCategoryId;
  159. }
  160. function deleteCategory($sName)
  161. {
  162. $sQuery = $this->prepare("DELETE FROM `sys_localization_categories` WHERE `Name`=?", $sName);
  163. $this->query($sQuery);
  164. }
  165. }
  166. /** @} */