From 57e349499b2c758b80b4d5af29a8a9e8e1dafd25 Mon Sep 17 00:00:00 2001 From: AntonLV Date: Mon, 7 Oct 2024 10:50:06 +0300 Subject: [PATCH] Ticket #4817 - Courses improvements. Core changes. --- .../classes/BxBaseModFilesFormUpload.php | 15 ++-- .../BxBaseModFilesFormsEntryHelper.php | 24 ++++++ .../classes/BxBaseModGeneralFormEntry.php | 84 +++++++++++++++---- .../BxBaseModGeneralFormsEntryHelper.php | 11 +++ .../classes/BxBaseModGeneralModule.php | 11 +++ 5 files changed, 119 insertions(+), 26 deletions(-) diff --git a/modules/base/files/classes/BxBaseModFilesFormUpload.php b/modules/base/files/classes/BxBaseModFilesFormUpload.php index 94886f0b0d..cd0493320a 100644 --- a/modules/base/files/classes/BxBaseModFilesFormUpload.php +++ b/modules/base/files/classes/BxBaseModFilesFormUpload.php @@ -55,15 +55,12 @@ public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithCont bx_import('BxDolPrivacy'); // get values form main form to pass it to each file later - $aFormValues = array(); - foreach ($this->aInputs as $aInput) { - if(in_array($aInput['name'], array('do_submit', 'profile_id', $CNF['FIELD_PHOTO']))) - continue; - - $aFormValues[$aInput['name']] = $this->getCleanValue($aInput['name']); - } + $aFormValues = []; + foreach ($this->aInputs as $aInput) + if(!empty($aInput['db']) && is_array($aInput['db'])) + $aFormValues[$aInput['name']] = $this->getCleanValue($aInput['name']); - $aContentIds = array(); + $aContentIds = []; foreach ($aGhostFiles as $aFile) { if (CHECK_ACTION_RESULT_ALLOWED !== $this->_oModule->checkAllowedAdd()) continue; @@ -74,7 +71,7 @@ public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithCont $iContentId = BxBaseModGeneralFormEntry::insert (array_merge(array( $CNF['FIELD_FOR_STORING_FILE_ID'] => $aFile['id'], $CNF['FIELD_TITLE'] => $this->getCleanValue('title-' . $aFile['id']), - $CNF['FIELD_AUTHOR'] => bx_get('profile_id') && $this->_oModule->serviceIsAllowedAddContentToProfile(bx_get('profile_id')) ? bx_get('profile_id') : '', + $CNF['FIELD_AUTHOR'] => bx_get('profile_id') && $this->_oModule->serviceIsAllowedAddContentToContext(bx_get('profile_id')) ? bx_get('profile_id') : '', $CNF['FIELD_STATUS_ADMIN'] => !$this->_oModule->_isModerator() && !$this->_oModule->_oConfig->isAutoApproveEnabled() ? BX_BASE_MOD_GENERAL_STATUS_PENDING : BX_BASE_MOD_TEXT_STATUS_ACTIVE ), $aFormValues)); if (!$iContentId) diff --git a/modules/base/files/classes/BxBaseModFilesFormsEntryHelper.php b/modules/base/files/classes/BxBaseModFilesFormsEntryHelper.php index 8bb3ae22e1..14736119d3 100644 --- a/modules/base/files/classes/BxBaseModFilesFormsEntryHelper.php +++ b/modules/base/files/classes/BxBaseModFilesFormsEntryHelper.php @@ -87,6 +87,30 @@ protected function addDataFormAction ($sDisplay = false, $sCheckFunction = false return array('need_redirect_after_action' => true, 'content_ids_array' => $aContentIds); } + + protected function getRedirectUrlAfterAdd($mixedContent) + { + $CNF = &$this->_oModule->_oConfig->CNF; + + $iProfileId = false; + $sUri = $CNF['URI_AUTHOR_ENTRIES']; + + $sKeyIds = 'content_ids_array'; + if(!empty($mixedContent[$sKeyIds]) && is_array($mixedContent[$sKeyIds])) { + $aContentInfo = $this->_oModule->_oDb->getContentInfoById(array_pop($mixedContent[$sKeyIds])); + + if(($sKey = 'FIELD_AUTHOR') && isset($CNF[$sKey]) && ($iAuthor = (int)$aContentInfo[$CNF[$sKey]])) { + $iProfileId = $iAuthor; + } + + if(($sKey = 'FIELD_ALLOW_VIEW_TO') && isset($CNF[$sKey]) && ($iVisibility = (int)$aContentInfo[$CNF[$sKey]]) < 0) { + $iProfileId = abs($iVisibility); + $sUri = $CNF['URI_ENTRIES_BY_CONTEXT']; + } + } + + return 'page.php?i=' . $sUri . '&profile_id=' . (($oProfile = BxDolProfile::getInstance($iProfileId)) !== false ? $oProfile->id() : bx_get_logged_profile_id()); + } } /** @} */ diff --git a/modules/base/general/classes/BxBaseModGeneralFormEntry.php b/modules/base/general/classes/BxBaseModGeneralFormEntry.php index 1d17356e14..12541e6824 100644 --- a/modules/base/general/classes/BxBaseModGeneralFormEntry.php +++ b/modules/base/general/classes/BxBaseModGeneralFormEntry.php @@ -418,6 +418,8 @@ function initChecker ($aValues = array (), $aSpecificValues = array()) parent::initChecker ($aValues, $aSpecificValues); + $this->_processContextBeforeAdd(); + foreach($aPrivacyFields as $sField => $sObject) $this->_validatePrivacyField($sField, $sObject, $aValues); } @@ -509,33 +511,35 @@ public function insert ($aValsToAdd = array(), $isIgnore = false) $bMulticatEnabled = $this->_isMulticatEnabled(); if ($bMulticatEnabled) $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd); + $iContentId = parent::insert ($aValsToAdd, $isIgnore); - - if(!empty($iContentId)) { - foreach($this->aInputs as $aInput) { - if ($aInput['type'] == 'nested_form') { - if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) { - foreach ($aInput['ghost_template'] as $oFormNested) { - $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId)); + if(empty($iContentId)) + return $iContentId; - if ($aInput['rateable']) { - BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId); - } + foreach($this->aInputs as $aInput) { + if ($aInput['type'] == 'nested_form') { + if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) { + foreach ($aInput['ghost_template'] as $oFormNested) { + $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId)); + + if ($aInput['rateable']) { + BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId); } } } } - - if ($bMulticatEnabled) - $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId); } - foreach($this->aInputs as $aInput) { - if (isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form'){ + if($bMulticatEnabled) + $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId); + + foreach($this->aInputs as $aInput) + if(isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form') BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName()); - } - } + if(($iContextNid = $this->getCleanValue('context_nid')) !== false) + $this->_processContextAfterAdd($iContentId, $iContextNid); + return $iContentId; } @@ -998,6 +1002,52 @@ protected function _validatePrivacyField($sField, $sPrivacyObject, $aValues) } } + protected function _processContextBeforeAdd() + { + $CNF = &$this->_oModule->_oConfig->CNF; + + $sKey = 'FIELD_ALLOW_VIEW_TO'; + if(!isset($CNF[$sKey], $this->aInputs[$CNF[$sKey]])) + return; + + if(($iContextId = bx_get('context_pid')) !== false) + $this->aInputs[$CNF[$sKey]] = array_merge($this->aInputs[$CNF[$sKey]], [ + 'type' => 'hidden', + 'value' => -abs((int)$iContextId) + ]); + + if(($iNodeId = bx_get('context_nid')) !== false && !isset($this->aInputs['context_nid'])) { + $this->aInputs['context_nid'] = [ + 'name' => 'context_nid', + 'type' => 'hidden', + 'value' => (int)$iNodeId + ]; + } + } + + protected function _processContextAfterAdd($iContentId, $iContextNid) + { + $CNF = &$this->_oModule->_oConfig->CNF; + + $sKey = 'FIELD_ALLOW_VIEW_TO'; + if(!isset($CNF[$sKey])) + return; + + $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId); + if(!is_numeric($aContentInfo[$CNF[$sKey]]) || (int)$aContentInfo[$CNF[$sKey]] > 0) + return; + + $iContextPid = abs($aContentInfo[$CNF[$sKey]]); + $oContext = BxDolProfile::getInstance($iContextPid); + if(!$oContext) + return; + + $sModule = $oContext->getModule(); + $sMethod = 'on_content_added'; + if(bx_is_srv($sModule, $sMethod)) + bx_srv($sModule, $sMethod, [$this->MODULE, $iContentId, $oContext->getContentId(), $iContextNid]); + } + /** * * MultiCategories related methods. diff --git a/modules/base/general/classes/BxBaseModGeneralFormsEntryHelper.php b/modules/base/general/classes/BxBaseModGeneralFormsEntryHelper.php index 552ef7116f..a4363ef210 100644 --- a/modules/base/general/classes/BxBaseModGeneralFormsEntryHelper.php +++ b/modules/base/general/classes/BxBaseModGeneralFormsEntryHelper.php @@ -626,6 +626,17 @@ public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile) if(($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false) $oPrivacy->deleteGroupCustomByContentId($iContentId); + $sKey = 'FIELD_ALLOW_VIEW_TO'; + if(isset($CNF[$sKey], $aContentInfo[$CNF[$sKey]]) && ($iContextPid = (int)$aContentInfo[$CNF[$sKey]]) < 0) { + $iContextPid = abs($iContextPid); + if(($oContext = BxDolProfile::getInstance($iContextPid)) !== false) { + $sModule = $oContext->getModule(); + $sMethod = 'on_content_deleted'; + if(bx_is_srv($sModule, $sMethod)) + bx_srv($sModule, $sMethod, [$this->_oModule->getName(), $iContentId, $oContext->getContentId()]); + } + } + bx_audit( $iContentId, $this->_oModule->getName(), diff --git a/modules/base/general/classes/BxBaseModGeneralModule.php b/modules/base/general/classes/BxBaseModGeneralModule.php index c8a9abccb8..82b688c10a 100644 --- a/modules/base/general/classes/BxBaseModGeneralModule.php +++ b/modules/base/general/classes/BxBaseModGeneralModule.php @@ -503,6 +503,17 @@ protected function _prepareBrowsingFiltersParamsGet($mParams = false) } // ====== SERVICE METHODS + public function serviceIsAllowedAddContentToContext($iContextPid) + { + $oContext = false; + if(!$iContextPid || !($oContext = BxDolProfile::getInstance((int)$iContextPid))) + return false; + + if($iContextPid == bx_get_logged_profile_id()) + return true; + + return bx_srv($oContext->getModule(), 'check_allowed_post_in_profile', [$oContext->getContentId(), $this->getName()]) === CHECK_ACTION_RESULT_ALLOWED; + } public function serviceIsBadgesAvaliable() {