BxResourcesModule.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. if(!$iListId) {
  92. $aList = $this->_oDb->getListDefault($iContextId);
  93. if(empty($aList) || !is_array($aList))
  94. $iListId = $this->_oDb->addListDefault($iContextId);
  95. else
  96. $iListId = (int)$aList[$CNF['FIELD_LIST_ID']];
  97. }
  98. $iContentId = $oForm->insert([$CNF['FIELD_ALLOW_VIEW_TO'] => -$iContextId, $CNF['FIELD_LIST'] => $iListId]);
  99. if($iContentId)
  100. $this->onPublished($iContentId);
  101. return echoJson([
  102. 'eval' => $this->_oConfig->getJsObject('main') . '.reloadData(oData, ' . $iContextId . ')',
  103. ]);
  104. }
  105. else {
  106. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  107. 'form_id' => $oForm->getId(),
  108. 'form' => $oForm->getCode(true)
  109. ]);
  110. if($oForm->isSubmitted())
  111. return echoJson(['form' => $sContent, 'form_id' => $oForm->getId()]);
  112. echo $sContent;
  113. }
  114. }
  115. public function serviceManageTools($sType = 'common')
  116. {
  117. $CNF = &$this->_oConfig->CNF;
  118. $mixedResults = '';
  119. if(!($mixedResults = parent::serviceManageTools($sType)))
  120. return $mixedResults;
  121. return array_merge(parent::serviceManageTools($sType), [
  122. 'menu' => BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU'])
  123. ]);
  124. }
  125. public function serviceCheckAllowedManageInContext($iContextPid)
  126. {
  127. if(!$this->isAllowManageByContext($iContextPid))
  128. return false;
  129. return true;
  130. }
  131. public function serviceCheckAllowedManage($iContentId)
  132. {
  133. if(!$this->isAllowManage($iContentId))
  134. return false;
  135. return true;
  136. }
  137. public function serviceIsAllowBadges($iContentId)
  138. {
  139. if (!$this->isAllowManage($iContentId))
  140. return false;
  141. if (!$this->serviceIsBadgesAvaliable())
  142. return false;
  143. return true;
  144. }
  145. public function serviceCheckAllowedCommentsPost($iContentId, $sObjectComments)
  146. {
  147. $CNF = &$this->_oConfig->CNF;
  148. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  149. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  150. return false;
  151. return parent::serviceCheckAllowedCommentsPost($iContentId, $sObjectComments);
  152. }
  153. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  154. {
  155. $CNF = &$this->_oConfig->CNF;
  156. //negative id used in comments for reports
  157. if($iContentId < 0)
  158. return CHECK_ACTION_RESULT_ALLOWED;
  159. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  160. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  161. return false;
  162. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  163. }
  164. public function serviceBrowseResources($iContextId = 0, $aParams = [])
  165. {
  166. if(!$iContextId)
  167. $iContextId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  168. if(!$iContextId)
  169. return '';
  170. if(!$this->isAllowView($iContextId))
  171. return '';
  172. if(!($oProfileContext = BxDolProfile::getInstance($iContextId)) || $oProfileContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  173. return '';
  174. return $this->_oTemplate->getEntriesList($iContextId);
  175. }
  176. /**
  177. * Common methods
  178. */
  179. public function checkAllowedSetThumb ($iContentId = 0)
  180. {
  181. return CHECK_ACTION_RESULT_ALLOWED;
  182. }
  183. public function isAllowView($iContextId)
  184. {
  185. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  186. return false;
  187. return true;
  188. }
  189. public function isAllowAdd($iContextId)
  190. {
  191. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedPostInProfile($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  192. return false;
  193. return true;
  194. }
  195. public function isAllowManageByContext($iContextId)
  196. {
  197. if(isAdmin())
  198. return true;
  199. $oProfileContext = BxDolProfile::getInstance($iContextId);
  200. if(BxDolService::call($oProfileContext->getModule(), 'is_admin', array($iContextId)))
  201. return true;
  202. return false;
  203. }
  204. public function isAllowManage($mixedContent)
  205. {
  206. $CNF = &$this->_oConfig->CNF;
  207. $aContentInfo = !is_array($mixedContent) ? $this->_oDb->getContentInfoById((int)$mixedContent) : $mixedContent;
  208. if($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED)
  209. return true;
  210. if($this->isAllowManageByContext(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  211. return true;
  212. return false;
  213. }
  214. /**
  215. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  216. */
  217. public function checkAllowedManage ($aDataEntry, $isPerformAction = false)
  218. {
  219. return $this->isAllowManage($aDataEntry) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  220. }
  221. }
  222. /** @} */