BxBaseModProfileFormsEntryHelper.php 9.3 KB

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