AntonLV 12 лет назад
Родитель
Сommit
eb0c479cdf

+ 27 - 22
studio/classes/BxDolStudioLanguagesUtils.php

@@ -184,8 +184,9 @@ class BxDolStudioLanguagesUtils extends BxDolLanguages implements iBxDolSingleto
         if(!empty($mixedLang)) {
             $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
             $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
-        } else
-            $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
+        } 
+        else
+			$this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
 
         $bResult = true;
         foreach($aLanguages as $aLanguage) {
@@ -193,27 +194,20 @@ class BxDolStudioLanguagesUtils extends BxDolLanguages implements iBxDolSingleto
                 continue;
 
             bx_import('BxDolModuleQuery');
-            $aLanguageModule = BxDolModuleQuery::getInstance()->getModuleByUri($aLanguage['name']);
+            $oModuleQuery = BxDolModuleQuery::getInstance();
+
+            $aLanguageModule = $oModuleQuery->getModuleByUri($aLanguage['name']);
             if(empty($aLanguageModule) || !is_array($aLanguageModule))
                 continue;
 
             $sPath = BX_DIRECTORY_PATH_MODULES . $aLanguageModule['path'] . 'data/langs/';
 
-            if(!empty($mixedModule))
+            if(empty($mixedModule))
                 $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $mixedModule);
             else {
-                if(!($rHandler = opendir($sPath)))
-                    continue;
-
-                while(($sFileName = readdir($rHandler)) !== false) {
-                    if(substr($sFileName, -3) != 'xml')
-                        continue;
-
-                    $sModule = substr($sFileName, 0, -4);
-                    $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $sModule);
-                }
-
-                closedir($rHandler);
+            	$aModules = $oModuleQuery->getModules();
+            	foreach($aModules as $aModule)
+            		$bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $aModule);
             }
 
             if($bRecompile)
@@ -391,7 +385,7 @@ class BxDolStudioLanguagesUtils extends BxDolLanguages implements iBxDolSingleto
 
     function deleteLanguageKeys($aKeys)
     {
-        $this->oDb->deleteKeys($aKeys);
+        return $this->oDb->deleteKeys($aKeys);
     }
 
     function addLanguageCategory($sName)
@@ -430,15 +424,26 @@ class BxDolStudioLanguagesUtils extends BxDolLanguages implements iBxDolSingleto
 
         $aCategory = array();
         $this->oDb->getCategoriesBy(array('type' => 'by_name', 'value' => $aModule['lang_category']), $aCategory, false);
-        if(empty($aCategory) || !is_array($aCategory))
-            return false;
 
-        $this->oDb->deleteStringsBy(array('type' => 'by_cat_and_lang', 'category_id' => $aCategory['id'], 'language_id' => $aLanguage['id']));
+        $iCategoryId = 0;
+        if(empty($aCategory) || !is_array($aCategory)) {
+        	$iCategoryId = $this->addLanguageCategory($aModule['lang_category']);
+        	if(empty($iCategoryId))
+        		return false;
+        }
+        else
+        	$iCategoryId = $aCategory['id'];
+
+		$bDeleteStringsByKey = !$this->oDb->deleteStringsBy(array('type' => 'by_cat_and_lang', 'category_id' => $iCategoryId, 'language_id' => $aLanguage['id']));
 
         $bResult = true;
         foreach($aLanguageInfo['strings'] as $sKey => $sValue)
-            if($sKey != '')
-                $bResult &= $this->addLanguageString($sKey, $sValue, $aLanguage['id'], $aCategory['id'], false) > 0;
+            if($sKey != '') {
+            	if($bDeleteStringsByKey)
+            		$this->oDb->deleteStringsBy(array('type' => 'by_key_and_lang', 'key' => $sKey, 'language_id' => $aLanguage['id']));
+
+                $bResult &= $this->addLanguageString($sKey, $sValue, $aLanguage['id'], $iCategoryId, false) > 0;
+            }
 
         return $bResult;
     }

+ 32 - 14
studio/classes/BxDolStudioLanguagesUtilsQuery.php

@@ -48,9 +48,14 @@ class BxDolStudioLanguagesUtilsQuery extends BxDolLanguagesQuery
 
     function addKey($iCategoryId, $sKey)
     {
-        $sSql = $this->prepare("SELECT `ID` FROM `sys_localization_keys` WHERE `Key`=? LIMIT 1", $sKey);
-        if(($iId = (int)$this->getOne($sSql)) != 0)
-            return $iId;
+        $sSql = $this->prepare("SELECT `ID`, `IDCategory` FROM `sys_localization_keys` WHERE `Key`=? LIMIT 1", $sKey);
+        $aKey = $this->getRow($sSql);
+        if(!empty($aKey) && is_array($aKey)) {
+        	if((int)$aKey['IDCategory'] != $iCategoryId)
+        		$this->updateKeys(array('IDCategory' => $iCategoryId), array('ID' => $aKey['ID']));
+        	
+            return $aKey['ID'];
+        }
 
         $sSql = $this->prepare("INSERT INTO `sys_localization_keys` SET `IDCategory`=?, `Key`=?", $iCategoryId, $sKey);
         return (int)$this->query($sSql) > 0 ? $this->lastId() : false;
@@ -87,10 +92,14 @@ class BxDolStudioLanguagesUtilsQuery extends BxDolLanguagesQuery
 
     function deleteKeys($aKeys)
     {
+    	$iResult = 0;
+
         foreach($aKeys as $sKey => $sValue) {
-            $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);
-            $this->query($sQuery);
+            $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);
+            $iResult += (int)$this->query($sQuery);
         }
+
+        return $iResult;
     }
 
     function deleteKeysBy($aParams = array())
@@ -115,6 +124,15 @@ class BxDolStudioLanguagesUtilsQuery extends BxDolLanguagesQuery
         return (int)$this->query($sSql);
     }
 
+    function updateKeys($aParamsSet, $aParamsWhere)
+    {
+        if(empty($aParamsSet) || empty($aParamsWhere))
+            return false;
+
+        $sSql = "UPDATE `sys_localization_keys` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
+        return $this->query($sSql);
+    }
+
     function addString($iKeyId, $iLanguageId, $sString)
     {
         $sSql = $this->prepare("INSERT INTO `sys_localization_strings` SET `IDKey`=?, `IDLanguage`=?, `String`=?", $iKeyId, $iLanguageId, $sString);
@@ -150,24 +168,24 @@ class BxDolStudioLanguagesUtilsQuery extends BxDolLanguagesQuery
             case 'by_cat_and_lang':
                 $sWhereClause .= $this->prepare(" AND `tc`.`ID`=? AND `tl`.`ID`=?", $aParams['category_id'], $aParams['language_id']);
                 break;
+            case 'by_key_and_lang':
+            	$sWhereClause .= $this->prepare(" AND `tk`.`Key`=? AND `tl`.`ID`=?", $aParams['key'], $aParams['language_id']);
+                break;
         }
 
         $sSql = "DELETE FROM `ts`
-                USING
-                    `sys_localization_categories` AS `tc`,
-                    `sys_localization_keys` AS `tk`,
-                    `sys_localization_languages` AS `tl`,
-                    `sys_localization_strings` AS `ts`
-                WHERE `tk`.`ID`=`ts`.`IDKey` AND `tk`.`IDCategory`=`tc`.`ID` AND `ts`.`IDLanguage`=`tl`.`ID`" . $sWhereClause;
+                USING `sys_localization_keys` AS `tk` 
+				LEFT JOIN `sys_localization_categories` AS `tc` ON `tk`.`IDCategory`=`tc`.`ID` 
+				LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey` 
+				LEFT JOIN `sys_localization_languages` AS `tl` ON `ts`.`IDLanguage`=`tl`.`ID`
+                WHERE 1" . $sWhereClause;
         return (int)$this->query($sSql);
     }
 
     function addCategory($sName)
     {
         $sQuery = $this->prepare("INSERT IGNORE INTO `sys_localization_categories` SET `Name`=?", $sName);
-        $iResult = (int)$this->query($sQuery);
-
-        if($iResult > 0)
+        if((int)$this->query($sQuery) > 0)
             return (int)$this->lastId();
 
         $iCategoryId = 0;