Pārlūkot izejas kodu

Ticket #4148 - Build-in API support: Comments action.

AntonLV 3 gadi atpakaļ
vecāks
revīzija
de807637cc

+ 7 - 3
inc/classes/BxDolCmts.php

@@ -10,8 +10,8 @@
 bx_import('BxDolAcl');
 bx_import('BxDolPrivacy');
 
-define('BX_DOL_CMT_TYPE_COMMENT', 'comment');
-define('BX_DOL_CMT_TYPE_REVIEW', 'review');
+define('BX_DOL_CMT_TYPE_COMMENTS', 'comments');
+define('BX_DOL_CMT_TYPE_REVIEWS', 'reviews');
 
 define('BX_CMT_OLD_VOTES', 365*86400); ///< comment votes older than this number of seconds will be deleted automatically
 
@@ -153,6 +153,10 @@ class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContent
     protected $_sTableImages = 'sys_cmts_images';
     protected $_sTableImages2Entries = 'sys_cmts_images2entries';
 
+    protected $_aElementDefaults;
+    protected $_aElementDefaultsApi;
+    protected $_aElementParamsApi; //--- Params from DefaultsApi array to be passed to Api
+
     protected $_bIsApi;
 
     protected $_sType;
@@ -214,7 +218,7 @@ class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContent
 
         $this->_bIsApi = bx_is_api();
 
-        $this->_sType = BX_DOL_CMT_TYPE_COMMENT;
+        $this->_sType = BX_DOL_CMT_TYPE_COMMENTS;
 
         $this->_aSystems = $this->getSystems();
         if(!isset($this->_aSystems[$sSystem]))

+ 1 - 1
inc/classes/BxDolCmtsReviews.php

@@ -43,7 +43,7 @@ class BxDolCmtsReviews extends BxTemplCmts
     {
         parent::__construct($sSystem, $iId, $iInit, $oTemplate);
 
-        $this->_sType = BX_DOL_CMT_TYPE_REVIEW;
+        $this->_sType = BX_DOL_CMT_TYPE_REVIEWS;
         $this->_oQuery = new BxDolCmtsReviewsQuery($this);
 
         $this->_sFormObject = 'sys_review';

+ 41 - 9
modules/boonex/timeline/classes/BxTimelineMenuItemActions.php

@@ -194,6 +194,47 @@ class BxTimelineMenuItemActions extends BxTemplMenuCustom
         return $oObject->$sViewsMethod($aViewsParams);
     }
 
+    protected function _getMenuItemItemComment($aItem)
+    {
+        /**
+         * For now use the default link (not Object based) for non-api calls.
+         */
+        if(!$this->_bIsApi)
+            return true;
+
+        if(!isset($this->_aEvent['comments']) || !is_array($this->_aEvent['comments']) || !isset($this->_aEvent['comments']['system'])) 
+            return false;
+
+        $sCommentsSystem = $this->_aEvent['comments']['system'];
+        $iCommentsObject = $this->_aEvent['comments']['object_id'];
+        $aCommentsParams = array(
+            'show_do_comment_label' => $this->_bShowTitles,
+            'show_counter' => $this->_bShowCounters,
+            'show_counter_empty' => $this->_bShowCountersEmpty,
+            'show_counter_label_icon' => $this->_bShowCountersIcons,
+            'dynamic_mode' => $this->_bDynamicMode
+        );
+
+        switch($this->_sMode) {
+            case self::$_sModeActions:
+                $sCommentsMethod = 'getElementInline';
+                break;
+
+            case self::$_sModeCounters:
+                $sCommentsMethod = 'getCounter';
+                break;
+        }
+
+        $oObject = $this->_oModule->getCmtsObject($sCommentsSystem, $iCommentsObject);
+
+        if($this->_bIsApi)
+            return $this->_getMenuItemElementApi($aItem, $oObject->getElementApi(array_merge($aCommentsParams, [
+                'show_counter' => true,
+            ])));
+
+        return $oObject->$sCommentsMethod($aCommentsParams);
+    }
+
     protected function _getMenuItemItemVote($aItem)
     {
         if(!isset($this->_aEvent['votes']) || !is_array($this->_aEvent['votes']) || !isset($this->_aEvent['votes']['system'])) 
@@ -385,15 +426,6 @@ class BxTimelineMenuItemActions extends BxTemplMenuCustom
             ]);
         }
 
-        switch($aItem['name']) {
-            case 'item-comment':
-                $aItem['link'] = $this->_oModule->_oConfig->getItemViewUrl($this->_aEvent);
-                if($this->_oModule->_oConfig->isSystem($sType, $sAction) && !empty($this->_aEvent['content']['url']))
-                    $aItem['link'] = $this->_aEvent['content']['url'];
-                $aItem['link'] = bx_api_get_relative_url($aItem['link']);
-                break;
-        }
-
         return $aItem;
     }
 

+ 2 - 2
template/comment_element_block.html

@@ -1,10 +1,10 @@
 <div id="__html_id__" class="sys-action __style_prefix__ __class__ flex items-center whitespace-nowrap" bx_comment_data_count="__count__">
     <div class="sys-action-cnt __style_prefix__-cnt flex items-center">
         <bx_if:show_do_comment>
-            <div class="__style_prefix__-element-holder">__do_comment__</div>
+            <div class="sys-action-element-holder __style_prefix__-element-holder">__do_comment__</div>
         </bx_if:show_do_comment>
         <bx_if:show_counter>
-            <div class="__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>
+            <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>
     </div>
     __script__

+ 2 - 2
template/comment_element_inline.html

@@ -1,7 +1,7 @@
 <span id="__html_id__" class="sys-action __style_prefix__ __class__ inline-flex items-center whitespace-nowrap" bx_comment_data_count="__count__">
     <span class="sys-action-cnt __style_prefix__-cnt flex items-center">
-        <bx_if:show_do_comment><span class="__style_prefix__-element-holder">__do_comment__</span></bx_if:show_do_comment>
-        <bx_if:show_counter><span class="__style_prefix__-counter-holder bx-def-margin-thd-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_comment><span class="sys-action-element-holder __style_prefix__-element-holder">__do_comment__</span></bx_if:show_do_comment>
+        <bx_if:show_counter><span class="sys-action-counter-holder __style_prefix__-counter-holder ml-2 first:ml-0"<bx_if:show_hidden> style="display:none"</bx_if:show_hidden>>__counter__</span></bx_if:show_counter>
     </span>
     __script__
 </span>

+ 2 - 4
template/score_counter_label.html

@@ -1,8 +1,6 @@
-<span class="sys-action-counter-label __style_prefix__-counter-label flex items-center text-xs font-medium">
+<span class="sys-action-counter-label __style_prefix__-counter-label flex items-center text-sm font-normal p-1">
     <bx_if:show_icon>
-        <span class="sys-action-counter-icon __style_prefix__-counter-icon relative flex items-center justify-center ring-1  h-6 w-6 ring-white dark:ring-gray-700 h-6 w-6 bg-gray-200 dark:bg-gray-700 rounded-full">
-            <i class="sys-icon __name__"></i>
-        </span>
+        <span class="sys-action-counter-icon __style_prefix__-counter-icon relative flex items-center justify-center ring-1  h-6 w-6 ring-white dark:ring-gray-700 h-6 w-6 bg-gray-200 dark:bg-gray-700 rounded-full">__icon__</span>
     </bx_if:show_icon>
     <bx_if:show_text>
         <span class="sys-action-counter-text __style_prefix__-counter-text ml-1.5">__text__</span>

+ 130 - 31
template/scripts/BxBaseCmts.php

@@ -25,8 +25,6 @@ class BxBaseCmts extends BxDolCmts
     protected $_sStylePrefix;
 
     protected $_aHtmlIds;
-
-    protected $_aElementDefaults;
     
     protected $_aAclId2Name;
 
@@ -50,7 +48,7 @@ class BxBaseCmts extends BxDolCmts
             'counter' => 'bx-cmt-counter-' . $sHtmlId
         );
 
-        $this->_aElementDefaults = array(
+        $this->_aElementDefaults = [
             'show_do_comment_as_button' => false,
             'show_do_comment_as_button_small' => false,
             'show_do_comment_icon' => true,
@@ -60,7 +58,11 @@ class BxBaseCmts extends BxDolCmts
             'show_counter_empty' => false,
             'show_counter_reversed' => false,
             'recalculate_counter' => false
-        );
+        ];
+        $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
+            'show_counter' => true,
+        ]);
+        $this->_aElementParamsApi = [];
 
         $this->_aAclId2Name = array();
         $aAclLevels = BxDolAcl::getInstance()->getMemberships(false, false, false);
@@ -705,25 +707,12 @@ class BxBaseCmts extends BxDolCmts
 
         //--- Do Comment
         $bTmplVarsDoComment = $this->_isShowDoComment($aParams, $isAllowedComment, $bCount);
-        $aTmplVarsDoComment = array();
-        if($bTmplVarsDoComment) {
-            $sClass = '';
-            if($bShowDoCommentAsButton)
-                $sClass = 'bx-btn';
-            else if ($bShowDoCommentAsButtonSmall)
-                $sClass = 'bx-btn bx-btn-small';
-
-            if(!$isAllowedComment)
-                $sClass .= $bShowDoCommentAsButton || $bShowDoCommentAsButtonSmall ? ' bx-btn-disabled' : 'bx-cmts-disabled';
-
-            $aTmplVarsDoComment = array(
+        $aTmplVarsDoComment = [];
+        if($bTmplVarsDoComment)
+            $aTmplVarsDoComment = [
                 'style_prefix' => $this->_sStylePrefix,
-                'do_comment' => $this->_oTemplate->parseLink($this->getListUrl(), $this->_getLabelDo($aParams), array(
-                    'class' => $this->_sStylePrefix . '-do-comment ' . $this->_sStylePrefix . '-dc ' . $sClass,
-                    'title' => _t($this->_getTitleDo())
-                )),
-            );
-        }
+                'do_comment' => $this->_getDoComment($aParams, $isAllowedComment),
+            ];
 
         //--- Counter
         $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedComment, $bCount);
@@ -762,17 +751,52 @@ class BxBaseCmts extends BxDolCmts
         ));
     }
 
-    protected function _getCounterItems($iCmtsLimit, $iCmtsStart = 0)
+    public function getElementAPI($aParams = [])
     {
-        return $this->_oQuery->getCommentsBy(array('type' => 'object_id', 'object_id' => $this->getId(), 'order_way' => 'desc', 'start' => $iCmtsStart, 'per_page' => $iCmtsLimit * 4));
-    }
-    
-    private function _isShowContent($aCmt)
-    {
-        $oProfileAuthor = BxDolProfile::getInstance($aCmt['cmt_author_id']);
-        return (int)$aCmt['cmt_author_id'] == 0 || ($oProfileAuthor && $oProfileAuthor->isActive()) || isAdmin() || BxDolAcl::getInstance()->isMemberLevelInSet([MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR]);
+        if(!($this->_bApi = bx_is_api()))
+            return;
+
+        //TODO: Replace with own lang key in UNA 14.
+        if(!$this->isEnabled())
+            return bx_api_get_msg('_vote_err_not_enabled');
+
+        $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
+
+        $bRecalculateCounter = isset($aParams['recalculate_counter']) && $aParams['recalculate_counter'] == true;
+
+        $iObjectId = $this->getId();
+        $iAuthorId = $this->_getAuthorId();
+
+        $iCount = $this->getCommentsCountAll(0, $bRecalculateCounter);
+        $bCount = (int)$iCount != 0;
+
+        $isAllowedComment = $this->isPostAllowed();
+        $aParams['count'] = $iCount;
+
+        //--- Do Comment
+        $bDoComment = $this->_isShowDoComment($aParams, $isAllowedComment, $bCount);
+        $aDoComment = $bDoComment ? $this->_getDoComment($aParams, $isAllowedComment) : [];
+
+        //--- Counter
+        $bCounter = $this->_isShowCounter($aParams, $isAllowedComment, $bCount);
+        $aCounter = $bCounter ? $this->getCounterAPI(array_merge($aParams, [
+            'show_counter_only' => false, 
+        ])) : [];
+
+
+        if(!$bDoComment && !$bCounter)
+            return bx_api_get_msg('');
+
+        return [
+            'type' => $this->_sType,
+            'system' => $this->_sSystem,
+            'object_id' => $this->_iId,
+            'params' => array_intersect_key($aParams, array_flip($this->_aElementParamsApi)),
+            'action' => $aDoComment,
+            'counter' => $aCounter
+        ];
     }
-    
+
     public function getCounter($aParams = [])
     {
         $aParams = array_merge($this->_aElementDefaults, $aParams);
@@ -849,6 +873,44 @@ class BxBaseCmts extends BxDolCmts
         ]);
     }
 
+    public function getCounterApi($aParams = [])
+    {
+        $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
+
+        $bShowReversed = isset($aParams['show_counter_reversed']) && $aParams['show_counter_reversed'] == true;
+        $bRecalculateCounter = isset($aParams['recalculate_counter']) && $aParams['recalculate_counter'] == true;        
+
+        $iCount = (int)$this->getCommentsCountAll(0, $bRecalculateCounter);   
+
+        $iCmtsLimit = 5;
+        $aCmts = $this->_getCounterItems($iCmtsLimit);
+        if(!$bShowReversed)
+            $aCmts = array_reverse($aCmts);
+
+        $aCmtsAuthors = [];
+        foreach($aCmts as $aCmt) {
+            $iAuthor = (int)$aCmt['cmt_author_id'];
+            if(array_key_exists($iAuthor, $aCmtsAuthors))
+                continue;
+
+            $aCmtsAuthors[$iAuthor] = BxDolProfile::getData($iAuthor);
+
+            if(count($aCmtsAuthors) >= $iCmtsLimit)
+                break;
+        }
+        $aCmtsAuthors = array_values($aCmtsAuthors);
+
+        return [
+            'count' => $iCount,
+            'authors' => $aCmtsAuthors
+        ];
+    }
+
+    protected function _getCounterItems($iCmtsLimit, $iCmtsStart = 0)
+    {
+        return $this->_oQuery->getCommentsBy(array('type' => 'object_id', 'object_id' => $this->getId(), 'order_way' => 'desc', 'start' => $iCmtsStart, 'per_page' => $iCmtsLimit * 4));
+    }
+
     protected function _getLabelDo($aParams = [])
     {
         return $this->_oTemplate->parseHtmlByContent($this->_getTmplLabelDo(), [
@@ -907,6 +969,12 @@ class BxBaseCmts extends BxDolCmts
     /**
      * private functions
      */
+    private function _isShowContent($aCmt)
+    {
+        $oProfileAuthor = BxDolProfile::getInstance($aCmt['cmt_author_id']);
+        return (int)$aCmt['cmt_author_id'] == 0 || ($oProfileAuthor && $oProfileAuthor->isActive()) || isAdmin() || BxDolAcl::getInstance()->isMemberLevelInSet([MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR]);
+    }
+
     protected function _getContentBefore($aBp = array(), $aDp = array())
     {
         return '';
@@ -1609,6 +1677,37 @@ class BxBaseCmts extends BxDolCmts
             ),
         );
     }
+
+    protected function _getDoComment($aParams = [], $isAllowedComment = true)
+    {
+        $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
+        $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
+
+        $iCount = isset($aParams['count']) ? (int)$aParams['count'] : 0;
+        $bDisabled = !$isAllowedComment;
+
+        $sClass = '';
+        if($bShowDoCommentAsButton)
+            $sClass = 'bx-btn';
+        else if ($bShowDoCommentAsButtonSmall)
+            $sClass = 'bx-btn bx-btn-small';
+
+        if(!$isAllowedComment)
+            $sClass .= $bShowDoCommentAsButton || $bShowDoCommentAsButtonSmall ? ' bx-btn-disabled' : 'bx-cmts-disabled';
+
+        if($this->_bApi)
+            return [
+                'is_disabled' => $bDisabled,
+                'count' => $iCount,
+                'link' => bx_api_get_relative_url($this->getListUrl()),
+                'title' => _t($this->_getTitleDo(), $iCount)
+            ];
+
+        return $this->_oTemplate->parseLink($this->getListUrl(), $this->_getLabelDo($aParams), [
+            'class' => $this->_sStylePrefix . '-do-comment ' . $this->_sStylePrefix . '-dc ' . $sClass,
+            'title' => _t($this->_getTitleDo(), $iCount)
+        ]);
+    }
 }
 
 /** @} */