Explorar el Código

Merge branch 'master' of https://github.com/unaio/una

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

+ 6 - 1
inc/classes/BxDolObject.php

@@ -16,6 +16,8 @@ class BxDolObject extends BxDolFactory implements iBxDolReplaceable
     protected $_oTemplate = null;
     protected $_oQuery = null;
 
+    protected $_bApi = false;
+
     protected $_iId = 0; ///< item id the action to be performed with
     protected $_sSystem = ''; ///< current system name
     protected $_aSystem = array(); ///< current system array
@@ -228,8 +230,11 @@ class BxDolObject extends BxDolFactory implements iBxDolReplaceable
         return true;
     }
 
-    public function getElementApi($aParams = [])
+    public function getElementAPI($aParams = [])
     {
+        if(!($this->_bApi = bx_is_api()))
+            return;
+
         //TODO: Implement for Objects like Views, Votes, etc.
         $sClass = get_class($this);
         return [

+ 1 - 0
inc/classes/BxDolVote.php

@@ -83,6 +83,7 @@ class BxDolVote extends BxDolObject
     protected $_aVote;
 
     protected $_aElementDefaults;
+    protected $_aElementDefaultsApi;
 
     protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
     {

+ 6 - 1
modules/base/general/classes/BxBaseModGeneralMenuViewActions.php

@@ -259,7 +259,12 @@ 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))

+ 49 - 2
template/scripts/BxBaseVote.php

@@ -27,7 +27,8 @@ class BxBaseVote extends BxDolVote
     {
         parent::__construct($sSystem, $iId, $iInit, $oTemplate);
 
-        $this->_aElementDefaults = array();
+        $this->_aElementDefaults = [];
+        $this->_aElementDefaultsApi = [];
 
         $this->_bCssJsAdded = false;
         $this->_sStylePrefix = 'bx-vote';
@@ -171,6 +172,52 @@ class BxBaseVote extends BxDolVote
 
         return $this->_oTemplate->parseHtmlByContent($this->$sMethod(), $aTmplVars);
     }
+    
+    public function getElementAPI($aParams = [])
+    {
+        if(!($this->_bApi = bx_is_api()))
+            return;
+
+        if(!$this->isEnabled())
+            return ['id' => 1, 'type' => 'msg', 'data' => _t('_vote_err_not_enabled')];
+
+        $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
+
+        $iObjectId = $this->getId();
+        $iAuthorId = $this->_getAuthorId();
+
+        $bCount = $this->_isCount();
+        $isAllowedVote = $this->isAllowedVote();
+        $isAllowedVoteView = $this->isAllowedVoteView();
+        $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId);
+        $aParams['track'] = $aParams['is_voted'] ? $this->_getTrack($iObjectId, $iAuthorId) : array();
+
+        //--- Do Vote
+        $bDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
+        $aDoVote = $bDoVote ? $this->_getDoVote($aParams, $isAllowedVote) : [];
+
+        //--- Counter
+        $bCounter = $this->_isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
+        $aCounter = $bCounter ? $this->getCounterAPI(array_merge($aParams, [
+            'show_counter_only' => false, 
+            'show_script' => false
+        ])) : [];
+
+        //--- Legend
+        $bLegend = $this->_isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
+        $aLegend = $bLegend ? $this->getLegend($aParams) : [];
+
+        if(!$bDoVote && !$bCounter && !$bLegend)
+            return ['id' => 1, 'type' => 'msg', 'data' => ''];
+
+        return [
+            'type' => $this->_sType,
+            'params' => $aParams,
+            'action' => $aDoVote,
+            'counter' => $aCounter,
+            'legend' => $aLegend,
+        ];
+    }
 
     /**
      * Internal methods.
@@ -269,7 +316,7 @@ class BxBaseVote extends BxDolVote
 
     protected function _getDoVote($aParams = array(), $isAllowedVote = true)
     {
-        return '';
+        return $this->_bApi ? [] : '';
     }
 
     protected function _getCounterLabel($iCount, $aParams = array())

+ 64 - 4
template/scripts/BxBaseVoteReactions.php

@@ -28,7 +28,7 @@ class BxBaseVoteReactions extends BxDolVoteReactions
             'do_popup' => 'bx-vr-do-popup-' . $sHtmlId
         ));
 
-        $this->_aElementDefaults = array(
+        $this->_aElementDefaults = [
             'show_do_vote_as_button' => false,
             'show_do_vote_as_button_small' => false,
             'show_do_vote_icon' => true,
@@ -39,7 +39,10 @@ class BxBaseVoteReactions extends BxDolVoteReactions
             'show_counter_style' => self::$_sCounterStyleDivided, //--- Alloved styles are 'simple', 'divided' and 'compound'
             'show_legend' => false,
             'show_script' => true
-        );
+        ];
+        $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
+            'show_counter' => true,
+        ]);
 
         $this->_sTmplNameBySummary = 'vote_by_summary_reactions.html';
         $this->_sTmplNameByList = 'vote_by_list_reactions.html';
@@ -98,6 +101,33 @@ class BxBaseVoteReactions extends BxDolVoteReactions
         return $this->$sMethod($aParams);
     }
 
+    public function getCounterAPI($aParams = [])
+    {
+        $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
+
+        $bVote = $this->_isVote();
+        $aVote = $this->_getVote();
+        $aReactions = $this->getReactions();
+
+        $aItems = [];
+        foreach($aReactions as $sName) {
+            $iCount = (int)$aVote['count_' . $sName];
+            $aItems[] = [
+                'name' => $sName,
+                'count' => $iCount,
+                'sum' => $aVote['sum_' . $sName],
+                'rate' => $aVote['rate_' . $sName],
+                'icon' => $this->getIcon($sName, false),
+                'emoji' => $this->getEmoji($sName),
+                'title' => _t($this->_aDataList[$sName]['title']),
+            ];
+        }
+
+        $sStyle = !empty($aParams['show_counter_style']) ? $aParams['show_counter_style'] : $this->_aElementDefaults['show_counter_style'];
+
+        return ['type' => $sStyle, 'items' => $aItems];
+    }
+
     public function _getCounterSimple($aParams = array())
     {
         $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
@@ -311,6 +341,16 @@ class BxBaseVoteReactions extends BxDolVoteReactions
 
         return parent::getElement($aParams);
     }
+    
+    public function getElementAPI($aParams = [])
+    {
+        $aResult = parent::getElementAPI($aParams);
+
+        if(($oDoVoteMenu = $this->_getDoVoteMenu()) !== false)
+            $aResult['action']['menu'] = $oDoVoteMenu->getCodeAPI();
+
+        return $aResult;
+    }
 
     /**
      * Internal methods.
@@ -365,6 +405,17 @@ class BxBaseVoteReactions extends BxDolVoteReactions
         else
             $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : ' ' . $this->_sStylePrefix . '-disabled';
 
+        if($this->_bApi)
+            return [
+                'is_undo' => $bUndo,
+                'is_voted' => $bVoted,
+                'is_disabled' => $bDisabled,
+                'value' => $iValue,
+                'reaction' => $sReaction,
+                'icon' => ($sEmoji = $this->getEmoji($sReaction)) != '' ? $sEmoji : '', 
+                'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
+            ];
+
         return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
             'class' => $this->_sStylePrefix . '-do-vote' . $sClass,
             'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
@@ -398,19 +449,28 @@ class BxBaseVoteReactions extends BxDolVoteReactions
         ));
     }
 
-    public function _getDoVotePopup($iValue = 0)
+    public function _getDoVoteMenu($iValue = 0)
     {
         if(empty($iValue))
             $iValue = $this->getValue();
 
         $oMenu = BxTemplMenu::getObjectInstance($this->_sMenuDoVote);
         if(!$oMenu)
-            return '';
+            return false;
 
         $oMenu->setParams(array(
             'object' => &$this,
             'value' => $iValue,
         ));
+        return $oMenu;
+    }
+
+    public function _getDoVotePopup($iValue = 0)
+    {
+        $oMenu = $this->_getDoVoteMenu($iValue);
+        if($oMenu === false)
+            return '';
+
         return $oMenu->getCode();
     }