1
0

BxBaseModGroupsFormsEntryHelper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. $this->makeAuthorAdmin ($oGroupProfile, bx_get('initial_members'));
  95. $this->inviteMembers ($oGroupProfile, bx_get('initial_members'));
  96. return '';
  97. }
  98. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  99. {
  100. if($s = parent::onDataEditAfter($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm))
  101. return $s;
  102. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
  103. if(!$oGroupProfile)
  104. return '';
  105. $this->inviteMembers ($oGroupProfile, bx_get('initial_members'));
  106. return '';
  107. }
  108. public function onDataInviteBefore ($iContentId, $aContentInfo)
  109. {
  110. return '';
  111. }
  112. public function onDataInviteAfter ($iContentId, $aContentInfo)
  113. {
  114. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
  115. if(!$oGroupProfile)
  116. return '';
  117. $this->inviteMembers($oGroupProfile, bx_get('initial_members'));
  118. return '';
  119. }
  120. protected function redirectAfterEdit($aContentInfo, $sUrl = '')
  121. {
  122. $CNF = &$this->_oModule->_oConfig->CNF;
  123. $sUrl = '';
  124. if(bx_get('initial_members')) {
  125. if(isset($CNF['URL_ENTRY_MANAGE']))
  126. $sUrl = $CNF['URL_ENTRY_MANAGE'];
  127. else if(isset($CNF['URL_ENTRY_FANS']))
  128. $sUrl = $CNF['URL_ENTRY_FANS'];
  129. $sUrl = bx_append_url_params($sUrl, ['profile_id' => $aContentInfo['profile_id']]);
  130. }
  131. else
  132. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  133. if(bx_is_api())
  134. return bx_api_get_block('redirect', ['uri' => '/' . BxDolPermalinks::getInstance()->permalink($sUrl), 'timeout' => 1000]);
  135. /**
  136. * @hooks
  137. * @hookdef hook-bx_base_groups-redirect_after_edit '{module_name}', 'redirect_after_edit' - hook to override redirect URL which is used after content changing
  138. * It's equivalent to @ref hook-bx_base_general-redirect_after_edit
  139. * @hook @ref hook-bx_base_groups-redirect_after_edit
  140. */
  141. bx_alert($this->_oModule->getName(), 'redirect_after_edit', 0, false, [
  142. 'content' => $aContentInfo,
  143. 'override_result' => &$sUrl,
  144. ]);
  145. $this->_redirectAndExit($sUrl);
  146. }
  147. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  148. {
  149. $CNF = &$this->_oModule->_oConfig->CNF;
  150. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
  151. if(isset($CNF['TABLE_QUESTIONS'], $CNF['TABLE_ANSWERS']))
  152. $this->_oModule->_oDb->deleteQuestionnaires($iContentId);
  153. if ($oGroupProfile && isset($CNF['TABLE_ADMINS']))
  154. $this->_oModule->_oDb->deleteAdminsByGroupId($oGroupProfile->id());
  155. if (isset($CNF['OBJECT_CONNECTIONS']) && $oGroupProfile && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  156. $oConnection->onDeleteInitiatorAndContent($oGroupProfile->id());
  157. if((isset($CNF['OBJECT_PRIVACY_VIEW']) && $oPrivacyView = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  158. $oPrivacyView->deleteGroupCustomByContentId($iContentId);
  159. if((isset($CNF['OBJECT_PRIVACY_POST']) && $oPrivacyPost = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false)
  160. $oPrivacyPost->deleteGroupCustomByContentId($iContentId);
  161. return '';
  162. }
  163. protected function inviteMembers ($oGroupProfile, $aInitialProfiles)
  164. {
  165. $CNF = &$this->_oModule->_oConfig->CNF;
  166. if(empty($CNF['OBJECT_CONNECTIONS']) || !$aInitialProfiles)
  167. return;
  168. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  169. if(!$oConnection)
  170. return;
  171. $iGroupId = $oGroupProfile->id();
  172. if (!is_array($aInitialProfiles))
  173. $aInitialProfiles = [$aInitialProfiles];
  174. // insert invited members, so they will join without confirmation
  175. foreach($aInitialProfiles as $iProfileId) {
  176. $oProfile = BxDolProfile::getInstance($iProfileId);
  177. if(!$oProfile)
  178. continue;
  179. if($oConnection->isConnected($iGroupId, $iProfileId, true) || $oConnection->isConnected($iGroupId, $iProfileId))
  180. continue;
  181. $this->_oModule->serviceAddMutualConnection ($iGroupId, $iProfileId, true);
  182. }
  183. }
  184. /**
  185. * Make author admin if their is in initial invitations list
  186. * @param $oGroupProfile group id
  187. * @param $aInitialProfiles array of initial profile ids
  188. * @return nothing
  189. */
  190. protected function makeAuthorAdmin ($oGroupProfile, $aInitialProfiles)
  191. {
  192. $this->makeAdmin (bx_get_logged_profile_id(), $oGroupProfile, $aInitialProfiles);
  193. }
  194. /**
  195. * Make profile admin if he is in initial invitations list
  196. * @param $iProfileId profile id
  197. * @param $oGroupProfile group id
  198. * @param $aInitialProfiles array of initial profile ids
  199. * @return nothing
  200. */
  201. protected function makeAdmin ($iProfileId, $oGroupProfile, $aInitialProfiles)
  202. {
  203. $CNF = &$this->_oModule->_oConfig->CNF;
  204. if (!isset($CNF['OBJECT_CONNECTIONS']) || !$CNF['OBJECT_CONNECTIONS'] || !is_array($aInitialProfiles) || !in_array($iProfileId, $aInitialProfiles))
  205. return;
  206. if (!($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  207. return;
  208. if (!$oConnection->isConnected($oGroupProfile->id(), (int)$iProfileId))
  209. $oConnection->addConnection($oGroupProfile->id(), (int)$iProfileId);
  210. if (!$oConnection->isConnected((int)$iProfileId, $oGroupProfile->id()))
  211. $oConnection->addConnection((int)$iProfileId, $oGroupProfile->id());
  212. if (!$this->_oModule->_oDb->isAdmin ($oGroupProfile->id(), $iProfileId))
  213. $this->_oModule->_oDb->toAdmins ($oGroupProfile->id(), $iProfileId);
  214. }
  215. }
  216. /** @} */