1
0

BxBaseModGroupsFormsEntryHelper.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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', '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_invite', '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. $this->redirectAfterEdit($aContentInfo);
  66. }
  67. protected function _getProfileAndContentData ($iContentId)
  68. {
  69. $CNF = &$this->_oModule->_oConfig->CNF;
  70. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  71. if(!$aContentInfo)
  72. return array(false, false);
  73. $oProfile = BxDolProfile::getInstanceMagic($aContentInfo[$CNF['FIELD_AUTHOR']]);
  74. return array($oProfile, $aContentInfo);
  75. }
  76. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  77. {
  78. $sMsg = parent::_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile);
  79. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  80. if ($sMsg && $oPrivacy->isPartiallyVisible($aContentInfo[$this->_oModule->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']]))
  81. return '';
  82. return $sMsg;
  83. }
  84. public function onDataAddAfter ($iAccountId, $iContentId)
  85. {
  86. if($s = parent::onDataAddAfter($iAccountId, $iContentId))
  87. return $s;
  88. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
  89. if(!$oGroupProfile)
  90. return '';
  91. $this->makeAuthorAdmin ($oGroupProfile, bx_get('initial_members'));
  92. $this->inviteMembers ($oGroupProfile, bx_get('initial_members'));
  93. return '';
  94. }
  95. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  96. {
  97. if($s = parent::onDataEditAfter($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm))
  98. return $s;
  99. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
  100. if(!$oGroupProfile)
  101. return '';
  102. $this->inviteMembers ($oGroupProfile, bx_get('initial_members'));
  103. return '';
  104. }
  105. public function onDataInviteBefore ($iContentId, $aContentInfo)
  106. {
  107. return '';
  108. }
  109. public function onDataInviteAfter ($iContentId, $aContentInfo)
  110. {
  111. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
  112. if(!$oGroupProfile)
  113. return '';
  114. $this->inviteMembers($oGroupProfile, bx_get('initial_members'));
  115. return '';
  116. }
  117. protected function redirectAfterEdit($aContentInfo, $sUrl = '')
  118. {
  119. $CNF = &$this->_oModule->_oConfig->CNF;
  120. $sUrl = '';
  121. if (bx_get('initial_members') && isset($CNF['URL_ENTRY_FANS'])){
  122. $sUrl = $CNF['URL_ENTRY_FANS'] . '&profile_id=' . $aContentInfo['profile_id'];
  123. }
  124. else{
  125. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  126. }
  127. if (bx_is_api())
  128. return bx_api_get_block('redirect', ['uri' => '/' . BxDolPermalinks::getInstance()->permalink($sUrl), 'timeout' => 1000]);
  129. bx_alert($this->_oModule->getName(), 'redirect_after_edit', 0, false, array(
  130. 'content' => $aContentInfo,
  131. 'override_result' => &$sUrl,
  132. ));
  133. $this->_redirectAndExit($sUrl);
  134. }
  135. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  136. {
  137. $CNF = &$this->_oModule->_oConfig->CNF;
  138. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
  139. if(isset($CNF['TABLE_QUESTIONS'], $CNF['TABLE_ANSWERS']))
  140. $this->_oModule->_oDb->deleteQuestionnaires($iContentId);
  141. if ($oGroupProfile && isset($CNF['TABLE_ADMINS']))
  142. $this->_oModule->_oDb->deleteAdminsByGroupId($oGroupProfile->id());
  143. if (isset($CNF['OBJECT_CONNECTIONS']) && $oGroupProfile && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  144. $oConnection->onDeleteInitiatorAndContent($oGroupProfile->id());
  145. if((isset($CNF['OBJECT_PRIVACY_VIEW']) && $oPrivacyView = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  146. $oPrivacyView->deleteGroupCustomByContentId($iContentId);
  147. if((isset($CNF['OBJECT_PRIVACY_POST']) && $oPrivacyPost = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false)
  148. $oPrivacyPost->deleteGroupCustomByContentId($iContentId);
  149. return '';
  150. }
  151. protected function inviteMembers ($oGroupProfile, $aInitialProfiles)
  152. {
  153. $CNF = &$this->_oModule->_oConfig->CNF;
  154. if(empty($CNF['OBJECT_CONNECTIONS']) || !$aInitialProfiles)
  155. return;
  156. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  157. if(!$oConnection)
  158. return;
  159. $iGroupId = $oGroupProfile->id();
  160. if (!is_array($aInitialProfiles))
  161. $aInitialProfiles = [$aInitialProfiles];
  162. // insert invited members, so they will join without confirmation
  163. foreach($aInitialProfiles as $iProfileId) {
  164. $oProfile = BxDolProfile::getInstance($iProfileId);
  165. if(!$oProfile)
  166. continue;
  167. if($oConnection->isConnected($iGroupId, $iProfileId, true) || $oConnection->isConnected($iGroupId, $iProfileId))
  168. continue;
  169. $this->_oModule->serviceAddMutualConnection ($iGroupId, $iProfileId, true);
  170. }
  171. }
  172. /**
  173. * Make author admin if their is in initial invitations list
  174. * @param $oGroupProfile group id
  175. * @param $aInitialProfiles array of initial profile ids
  176. * @return nothing
  177. */
  178. protected function makeAuthorAdmin ($oGroupProfile, $aInitialProfiles)
  179. {
  180. $this->makeAdmin (bx_get_logged_profile_id(), $oGroupProfile, $aInitialProfiles);
  181. }
  182. /**
  183. * Make profile admin if he is in initial invitations list
  184. * @param $iProfileId profile id
  185. * @param $oGroupProfile group id
  186. * @param $aInitialProfiles array of initial profile ids
  187. * @return nothing
  188. */
  189. protected function makeAdmin ($iProfileId, $oGroupProfile, $aInitialProfiles)
  190. {
  191. $CNF = &$this->_oModule->_oConfig->CNF;
  192. if (!isset($CNF['OBJECT_CONNECTIONS']) || !$CNF['OBJECT_CONNECTIONS'] || !is_array($aInitialProfiles) || !in_array($iProfileId, $aInitialProfiles))
  193. return;
  194. if (!($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  195. return;
  196. if (!$oConnection->isConnected($oGroupProfile->id(), (int)$iProfileId))
  197. $oConnection->addConnection($oGroupProfile->id(), (int)$iProfileId);
  198. if (!$oConnection->isConnected((int)$iProfileId, $oGroupProfile->id()))
  199. $oConnection->addConnection((int)$iProfileId, $oGroupProfile->id());
  200. if (!$this->_oModule->_oDb->isAdmin ($oGroupProfile->id(), $iProfileId))
  201. $this->_oModule->_oDb->toAdmins ($oGroupProfile->id(), $iProfileId);
  202. }
  203. }
  204. /** @} */