1
0

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. $sStatus = $oProfile->getStatus();
  75. if (!$this->isAutoApproval() && BX_PROFILE_STATUS_ACTIVE == $sStatus)
  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 updated data.
  85. */
  86. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  87. // change profile to 'pending' only if profile is 'active'
  88. $sStatus = $oProfile->getStatus();
  89. if (!$this->isAutoApproval() && BX_PROFILE_STATUS_ACTIVE == $sStatus && !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 use author's profile later.
  108. */
  109. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iContentId, BX_PROFILE_STATUS_PENDING, $this->_oModule->getName());
  110. $oProfile = BxDolProfile::getInstance($iProfileId);
  111. if($s = parent::onDataAddAfter($iAccountId, $iContentId))
  112. return $s;
  113. $CNF = &$this->_oModule->_oConfig->CNF;
  114. /*
  115. * Load updated data.
  116. */
  117. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  118. // approve profile if auto-approval is enabled and profile status is 'pending'
  119. $sStatus = $oProfile->getStatus();
  120. if ($sStatus == BX_PROFILE_STATUS_PENDING && $this->isAutoApproval())
  121. $oProfile->approve(BX_PROFILE_ACTION_AUTO, 0, $this->_oModule->serviceActAsProfile() && $this->_oModule->serviceIsEnableProfileActivationLetter());
  122. // set created profile some default membership
  123. if ((int)bx_get('level_id') && bx_srv('bx_acl', 'get_prices', [(int)bx_get('level_id'), true]))
  124. $iAclLevel = (int)bx_get('level_id');
  125. else
  126. $iAclLevel = !isset($CNF['PARAM_DEFAULT_ACL_LEVEL']) ? MEMBERSHIP_ID_STANDARD :
  127. (isAdmin() ? MEMBERSHIP_ID_ADMINISTRATOR : getParam($CNF['PARAM_DEFAULT_ACL_LEVEL']));
  128. BxDolAcl::getInstance()->setMembership($iProfileId, $iAclLevel, 0, true);
  129. // process uploaded files
  130. $oForm = $this->getObjectFormAdd();
  131. if (isset($CNF['FIELD_PICTURE']))
  132. $oForm->processFiles($CNF['FIELD_PICTURE'], $iContentId, true);
  133. if (isset($CNF['FIELD_COVER']))
  134. $oForm->processFiles($CNF['FIELD_COVER'], $iContentId, true);
  135. if(isset($CNF['FIELD_ALLOW_POST_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false)
  136. $oPrivacy->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]);
  137. // alert
  138. $aParams = array();
  139. if(isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]))
  140. $aParams['privacy_view'] = $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  141. bx_alert($this->_oModule->getName(), 'added', $iContentId, false, $aParams);
  142. // switch context to the created profile
  143. if ($this->_oModule->serviceActAsProfile()) {
  144. $oAccount = BxDolAccount::getInstance($iAccountId);
  145. $oAccount->updateProfileContext($iProfileId);
  146. }
  147. return '';
  148. }
  149. public function redirectAfterAdd($aContentInfo)
  150. {
  151. $CNF = &$this->_oModule->_oConfig->CNF;
  152. $oSession = BxDolSession::getInstance();
  153. $sRedirectType = empty($CNF['PARAM_REDIRECT_AADD']) ? BX_DOL_PROFILE_REDIRECT_PROFILE : getParam($CNF['PARAM_REDIRECT_AADD']);
  154. $sRedirectDefault = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  155. $sRedirectUrl = $sRedirectDefault;
  156. switch ($sRedirectType) {
  157. case BX_DOL_PROFILE_REDIRECT_PROFILE:
  158. break;
  159. // if user just joined the redirect to the page where user comes from.
  160. case BX_DOL_PROFILE_REDIRECT_LAST:
  161. $sJoinReferrer = $oSession->getValue('join-referrer');
  162. if($sJoinReferrer) {
  163. $sRedirectUrl = $sJoinReferrer;
  164. $oSession->unsetValue('join-referrer');
  165. }
  166. break;
  167. case BX_DOL_PROFILE_REDIRECT_CUSTOM:
  168. $sRedirectCustom = getParam($CNF['PARAM_REDIRECT_AADD_CUSTOM_URL']);
  169. if($sRedirectCustom) {
  170. $sRedirectUrl = $this->prepareCustomRedirectUrl($sRedirectCustom, $aContentInfo);
  171. }
  172. }
  173. $sCustomReferrer = $oSession->getValue('custom-referrer');
  174. if($sCustomReferrer) {
  175. $sRedirectUrl = $sCustomReferrer;
  176. $oSession->unsetValue('custom-referrer');
  177. }
  178. if($sRedirectUrl) {
  179. if(!bx_has_proto($sRedirectUrl))
  180. $sRedirectUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sRedirectUrl);
  181. header('Location: ' . $sRedirectUrl);
  182. exit;
  183. }
  184. parent::redirectAfterAdd($aContentInfo);
  185. }
  186. }
  187. /** @} */