Explorar el Código

#4369 - New PUSH server integration (Likes)

roman.lesnikov@gmail.com hace 3 años
padre
commit
516a4280ed

+ 5 - 0
inc/classes/BxDolSockets.php

@@ -37,6 +37,11 @@ class BxDolSockets extends BxDolFactory implements iBxDolSingleton
         return $GLOBALS['bxDolClasses']['BxDolSockets'];
     }
     
+     private function writeLog($sString)
+	{
+        bx_log('sys_sockets', $sString);
+	}
+    
     public function isEnable()
     {
         return $this->_sIsEnabled;

+ 14 - 8
inc/classes/BxDolSocketsSoketi.php

@@ -17,14 +17,20 @@ class BxDolSocketsSoketi extends BxDolSockets
     
     public function sendEvent($sModule, $iContentId, $sEvent, $sMessage)
     {
-        $oPusher = new Pusher\Pusher(getParam('sys_sockets_key'), getParam('sys_sockets_secret'), getParam('sys_sockets_app_id'), [
-            'host' => $this->_sHost,
-            'port' => $this->_sPort,
-            'scheme' => $this->_sScheme,
-            'encrypted' => true,
-            'useTLS' => false,
-        ]);
-        $b = $oPusher->trigger($sModule . '_' . $iContentId, $sEvent, $sMessage);
+        try{
+            $oPusher = new Pusher\Pusher(getParam('sys_sockets_key'), getParam('sys_sockets_secret'), getParam('sys_sockets_app_id'), [
+                'host' => $this->_sHost,
+                'port' => $this->_sPort,
+                'scheme' => $this->_sScheme,
+                'encrypted' => true,
+                'useTLS' => false,
+            ]);
+            $b = $oPusher->trigger($sModule . '_' . $iContentId, $sEvent, $sMessage);
+        }
+         catch (Exception $oException) {
+            $this->writeLog($oException->getFile() . ':' . $oException->getLine() . ' ' . $oException->getMessage());
+            return false;
+        }
         
     }
     

+ 4 - 1
inc/classes/BxDolVote.php

@@ -252,7 +252,10 @@ class BxDolVote extends BxDolObject
         bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'doVote', $iObjectId, $iAuthorId, array_merge(['vote_id' => $iId, 'vote_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId], $aVoteData));
         bx_alert('vote', ($bPerformUndo ? 'un' : '') . 'do', $iId, $iAuthorId, array_merge(['object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId], $aVoteData));
 
-        return $this->_returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aVoteData, !$bVoted, $aRequestParamsData);
+        $a = $this->_returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aVoteData, !$bVoted, $aRequestParamsData);        
+        BxDolSockets::getInstance()->sendEvent($this->_sSystem . '_' . $this->_sType, $iObjectId, 'voted', json_encode($a));
+        
+        return $a;
     }
 
     public function actionGetVotedBy()

+ 8 - 0
inc/js/classes/BxDolVote.js

@@ -64,8 +64,16 @@ BxDolVote.prototype.vote = function(oLink, iValue, onComplete)
     );
 };
 
+BxDolVote.prototype.onVoteAs = function(id, sys, data)
+{
+    var oData = JSON.parse(data);  
+    var oLink = $('#bx-vote-' + sys + '-' + id+' A').first();
+    this.onVote(oLink, oData)
+};
+
 BxDolVote.prototype.onVote = function(oLink, oData, onComplete)
 {
+    console.log(oLink, oData);
     if(oData && oData.code != 0)
         return;
 

+ 9 - 2
template/scripts/BxBaseVote.php

@@ -88,7 +88,6 @@ class BxBaseVote extends BxDolVote
     public function getCounter($aParams = [])
     {
         $aParams = array_merge($this->_aElementDefaults, $aParams);
-
         $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
         $bShowEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
         $bShowActive = $this->isAllowedVoteViewVoters() && (!isset($aParams['show_counter_active']) || (bool)$aParams['show_counter_active'] === true);
@@ -118,7 +117,15 @@ class BxBaseVote extends BxDolVote
         $aVote = !empty($aParams['vote']) && is_array($aParams['vote']) ? $aParams['vote'] : $this->_getVote();
         $sContent = $bShowEmpty || (int)$aVote['count'] > 0 ? $this->_getCounterLabel($aVote['count'], $aParams) : '';
 
-        return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounter(), array(
+        $oSockets= BxDolSockets::getInstance();
+        
+        $sJs = '';
+        
+        if ($oSockets->isEnable()){
+            $sJs .= $this->_oTemplate->_wrapInTagJsCode($oSockets->getSubscribeJsCode($this->_sSystem . '_' . $this->_sType, $this->getId(), 'voted', $this->getJsObjectName().'.onVoteAs('.$this->getId().',"' . $this->_sType . '-' . str_replace('_', '-', $this->_sSystem) . '", data)'));
+        }
+        
+        return $sJs . $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounter(), array(
             'bx_if:show_text' => array(
                 'condition' => !$bShowActive,
                 'content' => array(