Просмотр исходного кода

Ticket #4369 - New PUSH server integration: Usage in Likes (updated) and Reactions.

AntonLV 3 лет назад
Родитель
Сommit
916a6dd754

+ 20 - 6
inc/classes/BxDolVote.php

@@ -207,6 +207,11 @@ class BxDolVote extends BxDolObject
         return $aVote['rate'];
     }
 
+    public function getSocketName()
+    {
+        return $this->_sSystem . '_' . $this->_sType;
+    }
+
     /**
      * Actions functions
      */
@@ -252,10 +257,11 @@ 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));
 
-        $a = $this->_returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aVoteData, !$bVoted, $aRequestParamsData);        
-        BxDolSockets::getInstance()->sendEvent($this->_sSystem . '_' . $this->_sType, $iObjectId, 'voted', json_encode($a));
-        
-        return $a;
+        $aResult = $this->_returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aVoteData, !$bVoted, $aRequestParamsData);
+
+        BxDolSockets::getInstance()->sendEvent($this->getSocketName(), $iObjectId, 'voted', json_encode($this->_returnVoteDataForSocket($aResult)));
+
+        return $aResult;
     }
 
     public function actionGetVotedBy()
@@ -348,11 +354,11 @@ class BxDolVote extends BxDolObject
         return $this->_oQuery->putVote($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bPerformUndo);
     }
 
-    protected function _returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bVoted, $aParams = array())
+    protected function _returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bVoted, $aParams = [])
     {
         $bUndo = $this->isUndo();
         $aVote = $this->_getVote($iObjectId, true);
-        
+
         return [
             'code' => 0,
             'rate' => $aVote['rate'],
@@ -367,6 +373,14 @@ class BxDolVote extends BxDolObject
             'disabled' => $bVoted && !$bUndo,
         ];
     }
+    
+    protected function _returnVoteDataForSocket($aData, $aMask = [])
+    {
+        if(empty($aMask) || !is_array($aMask))
+            $aMask = ['code', 'rate', 'count', 'countf'];
+
+        return array_intersect_key($aData, array_flip($aMask));
+    }
 
     protected function _prepareRequestParamsData($aParams, $aParamsAdd = array())
     {

+ 20 - 15
inc/classes/BxDolVoteReactions.php

@@ -213,7 +213,7 @@ class BxDolVoteReactions extends BxTemplVote
         return $aData;
     }
 
-    protected function _returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bVoted, $aParams = array())
+    protected function _returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bVoted, $aParams = [])
     {
         $aReactions = $this->getReactions(true);
         $sReaction = $aData['reaction'];
@@ -245,9 +245,9 @@ class BxDolVoteReactions extends BxTemplVote
         $fTotalR = $iTotalC != 0 ? round($iTotalS / $iTotalC, 2) : 0;
 
         $iCount = (int)$aVote['count_' . $sReaction];
-        $aResult = array(
+        $aResult = [
             'code' => 0,
-            'reaction' => $aReactions[$sSwitchTo]['name'],
+            'reaction' => $this->_bApi ? $aReactions[$sSwitchTo]['name'] : $sReaction,
             'rate' => $aVote['rate_' . $sReaction],
             'count' => $iCount,
             'countf' => $iCount > 0 ? $this->_getCounterLabel($iCount, array('reaction' => $sReaction)) : '',
@@ -259,45 +259,50 @@ class BxDolVoteReactions extends BxTemplVote
             'label_click' => $sJsClick,
             'voted' => $bVoted,
             'disabled' => $bVoted && !$this->isUndo(),
-            'total' => array(
+            'total' => [
                 'rate' => $fTotalR,
                 'count' => $iTotalC,
-                'countf' => $iTotalC > 0 ? $this->_getCounterLabel($iTotalC, array('show_counter_label_icon' => false, 'reaction' => '')) : '',
-            )
-        );
+                'countf' => $iTotalC > 0 ? $this->_getCounterLabel($iTotalC, ['show_counter_label_icon' => false, 'reaction' => '']) : '',
+            ]
+        ];
 
         return $aResult;
     }
 
-    protected function _getIconDoWithTrack($bVoted, $aTrack = array())
+    protected function _returnVoteDataForSocket($aData, $aMask = [])
+    {
+        return parent::_returnVoteDataForSocket($aData, ['code', 'reaction', 'rate', 'count', 'countf', 'total']);
+    }
+
+    protected function _getIconDoWithTrack($bVoted, $aTrack = [])
     {
         $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
 
     	return $this->getIcon($sReaction);
     }
-    
-    protected function _getEmojiDoWithTrack($bVoted, $aTrack = array())
+
+    protected function _getEmojiDoWithTrack($bVoted, $aTrack = [])
     {
         $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
 
     	return $this->getEmoji($sReaction);
     }
-    
-    protected function _getImageDoWithTrack($bVoted, $aTrack = array())
+
+    protected function _getImageDoWithTrack($bVoted, $aTrack = [])
     {
         $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
 
     	return $this->getImage($sReaction);
     }
 
-    protected function _getTitleDoWithTrack($bVoted, $aTrack = array())
+    protected function _getTitleDoWithTrack($bVoted, $aTrack = [])
     {
         $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
 
     	return $this->_aDataList[$sReaction]['title'];
     }
-    
-    protected function _getTitleDoBy($aParams = array())
+
+    protected function _getTitleDoBy($aParams = [])
     {
         if(isset($aParams['show_counter_style']) && $aParams['show_counter_style'] == self::$_sCounterStyleCompound)
             return _t('_vote_do_by_reactions');

+ 4 - 5
inc/js/classes/BxDolVote.js

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

+ 23 - 20
template/scripts/BxBaseVote.php

@@ -61,7 +61,7 @@ class BxBaseVote extends BxDolVote
         return $this->_sJsObjName;
     }
 
-    public function getJsScript($aParams = array())
+    public function getJsScript($aParams = [])
     {
         $sJsObjName = $this->getJsObjectName();
         $sJsObjClass = $this->getJsClassName();
@@ -75,6 +75,17 @@ class BxBaseVote extends BxDolVote
         return $this->_oTemplate->_wrapInTagJsCode($sCode);
     }
 
+    public function getJsScriptSocket($aParams = [])
+    {
+        $oSockets = BxDolSockets::getInstance();
+        if(!$oSockets->isEnable())
+            return '';
+
+        $sCode = $oSockets->getSubscribeJsCode($this->getSocketName(), $this->getId(), 'voted', $this->getJsObjectName() . '.onVoteAs(data)');
+
+        return $this->_oTemplate->_wrapInTagJsCode($sCode);
+    }
+
     public function getJsClick($iValue = 0)
     {
         return $this->getJsObjectName() . '.vote(this, ' . $iValue . ')';
@@ -117,36 +128,28 @@ 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) : '';
 
-        $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(
+        return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounter(), [
+            'bx_if:show_text' => [
                 'condition' => !$bShowActive,
-                'content' => array(
+                'content' => [
                     'class' => $sClass,
                     'bx_repeat:attrs' => $aTmplVarsAttrs,
                     'content' => $sContent
-                )
-            ),
-            'bx_if:show_link' => array(
+                ]
+            ],
+            'bx_if:show_link' => [
                 'condition' => $bShowActive,
-                'content' => array(
+                'content' => [
                     'class' => $sClass,
                     'bx_repeat:attrs' => $aTmplVarsAttrs,
                     'content' => $sContent
-                )
-            ),
+                ]
+            ],
             'class' => $sClass,
             'bx_repeat:attrs' => $aTmplVarsAttrs,
             'content' => $sContent,
-            'script' => $bShowScript ? $this->getJsScript($aParams) : ''
-        ));
+            'script' => $bShowScript ? $this->getJsScript($aParams) . $this->getJsScriptSocket($aParams) : ''
+        ]);
     }
 
     public function getLegend($aParams = array())

+ 3 - 3
template/scripts/BxBaseVoteReactions.php

@@ -181,7 +181,7 @@ class BxBaseVoteReactions extends BxDolVoteReactions
                 'condition' => false,
                 'content' => array()
             ),
-            'script' => $bShowScript ? $this->getJsScript($aParams) : ''
+            'script' => $bShowScript ? $this->getJsScript($aParams) . $this->getJsScriptSocket($aParams) : ''
         ));
     }
 
@@ -235,7 +235,7 @@ class BxBaseVoteReactions extends BxDolVoteReactions
                     'counter' => $sResult,
                 )
             ),
-            'script' => $bShowScript ? $this->getJsScript($aParams) : ''
+            'script' => $bShowScript ? $this->getJsScript($aParams) . $this->getJsScriptSocket($aParams) : ''
         ));
     }
 
@@ -321,7 +321,7 @@ class BxBaseVoteReactions extends BxDolVoteReactions
                 'condition' => false,
                 'content' => array()
             ),
-            'script' => $bShowScript ? $this->getJsScript($aParams) : ''
+            'script' => $bShowScript ? $this->getJsScript($aParams) . $this->getJsScriptSocket($aParams) : ''
         ));
     }