Forráskód Böngészése

#4148 - Build-in API support (comments)

roman.lesnikov@gmail.com 3 éve
szülő
commit
4088c533ac

+ 2 - 13
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -3504,20 +3504,9 @@ class BxBaseModGeneralModule extends BxDolModule
             return false;
         
         if (bx_is_api()){
-            $aForm = $oCmts->getFormPost(0);
-            if (is_array($aForm)){
-                $aForm['id'] = 2;
-            }
-            else
-                $aForm = ['id' => 2, 'type' => 'form', 'name' => 'comment', 'data' => $aForm->getCodeAPI(), 'request' => ['url' => '/api.php?r=' . $this->_aModule['name'] . '/entity_comments', 'immutable' => true]];
-            
-            
-            return [
-                ['id' => 1, 'type' => 'browse', 'data' => $oCmts->getCommentsBlockAPI([], ['in_designbox' => false, 'show_empty' => false])],
-                $aForm 
-            ];
+            return [bx_srv('system', 'get_form', [$sObject, $iId, 0], 'TemplCmtsServices')];
         }
-        return $oCmts->getCommentsBlock(array(), array('in_designbox' => false, 'show_empty' => false));
+        return $oCmts->getCommentsBlock([], ['in_designbox' => false, 'show_empty' => false]);
     }
 
     protected function _getFields($iContentId)

+ 29 - 9
template/scripts/BxBaseCmts.php

@@ -134,26 +134,46 @@ class BxBaseCmts extends BxDolCmts
         return $this->_oTemplate->_wrapInTagJsCode("if(window['" . $this->_sJsObjName . "'] == undefined) var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . "); " . $this->_sJsObjName . ".cmtInit();");
     }
 
-    function getCommentsBlockAPI($aBp = [], $aDp = [])
+    function getCommentsBlockAPI($aBp = [], $aDp = [], $iCommentId = 0, $iStart = 0)
     {
         $mixedResult = $this->isViewAllowed();
         if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
             return $mixedResult; // TODO: error checking
-
+        
+        $aBp['type'] = 'head';
         $this->_getParams($aBp, $aDp);
         $this->_prepareParams($aBp, $aDp);
+        $aBp['order']['way'] = 'desc';
+        $aBp['order_way'] = 'desc';
+        $aBp['start'] = $iStart ; 
+        $aPp = $aBp['per_view'];
+        $aBp['per_view'] =  $aBp['per_view'] + 1; 
 
-        $aCmts = $this->getCommentsArray($aBp['vparent_id'], $aBp['filter'], $aBp['order'], $aBp['start'], $aBp[($aBp['init_view'] != -1 ? 'init' : 'per') . '_view']);
+        $aCmts = [];
+        
+        if ($iCommentId == 0)
+            $aCmts = $this->getCommentsArray($aBp['vparent_id'], $aBp['filter'], $aBp['order'], $aBp['start'], $aBp[($aBp['init_view'] != -1 ? 'init' : 'per') . '_view']);
+        else
+            $aCmts = [['cmt_id' => $iCommentId]];
+        
+        $iStartFrom = 0;
+        if (count($aCmts) == $aBp['per_view']){
+            $aBp['per_view'] = $aPp;
+            $aCmts = array_slice($aCmts, 0, $aBp['per_view']); 
+            $iStartFrom = $aBp['start'] + $aBp['per_view'];
+        }
+            
         $aCmtsRv = [];
         
         foreach ($aCmts as $aCmt) {
             $aCmtsRv[] = $this->getCommentStructure($aCmt['cmt_id'], $aBp, $aDp);
         }
-
-       // print_r($aCmtsRv);
         
         return [
             'unit' => 'comments',
+            'start' => $iStartFrom,
+            'module' => $this->_sSystem, 
+            'object_id' => $this->_iId,
             'data' => $aCmtsRv,
         ];
     }
@@ -1178,7 +1198,7 @@ class BxBaseCmts extends BxDolCmts
             if(!$bCmtText && !$bImageIds) {
                 $oForm->aInputs['cmt_text']['error'] =  _t('_Please enter characters');
                 $oForm->setValid(false);
-                return bx_is_api() ? $oForm : ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id];
+                return bx_is_api() ? ['form' => $oForm, 'res' => 0] : ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id];
             }
 
             $aParent = [];
@@ -1227,16 +1247,16 @@ class BxBaseCmts extends BxDolCmts
                 if(($mixedResult = $this->onPostAfter($iCmtId, $aDp)) !== false){
                     if (bx_is_api()){
                         $this->_unsetFormObject(BX_CMT_ACTION_POST);
-                        return $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
+                        return ['form' => $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId), 'res' => $iCmtId];
                     }
                     else{
-                        return $mixedResult;
+                         return ['form' => $mixedResult, 'res' => $iCmtId];
                     }
                 }
             }
             return bx_is_api() ? ['id' => 1, 'type' => 'msg', 'data' => _t('_cmt_err_cannot_perform_action')] : ['msg' => _t('_cmt_err_cannot_perform_action')];
         }
-        return bx_is_api() ? $oForm : ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id];
+        return bx_is_api() ? ['form' => $oForm, 'res' => 0] : ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id];
     }
 
     protected function _getFormEdit($iCmtId, $aDp = [])

+ 41 - 0
template/scripts/BxBaseCmtsServices.php

@@ -407,6 +407,47 @@ class BxBaseCmtsServices extends BxDol
             'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
         );
     }
+    
+    public function serviceGetForm($sObject, $iId, $iParent)
+    {
+        $oCmts = BxDolCmts::getObjectInstance($sObject, $iId);
+        if (!$oCmts || !$oCmts->isEnabled())
+            return false;
+        
+        $oForm = $oCmts->getFormPost($iParent);
+        
+        if (isset($oForm['form'])){
+            $aRv = [
+                'id' => '1', 
+                'type' => 'comments', 
+                'form' => ['id' => 'cmt_form', 'type' => 'form', 'name' => 'comment', 'data' => $oForm['form']->getCodeAPI(), 'request' => ['url' => '/api.php?r=system/get_form/TemplCmtsServices/&params=' . json_encode([$sObject, $iId, $iParent]), 'immutable' => true]]];
+            
+            $aRv['browse'] = [
+               'id' => 'cmt_list', 
+               'type' => 'browse', 
+               'insert' => 'before', 
+               'data' => $oCmts->getCommentsBlockAPI([], ['in_designbox' => false, 'show_empty' => false], $oForm['res'] > 0 ? $oForm['res'] : 0)
+            ];
+        }
+        
+        return $aRv;
+    }
+    
+    public function serviceGetList($sObject, $iId, $iStart)
+    {
+        $oCmts = BxDolCmts::getObjectInstance($sObject, $iId);
+        
+        if (!$oCmts || !$oCmts->isEnabled())
+            return false;
+        
+        $aRv['browse'] = [
+            'id' => 'cmt_list', 
+            'type' => 'browse', 
+            'insert' => 'after', 
+            'data' => $oCmts->getCommentsBlockAPI([], ['in_designbox' => false, 'show_empty' => false], 0, $iStart)
+        ];
+        return $aRv;
+    }
 }
 
 /** @} */