Răsfoiți Sursa

Ticket #1184 - Core changes.

AntonLV 8 ani în urmă
părinte
comite
1b21957a12

+ 60 - 10
inc/classes/BxDolLiveUpdates.php

@@ -10,15 +10,30 @@
 class BxDolLiveUpdates extends BxDolFactory implements iBxDolSingleton
 {
 	protected $_oQuery;
+
+	/*
+	 * List of all systems (active and inactive) with full description.
+	 */
     protected $_aSystems;
+
+    /*
+     * List of active systems without any additional info.
+     */
     protected $_aSystemsActive;
 
+    /*
+     * List of dynamically added transient systems without any additional info. 
+     * It depends on the page where a user is.  
+     */
+    protected $_aSystemsTransient;  
+
     protected $_iProfileId;
 
     protected $_iInterval;
 
-	protected $_iCacheTTL;  
+	protected $_oCacheObject;
 	protected $_sCacheKey;
+	protected $_iCacheTTL;
 
     protected $_sJsClass;
     protected $_sJsObject;
@@ -39,15 +54,17 @@ class BxDolLiveUpdates extends BxDolFactory implements iBxDolSingleton
         $this->_oQuery = new BxDolLiveUpdatesQuery();
 
         $this->_iInterval = (int)$this->_oQuery->getParam('sys_live_updates_interval');
-
-        $this->_iCacheTTL = 86400;
+        
+        $this->_oCacheObject = $this->_oQuery->getDbCacheObject();
         $this->_sCacheKey = 'sys_live_updates_' . $this->_iProfileId;
+        $this->_iCacheTTL = 86400;
 
         $this->_sJsClass = 'BxDolLiveUpdates';
     	$this->_sJsObject = 'oLiveUpdates';
 
     	$this->_aSystems = $this->_getCachedSystems();
     	$this->_aSystemsActive = array_keys($this->_aSystems);
+    	$this->_aSystemsTransient = array();
     }
 
     /**
@@ -70,10 +87,18 @@ class BxDolLiveUpdates extends BxDolFactory implements iBxDolSingleton
     {
 		$iIndex = (int)bx_get('index');
 
+		$mixedCacheKey = bx_get('hash');
+		if($mixedCacheKey !== false)
+		    $this->_sCacheKey = base64_decode(bx_process_input($mixedCacheKey));
+
 		$mixedSystemsActive = bx_get('systems_active');
 		if($mixedSystemsActive !== false)
 			$this->_aSystemsActive = array_keys($mixedSystemsActive);
 
+        $mixedSystemsTransient = bx_get('systems_transient');
+		if($mixedSystemsTransient !== false)
+			$this->_aSystemsTransient = array_keys($mixedSystemsTransient);
+
 		$this->_aSystems = $this->_getCachedSystems();
     	if(empty($this->_aSystems) || !is_array($this->_aSystems))
     		return array();
@@ -125,6 +150,12 @@ class BxDolLiveUpdates extends BxDolFactory implements iBxDolSingleton
         if(!$this->_iProfileId)
             return false;
 
+        if(empty($this->_aSystemsTransient))
+            $this->_sCacheKey .= $this->_getPageId();
+
+        if(!in_array($sName, $this->_aSystemsTransient))
+    		$this->_aSystemsTransient[] = $sName;
+
     	if(!in_array($sName, $this->_aSystemsActive))
     		$this->_aSystemsActive[] = $sName;
 
@@ -140,19 +171,22 @@ class BxDolLiveUpdates extends BxDolFactory implements iBxDolSingleton
     	}
 
     	$mixedResponce = $this->_getRequestedDataBySystem($this->_aSystems[$sName]);
-    	if($mixedResponce !== false) {
-    		$aCachedData = $this->_getCachedData();
-    		$aCachedData[$sName] = $mixedResponce['count'];
-    		$this->_updateCached('data', $aCachedData);
-    	}
 
-    	return true;
+    	$iCount = 0;
+    	if($mixedResponce !== false && isset($mixedResponce['count']))
+    	    $iCount = (int)$mixedResponce['count'];
+
+    	$aCachedData = $this->_getCachedData();
+		$aCachedData[$sName] = $iCount;
+		$this->_updateCached('data', $aCachedData);
+
+    	return $iCount;
     }
 
     protected function _getCacheInfo()
     {
     	return array(
-    		$this->_oQuery->getDbCacheObject(),
+    		$this->_oCacheObject,
     		$this->_oQuery->genDbCacheKey($this->_sCacheKey),
     		$this->_iCacheTTL
     	);
@@ -200,6 +234,13 @@ class BxDolLiveUpdates extends BxDolFactory implements iBxDolSingleton
     	return $this->_getCached('data');
     }
 
+    protected function _clearCached()
+    {
+        list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo();
+
+        $oCache->delData($sCacheKey);
+    }
+
 	protected function _updateCached($sKey, $aData)
     {
     	list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo();
@@ -242,6 +283,15 @@ class BxDolLiveUpdates extends BxDolFactory implements iBxDolSingleton
 
 		return $aResponce;
     }
+
+    protected function _getPageId()
+    {
+        $aPageParams = array();
+        parse_str($_SERVER['QUERY_STRING'], $aPageParams);
+        $aPageParams = array_diff_assoc($aPageParams, array('start', 'per_page', 'order', 'filter'));
+
+        return '_' . md5(bx_append_url_params($_SERVER['PHP_SELF'], $aPageParams));
+    }
 }
 
 /** @} */

+ 21 - 1
inc/js/classes/BxDolLiveUpdates.js

@@ -12,7 +12,9 @@ function BxDolLiveUpdates(oOptions)
 	this._sObjName = oOptions.sObjName == undefined ? 'oLiveUpdates' : oOptions.sObjName;
 	this._iInterval = oOptions.iInterval == undefined ? 3000 : oOptions.iInterval;
 	this._aSystemsActive = oOptions.aSystemsActive == undefined ? {} : oOptions.aSystemsActive;
+	this._aSystemsTransient = oOptions.aSystemsTransient == undefined ? {} : oOptions.aSystemsTransient;
 	this._bServerRequesting = oOptions.bServerRequesting == undefined ? false : oOptions.bServerRequesting;
+	this._sHash = oOptions.sHash == undefined ? '' : oOptions.sHash;
 
 	this._iIndex = 0;
 	this._iHandler = 0;
@@ -34,6 +36,22 @@ BxDolLiveUpdates.prototype.init = function() {
     });
 };
 
+BxDolLiveUpdates.prototype.add = function(oData) {
+	if(!oData)
+		return;
+
+	if(oData.name != undefined && oData.value != undefined) {
+		if(!this._aSystemsActive[oData.name])
+			this._aSystemsActive[oData.name] = oData.value;
+	
+		if(!this._aSystemsTransient[oData.name])
+			this._aSystemsTransient[oData.name] = 1;
+	}
+
+	if(oData.hash != undefined)
+		this._sHash = oData.hash;
+};
+
 BxDolLiveUpdates.prototype.destroy = function() {
 	if(this._iHandler)
 		clearInterval(this._iHandler);
@@ -51,8 +69,10 @@ BxDolLiveUpdates.prototype.perform = function() {
     $.post(
     	this._sActionsUrl,
         {
-    		systems_active: this._aSystemsActive,
     		index: this._iIndex,
+    		systems_active: this._aSystemsActive,
+    		systems_transient: this._aSystemsTransient,
+    		hash: this._sHash,
     		_t: oDate.getTime()
         },
         function(aData) {

+ 2 - 1
inc/js/functions.js

@@ -255,6 +255,7 @@ function bx_loading_animate (e, aOptions) {
 
 function bx_loading_btn (oElement, bEnable) {
     var oButton = $(oElement);
+    var sClassHeight = oButton.hasClass('bx-btn-small') ? 'bx-btn-small-height' : 'bx-btn-height';
 
     if (oButton.children('div').size())
         oButton = oButton.children('div').first();
@@ -262,7 +263,7 @@ function bx_loading_btn (oElement, bEnable) {
     if(!bEnable)
     	oButton.find('.bx-loading-ajax-btn').remove();
     else if (!oButton.find('.bx-loading-ajax-btn').length)
-    	oButton.append('<b class="bx-loading-ajax-btn bx-btn-height">' + (bUseSvgLoading ? bx_loading_svg('colored', 'bx-btn-height') : '') + '</b>');
+    	oButton.append('<b class="bx-loading-ajax-btn ' + sClassHeight + '">' + (bUseSvgLoading ? bx_loading_svg('colored', sClassHeight) : '') + '</b>');
 
     if(!bUseSvgLoading)
     	bx_loading_animate(oButton.find('.bx-loading-ajax-btn'), aSpinnerSmallOpts);    

+ 29 - 1
template/scripts/BxBaseLiveUpdates.php

@@ -22,6 +22,16 @@ class BxBaseLiveUpdates extends BxDolLiveUpdates
         if(!$this->_iProfileId)
             return '';
 
+		/*
+		 * Clear cache for current user.
+		 * It's needed to reset Transient live updates.
+		 */
+        //$this->_clearCached();
+
+        /*
+         * Load and cache(if it's needed) default(system) 
+         * live updates for current user.
+         */ 
         $aCached = $this->_getCachedData();
 
         $aParams = array_merge(array(
@@ -29,7 +39,9 @@ class BxBaseLiveUpdates extends BxDolLiveUpdates
         	'sObjName' => $this->_sJsObject,
         	'iInterval' => $this->_iInterval,
         	'aSystemsActive' => array_intersect_key($aCached, array_flip($this->_aSystemsActive)),
-        	'bServerRequesting' => !empty($this->_aSystems)
+            'aSystemsTransient' => array_flip($this->_aSystemsTransient),
+        	'bServerRequesting' => !empty($this->_aSystems),
+        	'sHash' => base64_encode($this->_sCacheKey),
         ), $aParams);
 
 		$sContent = "var " . $this->_sJsObject . " = new " . $this->_sJsClass . "(" . json_encode($aParams) . ");";
@@ -39,6 +51,22 @@ class BxBaseLiveUpdates extends BxDolLiveUpdates
 		$oTemplate->addJs(array('BxDolLiveUpdates.js'));
         return $oTemplate->_wrapInTagJsCode($sContent);
     }
+
+    public function add($sName, $iFrequency, $sServiceCall, $bActive = true)
+    {
+        $sCacheKey = $this->_sCacheKey;
+
+        $mixedResult = parent::add($sName, $iFrequency, $sServiceCall, $bActive);
+        if($mixedResult === false)
+            return '';
+
+        $aParams = array('name' => $sName, 'value' => $mixedResult);
+        if($sCacheKey != $this->_sCacheKey)
+            $aParams['hash'] = base64_encode($this->_sCacheKey);
+
+        $sContent = "if(" . $this->_sJsObject . " != undefined) " . $this->_sJsObject . ".add(" . json_encode($aParams) . ");";
+        return BxDolTemplate::getInstance()->_wrapInTagJsCode($sContent);
+    }
 }
 
 /** @} */