1
0

BxBaseModProfileFormsEntryHelper.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 BaseProfile Base classes for profile modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Profile forms helper functions
  13. */
  14. class BxBaseModProfileFormsEntryHelper extends BxBaseModGeneralFormsEntryHelper
  15. {
  16. protected $_sAutoApproval = false;
  17. public function __construct($oModule)
  18. {
  19. parent::__construct($oModule);
  20. $CNF = &$this->_oModule->_oConfig->CNF;
  21. $sAutoApproval = isset($CNF['PARAM_AUTOAPPROVAL']) ? getParam($CNF['PARAM_AUTOAPPROVAL']) : BX_DOL_PROFILE_ACTIVATE_ALWAYS;
  22. $bAdministrator = BxDolAcl::getInstance()->isMemberLevelInSet(array(MEMBERSHIP_ID_ADMINISTRATOR));
  23. if (isAdmin() || $bAdministrator)
  24. $sAutoApproval = BX_DOL_PROFILE_ACTIVATE_ALWAYS;
  25. $this->setAutoApproval($sAutoApproval);
  26. }
  27. public function isAutoApproval($sAction = BX_DOL_PROFILE_ACTIVATE_ALWAYS)
  28. {
  29. if ($this->_sAutoApproval == BX_DOL_PROFILE_ACTIVATE_ALWAYS || $this->_sAutoApproval == $sAction)
  30. return true;
  31. return false;
  32. }
  33. public function setAutoApproval($mValue)
  34. {
  35. if ($mValue === true)
  36. $mValue = BX_DOL_PROFILE_ACTIVATE_ALWAYS;
  37. return ($this->_sAutoApproval = $mValue);
  38. }
  39. protected function _getProfileAndContentData ($iContentId)
  40. {
  41. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  42. if(!$aContentInfo)
  43. return array (false, false);
  44. $oProfile = BxDolProfile::getInstanceMagic($aContentInfo['profile_id']);
  45. return array($oProfile, $aContentInfo);
  46. }
  47. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  48. {
  49. if (!$aContentInfo)
  50. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  51. // delete profile with content
  52. $oProfile = BxDolProfile::getInstance($aContentInfo['profile_id']);
  53. if (!$oProfile->delete(false, true))
  54. return _t('_sys_txt_error_entry_delete');
  55. return '';
  56. }
  57. public function deleteDataService ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  58. {
  59. return parent::deleteData ($iContentId, $aContentInfo, $oProfile, $oForm);
  60. }
  61. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  62. {
  63. if($s = parent::onDataDeleteAfter($iContentId, $aContentInfo, $oProfile))
  64. return $s;
  65. $CNF = &$this->_oModule->_oConfig->CNF;
  66. $bActAsProfile = BxDolService::call($oProfile->getModule(), 'act_as_profile');
  67. if(($oPrivacyView = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false && $bActAsProfile)
  68. $oPrivacyView->deleteGroupCustomByProfileId($oProfile->id());
  69. if(($oPrivacyPost = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false) {
  70. $oPrivacyPost->deleteGroupCustomByContentId($iContentId);
  71. if($bActAsProfile)
  72. $oPrivacyPost->deleteGroupCustomByProfileId($oProfile->id());
  73. }
  74. return '';
  75. }
  76. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges, &$oProfile, &$oForm)
  77. {
  78. $CNF = &$this->_oModule->_oConfig->CNF;
  79. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  80. $oEditedProfile = BxDolProfile::getInstanceMagic($aContentInfo['profile_id']);
  81. $sStatus = $oEditedProfile->getStatus();
  82. if (!$this->isAutoApproval(BX_DOL_PROFILE_ACTIVATE_EDIT) && BX_PROFILE_STATUS_ACTIVE == $sStatus){
  83. $aTrackTextFieldsChanges = array ();
  84. }
  85. }
  86. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  87. {
  88. if ($s = parent::onDataEditAfter($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)){
  89. return $s;
  90. }
  91. $CNF = &$this->_oModule->_oConfig->CNF;
  92. /*
  93. * Load updated data.
  94. */
  95. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  96. // change profile to 'pending' only if profile is 'active'
  97. $oEditedProfile = BxDolProfile::getInstanceMagic($aContentInfo['profile_id']);
  98. $sStatus = $oEditedProfile->getStatus();
  99. if (!$this->isAutoApproval(BX_DOL_PROFILE_ACTIVATE_EDIT) && BX_PROFILE_STATUS_ACTIVE == $sStatus && !empty($aTrackTextFieldsChanges['changed_fields']))
  100. $oEditedProfile->disapprove(BX_PROFILE_ACTION_AUTO, 0, $this->_oModule->serviceActAsProfile());
  101. // process uploaded files
  102. if (isset($CNF['FIELD_PICTURE']))
  103. $oForm->processFiles($CNF['FIELD_PICTURE'], $iContentId, false);
  104. if (isset($CNF['FIELD_COVER']))
  105. $oForm->processFiles($CNF['FIELD_COVER'], $iContentId, false);
  106. if(isset($CNF['FIELD_ALLOW_POST_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false)
  107. $oPrivacy->reassociateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]);
  108. return '';
  109. }
  110. public function onDataAddAfter ($iAccountId, $iContentId)
  111. {
  112. /*
  113. * Add account and content association.
  114. * Note. It should be done first to correctly get and use author's profile later.
  115. */
  116. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iContentId, BX_PROFILE_STATUS_PENDING, $this->_oModule->getName());
  117. $oProfile = BxDolProfile::getInstance($iProfileId);
  118. if($s = parent::onDataAddAfter($iAccountId, $iContentId))
  119. return $s;
  120. $CNF = &$this->_oModule->_oConfig->CNF;
  121. /*
  122. * Load updated data.
  123. */
  124. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  125. // approve profile if auto-approval is enabled and profile status is 'pending'
  126. $sStatus = $oProfile->getStatus();
  127. if ($sStatus == BX_PROFILE_STATUS_PENDING && $this->isAutoApproval(BX_DOL_PROFILE_ACTIVATE_ADD))
  128. $oProfile->approve(BX_PROFILE_ACTION_AUTO, 0, $this->_oModule->serviceActAsProfile() && $this->_oModule->serviceIsEnableProfileActivationLetter());
  129. // set created profile some default membership
  130. if ((int)bx_get('level_id') && bx_srv('bx_acl', 'get_prices', [(int)bx_get('level_id'), true]))
  131. $iAclLevel = (int)bx_get('level_id');
  132. else if(isset($CNF['PARAM_DEFAULT_ACL_LEVEL']))
  133. $iAclLevel = isAdmin() && getLoggedId() == $iAccountId ? MEMBERSHIP_ID_ADMINISTRATOR : getParam($CNF['PARAM_DEFAULT_ACL_LEVEL']);
  134. else
  135. $iAclLevel = MEMBERSHIP_ID_STANDARD;
  136. BxDolAcl::getInstance()->setMembership($iProfileId, $iAclLevel, 0, true);
  137. // process uploaded files
  138. $oForm = $this->getObjectFormAdd();
  139. if (isset($CNF['FIELD_PICTURE']))
  140. $oForm->processFiles($CNF['FIELD_PICTURE'], $iContentId, true);
  141. if (isset($CNF['FIELD_COVER']))
  142. $oForm->processFiles($CNF['FIELD_COVER'], $iContentId, true);
  143. if(isset($CNF['FIELD_ALLOW_POST_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false)
  144. $oPrivacy->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]);
  145. // switch context to the created profile
  146. if ($this->_oModule->serviceActAsProfile()) {
  147. $oAccount = BxDolAccount::getInstance($iAccountId);
  148. $oAccount->updateProfileContext($iProfileId);
  149. }
  150. return '';
  151. }
  152. public function redirectAfterAdd($aContentInfo)
  153. {
  154. $CNF = &$this->_oModule->_oConfig->CNF;
  155. $oSession = BxDolSession::getInstance();
  156. $sRedirectType = empty($CNF['PARAM_REDIRECT_AADD']) ? BX_DOL_PROFILE_REDIRECT_PROFILE : getParam($CNF['PARAM_REDIRECT_AADD']);
  157. $sRedirectDefault = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  158. $sRedirectUrl = $sRedirectDefault;
  159. switch ($sRedirectType) {
  160. case BX_DOL_PROFILE_REDIRECT_PROFILE:
  161. break;
  162. // if user just joined the redirect to the page where user comes from.
  163. case BX_DOL_PROFILE_REDIRECT_LAST:
  164. $sJoinReferrer = $oSession->getValue('join-referrer');
  165. if($sJoinReferrer) {
  166. $sRedirectUrl = $sJoinReferrer;
  167. $oSession->unsetValue('join-referrer');
  168. }
  169. break;
  170. case BX_DOL_PROFILE_REDIRECT_CUSTOM:
  171. $sRedirectCustom = getParam($CNF['PARAM_REDIRECT_AADD_CUSTOM_URL']);
  172. if($sRedirectCustom) {
  173. $sRedirectUrl = $this->prepareCustomRedirectUrl($sRedirectCustom, $aContentInfo);
  174. }
  175. }
  176. $sCustomReferrer = $oSession->getValue('custom-referrer');
  177. if($sCustomReferrer) {
  178. $sRedirectUrl = $sCustomReferrer;
  179. $oSession->unsetValue('custom-referrer');
  180. }
  181. if($sRedirectUrl) {
  182. if(!bx_has_proto($sRedirectUrl))
  183. $sRedirectUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sRedirectUrl);
  184. header('Location: ' . $sRedirectUrl);
  185. exit;
  186. }
  187. parent::redirectAfterAdd($aContentInfo);
  188. }
  189. }
  190. /** @} */