Browse Source

Ticket #4148 - Build-in API support: Likes and Reactions.

AntonLV 3 years ago
parent
commit
0f96b76228

+ 3 - 4
inc/classes/BxDolVote.php

@@ -355,7 +355,9 @@ class BxDolVote extends BxDolObject
             'count' => $aVote['count'],
             'countf' => (int)$aVote['count'] > 0 ? $this->_getCounterLabel($aVote['count'], $aParams) : '',
             'label_icon' => $this->_getIconDo($bVoted),
+            'label_emoji' => $this->_getEmojiDo($bVoted),
             'label_title' => _t($this->_getTitleDo($bVoted)),
+            'voted' => $bVoted,
             'disabled' => $bVoted && !$bUndo,
         );
     }
@@ -383,11 +385,8 @@ class BxDolVote extends BxDolObject
     protected function _isVote($iObjectId = 0, $bForceGet = false)
     {
         $aVote = $this->_getVote($iObjectId, $bForceGet);
-        foreach($aVote as $sKey => $iValue)
-            if(strpos($sKey, 'count_') !== false && !empty($iValue))
-                return true;
 
-        return false;
+        return (int)$aVote['count'] > 0;
     }
 
     protected function _getTrack($iObjectId, $iAuthorId)

+ 5 - 0
inc/classes/BxDolVoteLikes.php

@@ -41,6 +41,11 @@ class BxDolVoteLikes extends BxTemplVote
     	return $bVoted && $this->isUndo() ? 'thumbs-up' : 'thumbs-up';
     }
 
+    protected function _getEmojiDo($bVoted)
+    {
+    	return $bVoted && $this->isUndo() ? '👍' : '👍';
+    }
+
     protected function _getTitleDo($bVoted)
     {
     	return $bVoted && $this->isUndo() ? '_vote_do_unlike' : '_vote_do_like';

+ 10 - 0
inc/classes/BxDolVoteReactions.php

@@ -156,6 +156,16 @@ class BxDolVoteReactions extends BxTemplVote
     /**
      * Internal functions
      */
+    protected function _isVote($iObjectId = 0, $bForceGet = false)
+    {
+        $aVote = $this->_getVote($iObjectId, $bForceGet);
+        foreach($aVote as $sKey => $iValue)
+            if(strpos($sKey, 'count_') !== false && !empty($iValue))
+                return true;
+
+        return false;
+    }
+
     protected function _isDuplicate($iObjectId, $iAuthorId, $iAuthorIp, $bVoted)
     {
         return $bVoted && !$this->isUndo();

+ 7 - 2
modules/base/general/classes/BxBaseModGeneralMenuViewActions.php

@@ -225,8 +225,13 @@ class BxBaseModGeneralMenuViewActions extends BxTemplMenuCustom
             $aObjectOptions = array_merge($aObjectOptions, $aParams['object_options']);
 
         if($this->_bIsApi)
-            return $oObject->getElementApi($aObjectOptions);
-        
+            return [
+                'id' => $aItem['id'],
+                'name' => $aItem['name'],
+                'display_type' => 'element',
+                'data' => $oObject->getElementApi($aObjectOptions)
+            ];
+
         $sResult = $oObject->getElementBlock($aObjectOptions);
         if(empty($sResult))
             return '';

+ 28 - 2
template/scripts/BxBaseVoteLikes.php

@@ -23,7 +23,7 @@ class BxBaseVoteLikes extends BxDolVoteLikes
             'main' => 'bx-vote-likes-' . $sHtmlId
         ));
 
-        $this->_aElementDefaults = array(
+        $this->_aElementDefaults = [
             'show_do_vote_as_button' => false,
             'show_do_vote_as_button_small' => false,
             'show_do_vote_icon' => true,
@@ -35,7 +35,10 @@ class BxBaseVoteLikes extends BxDolVoteLikes
             'show_counter_label_text' => true,
             'show_legend' => false,
             'show_script' => true
-        );
+        ];
+        $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
+            'show_counter' => true,
+        ]);
 
         $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('vote_do_vote_label_likes.html');
         $this->_sTmplContentCounterLabel = $this->_oTemplate->getHtml('vote_counter_label_likes.html');
@@ -64,6 +67,19 @@ class BxBaseVoteLikes extends BxDolVoteLikes
 
         return parent::getCounter($aParams);
     }
+    
+    public function getCounterAPI($aParams = [])
+    {
+        $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
+
+        $aVote = $this->_getVote();
+        $bVoted = false;
+
+        return array_merge($aVote, [
+            'icon' => $this->_getEmojiDo($bVoted), 
+            'title' => _t($this->_getTitleDo($bVoted)),
+        ]);
+    }
 
     public function getElement($aParams = array())
     {
@@ -98,6 +114,16 @@ class BxBaseVoteLikes extends BxDolVoteLikes
         if($bDisabled)
             $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-vote-disabled';
 
+        if($this->_bApi)
+            return [
+                'is_undo' => $this->isUndo(),
+                'is_voted' => $bVoted,
+                'is_disabled' => $bDisabled,
+                'value' => $this->getValue(),
+                'icon' => $this->_getEmojiDo($bVoted), 
+                'title' => _t($this->_getTitleDo($bVoted)),
+            ];
+
         return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
             'class' => $this->_sStylePrefix . '-do-vote ' . $sClass,
             'title' => _t($this->_getTitleDo($bVoted)),

+ 1 - 3
template/scripts/BxBaseVoteReactions.php

@@ -105,7 +105,6 @@ class BxBaseVoteReactions extends BxDolVoteReactions
     {
         $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
 
-        $bVote = $this->_isVote();
         $aVote = $this->_getVote();
         $aReactions = $this->getReactions();
 
@@ -117,8 +116,7 @@ class BxBaseVoteReactions extends BxDolVoteReactions
                 'count' => $iCount,
                 'sum' => $aVote['sum_' . $sName],
                 'rate' => $aVote['rate_' . $sName],
-                'icon' => $this->getIcon($sName, false),
-                'emoji' => $this->getEmoji($sName),
+                'icon' => $this->getEmoji($sName),
                 'title' => _t($this->_aDataList[$sName]['title']),
             ];
         }

+ 18 - 2
template/scripts/BxBaseVoteServices.php

@@ -45,8 +45,8 @@ class BxBaseVoteServices extends BxDol
         return [
             'is_voted' => $aResult['voted'],
             'is_disabled' => $aResult['disabled'],
-            'icon' => !empty($aResult['label_emoji']) ? $aResult['label_emoji'] : $aDefaultInfo['emoji'],
-            'title' => !empty($aResult['label_title']) ? $aResult['label_title'] : '',
+            'icon' => $aResult['label_emoji'],
+            'title' => $aResult['label_title'],
             'counter' => $oVote->getVote()
         ];
     }
@@ -89,6 +89,22 @@ class BxBaseVoteServices extends BxDol
         return $this->$sMethod($aParams, $oVote);
     }
 
+    protected function _serviceGetPerformedByLikes($aParams, &$oVote)
+    {
+        $aValues = $oVote->getQueryObject()->getPerformedBy($oVote->getId());
+
+        $aTmplUsers = [];
+        foreach($aValues as $mValue) {
+            $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
+
+            $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
+        }
+
+        return [
+            'performed_by' => $aTmplUsers
+        ];
+    }
+
     protected function _serviceGetPerformedByReactions($aParams, &$oVote)
     {
         $sReaction = $aParams['reaction'];