Selaa lähdekoodia

#4148 - Build-in API support (cmts+timeline)

roman.lesnikov@gmail.com 3 vuotta sitten
vanhempi
sitoutus
d508377773

+ 0 - 2
cmts.php

@@ -12,8 +12,6 @@ require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
 
 bx_import('BxDolLanguages');
 
-bx_api_check_origins();
-
 check_logged();
 
 $sSys = isset($_REQUEST['sys']) ? bx_process_input($_REQUEST['sys']) : '';

+ 7 - 0
inc/classes/BxDolCmts.php

@@ -1793,6 +1793,13 @@ class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContent
 
         return BxDolForm::getObjectInstance($this->_sFormObject, $this->$sDisplayName, false, $this->_sSystem);
     }
+    
+    protected function _unsetFormObject($sAction = BX_CMT_ACTION_POST)
+    {
+        $sDisplayName = '_sFormDisplay' . ucfirst($sAction);
+
+        return BxDolForm::unsetObjectInstance($this->_sFormObject, $this->$sDisplayName, false, $this->_sSystem);
+    }
 
     protected function _getParams(&$aBp, &$aDp)
     {

+ 6 - 0
inc/classes/BxDolForm.php

@@ -803,6 +803,12 @@ class BxDolForm extends BxDol implements iBxDolReplaceable
 
         return ($GLOBALS['bxDolClasses'][$sKey] = $o);
     }
+    
+     static public function unSetObjectInstance($sObject, $sDisplayName, $oTemplate = false, $sParam = '')
+     {
+        $sKey = 'BxDolForm!'.$sObject.'!'.$sDisplayName.'!'.$sParam;
+         unset($GLOBALS['bxDolClasses'][$sKey]);
+     }
 
     /**
      * Get data items array

+ 0 - 3
inc/utils.inc.php

@@ -2347,13 +2347,10 @@ function bx_api_check_origins()
             header('Access-Control-Allow-Headers: Accept-Encoding, Authorization, Cache-Control, Connection, Host, Origin, Pragma, Referer, User-Agent, X-Custom-Header, X-Requested-With');                    
             exit;
         } 
-        
-      
     }
     //TODO: Temporatery for use logged state 
     bx_login(1);
     check_logged();
-    //echoDbgLog($_SERVER);
 }
 
 /** @} */

+ 4 - 4
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -3456,13 +3456,13 @@ class BxBaseModGeneralModule extends BxDolModule
         $oCmts = BxDolCmts::getObjectInstance($sObject, $iId);
         if (!$oCmts || !$oCmts->isEnabled())
             return false;
-
         
         if (bx_is_api()){
-            $aData = $oCmts->getFormPost(0);
+            $aForm = $oCmts->getFormPost(0)->getCodeAPI();
+          
             return [
-                ['id' => 1, 'type' => 'browse', 'data' => $oCmts->getCommentsBlockAPI(array(), array('in_designbox' => false, 'show_empty' => false))],
-                ['id' => 2, 'type' => 'form', 'data' => $aData['frm']->getCodeAPI(), 'request' => ['url' => '/cmts.php?api=1', 'immutable' => true]]
+                ['id' => 1, 'type' => 'browse', 'data' => $oCmts->getCommentsBlockAPI([], ['in_designbox' => false, 'show_empty' => false])],
+                ['id' => 2, 'type' => 'form', 'data' => $aForm, 'request' => ['url' => '/api.php?r=' . $this->_aModule['name'] . '/entity_comments', 'immutable' => true]]
             ];
         }
         return $oCmts->getCommentsBlock(array(), array('in_designbox' => false, 'show_empty' => false));

+ 14 - 10
template/scripts/BxBaseCmts.php

@@ -1135,11 +1135,11 @@ class BxBaseCmts extends BxDolCmts
             else
                 $sMsg = $this->msgErrPostAllowed();
 
-            return ['msg' => $sMsg];
+            return bx_is_api() ? [['id' => 1, 'type' => 'msg', 'data' => $sMsg]] : ['msg' => $sMsg];
         }
 
         if($bCmtParentId && !$this->isReplyAllowed($iCmtParentId))
-            return ['msg' => $this->msgErrReplyAllowed()];
+            return bx_is_api() ? [['id' => 1, 'type' => 'msg', 'data' => $sMsg]] : ['msg' => $this->msgErrReplyAllowed()];
 
         $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
         $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
@@ -1171,8 +1171,7 @@ class BxBaseCmts extends BxDolCmts
             if(!$bCmtText && !$bImageIds) {
                 $oForm->aInputs['cmt_text']['error'] =  _t('_Please enter characters');
                 $oForm->setValid(false);
-
-            	return ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id];
+                return bx_is_api() ? $oForm : ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id];
             }
 
             $aParent = [];
@@ -1218,14 +1217,19 @@ class BxBaseCmts extends BxDolCmts
                 if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
                     $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
 
-                if(($mixedResult = $this->onPostAfter($iCmtId, $aDp)) !== false)
-                    return $mixedResult;
+                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);
+                    }
+                    else{
+                        return $mixedResult;
+                    }
+                }
             }
-
-            return ['msg' => _t('_cmt_err_cannot_perform_action')];
+            return bx_is_api() ? [['id' => 1, 'type' => 'msg', 'data' => _t('_cmt_err_cannot_perform_action')]] : ['msg' => _t('_cmt_err_cannot_perform_action')];
         }
-
-        return ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id, 'frm' => $oForm];
+        return bx_is_api() ? $oForm : ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id];
     }
 
     protected function _getFormEdit($iCmtId, $aDp = [])