1
0

BxBaseModGroupsFormsEntryHelper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. *
  6. * @defgroup BaseGroups Base classes for groups modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Group profile forms functions
  13. */
  14. class BxBaseModGroupsFormsEntryHelper extends BxBaseModProfileFormsEntryHelper
  15. {
  16. public function __construct($oModule)
  17. {
  18. parent::__construct($oModule);
  19. }
  20. public function getObjectFormInvite ($sDisplay = false)
  21. {
  22. $CNF = &$this->_oModule->_oConfig->CNF;
  23. if($sDisplay === false)
  24. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE'];
  25. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  26. if($this->_bAjaxMode)
  27. $oForm->setAjaxMode($this->_bAjaxMode);
  28. if($this->_bAbsoluteActionUrl)
  29. $this->_setAbsoluteActionUrl('edit', $oForm);
  30. return $oForm;
  31. }
  32. public function inviteForm ($iContentId, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  33. {
  34. if (!$sCheckFunction)
  35. $sCheckFunction = 'checkAllowedInvite';
  36. $CNF = &$this->_oModule->_oConfig->CNF;
  37. // get content data and profile info
  38. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  39. if(!$aContentInfo)
  40. return $bErrorMsg ? ($this->_bIsApi ? [bx_api_get_msg('_sys_txt_error_entry_is_not_defined')] : MsgBox('_sys_txt_error_entry_is_not_defined')) : '';
  41. // check access
  42. if(($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  43. return $bErrorMsg ? ($this->_bIsApi ? [bx_api_get_msg($sMsg)] : MsgBox($sMsg)) : '';
  44. // check and display form
  45. $oForm = $this->getObjectFormInvite($sDisplay);
  46. if(!$oForm)
  47. return $bErrorMsg ? ($this->_bIsApi ? [bx_api_get_msg('_sys_txt_error_occured')] : MsgBox(_t('_sys_txt_error_occured'))) : '';
  48. $oForm->initChecker($aContentInfo);
  49. if (!$oForm->isSubmittedAndValid())
  50. return $this->_bIsApi ? [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['name' => $this->_oModule->getName(), 'request' => ['url' => '/api.php?r=' . $this->_oModule->getName() . '/entity_invite&params[]=' . $iContentId . '&params[]=' . $sDisplay, 'immutable' => true]]])] : $oForm->getCode();
  51. $this->onDataInviteBefore($aContentInfo[$CNF['FIELD_ID']], $aContentInfo);
  52. if (!$oForm->update($aContentInfo[$CNF['FIELD_ID']])) {
  53. if (!$oForm->isValid())
  54. return $this->_bIsApi ? [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['name' => $this->_oModule->getName(), 'request' => ['url' => '/api.php?r=' . $this->_oModule->getName() . '/entity_invit&params[]=' . $iContentId . '&params[]=' . $sDisplay, 'immutable' => true]]])] : $oForm->getCode();
  55. else
  56. return $this->_bIsApi ? [bx_api_get_msg('_sys_txt_error_entry_update')] : MsgBox(_t('_sys_txt_error_entry_update'));
  57. }
  58. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  59. $sResult = $this->onDataInviteAfter($aContentInfo[$CNF['FIELD_ID']], $aContentInfo);
  60. if($sResult)
  61. return $sResult;
  62. // Perform ACL action
  63. $this->_oModule->$sCheckFunction($aContentInfo, true);
  64. // Redirect
  65. if (bx_is_api())
  66. return [$this->redirectAfterEdit($aContentInfo)];
  67. else
  68. $this->redirectAfterEdit($aContentInfo);
  69. }
  70. protected function _getProfileAndContentData ($iContentId)
  71. {
  72. $CNF = &$this->_oModule->_oConfig->CNF;
  73. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  74. if(!$aContentInfo)
  75. return array(false, false);
  76. $oProfile = BxDolProfile::getInstanceMagic($aContentInfo[$CNF['FIELD_AUTHOR']]);
  77. return array($oProfile, $aContentInfo);
  78. }
  79. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  80. {
  81. $sMsg = parent::_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile);
  82. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  83. if ($sMsg && $oPrivacy->isPartiallyVisible($aContentInfo[$this->_oModule->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']]))
  84. return '';
  85. return $sMsg;
  86. }
  87. public function onDataAddAfter ($iAccountId, $iContentId)
  88. {
  89. if($s = parent::onDataAddAfter($iAccountId, $iContentId))
  90. return $s;
  91. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
  92. if(!$oGroupProfile)
  93. return '';
  94. $mInitialMembers = bx_get('initial_members');
  95. if($this->_bIsApi && $mInitialMembers && is_string($mInitialMembers))
  96. $mInitialMembers = explode(',', $mInitialMembers);
  97. $this->makeAuthorAdmin($oGroupProfile, $mInitialMembers);
  98. $this->inviteMembers($oGroupProfile, $mInitialMembers);
  99. return '';
  100. }
  101. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  102. {
  103. if($s = parent::onDataEditAfter($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm))
  104. return $s;
  105. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
  106. if(!$oGroupProfile)
  107. return '';
  108. $this->inviteMembers ($oGroupProfile, bx_get('initial_members'));
  109. return '';
  110. }
  111. public function onDataInviteBefore ($iContentId, $aContentInfo)
  112. {
  113. return '';
  114. }
  115. public function onDataInviteAfter ($iContentId, $aContentInfo)
  116. {
  117. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
  118. if(!$oGroupProfile)
  119. return '';
  120. $this->inviteMembers($oGroupProfile, bx_get('initial_members'));
  121. return '';
  122. }
  123. protected function redirectAfterEdit($aContentInfo, $sUrl = '')
  124. {
  125. $CNF = &$this->_oModule->_oConfig->CNF;
  126. $sUrl = '';
  127. if(bx_get('initial_members')) {
  128. if(isset($CNF['URL_ENTRY_MANAGE']))
  129. $sUrl = $CNF['URL_ENTRY_MANAGE'];
  130. else if(isset($CNF['URL_ENTRY_FANS']))
  131. $sUrl = $CNF['URL_ENTRY_FANS'];
  132. $sUrl = bx_append_url_params($sUrl, ['profile_id' => $aContentInfo['profile_id']]);
  133. }
  134. else
  135. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  136. if(bx_is_api())
  137. return bx_api_get_block('redirect', ['uri' => '/' . BxDolPermalinks::getInstance()->permalink($sUrl), 'timeout' => 1000]);
  138. /**
  139. * @hooks
  140. * @hookdef hook-bx_base_groups-redirect_after_edit '{module_name}', 'redirect_after_edit' - hook to override redirect URL which is used after content changing
  141. * It's equivalent to @ref hook-bx_base_general-redirect_after_edit
  142. * @hook @ref hook-bx_base_groups-redirect_after_edit
  143. */
  144. bx_alert($this->_oModule->getName(), 'redirect_after_edit', 0, false, [
  145. 'content' => $aContentInfo,
  146. 'override_result' => &$sUrl,
  147. ]);
  148. $this->_redirectAndExit($sUrl);
  149. }
  150. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  151. {
  152. $CNF = &$this->_oModule->_oConfig->CNF;
  153. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
  154. if(isset($CNF['TABLE_QUESTIONS'], $CNF['TABLE_ANSWERS']))
  155. $this->_oModule->_oDb->deleteQuestionnaires($iContentId);
  156. if ($oGroupProfile && isset($CNF['TABLE_ADMINS']))
  157. $this->_oModule->_oDb->deleteAdminsByGroupId($oGroupProfile->id());
  158. if (isset($CNF['OBJECT_CONNECTIONS']) && $oGroupProfile && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  159. $oConnection->onDeleteInitiatorAndContent($oGroupProfile->id());
  160. if((isset($CNF['OBJECT_PRIVACY_VIEW']) && $oPrivacyView = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  161. $oPrivacyView->deleteGroupCustomByContentId($iContentId);
  162. if((isset($CNF['OBJECT_PRIVACY_POST']) && $oPrivacyPost = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false)
  163. $oPrivacyPost->deleteGroupCustomByContentId($iContentId);
  164. return '';
  165. }
  166. protected function inviteMembers ($oGroupProfile, $aInitialProfiles)
  167. {
  168. $CNF = &$this->_oModule->_oConfig->CNF;
  169. if(empty($CNF['OBJECT_CONNECTIONS']) || !$aInitialProfiles)
  170. return;
  171. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  172. if(!$oConnection)
  173. return;
  174. $iGroupId = $oGroupProfile->id();
  175. if (!is_array($aInitialProfiles))
  176. $aInitialProfiles = [$aInitialProfiles];
  177. // insert invited members, so they will join without confirmation
  178. foreach($aInitialProfiles as $iProfileId) {
  179. $oProfile = BxDolProfile::getInstance($iProfileId);
  180. if(!$oProfile)
  181. continue;
  182. if($oConnection->isConnected($iGroupId, $iProfileId, true) || $oConnection->isConnected($iGroupId, $iProfileId))
  183. continue;
  184. $this->_oModule->serviceAddMutualConnection ($iGroupId, $iProfileId, true);
  185. }
  186. }
  187. /**
  188. * Make author admin if their is in initial invitations list
  189. * @param $oGroupProfile group id
  190. * @param $aInitialProfiles array of initial profile ids
  191. * @return nothing
  192. */
  193. protected function makeAuthorAdmin ($oGroupProfile, $aInitialProfiles)
  194. {
  195. $this->makeAdmin (bx_get_logged_profile_id(), $oGroupProfile, $aInitialProfiles);
  196. }
  197. /**
  198. * Make profile admin if he is in initial invitations list
  199. * @param $iProfileId profile id
  200. * @param $oGroupProfile group id
  201. * @param $aInitialProfiles array of initial profile ids
  202. * @return nothing
  203. */
  204. protected function makeAdmin ($iProfileId, $oGroupProfile, $aInitialProfiles)
  205. {
  206. $CNF = &$this->_oModule->_oConfig->CNF;
  207. if (!isset($CNF['OBJECT_CONNECTIONS']) || !$CNF['OBJECT_CONNECTIONS'] || !is_array($aInitialProfiles) || !in_array($iProfileId, $aInitialProfiles))
  208. return;
  209. if (!($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  210. return;
  211. if (!$oConnection->isConnected($oGroupProfile->id(), (int)$iProfileId))
  212. $oConnection->addConnection($oGroupProfile->id(), (int)$iProfileId);
  213. if (!$oConnection->isConnected((int)$iProfileId, $oGroupProfile->id()))
  214. $oConnection->addConnection((int)$iProfileId, $oGroupProfile->id());
  215. if (!$this->_oModule->_oDb->isAdmin ($oGroupProfile->id(), $iProfileId))
  216. $this->_oModule->_oDb->toAdmins ($oGroupProfile->id(), $iProfileId);
  217. }
  218. }
  219. /** @} */