Przeglądaj źródła

#31 - Improvements in Module (Update) installation process.

AntonLV 12 lat temu
rodzic
commit
03b3aadcbf

+ 12 - 0
inc/classes/BxDolModuleQuery.php

@@ -151,11 +151,23 @@ class BxDolModuleQuery extends BxDolDb implements iBxDolSingleton
         $sSql = "SELECT `uri` FROM `sys_modules` ORDER BY `uri`";
         return $this->fromMemory('sys_modules_uri', 'getColumn', $sSql);
     }
+
     function getDependent($sUri)
     {
         $sSql = "SELECT `id`, `title` FROM `sys_modules` WHERE `dependencies` LIKE '%" . $this->escape($sUri) . "%'";
         return $this->getAll($sSql);
     }
+
+	public function updateModule($aParamsSet, $aParamsWhere = array())
+    {
+        if(empty($aParamsSet))
+            return false;
+
+		$sWhereClause = !empty($aParamsWhere) ? $this->arrayToSQL($aParamsWhere, " AND ") : "1";
+
+        $sSql = "UPDATE `sys_modules` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause;
+        return $this->query($sSql);
+    }
 }
 
 /** @} */

+ 0 - 1
install/sql/system.sql

@@ -216,7 +216,6 @@ SET @iCategoryId = LAST_INSERT_ID();
 
 INSERT INTO `sys_options`(`category_id`, `name`, `caption`, `value`, `type`, `extra`, `check`, `check_error`, `order`) VALUES
 (@iCategoryId, 'sys_install_time', '', '0', 'digit', '', '', '', 3),
-(@iCategoryId, 'sys_oauth_user', '', '0', 'digit', '', '', '', 4),
 
 (@iCategoryId, 'sys_ftp_login', '_adm_stg_cpt_option_sys_ftp_login', '', 'digit', '', '', '', 10),
 (@iCategoryId, 'sys_ftp_password', '_adm_stg_cpt_option_sys_ftp_password', '', 'digit', '', '', '', 11),

+ 3 - 0
studio/classes/BxDolStudioInstaller.php

@@ -226,6 +226,9 @@ class BxDolStudioInstaller extends BxDolInstallerUtils
             $oLanguages->addLanguageString($sTitleKey, $this->_aConfig['title']);
             $oLanguages->compileLanguage();
 
+            bx_import('BxDolStudioInstallerUtils');
+            BxDolStudioInstallerUtils::getInstance()->checkModules();
+
             $aFiles = array();
             $this->hashFiles($this->_sModulePath, $aFiles);
 

+ 88 - 40
studio/classes/BxDolStudioInstallerUtils.php

@@ -58,8 +58,7 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
         $aModules = array();
         $oTemplate = BxDolStudioTemplate::getInstance();
 
-        $aInstalledPathes = $aInstalledInfo = array();
-        $this->getInstalledInfo($aInstalledPathes, $aInstalledInfo);
+        $aInstalledInfo = $this->getInstalledInfo();
 
         $sPath = BX_DIRECTORY_PATH_MODULES;
         if(($rHandleVendor = opendir($sPath)) !== false) {
@@ -72,8 +71,8 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
                         if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
                             continue;
 
-                        $sConfigPath = $sPath . $sVendor . '/' . $sModule . '/install/config.php';
-                        $aModule = $this->getConfigModule($sConfigPath, $aInstalledPathes, $aInstalledInfo);
+						$sModulePath = $sVendor . '/' . $sModule . '/';
+                        $aModule = $this->getConfigModule($sPath . $sModulePath . 'install/config.php', !empty($aInstalledInfo[$sModulePath]) ? $aInstalledInfo[$sModulePath] : array());
                         if(empty($aModule))
                             continue;
 
@@ -96,8 +95,7 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
         bx_import('BxDolStudioTemplate');
         $oTemplate = BxDolStudioTemplate::getInstance();
 
-        $aInstalledPathes = $aInstalledInfo = array();
-        $this->getInstalledInfo($aInstalledPathes, $aInstalledInfo);
+        $aInstalledInfo = $this->getInstalledInfo();
 
         $sPath = BX_DIRECTORY_PATH_MODULES;
         if(($rHandleVendor = opendir($sPath)) !== false) {
@@ -110,21 +108,13 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
                         if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
                             continue;
 
-                        if(($rHandleUpdate = @opendir($sPath . $sVendor . '/' . $sModule . '/updates/')) !== false) {
-                            while(($sUpdate = readdir($rHandleUpdate)) !== false) {
-                                if(!is_dir($sPath . $sVendor . '/' . $sModule . '/updates/' . $sUpdate) || substr($sUpdate, 0, 1) == '.')
-                                    continue;
-
-                                $sConfigPathModule = $sPath . $sVendor . '/' . $sModule . '/install/config.php';
-                                $sConfigPathUpdate = $sPath . $sVendor . '/' . $sModule . '/updates/' . $sUpdate . '/install/config.php';
-                                $aUpdate = $this->getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledPathes, $aInstalledInfo);
-                                if(empty($aUpdate) || !is_array($aUpdate) || version_compare($aUpdate['module_version'], $aUpdate['version_from']) != 0)
-                                    continue;
-
-                                $aUpdates[$aUpdate['title']] = $aUpdate;
-                            }
-                            closedir($rHandleUpdate);
-                        }
+						$sModulePath = $sVendor . '/' . $sModule . '/';
+						if(empty($aInstalledInfo[$sModulePath]))
+							continue;
+
+						$aUpdate = $this->getUpdate($aInstalledInfo[$sModulePath]);
+						if(!empty($aUpdate))
+							$aUpdates[$aUpdate['title']] = $aUpdate;
                     }
                     closedir($rHandleModule);
                 }
@@ -136,6 +126,43 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
         return $aUpdates;
     }
 
+    public function getUpdate($mixedModule)
+    {
+    	$aResult = array();
+
+    	if(!is_array($mixedModule)) {
+    		bx_import('BxDolModuleQuery');
+			$mixedModule = BxDolModuleQuery::getInstance()->{is_numeric($mixedModule) ? 'getModuleById' : 'getModuleByName'}($mixedModule); 
+    	}
+
+    	if(empty($mixedModule) || !is_array($mixedModule))
+    		return $aResult;
+
+    	$sPathModule = BX_DIRECTORY_PATH_MODULES . $mixedModule['path'];
+    	$sPathUpdates = $sPathModule . 'updates/';
+
+    	$rHandleUpdate = @opendir($sPathUpdates);
+		if($rHandleUpdate === false)
+			return $aResult; 
+
+		while(($sUpdate = readdir($rHandleUpdate)) !== false) {
+			$sPathUpdate = $sPathUpdates . $sUpdate . '/';
+			if(substr($sUpdate, 0, 1) == '.' || !is_dir($sPathUpdate))
+            	continue;
+
+			$aUpdate = $this->getConfigUpdate($sPathModule . 'install/config.php', $sPathUpdate . 'install/config.php', $mixedModule);
+			if(empty($aUpdate) || !is_array($aUpdate) || version_compare($aUpdate['module_version'], $aUpdate['version_from']) != 0)
+            	continue;
+
+			$aResult = $aUpdate;
+			break;
+		}
+
+		closedir($rHandleUpdate);
+
+        return $aResult;
+    }
+
     public function perform($sDirectory, $sOperation, $aParams = array())
     {
     	if(!defined('BX_DOL_CRON_EXECUTE') && !self::isRealOwner() && !in_array($sOperation, array('install', 'uninstall', 'enable', 'disable'))) {
@@ -164,10 +191,34 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
         return array('code' => $aResult['result'] ? 0 : 2, 'message' => $aResult['message']);
     }
 
+    public function checkModules($bAuthorizedAccess = false)
+    {
+    	if($bAuthorizedAccess) {
+    		bx_import('BxDolStudioOAuth');
+        	$aProducts = BxDolStudioOAuth::getInstance()->loadItems(array('dol_type' => 'purchased_products', 'dol_domain' => BX_DOL_URL_ROOT));
+    	}
+    	else {
+	    	bx_import('BxDolStudioJson');
+			$aProducts = BxDolStudioJson::getInstance()->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_purchased', array(
+				'key' => getParam('sys_oauth_key')
+			));
+    	}
+
+    	bx_import('BxDolModuleQuery');
+    	$oModuleDb = BxDolModuleQuery::getInstance();
+
+    	$oModuleDb->updateModule(array('hash' => ''));
+        if(!empty($aProducts) && is_array($aProducts))
+	        foreach ($aProducts as $aProduct)
+	        	$oModuleDb->updateModule(array('hash' => $aProduct['hash']), array('name' => $aProduct['name']));
+
+		return $aProducts;
+    }
+
     public function checkUpdates($bAuthorizedAccess = false)
     {
 		bx_import('BxDolModuleQuery');
-        $aModules = BxDolModuleQuery::getInstance()->getModules();
+		$aModules = BxDolModuleQuery::getInstance()->getModules();
 
         $aProducts = array();
         foreach($aModules as $aModule) {
@@ -180,6 +231,7 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
             	'hash' => $aModule['hash'],
             );
         }
+
         $sProducts = base64_encode(serialize($aProducts));
 
         if($bAuthorizedAccess) {
@@ -189,9 +241,8 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
 
 		bx_import('BxDolStudioJson');
 		return BxDolStudioJson::getInstance()->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_updates', array(
-			'products' => $sProducts,
-			'domain' => BX_DOL_URL_ROOT,
-			'user' => (int)getParam('sys_oauth_user') 
+			'key' => getParam('sys_oauth_key'),
+			'products' => $sProducts
 		));
     }
 
@@ -210,13 +261,12 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
 
 		bx_import('BxDolStudioJson');
 		$aItem = BxDolStudioJson::getInstance()->load(BX_DOL_UNITY_URL_MARKET . 'json_download_update', array(
+			'key' => getParam('sys_oauth_key'),
 			'product' => base64_encode(serialize(array(
 				'name' => $aModule['name'],
 	            'version' => $aModule['version'],
 	            'hash' => $aModule['hash'],
 			))),
-			'domain' => BX_DOL_URL_ROOT,
-			'user' => (int)getParam('sys_oauth_user') 
 		));
 
 		return $this->downloadFileInit($aItem);
@@ -344,17 +394,15 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
 		return true;
     }
 
-    protected function getConfigModule($sConfigPath, $aInstalledPathes = array(), $aInstalledInfo = array())
+    protected function getConfigModule($sConfigPath, $aInstalled = array())
     {
     	$aConfig = self::getModuleConfig($sConfigPath);
         if(empty($aConfig) || !is_array($aConfig))
             return array();
 
-        $sModulePath = $aConfig['home_dir'];
-
-        $bInstalled = !empty($aInstalledPathes) && in_array($sModulePath, $aInstalledPathes);
-        $bEnabled = $bInstalled && !empty($aInstalledInfo) && (int)$aInstalledInfo[$sModulePath]['enabled'] == 1;
-        $sVersion = $bInstalled && !empty($aInstalledInfo) ? $aInstalledInfo[$sModulePath]['version'] : $aConfig['version'];
+        $bInstalled = !empty($aInstalled);
+        $bEnabled = $bInstalled && (int)$aInstalled['enabled'] == 1;
+        $sVersion = $bInstalled ? $aInstalled['version'] : $aConfig['version'];
 
         return array(
         	'type' => $aConfig['type'],
@@ -366,13 +414,13 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
             'dir' => $aConfig['home_dir'],
             'note' => isset($aConfig['note']) ? bx_process_output($aConfig['note']) : '',
             'installed' => $bInstalled,
-            'enabled' => $bInstalled && $bEnabled
+            'enabled' => $bEnabled
         );
     }
 
-    protected function getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledPathes = array(), $aInstalledInfo = array())
+    protected function getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledModule = array())
     {
-        $aModule = $this->getConfigModule($sConfigPathModule, $aInstalledPathes, $aInstalledInfo);
+        $aModule = $this->getConfigModule($sConfigPathModule, $aInstalledModule);
         if(empty($aModule) || !$aModule['installed'])
             return array();
 
@@ -393,16 +441,16 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
         );
     }
 
-    private function getInstalledInfo(&$aInstalledPathes, &$aInstalledInfo)
+    private function getInstalledInfo()
     {
-        bx_import('BxDolModuleDb');
-        $aModules = BxDolModuleDb::getInstance()->getModules();
+        bx_import('BxDolModuleQuery');
+        $aModules = BxDolModuleQuery::getInstance()->getModules();
 
         $aInstalledInfo = array();
         foreach($aModules as $aModule)
             $aInstalledInfo[$aModule['path']] = $aModule;
 
-        $aInstalledPathes = array_keys($aInstalledInfo);
+        return $aInstalledInfo;
     }
 
     private function addTransientJob($sAction, $aParams)

+ 0 - 11
studio/classes/BxDolStudioModulesQuery.php

@@ -15,17 +15,6 @@ class BxDolStudioModulesQuery extends BxDolModuleQuery
     {
         parent::__construct();
     }
-
-	public function updateModule($aParamsSet, $aParamsWhere = array())
-    {
-        if(empty($aParamsSet))
-            return false;
-
-		$sWhereClause = !empty($aParamsWhere) ? $this->arrayToSQL($aParamsWhere, " AND ") : "1";
-
-        $sSql = "UPDATE `sys_modules` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause;
-        return $this->query($sSql);
-    }
 }
 
 /** @} */

+ 1 - 4
studio/classes/BxDolStudioOAuth.php

@@ -143,13 +143,10 @@ class BxDolStudioOAuth extends BxDol implements iBxDolSingleton
                 if($this->isServerError($aAccessToken))
                     return $this->processServerError($aAccessToken);
 
-				$iUser = (int)bx_get('oauth_user');
-				setParam('sys_oauth_user', $iUser);
-
                 $this->oSession->setValue('sys_oauth_token', $aAccessToken['oauth_token']);
                 $this->oSession->setValue('sys_oauth_secret', $aAccessToken['oauth_token_secret']);
                 $this->oSession->setValue('sys_oauth_authorized', 1);
-                $this->oSession->setValue('sys_oauth_authorized_user', $iUser);
+                $this->oSession->setValue('sys_oauth_authorized_user', (int)bx_get('oauth_user'));
 
                 return true;
             }

+ 2 - 9
studio/classes/BxDolStudioStore.php

@@ -239,15 +239,8 @@ class BxDolStudioStore extends BxTemplStudioPage
 
     protected function loadPurchases()
     {
-        bx_import('BxDolStudioOAuth');
-        $aProducts = BxDolStudioOAuth::getInstance()->loadItems(array('dol_type' => 'purchased_products', 'dol_domain' => BX_DOL_URL_ROOT));
-
-        $this->oDb->updateModule(array('hash' => ''));
-        if(!empty($aProducts) && is_array($aProducts))
-	        foreach ($aProducts as $aProduct)
-	        	$this->oDb->updateModule(array('hash' => $aProduct['hash']), array('name' => $aProduct['name']));
-
-        return $aProducts;
+		bx_import('BxDolStudioInstallerUtils');
+        return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
     }
 
     protected function loadUpdates()

+ 28 - 5
studio/js/store.js

@@ -127,14 +127,29 @@ BxDolStudioStore.prototype.checkoutCart = function(sVendor, oButton) {
 };
 
 BxDolStudioStore.prototype.getFile = function(iFileId, oButton) {
-	this._getFile('get-file', iFileId, oButton);
+	this._getFile('get-file', iFileId, '', oButton);
 };
 
 BxDolStudioStore.prototype.getUpdate = function(sModuleName, oButton) {
-	this._getFile('get-update', sModuleName, oButton);
+	this._getFile('get-update', sModuleName, '', oButton);
 };
 
-BxDolStudioStore.prototype._getFile = function(sAction, mixedId, oButton) {
+BxDolStudioStore.prototype.getUpdateAndInstall = function(sModuleName, oButton) {
+	var $this = this;
+
+	var onResult = function(oData, oButton) {
+		if(oData.code != 0 || !oData.path) {
+			$this._onGetFile(oData, oButton);
+			return;
+		}
+
+		$this.update(oData.path, oButton);
+	};
+
+	this._getFile('get-update', sModuleName, onResult, oButton);
+};
+
+BxDolStudioStore.prototype._getFile = function(sAction, mixedId, onResult, oButton) {
 	var oDate = new Date();
 	var $this = this;
 	bx_loading(this.sIdPageContent, true);
@@ -147,13 +162,21 @@ BxDolStudioStore.prototype._getFile = function(sAction, mixedId, oButton) {
 			_t:oDate.getTime()
 		},
 		function(oData) {
-			bx_loading($this.sIdPageContent, false);
-			$this.showPopup($this.sIdPopupFile, oData.message, oButton);
+			if(typeof onResult == 'function')
+				onResult(oData, oButton);
+			else
+				$this._onGetFile(oData, oButton);
 		},
 		'json'
 	);
 };
 
+BxDolStudioStore.prototype._onGetFile = function(oData, oButton) {
+	bx_loading(this.sIdPageContent, false);
+
+	this.showPopup(this.sIdPopupFile, oData.message, oButton);
+};
+
 /*
  * NOTE. Is needed for download popup selector. Isn't used for now.
  */

+ 6 - 2
studio/template/scripts/BxBaseStudioStore.php

@@ -552,7 +552,10 @@ class BxBaseStudioStore extends BxDolStudioStore
         if($mixedResult !== true)
             return array('code' => 1, 'message' => (!empty($mixedResult) ? $mixedResult : _t('_adm_str_err_download_failed')));
 
-        return array('code' => 0, 'message' => _t('_adm_str_msg_download_successfully'));
+		bx_import('BxDolStudioInstallerUtils');
+		$aUpdate = BxDolStudioInstallerUtils::getInstance()->getUpdate($sModuleName);
+
+        return array('code' => 0, 'message' => _t('_adm_str_msg_download_successfully'), 'path' => $aUpdate['dir']);
     }
 
     protected function displayProducts($aItems, $aParams = array())
@@ -681,7 +684,8 @@ class BxBaseStudioStore extends BxDolStudioStore
                 'bx_if:show_download' => array(
                     'condition' => $bDownloadable,
                     'content' => array(
-	            		'on_click' => $sJsObject . "." . ($this->bAuthAccessUpdates ? "getFile(" . $aItem['file_id'] . ", this)" : "getUpdate('" . $aItem['name'] . "', this)")
+	            		'caption' => _t($this->bAuthAccessUpdates ? '_adm_btn_download_submit' : '_adm_btn_install_submit'),
+	            		'on_click' => $sJsObject . "." . ($this->bAuthAccessUpdates ? "getFile(" . $aItem['file_id'] . ", this)" : "getUpdateAndInstall('" . $aItem['name'] . "', this)")
                     )
                 )
             ));

+ 1 - 1
studio/template/str_update_v2.html

@@ -15,7 +15,7 @@
         <div class="bx-std-pc-vendor bx-def-margin-thd-top bx-def-font-small">__vendor__ &bull; <span class="bx-std-pc-versions">__versions__</span></div>
         <div class="bx-std-pc-action bx-def-margin-thd-top bx-clearfix">
         	<bx_if:show_download>
-            	<input class="bx-btn bx-btn-small bx-std-pc-download" type="button" value="<bx_text:_adm_btn_download_submit />" onclick="javascript:__on_click__" />
+            	<input class="bx-btn bx-btn-small bx-std-pc-download" type="button" value="__caption__" onclick="javascript:__on_click__" />
             </bx_if:show_download>
         </div>
     </div>