| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- * @defgroup Resources Resources
- * @ingroup UnaModules
- *
- * @{
- */
- class BxResourcesModule extends BxBaseModTextModule
- {
- function __construct(&$aModule)
- {
- parent::__construct($aModule);
- $CNF = &$this->_oConfig->CNF;
- $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, [
- $CNF['FIELD_PUBLISHED'],
- $CNF['FIELD_ALLOW_COMMENTS']
- ]);
- }
-
- /**
- * Action methods
- */
- public function actionProcessListForm($iContextId, $iId)
- {
- if(!$this->isAllowAdd($iContextId))
- return '';
- $CNF = &$this->_oConfig->CNF;
- $bAdd = $iId == 0;
- $oForm = null;
- $sPopupTitle = "";
- $aContentInfo = [];
- if ($bAdd) {
- $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_ADD']);
- $sPopupTitle = _t('_bx_resources_form_list_entry_display_add');
- }
- else {
- $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_EDIT']);
- $sPopupTitle = _t('_bx_resources_form_list_entry_display_edit');
- $aContentInfo = $this->_oDb->getList($iId);
- }
- $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_list_form/' . $iContextId . '/' . $iId . '/';
- if(!$oForm)
- return '';
-
- $oForm->initChecker($aContentInfo, []);
- if($oForm->isSubmittedAndValid()) {
- if ($bAdd)
- $oForm->insert(['context_id' => $iContextId]);
- else
- $oForm->update($iId);
- return echoJson([
- 'eval' => $this->_oConfig->getJsObject('main') . '.reloadData(oData, ' . $iContextId . ')',
- ]);
- }
- else {
- $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
- 'form_id' => $oForm->getId(),
- 'form' => $oForm->getCode(true)
- ]);
- if($oForm->isSubmitted() && !$oForm->isValid())
- return echoJson([
- 'form' => $sContent,
- 'form_id' => $oForm->getId()
- ]);
- echo $sContent;
- }
- }
- public function actionDeleteList($iContextId, $iListId)
- {
- if(!$this->isAllowManageByContext($iContextId))
- return echoJson([]);
- $this->_oDb->deleteList($iListId);
- echoJson([
- 'context_id' => $iContextId,
- ]);
- }
-
- public function actionProcessEntryForm($iContextId, $iListId = 0)
- {
- if(!$this->isAllowAdd($iContextId))
- return echoJson([]);
- $CNF = &$this->_oConfig->CNF;
- $sForm = $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'];
- $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sForm);
- if(!$oForm)
- return echoJson([]);
- $oForm->setId($sForm);
- $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_entry_form/' . $iContextId . '/' . $iListId . '/');
- $oForm->setContextId($iContextId);
- $oForm->initChecker();
- if($oForm->isSubmittedAndValid()) {
- $iContentId = $oForm->insert([$CNF['FIELD_ALLOW_VIEW_TO'] => -$iContextId, $CNF['FIELD_LIST'] => $iListId ?: $this->getListDefault($iContextId)]);
- if($iContentId)
- $this->onPublished($iContentId);
- return echoJson([
- 'eval' => $this->_oConfig->getJsObject('main') . '.reloadData(oData, ' . $iContextId . ')',
- ]);
- }
- else {
- $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
- 'form_id' => $oForm->getId(),
- 'form' => $oForm->getCode(true)
- ]);
- if($oForm->isSubmitted())
- return echoJson(['form' => $sContent, 'form_id' => $oForm->getId()]);
- echo $sContent;
- }
- }
- public function serviceCheckAllowedManageInContext($iContextPid)
- {
- if(!$this->isAllowManageByContext($iContextPid))
- return false;
- return true;
- }
- public function serviceCheckAllowedManage($iContentId)
- {
- if(!$this->isAllowManage($iContentId))
- return false;
- return true;
- }
-
- public function serviceIsAllowBadges($iContentId)
- {
- if (!$this->isAllowManage($iContentId))
- return false;
-
- if (!$this->serviceIsBadgesAvaliable())
- return false;
-
- return true;
- }
- public function serviceCheckAllowedCommentsPost($iContentId, $sObjectComments)
- {
- $CNF = &$this->_oConfig->CNF;
- $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
- if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
- return false;
- return parent::serviceCheckAllowedCommentsPost($iContentId, $sObjectComments);
- }
- public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
- {
- $CNF = &$this->_oConfig->CNF;
- //negative id used in comments for reports
- if($iContentId < 0)
- return CHECK_ACTION_RESULT_ALLOWED;
- $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
- if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
- return false;
- return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
- }
- public function serviceManageTools($sType = 'common')
- {
- $aResult = parent::serviceManageTools('common');
- if($sType == 'common' && isset($aResult['menu']))
- unset($aResult['menu']);
- return $aResult;
- }
- public function serviceBrowseContextByCategory($iProfileId = 0, $mixedCategory = false)
- {
- $aParams = [];
- if($mixedCategory)
- $aParams['category'] = $mixedCategory;
- return parent::serviceBrowseContext($iProfileId, $aParams);
- }
- public function serviceBrowseResources($iContextId = 0, $aParams = [])
- {
- if(!$iContextId)
- $iContextId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
- if(!$iContextId)
- return '';
- if(!$this->isAllowView($iContextId))
- return '';
- if(!($oProfileContext = BxDolProfile::getInstance($iContextId)) || $oProfileContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
- return '';
- return $this->_oTemplate->getEntriesList($iContextId);
- }
- /**
- * Common methods
- */
- public function checkAllowedSetThumb ($iContentId = 0)
- {
- return CHECK_ACTION_RESULT_ALLOWED;
- }
- public function isAllowView($iContextId)
- {
- if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
- return false;
- return true;
- }
- public function isAllowAdd($iContextId)
- {
- if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedPostInProfile($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
- return false;
- return true;
- }
- public function isAllowManageByContext($iContextId)
- {
- if(isAdmin())
- return true;
-
- $oProfileContext = BxDolProfile::getInstance($iContextId);
- if(BxDolService::call($oProfileContext->getModule(), 'is_admin', array($iContextId)))
- return true;
-
- return false;
- }
- public function isAllowManage($mixedContent)
- {
- $CNF = &$this->_oConfig->CNF;
- $aContentInfo = !is_array($mixedContent) ? $this->_oDb->getContentInfoById((int)$mixedContent) : $mixedContent;
- if($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED)
- return true;
- if($this->isAllowManageByContext(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
- return true;
- return false;
- }
-
- public function getListDefault($iContextId)
- {
- $CNF = &$this->_oConfig->CNF;
- $aList = $this->_oDb->getListDefault($iContextId);
- $iListId = 0;
- if(empty($aList) || !is_array($aList))
- $iListId = $this->_oDb->addListDefault($iContextId);
- else
- $iListId = (int)$aList[$CNF['FIELD_LIST_ID']];
- return $iListId;
- }
- /**
- * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
- */
- public function checkAllowedManage ($aDataEntry, $isPerformAction = false)
- {
- return $this->isAllowManage($aDataEntry) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
- }
-
- public function decodeDataAPI($aData, $aParams = [])
- {
- return array_merge(parent::decodeDataAPI($aData, $aParams), [
- 'url' => $this->_oTemplate->getUnitLink($aData)
- ]);
- }
- }
- /** @} */
|