Przeglądaj źródła

Ticket #4148 - Build-in API support: Actions in comments.

AntonLV 3 lat temu
rodzic
commit
596e2f8722
2 zmienionych plików z 62 dodań i 18 usunięć
  1. 30 18
      inc/classes/BxDolCmts.php
  2. 32 0
      template/scripts/BxBaseCmtsMenuActions.php

+ 30 - 18
inc/classes/BxDolCmts.php

@@ -2197,21 +2197,22 @@ class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContent
     {
         $bItem = !empty($mixedItem) && is_array($mixedItem);
 
-        if($bItem) {
-            $iI = $mixedItem['cmt_id'];
-            $aStructure[$iI] = array(
+        $iCmtId = $bItem ? (int)$mixedItem['cmt_id'] : 0;
+        $iCmtIndex = $iCmtId;
+
+        if($bItem)
+            $aStructure[$iCmtIndex] = [
                 'id' => $mixedItem['cmt_id'], 
                 'order' => isset($mixedItem['cmt_order']) ? $mixedItem['cmt_order'] : 0, 
-                'items' => array(),
-            );
-        }
+                'items' => [],
+            ];
 
         if(!$bItem || (int)$mixedItem['cmt_replies'] > 0) {
-            $aItems = $this->_oQuery->getStructure($this->_iId, $this->_iAuthorId, $bItem ? $mixedItem['cmt_id'] : 0, $aBp['filter'], $aBp['order']);
+            $aItems = $this->_oQuery->getStructure($this->_iId, $this->_iAuthorId, $iCmtId, $aBp['filter'], $aBp['order']);
             if(!empty($aItems)) {
                 if($bItem && $iLevel < (int)$this->_aSystem['number_of_levels']) {
                     $iPassLevel = $iLevel + 1;
-                    $aPassStructure = &$aStructure[$iI]['items'];
+                    $aPassStructure = &$aStructure[$iCmtIndex]['items'];
                 }
                 else {
                     $iPassLevel = $iLevel;
@@ -2237,24 +2238,34 @@ class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContent
     {
         $bItem = !empty($mixedItem) && is_array($mixedItem);
 
+        $iCmtId = $bItem ? (int)$mixedItem['cmt_id'] : 0;
+        $sCmtIndex = 'i' . $iCmtId;
+
         if($bItem) {
-            $iI = 'i' . $mixedItem['cmt_id'];
-            $aStructure[$iI] = array(
-                'id' => $mixedItem['cmt_id'], 
+            $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
+            $oMenuActions->setCmtsData($this, $iCmtId, $aBp);
+
+            $aData = $this->getCommentSimple($iCmtId);
+            $aData = array_merge($aData, [
+                'author_data' => BxDolProfile::getData($aData['cmt_author_id']),
+                'menu_actions' => $oMenuActions->getCodeAPI()
+            ]);
+
+            $aStructure[$sCmtIndex] = [
+                'id' => $iCmtId, 
                 'order' => isset($mixedItem['cmt_order']) ? $mixedItem['cmt_order'] : 0, 
-                'data' => $this->getCommentSimple((int)$mixedItem['cmt_id']),
+                'data' => $aData,
                 'files' => $this->_getAttachments($mixedItem),
-                'items' => array(),
-            );
-            $aStructure[$iI]['data']['author_data'] = BxDolProfile::getData($aStructure[$iI]['data']['cmt_author_id']);
+                'items' => [],
+            ];
         }
 
         if(!$bItem || (int)$mixedItem['cmt_replies'] > 0) {
-            $aItems = $this->_oQuery->getStructure($this->_iId, $this->_iAuthorId, $bItem ? $mixedItem['cmt_id'] : 0, $aBp['filter'], $aBp['order']);
+            $aItems = $this->_oQuery->getStructure($this->_iId, $this->_iAuthorId, $iCmtId, $aBp['filter'], $aBp['order']);
             if(!empty($aItems)) {
                 if($bItem && $iLevel < (int)$this->_aSystem['number_of_levels']) {
                     $iPassLevel = $iLevel + 1;
-                    $aPassStructure = &$aStructure[$iI]['items'];
+                    $aPassStructure = &$aStructure[$sCmtIndex]['items'];
                 }
                 else {
                     $iPassLevel = $iLevel;
@@ -2273,7 +2284,8 @@ class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContent
 
                     return $iWay * ($aItem1['order'] < $aItem2['order'] ? -1 : 1);
                 });
-                $aStructure[$iI]['items'] = $aPassStructure;
+
+                $aStructure[$sCmtIndex]['items'] = $aPassStructure;
             }
         }
     }

+ 32 - 0
template/scripts/BxBaseCmtsMenuActions.php

@@ -16,6 +16,8 @@ class BxBaseCmtsMenuActions extends BxTemplMenuCustom
     protected static $_sModeActions = 'actions';
     protected static $_sModeCounters = 'counters';
 
+    protected $_bIsApi;
+
     protected $_oCmts;
     protected $_aCmt;
     protected $_aBp;
@@ -31,6 +33,8 @@ class BxBaseCmtsMenuActions extends BxTemplMenuCustom
     {
         parent::__construct ($aObject, $oTemplate);
 
+        $this->_bIsApi = bx_is_api();
+
         $this->_aBp = [];
         $this->_aDp = [];
 
@@ -104,6 +108,11 @@ class BxBaseCmtsMenuActions extends BxTemplMenuCustom
                 break;
         }
 
+        if($this->_bIsApi)
+            return $this->_getMenuItemElementApi($aItem, $oVote->getElementApi(array_merge($aVotesParams, [
+                'show_counter' => true,
+            ])));
+
         return $oVote->$sVotesMethod($aVotesParams);
     }
 
@@ -132,6 +141,11 @@ class BxBaseCmtsMenuActions extends BxTemplMenuCustom
                 break;
         }
 
+        if($this->_bIsApi)
+            return $this->_getMenuItemElementApi($aItem, $oReaction->getElementApi(array_merge($aReactionsParams, [
+                'show_counter' => true,
+            ])));
+
         return $oReaction->$sReactionsMethod($aReactionsParams);
     }
 
@@ -159,6 +173,11 @@ class BxBaseCmtsMenuActions extends BxTemplMenuCustom
                 break;
         }
 
+        if($this->_bIsApi)
+            return $this->_getMenuItemElementApi($aItem, $oScore->getElementApi(array_merge($aScoresParams, [
+                'show_counter' => true,
+            ])));
+
     	return $oScore->$sScoresMethod($aScoresParams);
     }
 
@@ -177,6 +196,19 @@ class BxBaseCmtsMenuActions extends BxTemplMenuCustom
     	return $oReport->getElementInline($aReportParams);
     }
 
+    protected function _getMenuItemElementApi($aItem, $aElement)
+    {
+        if(!$this->_bIsApi)
+            return $aItem;
+
+        return [
+                'id' => $aItem['id'],
+                'name' => $aItem['name'],
+                'display_type' => 'element',
+                'data' => $aElement
+            ];
+    }
+
     /**
      * Check if menu items is visible.
      * @param $a menu item array