BxResourcesModule.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. * @defgroup Resources Resources
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. class BxResourcesModule extends BxBaseModTextModule
  11. {
  12. function __construct(&$aModule)
  13. {
  14. parent::__construct($aModule);
  15. $CNF = &$this->_oConfig->CNF;
  16. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, [
  17. $CNF['FIELD_PUBLISHED'],
  18. $CNF['FIELD_ALLOW_COMMENTS']
  19. ]);
  20. }
  21. /**
  22. * Action methods
  23. */
  24. public function actionProcessListForm($iContextId, $iId)
  25. {
  26. if(!$this->isAllowAdd($iContextId))
  27. return '';
  28. $CNF = &$this->_oConfig->CNF;
  29. $bAdd = $iId == 0;
  30. $oForm = null;
  31. $sPopupTitle = "";
  32. $aContentInfo = [];
  33. if ($bAdd) {
  34. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_ADD']);
  35. $sPopupTitle = _t('_bx_resources_form_list_entry_display_add');
  36. }
  37. else {
  38. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_EDIT']);
  39. $sPopupTitle = _t('_bx_resources_form_list_entry_display_edit');
  40. $aContentInfo = $this->_oDb->getList($iId);
  41. }
  42. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_list_form/' . $iContextId . '/' . $iId . '/';
  43. if(!$oForm)
  44. return '';
  45. $oForm->initChecker($aContentInfo, []);
  46. if($oForm->isSubmittedAndValid()) {
  47. if ($bAdd)
  48. $oForm->insert(['context_id' => $iContextId]);
  49. else
  50. $oForm->update($iId);
  51. return echoJson([
  52. 'eval' => $this->_oConfig->getJsObject('main') . '.reloadData(oData, ' . $iContextId . ')',
  53. ]);
  54. }
  55. else {
  56. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  57. 'form_id' => $oForm->getId(),
  58. 'form' => $oForm->getCode(true)
  59. ]);
  60. if($oForm->isSubmitted() && !$oForm->isValid())
  61. return echoJson([
  62. 'form' => $sContent,
  63. 'form_id' => $oForm->getId()
  64. ]);
  65. echo $sContent;
  66. }
  67. }
  68. public function actionDeleteList($iContextId, $iListId)
  69. {
  70. if(!$this->isAllowManageByContext($iContextId))
  71. return echoJson([]);
  72. $this->_oDb->deleteList($iListId);
  73. echoJson([
  74. 'context_id' => $iContextId,
  75. ]);
  76. }
  77. public function actionProcessEntryForm($iContextId, $iListId = 0)
  78. {
  79. if(!$this->isAllowAdd($iContextId))
  80. return echoJson([]);
  81. $CNF = &$this->_oConfig->CNF;
  82. $sForm = $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'];
  83. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sForm);
  84. if(!$oForm)
  85. return echoJson([]);
  86. $oForm->setId($sForm);
  87. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_entry_form/' . $iContextId . '/' . $iListId . '/');
  88. $oForm->setContextId($iContextId);
  89. $oForm->initChecker();
  90. if($oForm->isSubmittedAndValid()) {
  91. $iContentId = $oForm->insert([$CNF['FIELD_ALLOW_VIEW_TO'] => -$iContextId, $CNF['FIELD_LIST'] => $iListId ?: $this->getListDefault($iContextId)]);
  92. if($iContentId)
  93. $this->onPublished($iContentId);
  94. return echoJson([
  95. 'eval' => $this->_oConfig->getJsObject('main') . '.reloadData(oData, ' . $iContextId . ')',
  96. ]);
  97. }
  98. else {
  99. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  100. 'form_id' => $oForm->getId(),
  101. 'form' => $oForm->getCode(true)
  102. ]);
  103. if($oForm->isSubmitted())
  104. return echoJson(['form' => $sContent, 'form_id' => $oForm->getId()]);
  105. echo $sContent;
  106. }
  107. }
  108. public function serviceCheckAllowedManageInContext($iContextPid)
  109. {
  110. if(!$this->isAllowManageByContext($iContextPid))
  111. return false;
  112. return true;
  113. }
  114. public function serviceCheckAllowedManage($iContentId)
  115. {
  116. if(!$this->isAllowManage($iContentId))
  117. return false;
  118. return true;
  119. }
  120. public function serviceIsAllowBadges($iContentId)
  121. {
  122. if (!$this->isAllowManage($iContentId))
  123. return false;
  124. if (!$this->serviceIsBadgesAvaliable())
  125. return false;
  126. return true;
  127. }
  128. public function serviceCheckAllowedCommentsPost($iContentId, $sObjectComments)
  129. {
  130. $CNF = &$this->_oConfig->CNF;
  131. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  132. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  133. return false;
  134. return parent::serviceCheckAllowedCommentsPost($iContentId, $sObjectComments);
  135. }
  136. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  137. {
  138. $CNF = &$this->_oConfig->CNF;
  139. //negative id used in comments for reports
  140. if($iContentId < 0)
  141. return CHECK_ACTION_RESULT_ALLOWED;
  142. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  143. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  144. return false;
  145. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  146. }
  147. public function serviceManageTools($sType = 'common')
  148. {
  149. $aResult = parent::serviceManageTools('common');
  150. if($sType == 'common' && isset($aResult['menu']))
  151. unset($aResult['menu']);
  152. return $aResult;
  153. }
  154. public function serviceBrowseContextByCategory($iProfileId = 0, $mixedCategory = false)
  155. {
  156. $aParams = [];
  157. if($mixedCategory)
  158. $aParams['category'] = $mixedCategory;
  159. return parent::serviceBrowseContext($iProfileId, $aParams);
  160. }
  161. public function serviceBrowseResources($iContextId = 0, $aParams = [])
  162. {
  163. if(!$iContextId)
  164. $iContextId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  165. if(!$iContextId)
  166. return '';
  167. if(!$this->isAllowView($iContextId))
  168. return '';
  169. if(!($oProfileContext = BxDolProfile::getInstance($iContextId)) || $oProfileContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  170. return '';
  171. return $this->_oTemplate->getEntriesList($iContextId);
  172. }
  173. /**
  174. * Common methods
  175. */
  176. public function checkAllowedSetThumb ($iContentId = 0)
  177. {
  178. return CHECK_ACTION_RESULT_ALLOWED;
  179. }
  180. public function isAllowView($iContextId)
  181. {
  182. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  183. return false;
  184. return true;
  185. }
  186. public function isAllowAdd($iContextId)
  187. {
  188. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedPostInProfile($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  189. return false;
  190. return true;
  191. }
  192. public function isAllowManageByContext($iContextId)
  193. {
  194. if(isAdmin())
  195. return true;
  196. $oProfileContext = BxDolProfile::getInstance($iContextId);
  197. if(BxDolService::call($oProfileContext->getModule(), 'is_admin', array($iContextId)))
  198. return true;
  199. return false;
  200. }
  201. public function isAllowManage($mixedContent)
  202. {
  203. $CNF = &$this->_oConfig->CNF;
  204. $aContentInfo = !is_array($mixedContent) ? $this->_oDb->getContentInfoById((int)$mixedContent) : $mixedContent;
  205. if($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED)
  206. return true;
  207. if($this->isAllowManageByContext(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  208. return true;
  209. return false;
  210. }
  211. public function getListDefault($iContextId)
  212. {
  213. $CNF = &$this->_oConfig->CNF;
  214. $aList = $this->_oDb->getListDefault($iContextId);
  215. $iListId = 0;
  216. if(empty($aList) || !is_array($aList))
  217. $iListId = $this->_oDb->addListDefault($iContextId);
  218. else
  219. $iListId = (int)$aList[$CNF['FIELD_LIST_ID']];
  220. return $iListId;
  221. }
  222. /**
  223. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  224. */
  225. public function checkAllowedManage ($aDataEntry, $isPerformAction = false)
  226. {
  227. return $this->isAllowManage($aDataEntry) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  228. }
  229. public function decodeDataAPI($aData, $aParams = [])
  230. {
  231. return array_merge(parent::decodeDataAPI($aData, $aParams), [
  232. 'url' => $this->_oTemplate->getUnitLink($aData)
  233. ]);
  234. }
  235. }
  236. /** @} */