AntonLV 7 tahun lalu
induk
melakukan
fceca8a70a

+ 13 - 8
modules/base/files/classes/BxBaseModFilesFormsEntryHelper.php

@@ -34,18 +34,23 @@ class BxBaseModFilesFormsEntryHelper extends BxBaseModTextFormsEntryHelper
     {
         $CNF = &$this->_oModule->_oConfig->CNF;
 
-        // check access
-        if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedAdd()))
-            return $this->prepareResponse(MsgBox($sMsg), $this->_bAjaxMode, 'msg');
+        $bAsJson = false;
 
-        // check and display form
+        // get form object
         $oForm = $this->getObjectFormAdd();
         if (!$oForm)
-            return $this->prepareResponse(MsgBox(_t('_sys_txt_error_occured')), $this->_bAjaxMode, 'msg');
+            return $this->prepareResponse(MsgBox(_t('_sys_txt_error_occured')), $bAsJson, 'msg');
+
+        $bAsJson = $this->_bAjaxMode && $oForm->isSubmitted();
 
+        // check access
+        if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedAdd()))
+            return $this->prepareResponse(MsgBox($sMsg), $bAsJson, 'msg');       
+
+        // check and display form
         $oForm->initChecker();
         if (!$oForm->isSubmittedAndValid())
-            return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $this->_bAjaxMode && $oForm->isSubmitted(), 'form', array(
+            return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
             	'form_id' => $oForm->getId()
             ));
 
@@ -54,11 +59,11 @@ class BxBaseModFilesFormsEntryHelper extends BxBaseModTextFormsEntryHelper
         $aContentIds = $oForm->insert ($aValsToAdd);
         if (false === $aContentIds || !is_array($aContentIds)) {
             if (!$oForm->isValid() || !is_array($aContentIds))
-                return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $this->_bAjaxMode, 'form', array(
+                return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
                 	'form_id' => $oForm->getId()
                 ));
             else
-                return $this->prepareResponse(MsgBox(_t('_sys_txt_error_entry_creation')), $this->_bAjaxMode, 'msg');
+                return $this->prepareResponse(MsgBox(_t('_sys_txt_error_entry_creation')), $bAsJson, 'msg');
         }
 
         foreach ($aContentIds as $iContentId) {

+ 38 - 24
modules/base/general/classes/BxBaseModGeneralFormsEntryHelper.php

@@ -15,7 +15,15 @@
 class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
 {
     protected $_oModule;
+
+    /**
+     * 'Ajax Mode' determines the format of response. If it's TRUE the response 
+     * (a form or an error appeared during form creation) should be returned 
+     * as text during initial loading, while all other responses appeared after 
+     * form submit should be arrays, which are ready to path to echoJson.
+     */
     protected $_bAjaxMode;
+
     protected $_bDynamicMode;
 
     public function __construct($oModule)
@@ -163,26 +171,31 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
     {
         if (!$sCheckFunction)
             $sCheckFunction = 'checkAllowedAdd';
-        
+
         $CNF = &$this->_oModule->_oConfig->CNF;
 
+        $bAsJson = false;
+
+        // get form object
+        $oForm = $this->getObjectFormAdd($sDisplay);
+        if (!$oForm)
+            return $this->prepareResponse(MsgBox(_t('_sys_txt_error_occured')), $bAsJson, 'msg');
+
+        $bAsJson = $this->_bAjaxMode && $oForm->isSubmitted();
+
         // check access
         if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction())) {
             $oProfile = BxDolProfile::getInstance();
             if ($oProfile && ($aProfileInfo = $oProfile->getInfo()) && $aProfileInfo['type'] == 'system' && is_subclass_of($this->_oModule, 'BxBaseModProfileModule') && $this->_oModule->serviceActAsProfile()) // special check for system profile is needed, because of incorrect error message
-                return $this->prepareResponse(MsgBox(_t('_sys_txt_access_denied')), $this->_bAjaxMode, 'msg');
+                return $this->prepareResponse(MsgBox(_t('_sys_txt_access_denied')), $bAsJson, 'msg');
             else
-                return $this->prepareResponse(MsgBox($sMsg), $this->_bAjaxMode, 'msg');
+                return $this->prepareResponse(MsgBox($sMsg), $bAsJson, 'msg');
         }
 
         // check and display form
-        $oForm = $this->getObjectFormAdd($sDisplay);
-        if (!$oForm)
-            return $this->prepareResponse(MsgBox(_t('_sys_txt_error_occured')), $this->_bAjaxMode, 'msg');
-
         $oForm->initChecker();
         if (!$oForm->isSubmittedAndValid())
-            return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $this->_bAjaxMode && $oForm->isSubmitted(), 'form', array(
+            return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
             	'form_id' => $oForm->getId()
             ));
 
@@ -191,16 +204,16 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
         $iContentId = $oForm->insert ($aValsToAdd);
         if (!$iContentId) {
             if (!$oForm->isValid())
-                return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $this->_bAjaxMode, 'form', array(
-                	'form_id' => $oForm->getId()
+                return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
+                    'form_id' => $oForm->getId()
                 ));
             else
-                return $this->prepareResponse(MsgBox(_t('_sys_txt_error_entry_creation')), $this->_bAjaxMode, 'msg');
+                return $this->prepareResponse(MsgBox(_t('_sys_txt_error_entry_creation')), $bAsJson, 'msg');
         }
 
         $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
         if ($sResult)
-            return $this->prepareResponse($sResult, $this->_bAjaxMode, 'msg');
+            return $this->prepareResponse($sResult, $bAsJson, 'msg');
 
         // process uploaded files
         if (isset($CNF['FIELD_PHOTO']))
@@ -219,10 +232,12 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
     	$CNF = &$this->_oModule->_oConfig->CNF;
 
     	$sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
-        if($this->_bAjaxMode)
-        	$this->prepareResponse(BX_DOL_URL_ROOT . $sUrl, $this->_bAjaxMode, 'redirect');
-		else
-        	$this->_redirectAndExit($sUrl);
+        if($this->_bAjaxMode) {
+            echoJson($this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect'));
+            exit;
+        }
+        else
+            $this->_redirectAndExit($sUrl);
     }
 
     public function editDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
@@ -491,17 +506,16 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
     protected function prepareResponse($mixedResponse, $bAsJson = false, $sKey = 'msg', $aAdditional = array())
     {
     	if(!$bAsJson)
-    		return $mixedResponse;
+            return $mixedResponse;
 
-		$aResponse = array(
-			$sKey => $mixedResponse
-		);
+        $aResponse = array(
+            $sKey => $mixedResponse
+        );
 
-		if(!empty($aAdditional) && is_array($aAdditional))
-			$aResponse = array_merge($aResponse, $aAdditional);
+        if(!empty($aAdditional) && is_array($aAdditional))
+            $aResponse = array_merge($aResponse, $aAdditional);
 
-		echoJson($aResponse);
-		exit;
+        return $aResponse;
     }
 }
 

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

@@ -50,16 +50,22 @@ class BxBaseModGeneralModule extends BxDolModule
     
     public function actionGetCreatePostForm()
     {
+        $sName = $this->_oConfig->getName();
+
     	$aParams = bx_process_input(array_intersect_key($_GET, $this->_aFormParams));
     	$aParams = array_merge($this->_aFormParams, $aParams);
 
-    	$sForm = $this->serviceGetCreatePostForm($aParams);
-    	if(empty($sForm))
+    	$mixedResponse = $this->serviceGetCreatePostForm($aParams);
+    	if(empty($mixedResponse))
             return echoJson(array());
+        else if(is_array($mixedResponse)) {
+            $mixedResponse['module'] = $sName;
+            return echoJson($mixedResponse);
+        }
 
         return echoJson(array(
-            'module' => $this->_oConfig->getName(),
-            'content' => $sForm
+            'module' => $sName,
+            'content' => $mixedResponse
     	));
     }
 

+ 19 - 16
modules/boonex/files/classes/BxFilesFormsEntryHelper.php

@@ -23,22 +23,25 @@ class BxFilesFormsEntryHelper extends BxBaseModFilesFormsEntryHelper
 
     public function addDataForm ($sDisplay = false, $sCheckFunction = false)
     {
-		$mixedContent = $this->addDataFormAction($sDisplay, $sCheckFunction);
-		if (is_array($mixedContent) && $mixedContent['need_redirect_after_action']){
-			$CNF = &$this->_oModule->_oConfig->CNF;
-			$aContentIds = $mixedContent['content_ids_array'];
-			$iContentId = array_pop($aContentIds);
-			$aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
-			$oProfile = BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']]);
-			$sUrl = 'page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . ($oProfile ? $oProfile->id() : bx_get_logged_profile_id());
-			if($this->_bAjaxMode)
-	        	$this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect');
-			else
-	        	$this->_redirectAndExit($sUrl);
-		}
-		else {
-			return $mixedContent;	
-		}
+        $mixedContent = $this->addDataFormAction($sDisplay, $sCheckFunction);
+        if (is_array($mixedContent) && $mixedContent['need_redirect_after_action']) {
+            $CNF = &$this->_oModule->_oConfig->CNF;
+            
+            $aContentIds = $mixedContent['content_ids_array'];
+            $iContentId = array_pop($aContentIds);
+            $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
+            $oProfile = BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']]);
+            $sUrl = 'page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . ($oProfile ? $oProfile->id() : bx_get_logged_profile_id());
+            if($this->_bAjaxMode) {
+                echoJson($this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect'));
+                exit;
+            }
+            else
+                $this->_redirectAndExit($sUrl);
+        }
+        else {
+                return $mixedContent;	
+        }
     }
 
     protected function redirectAfterDelete($aContentInfo)

+ 13 - 11
modules/boonex/photos/classes/BxPhotosFormsEntryHelper.php

@@ -24,18 +24,20 @@ class BxPhotosFormsEntryHelper extends BxBaseModFilesFormsEntryHelper
     public function addDataForm ($sDisplay = false, $sCheckFunction = false)
     {
         $mixedContent = $this->addDataFormAction($sDisplay, $sCheckFunction);
-		if (is_array($mixedContent) && $mixedContent['need_redirect_after_action']){
-			$CNF = &$this->_oModule->_oConfig->CNF;
+        if (is_array($mixedContent) && $mixedContent['need_redirect_after_action']){
+            $CNF = &$this->_oModule->_oConfig->CNF;
 
-			$sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . bx_get_logged_profile_id());
-			if($this->_bAjaxMode)
-	        	$this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect');
-			else
-	        	$this->_redirectAndExit($sUrl);
-		}
-		else {
-			return $mixedContent;
-		}
+            $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . bx_get_logged_profile_id());
+            if($this->_bAjaxMode) {
+                echoJson($this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect'));
+                exit;
+            }
+            else
+                $this->_redirectAndExit($sUrl);
+        }
+        else {
+                return $mixedContent;
+        }
     }
 }