Bläddra i källkod

Ticket #5022 - Force profile creation when 'Automatic profile creation...' setting is enabled.

AntonLV 1 år sedan
förälder
incheckning
8fc4ceb190

+ 18 - 2
modules/base/general/classes/BxBaseModGeneralFormsEntryHelper.php

@@ -27,6 +27,13 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
      */
     protected $_bAjaxMode;
 
+    /**
+     * 'Valid Mode' allows to create content even if some fields values 
+     * didn't pass checking. It's needed when content was created 
+     * with a service call. For example, automatic profile creation after join.
+     */
+    protected $_bValidMode;
+
     /**
      * Use absolute Action URL in generated form object. 
      * It's needed in Ajax Mode.
@@ -64,6 +71,11 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
             $this->setDynamicMode(true);
     }
 
+    public function setValidMode($bValidMode)
+    {
+        $this->_bValidMode = (bool)$bValidMode;
+    }
+
     public function setAbsoluteActionUrl($bAbsoluteActionUrl)
     {
         $this->_bAbsoluteActionUrl = (bool)$bAbsoluteActionUrl;
@@ -204,8 +216,12 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
                 $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
         }
 
-        $oForm->initChecker(array(), $aValues);
-        if (!$oForm->isSubmittedAndValid()) {
+        //TODO: Use in Edit too.
+        $oForm->initChecker([], $aValues);
+        if($this->_bValidMode && !$oForm->isValid())
+            $oForm->setValid(true);
+
+        if(!$oForm->isSubmittedAndValid()) {
             $aErrors = array();
             array_walk($oForm->aInputs, function($aInput, $sKey) use (&$aErrors) {
                 if(!empty($aInput['error']))

+ 15 - 5
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -1625,22 +1625,32 @@ class BxBaseModGeneralModule extends BxDolModule
         return new $sClass($this);
     }
 
-    public function serviceFormsHelper ()
+    public function serviceFormsHelper()
     {
         return $this->getFormsHelper();
     }
 
-	/**
+    /**
      * Add entry using provided fields' values.
-     * @return array with result: 'code' is 0 on success or non-zero on error, 'message' is error message in case of error, 'content' is content info array in case of success
+     * @return array with result: 
+     * 'code' is 0 on success or non-zero on error, 
+     * 'message' is error message in case of error, 
+     * 'content' is content info array in case of success
      */
-    public function serviceEntityAdd ($iProfile, $aValues, $sDisplay = false)
+    public function serviceEntityAdd($iProfile, $aValues, $sDisplay = false)
     {
         $oFormsHelper = $this->getFormsHelper();
         return $oFormsHelper->addData($iProfile, $aValues, $sDisplay);
     }
 
-	/**
+    public function serviceEntityAddForcedly($iProfile, $aValues, $sDisplay = false)
+    {
+        $oFormsHelper = $this->getFormsHelper();
+        $oFormsHelper->setValidMode(true);
+        return $oFormsHelper->addData($iProfile, $aValues, $sDisplay);
+    }
+
+    /**
      * Perform redirect after content creation
      * @return nothing, rediret header is sent
      */    

+ 12 - 19
modules/boonex/accounts/classes/BxAccntGridAdministration.php

@@ -239,32 +239,25 @@ class BxAccntGridAdministration extends BxBaseModProfileGridAdministration
 
                 // perform action
                 BxDolAccount::isAllowedCreate ($iProfileId, true);
-                
+
                 // check
-                $aModulesProfile = array(); 
-                $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
-                foreach($aModules as $aModule) {
-                    $oModule = BxDolModule::getInstance($aModule['name']);
-                    if($oModule instanceof iBxDolProfileService && BxDolService::call($aModule['name'], 'act_as_profile') === true)
-                        $aModulesProfile[] = $aModule;
-                }
+                $aModulesProfile = bx_srv('system', 'get_modules_by_type', ['profile']);
 
-                $sDefaultProfileType = getParam('sys_account_default_profile_type');
                 if(count($aModulesProfile) == 1)
-                    $sProfileModule = $aModulesProfile[0]['name'];
-                else if(!empty($sDefaultProfileType)) 
+                    $sProfileModule = reset($aModulesProfile)['name'];
+                else if(($sDefaultProfileType = getParam('sys_account_default_profile_type')) !== '') 
                     $sProfileModule = $sDefaultProfileType;
 
-                if (getParam('sys_account_auto_profile_creation') && !empty($sProfileModule)) {
-                    $oAccount = BxDolAccount::getInstance($iAccountId);
-                    $aProfileInfo = BxDolService::call($sProfileModule, 'prepare_fields', array(array(
+                if(!empty($sProfileModule) && getParam('sys_account_auto_profile_creation') == 'on') {
+                    $aProfileFields = bx_srv($sProfileModule, 'prepare_fields', [[
                         'author' => $iProfileId,
-                        'name' => $oAccount->getDisplayName(),
-                    )));
-                    $a = BxDolService::call($sProfileModule, 'entity_add', array($iProfileId, $aProfileInfo));
-                    if (isset($a['content']['profile_id']) && (int)$a['content']['profile_id'] > 0){
+                        'name' => BxDolAccount::getInstance($iAccountId)->getDisplayName(),
+                    ]]);
+
+                    $a = bx_srv($sProfileModule, 'entity_add_forcedly', [$iProfileId, $aProfileFields]);
+                    if(isset($a['content']['profile_id']) && (int)$a['content']['profile_id'] > 0)
                         BxDolAcl::getInstance()->setMembership((int)$a['content']['profile_id'], MEMBERSHIP_ID_STANDARD);
-                    }
+
                     echoJson($aRes);
                 }
             }

+ 9 - 17
template/scripts/BxBaseAccountForms.php

@@ -142,28 +142,20 @@ class BxBaseAccountForms extends BxDolProfileForms
         $bRelocateCustom = !empty($sRelocateCustom);
 
         // check and redirect
-        $aModulesProfile = array(); 
-        $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
-        foreach($aModules as $aModule) {
-        	$oModule = BxDolModule::getInstance($aModule['name']);
-        	if($oModule instanceof iBxDolProfileService && BxDolService::call($aModule['name'], 'act_as_profile') === true)
-        		$aModulesProfile[] = $aModule;
-        }
+        $aModulesProfile = bx_srv('system', 'get_modules_by_type', ['profile']);
 
-        $sDefaultProfileType = getParam('sys_account_default_profile_type');
         if(count($aModulesProfile) == 1)
-        	$sProfileModule = $aModulesProfile[0]['name'];
-        else if(!empty($sDefaultProfileType)) 
+            $sProfileModule = reset($aModulesProfile)['name'];
+        else if(($sDefaultProfileType = getParam('sys_account_default_profile_type')) !== '') 
             $sProfileModule = $sDefaultProfileType;
 
-        if (getParam('sys_account_auto_profile_creation') && !empty($sProfileModule)) {
-            $oAccount = BxDolAccount::getInstance($iAccountId);
-            $aProfileInfo = BxDolService::call($sProfileModule, 'prepare_fields', array(array(
+        if(!empty($sProfileModule) && getParam('sys_account_auto_profile_creation') == 'on') {
+            $aProfileFields = BxDolService::call($sProfileModule, 'prepare_fields', [[
                 'author' => $iProfileId,
-                'name' => $oAccount->getDisplayName(),
-            )));
-            
-            $a = BxDolService::call($sProfileModule, 'entity_add', array($iProfileId, $aProfileInfo));
+                'name' => BxDolAccount::getInstance($iAccountId)->getDisplayName(),
+            ]]);
+
+            $a = BxDolService::call($sProfileModule, 'entity_add_forcedly', [$iProfileId, $aProfileFields]);
 
             // in case of successful profile add redirect to the page after profile creation
             if (0 == $a['code'] && !$bIsApi) {