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

Ticket #4237 - Separated counters in Vote Up/Down.

AntonLV 2 лет назад
Родитель
Сommit
eb60759f2e

+ 12 - 1
inc/classes/BxDolScore.php

@@ -78,6 +78,9 @@ define('BX_DOL_SCORE_DO_DOWN', 'down');
 
 class BxDolScore extends BxDolObject
 {
+    protected static $_sCounterStyleSimple = 'simple'; // total counter only.
+    protected static $_sCounterStyleDivided = 'divided'; // counters [icon + counter] divided by action (up/down).
+
     protected $_aScore;
 
     protected $_aElementDefaults;
@@ -235,6 +238,14 @@ class BxDolScore extends BxDolObject
            return '';
 
         $aParams = $this->_getRequestParamsData();
+
+        if(($sType = bx_get('type')) !== false) {
+            $sType = bx_process_input($sType);
+
+            if(in_array($sType, [BX_DOL_SCORE_DO_UP, BX_DOL_SCORE_DO_DOWN]))
+                $aParams['type'] = $sType;
+        }
+
         return $this->_getVotedBy($aParams);
     }
 
@@ -479,7 +490,7 @@ class BxDolScore extends BxDolObject
     {
         $aScore = $this->_getVote($iObjectId, $bForceGet);
 
-        return (int)$aScore['count'] > 0;
+        return $this->_isCount($aScore);
     }
 
     protected function _isCount($aScore = [])

+ 15 - 7
inc/classes/BxDolScoreQuery.php

@@ -32,22 +32,30 @@ class BxDolScoreQuery extends BxDolObjectQuery
         $this->_sMethodGetEntry = 'getScore';
     }
 
-    public function getPerformedBy($iObjectId, $iStart = 0, $iPerPage = 0)
+    public function getPerformedBy($iObjectId, $aParams = [], $iStart = 0, $iPerPage = 0)
     {
+        $aBindings = [
+            'object_id' => $iObjectId
+        ];
+        $sWhereClause = " AND `object_id`=:object_id";
+
+        if(!empty($aParams['type'])) {
+            $sWhereClause .= " AND `type`=:type";
+            $aBindings['type'] = $aParams['type'];
+        }
+
         $sLimitClause = "";
-        if(!empty($iPerPage))
-            $sLimitClause = $this->prepareAsString(" LIMIT ?, ?", $iStart, $iPerPage);
+        if(!empty($aParams['per_page']))
+            $sLimitClause = $this->prepareAsString(" LIMIT ?, ?", $aParams['start'], $aParams['per_page']);
 
         $sQuery = "SELECT 
             	`author_id` AS `id`, 
             	`type` AS `vote_type`, 
             	`date` AS `vote_date` 
             FROM `{$this->_sTableTrack}` 
-            WHERE `object_id`=:object_id" . $sLimitClause;
+            WHERE 1" . $sWhereClause . $sLimitClause;
 
-        return $this->getAll($sQuery, array(
-            'object_id' => $iObjectId
-        ));
+        return $this->getAll($sQuery, $aBindings);
     }
 
     public function isPostTimeoutEnded($iObjectId, $iAuthorId, $sAuthorIp)

+ 7 - 5
inc/js/classes/BxDolScore.js

@@ -44,14 +44,16 @@ BxDolScore.prototype.init = function()
     return;
 };
 
-BxDolScore.prototype.toggleByPopup = function(oLink) {
-    var $this = this;
-    var oData = this._getDefaultParams();
-    oData['action'] = 'GetVotedBy';
+BxDolScore.prototype.toggleByPopup = function(oLink, sType) {
+    var oParams = this._getDefaultParams();
+    oParams['action'] = 'GetVotedBy';
+    if(sType)
+        oParams['type'] = sType;
 
     $(oLink).dolPopupAjax({
         id: this._aHtmlIds['by_popup'], 
-        url: bx_append_url_params(this._sActionsUri, oData)
+        url: bx_append_url_params(this._sActionsUri, oParams),
+        removeOnClose: true
     });
 };
 

+ 6 - 0
template/css/score.css

@@ -40,6 +40,9 @@ div.bx-score-by-item .bx-score-bi-vote {
 
 
 /*--- Counter Styles ---*/
+.bx-score-counter-wrapper.bx-sc-hidden {
+    display: none;
+}
 .bx-score-counter .bx-score-counter-label .bx-score-counter-text {
     width: auto;
     height: auto;
@@ -49,6 +52,9 @@ div.bx-score-by-item .bx-score-bi-vote {
 .bx-base-general-entity-actions .bx-menu-custom-hor.bx-menu-more-auto .bx-score-counter-holder {
     border-width: 0;
 }
+.bx-base-general-entity-actions .bx-menu-custom-hor.bx-menu-more-auto .bx-score-counter-holder.bx-score-ch-divided {
+    margin-left: 0.125rem;
+}
 .bx-base-general-entity-actions .bx-menu-custom-hor.bx-menu-more-auto .bx-score-counter-holder .bx-score-counter-label {
     padding: 0;
 }

+ 6 - 0
template/score_counter_wrapper.html

@@ -0,0 +1,6 @@
+<div id="__html_id__" class="__style_prefix__-counter-wrapper __style_prefix__-cw-__style__ __class__">
+    <div class="__style_prefix__-counter-wrapper-cnt flex items-center">
+        __counter__
+        __script__
+    </div>
+</div>

+ 8 - 2
template/score_element_block.html

@@ -1,13 +1,19 @@
 <div id="__html_id__" class="sys-action __style_prefix__ __class__ flex items-center whitespace-nowrap" bx_score_data_score="__score__" bx_score_data_cup="__cup__" bx_score_data_cdown="__cdown__">
     <div class="sys-action-cnt __style_prefix__-cnt bx-def-valign-center bx-clearfix">
         <bx_if:show_do_vote_up>
-            <div class="sys-action-element-holder __style_prefix__-element-holder __style_prefix__-eh-up">__do_vote__</div>
+            <div class="sys-action-element-holder __style_prefix__-element-holder __style_prefix__-eh-__style__">__do_vote__</div>
+            <bx_if:show_counter_up>
+                <div class="sys-action-counter-holder __style_prefix__-counter-holder __style_prefix__-ch-__style__ bx-def-margin-sec-left-auto"<bx_if:show_cup_hidden> style="display:none"</bx_if:show_cup_hidden>>__counter__</div>
+            </bx_if:show_counter_up>
         </bx_if:show_do_vote_up>
         <bx_if:show_counter>
             <div class="sys-action-counter-holder __style_prefix__-counter-holder bx-def-margin-sec-left-auto bx-btn-border"<bx_if:show_hidden> style="display:none"</bx_if:show_hidden>>__counter__</div>
         </bx_if:show_counter>
         <bx_if:show_do_vote_down>
-            <div class="sys-action-element-holder __style_prefix__-element-holder __style_prefix__-eh-down bx-def-margin-sec-left-auto">__do_vote__</div>
+            <div class="sys-action-element-holder __style_prefix__-element-holder __style_prefix__-eh-__style__ bx-def-margin-sec-left-auto">__do_vote__</div>
+            <bx_if:show_counter_down>
+                <div class="sys-action-counter-holder __style_prefix__-counter-holder __style_prefix__-ch-__style__ bx-def-margin-sec-left-auto"<bx_if:show_cdown_hidden> style="display:none"</bx_if:show_cdown_hidden>>__counter__</div>
+            </bx_if:show_counter_down>
         </bx_if:show_do_vote_down>
     </div>
     <bx_if:show_legend>__legend__</bx_if:show_legend>

+ 5 - 1
template/score_element_inline.html

@@ -1,3 +1,7 @@
 <span id="__html_id__" class="sys-action __style_prefix__ __class__ inline-flex items-center whitespace-nowrap" bx_score_data_score="__score__" bx_score_data_cup="__cup__" bx_score_data_cdown="__cdown__">
-    <span class="sys-action-cnt __style_prefix__-cnt bx-def-valign-center"><bx_if:show_do_vote_up><span class="sys-action-element-holder __style_prefix__-element-holder __style_prefix__-eh-up">__do_vote__</span></bx_if:show_do_vote_up><bx_if:show_counter><span class="sys-action-counter-holder __style_prefix__-counter-holder bx-def-margin-sec-left-auto bx-btn-border"<bx_if:show_hidden> style="display:none"</bx_if:show_hidden>>__counter__</span></bx_if:show_counter><bx_if:show_do_vote_down><span class="sys-action-element-holder __style_prefix__-element-holder __style_prefix__-eh-down bx-def-margin-sec-left-auto">__do_vote__</span></bx_if:show_do_vote_down></span>__script__
+    <span class="sys-action-cnt __style_prefix__-cnt bx-def-valign-center">
+        <bx_if:show_do_vote_up><span class="sys-action-element-holder __style_prefix__-element-holder __style_prefix__-eh-__style__">__do_vote__</span><bx_if:show_counter_up><span class="sys-action-counter-holder __style_prefix__-counter-holder __style_prefix__-ch-__style__ bx-def-margin-sec-left-auto"<bx_if:show_cup_hidden> style="display:none"</bx_if:show_cup_hidden>>__counter__</span></bx_if:show_counter_up></bx_if:show_do_vote_up>
+        <bx_if:show_counter><span class="sys-action-counter-holder __style_prefix__-counter-holder __style_prefix__-ch-__style__ bx-def-margin-sec-left-auto bx-btn-border"<bx_if:show_hidden> style="display:none"</bx_if:show_hidden>>__counter__</span></bx_if:show_counter>
+        <bx_if:show_do_vote_down><span class="sys-action-element-holder __style_prefix__-element-holder __style_prefix__-eh-__style__ bx-def-margin-sec-left-auto">__do_vote__</span><bx_if:show_counter_down><span class="sys-action-counter-holder __style_prefix__-counter-holder __style_prefix__-ch-__style__ bx-def-margin-sec-left-auto"<bx_if:show_cdown_hidden> style="display:none"</bx_if:show_cdown_hidden>>__counter__</span></bx_if:show_counter_down></bx_if:show_do_vote_down>
+    </span>__script__
 </span>

+ 133 - 34
template/scripts/BxBaseScore.php

@@ -47,6 +47,7 @@ class BxBaseScore extends BxDolScore
             'show_counter' => true,
             'show_counter_only' => true,
             'show_counter_empty' => true,
+            'show_counter_style' => self::$_sCounterStyleSimple, //--- Alloved styles are 'simple', 'divided'
             'show_counter_label_icon' => false,
             'show_counter_label_text' => true,
             'show_legend' => false,
@@ -98,28 +99,93 @@ class BxBaseScore extends BxDolScore
         return $this->getJsObjectName() . '.vote' . ucfirst($sType) . '(this)';
     }
 
-    public function getJsClickCounter()
+    public function getJsClickCounter($aParams = [])
     {
-        return $this->getJsObjectName() . '.toggleByPopup(this)';
+        $sJsObject = $this->getJsObjectName();
+        $sJsMethod = 'toggleByPopup';
+
+        if(!isset($aParams['type'], $aParams['show_counter_style']) || $aParams['show_counter_style'] == self::$_sCounterStyleSimple)
+            return $sJsObject . '.' . $sJsMethod . '(this)';
+
+        return $sJsObject . '.' . $sJsMethod . '(this, \'' . $aParams['type'] . '\')';
     }
 
     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;
-        $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
-        $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
-        $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
+        $sDefault = $this->_aElementDefaults['show_counter_style'];
+        $sCounterStyle = !empty($aParams['show_counter_style']) ? $aParams['show_counter_style'] : $sDefault;
 
-        $bAllowedViewViewVoters = $this->isAllowedVoteViewVoters();
+        $sMethodPrefix = '_getCounter';
+        $sMethod = $sMethodPrefix . bx_gen_method_name($sCounterStyle);
+        if(!method_exists($this, $sMethod))
+            $sMethod = $sMethodPrefix . bx_gen_method_name($sDefault);
 
+        return $this->$sMethod($aParams);
+    }
+
+    public function _getCounterSimple($aParams = [])
+    {
         $aScore = $this->_oQuery->getScore($this->getId());
 
         $iCup = (int)$aScore['count_up'];
-        $iCdown = (int)(int)$aScore['count_down'];
-        $bEmpty = $iCup == 0 && $iCdown == 0;
+        $iCdown = (int)$aScore['count_down'];
+
+        return $this->_getCounterItem(array_merge($aParams, [
+            'allowed_view_voters' => $this->isAllowedVoteViewVoters(),
+            'value' => $iCup - $iCdown,
+            'value_empty' => $iCup == 0 && $iCdown == 0,
+        ]));
+    }
+
+    public function _getCounterDivided($aParams = [])
+    {
+        $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
+        $bShowCounterOnly = isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true;
+        $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
+
+        $bAllowedViewVoters = $this->isAllowedVoteViewVoters();
+
+        $bScore = $this->_isVote();
+        $aScore = $this->_getVote();
+
+        $aCounters = [
+            BX_DOL_SCORE_DO_UP => (int)$aScore['count_up'],
+            BX_DOL_SCORE_DO_DOWN => (int)$aScore['count_down']
+        ];
+
+        $sCounters = '';
+        foreach($aCounters as $sType => $iValue)
+            $sCounters .= $this->_getCounterItem(array_merge($aParams, [
+                'show_counter_label_icon' => $bShowCounterOnly,
+                'show_script' => false,
+                'allowed_view_voters' => $bAllowedViewVoters,
+                'type' => $sType,
+                'value_html_id' => $this->_aHtmlIds['counter'] . '-' . $sType,
+                'value' => $iValue,
+                'value_empty' => !$bScore,
+            ]));
+
+        return $this->_oTemplate->parseHtmlByName('score_counter_wrapper.html', [
+            'html_id' => $this->_aHtmlIds['counter'],
+            'style_prefix' => $this->_sStylePrefix,
+            'class' => $this->_aHtmlIds['counter'] . (!$bScore && !$bShowCounterEmpty ? ' bx-sc-hidden' : ''),
+            'style' => self::$_sCounterStyleDivided,
+            'counter' => $sCounters,
+            'script' => $bShowScript ? $this->getJsScript($aParams) : ''
+        ]);
+    }
+
+    public function _getCounterItem($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;
+        $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
+        $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
+        $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
 
         $sClass = 'sys-action-counter';
         if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
@@ -131,18 +197,18 @@ class BxBaseScore extends BxDolScore
         if($bShowDoVoteAsButton)
             $sClass .= ' bx-btn-height';
 
-        $bLink = $bAllowedViewViewVoters && !$bEmpty;
-        $sCounter = !$bEmpty || $bShowEmpty ? $this->_getCounterLabel($iCup - $iCdown, $aParams) : '';
+        $bLink = $aParams['allowed_view_voters'] && !$aParams['value_empty'];
+        $sCounter = !$aParams['value_empty'] || $bShowEmpty ? $this->_getCounterLabel($aParams['value'], $aParams) : '';
 
         $aTmplVarsText = [
-            'id' => $this->_aHtmlIds['counter'],
+            'id' => isset($aParams['value_html_id']) ? $aParams['value_html_id'] : $this->_aHtmlIds['counter'],
             'class' => $sClass,
             'content' => $sCounter,
             'title' => _t($this->_getTitleDoBy()),
         ];
 
         $aTmplVarsLink = array_merge($aTmplVarsText, [
-            'onclick' => $bLink ? 'javascript:' . $this->getJsClickCounter() : ''
+            'onclick' => $bLink ? 'javascript:' . $this->getJsClickCounter($aParams) : ''
         ]);
 
         return $this->_oTemplate->parseHtmlByName('score_counter.html', array(
@@ -209,34 +275,63 @@ class BxBaseScore extends BxDolScore
         $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
         $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
         $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
+        $bShowCounterSimple = $bShowCounterDivided = false;
+        if(isset($aParams['show_counter_style'])) {
+            if($aParams['show_counter_style'] == self::$_sCounterStyleSimple)
+                $bShowCounterSimple = true;
+            if($aParams['show_counter_style'] == self::$_sCounterStyleDivided)
+                $bShowCounterDivided = true;
+        }
 
         $iObjectId = $this->getId();
         $iAuthorId = $this->_getAuthorId();
         $iAuthorIp = $this->_getAuthorIp();
-        $aScore = $this->_oQuery->getScore($iObjectId);
-        $bCount = (int)$aScore['count_up'] != 0 || (int)$aScore['count_down'] != 0;
-
         $isAllowedVote = $this->isAllowedVote();
         $isAllowedVoteView = $this->isAllowedVoteView();
+        $isAllowedVoteViewVoters = $this->isAllowedVoteViewVoters();
+        
+        $aScore = $this->_oQuery->getScore($iObjectId);
+        $bCount = $this->_isCount($aScore);
+        $bCounter = $this->_isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
+
         $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId, $iAuthorIp) ? true : false;
 
         //--- Do Vote
         $bTmplVarsDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
-        $aTmplVarsDoVoteUp = $aTmplVarsDoVoteDown = array();
-        if($bTmplVarsDoVote)
-            $aTmplVarsDoVoteUp = array(
-                'style_prefix' => $this->_sStylePrefix,
-                'do_vote' => $this->_getDoVote(BX_DOL_SCORE_DO_UP, $aParams, $isAllowedVote),
-            );
-        
+        $aTmplVarsDoVoteUp = $aTmplVarsDoVoteDown = [];
         if($bTmplVarsDoVote)
-            $aTmplVarsDoVoteDown = array(
-                'style_prefix' => $this->_sStylePrefix,
-                'do_vote' => $this->_getDoVote(BX_DOL_SCORE_DO_DOWN, $aParams, $isAllowedVote),
-            );
-
-        //--- Counter
-        $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
+            foreach([BX_DOL_SCORE_DO_UP, BX_DOL_SCORE_DO_DOWN] as $sType)
+                ${'aTmplVarsDoVote' . ucfirst($sType)} = [
+                    'style_prefix' => $this->_sStylePrefix,
+                    'style' => $aParams['show_counter_style'],
+                    'do_vote' => $this->_getDoVote($sType, $aParams, $isAllowedVote),
+                    'bx_if:show_counter_' . $sType => [
+                        'condition' => $bShowCounterDivided && $bCounter,
+                        'content' => [
+                            'style_prefix' => $this->_sStylePrefix,
+                            'style' => $aParams['show_counter_style'],
+                            'bx_if:show_c' . $sType . '_hidden' => [
+                                'condition' => !$bShowCounterEmpty && !$bCount,
+                                'content' => []
+                            ],
+                            'counter' => $this->_getCounterItem(array_merge($aParams, [
+                                'show_do_vote_as_button' => false,
+                                'show_do_vote_as_button_small' => false,
+                                'show_counter_only' => false,
+                                'show_counter_label_icon' => false,
+                                'show_script' => false,
+                                'allowed_view_voters' => $isAllowedVoteViewVoters,
+                                'type' => $sType,
+                                'value_html_id' => $this->_aHtmlIds['counter'] . '-' . $sType,
+                                'value' => isset($aScore['count_' . $sType]) ? (int)$aScore['count_' . $sType] : 0,
+                                'value_empty' => !$bCount,
+                            ]))
+                        ]
+                    ]
+                ];
+
+        //--- Counter (Simple)
+        $bTmplVarsCounter = $bShowCounterSimple && $bCounter;
         $aTmplVarsCounter = array();
         if($bTmplVarsCounter)
             $aTmplVarsCounter = array(
@@ -369,7 +464,7 @@ class BxBaseScore extends BxDolScore
         ));
     }
 
-    protected function _getCounterLabel($iCount, $aParams = array())
+    protected function _getCounterLabel($iCount, $aParams = [])
     {
         return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
             'style_prefix' => $this->_sStylePrefix,
@@ -377,7 +472,7 @@ class BxBaseScore extends BxDolScore
                 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
                 'content' => array(
                     'style_prefix' => $this->_sStylePrefix,
-                    'icon' => $this->_oTemplate->getImageAuto($this->_getIconDo())
+                    'icon' => $this->_oTemplate->getImageAuto($this->_getIconDo(isset($aParams['type']) ? $aParams['type'] : ''))
                 )
             ),
             'bx_if:show_text' => array(
@@ -418,8 +513,12 @@ class BxBaseScore extends BxDolScore
             'show_do_vote_label' => false
         ]);
 
+        $aParamsDb = [];
+        if(!empty($aParams['type']))
+            $aParamsDb['type'] = $aParams['type'];
+        $aUsers = $this->_oQuery->getPerformedBy($this->getId(), $aParamsDb);
+
         $aTmplUsers = [];
-        $aUsers = $this->_oQuery->getPerformedBy($this->getId());
         foreach($aUsers as $aUser) {
             list($sUserName, $sUserLink, $sUserIcon, $sUserUnit, $sUserUnitWoInfo) = $this->_getAuthorInfo($aUser['id']);
             $aTmplUsers[] = [