BxBaseModProfileFormsEntryHelper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. foreach(['TITLE', 'BIRTHDAY', 'PICTURE', 'COVER', 'BADGE'] as $sIt)
  86. if(($sField = 'FIELD_' . $sIt) && isset($CNF[$sField], $aContentInfo[$CNF[$sField]]))
  87. $oForm->addTrackFields($CNF[$sField], $aContentInfo);
  88. }
  89. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  90. {
  91. if($s = parent::onDataEditAfter($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm))
  92. return $s;
  93. $CNF = &$this->_oModule->_oConfig->CNF;
  94. /*
  95. * Load updated data.
  96. */
  97. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  98. // change profile to 'pending' only if profile is 'active'
  99. $oEditedProfile = BxDolProfile::getInstanceMagic($aContentInfo['profile_id']);
  100. $sStatus = $oEditedProfile->getStatus();
  101. if(!$this->isAutoApproval(BX_DOL_PROFILE_ACTIVATE_EDIT) && BX_PROFILE_STATUS_ACTIVE == $sStatus && !empty($aTrackTextFieldsChanges['changed_fields']))
  102. $oEditedProfile->disapprove(BX_PROFILE_ACTION_AUTO, 0, $this->_oModule->serviceActAsProfile());
  103. // process uploaded files
  104. foreach(['PICTURE', 'COVER', 'BADGE'] as $sIt) {
  105. $sField = 'FIELD_' . $sIt;
  106. if(!isset($CNF[$sField]))
  107. continue;
  108. $oForm->processFiles($CNF[$sField], $iContentId, false);
  109. if($this->_bIsApi)
  110. $oForm->processFilesFlagsApi($CNF[$sField], $iContentId);
  111. if(($aChanged = $oForm->isTrackFieldChanged($CNF[$sField], true)) !== false)
  112. $oForm->processFileDeletion($CNF[$sField], $aChanged['old']);
  113. }
  114. if(isset($CNF['FIELD_ALLOW_POST_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false)
  115. $oPrivacy->reassociateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]);
  116. // update content filters
  117. if(isset($CNF['FIELD_BIRTHDAY']) && $oForm->isTrackFieldChanged($CNF['FIELD_BIRTHDAY']))
  118. BxDolContentFilter::getInstance()->updateValuesByProfile($oProfile->getInfo());
  119. // reset profile switcher cache
  120. if ($oEditedProfile->id() !== bx_get_logged_profile_id() && $oForm->isTrackFieldChanged($CNF['FIELD_TITLE']) || $oForm->isTrackFieldChanged($CNF['FIELD_PICTURE'])) {
  121. bx_content_cache_del("profile_switcher_" . $oEditedProfile->getAccountId() . "_0");
  122. bx_content_cache_del("profile_switcher_" . $oEditedProfile->getAccountId() . "_1");
  123. }
  124. bx_content_cache_del_by_prefix('sprofile_unit_vars:' . $oEditedProfile->id() . ':');
  125. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled()) {
  126. $aProfileData = BxDolProfile::getDataForPage($oEditedProfile);
  127. $oSockets->sendEvent('profile', $aContentInfo['profile_id'], 'changed', json_encode($aProfileData));
  128. }
  129. return '';
  130. }
  131. public function onDataAddAfter ($iAccountId, $iContentId)
  132. {
  133. /*
  134. * Add account and content association.
  135. * Note. It should be done first to correctly get and use author's profile later.
  136. */
  137. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iContentId, BX_PROFILE_STATUS_PENDING, $this->_oModule->getName());
  138. $oProfile = BxDolProfile::getInstance($iProfileId);
  139. if($s = parent::onDataAddAfter($iAccountId, $iContentId))
  140. return $s;
  141. $CNF = &$this->_oModule->_oConfig->CNF;
  142. /*
  143. * Load updated data.
  144. */
  145. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  146. /*
  147. * approve profile if auto-approval is enabled and profile status is 'pending'
  148. * don't send Activation letter when 'Auto Approval' mode is enabled.
  149. */
  150. $sStatus = $oProfile->getStatus();
  151. if ($sStatus == BX_PROFILE_STATUS_PENDING && $this->isAutoApproval(BX_DOL_PROFILE_ACTIVATE_ADD))
  152. $oProfile->approve(BX_PROFILE_ACTION_AUTO, 0, false);
  153. // set created profile some default membership
  154. if ((int)bx_get('level_id') && bx_srv('bx_acl', 'get_prices', [(int)bx_get('level_id'), true]))
  155. $iAclLevel = (int)bx_get('level_id');
  156. else if(isset($CNF['PARAM_DEFAULT_ACL_LEVEL']))
  157. $iAclLevel = isAdmin() && getLoggedId() == $iAccountId ? MEMBERSHIP_ID_ADMINISTRATOR : getParam($CNF['PARAM_DEFAULT_ACL_LEVEL']);
  158. else
  159. $iAclLevel = MEMBERSHIP_ID_STANDARD;
  160. BxDolAcl::getInstance()->setMembership($iProfileId, $iAclLevel, 0, true);
  161. // process uploaded files
  162. $oForm = $this->getObjectFormAdd();
  163. if (isset($CNF['FIELD_PICTURE']))
  164. $oForm->processFiles($CNF['FIELD_PICTURE'], $iContentId, true);
  165. if (isset($CNF['FIELD_COVER']))
  166. $oForm->processFiles($CNF['FIELD_COVER'], $iContentId, true);
  167. if (isset($CNF['FIELD_BADGE']))
  168. $oForm->processFiles($CNF['FIELD_BADGE'], $iContentId, true);
  169. if(isset($CNF['FIELD_ALLOW_POST_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false)
  170. $oPrivacy->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]);
  171. // switch context to the created profile
  172. if ($this->_oModule->serviceActAsProfile()) {
  173. $oAccount = BxDolAccount::getInstance($iAccountId);
  174. $oAccount->updateProfileContext($iProfileId);
  175. //update recomedations
  176. BxDolRecommendation::updateData($iProfileId, true);
  177. // update content filters
  178. BxDolContentFilter::getInstance()->updateValuesByProfile($oProfile->getInfo());
  179. }
  180. return '';
  181. }
  182. public function redirectAfterAdd($aContentInfo, $sUrl = '')
  183. {
  184. $CNF = &$this->_oModule->_oConfig->CNF;
  185. $oSession = BxDolSession::getInstance();
  186. $sRedirectType = empty($CNF['PARAM_REDIRECT_AADD']) ? BX_DOL_PROFILE_REDIRECT_PROFILE : getParam($CNF['PARAM_REDIRECT_AADD']);
  187. $sRedirectDefault = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  188. $sRedirectUrl = $sRedirectDefault;
  189. switch ($sRedirectType) {
  190. case BX_DOL_PROFILE_REDIRECT_PROFILE:
  191. break;
  192. // if user just joined the redirect to the page where user comes from.
  193. case BX_DOL_PROFILE_REDIRECT_LAST:
  194. $sJoinReferrer = $oSession->getValue('join-referrer');
  195. if($sJoinReferrer) {
  196. $sRedirectUrl = $sJoinReferrer;
  197. $oSession->unsetValue('join-referrer');
  198. }
  199. break;
  200. case BX_DOL_PROFILE_REDIRECT_CUSTOM:
  201. $sRedirectCustom = getParam($CNF['PARAM_REDIRECT_AADD_CUSTOM_URL']);
  202. if($sRedirectCustom) {
  203. $sRedirectUrl = $this->prepareCustomRedirectUrl($sRedirectCustom, $aContentInfo);
  204. }
  205. break;
  206. case BX_DOL_PROFILE_REDIRECT_HOMEPAGE:
  207. $sRedirectUrl = BX_DOL_URL_ROOT;
  208. break;
  209. }
  210. $sCustomReferrer = $oSession->getValue('custom-referrer');
  211. if($sCustomReferrer) {
  212. $sRedirectUrl = $sCustomReferrer;
  213. $oSession->unsetValue('custom-referrer');
  214. }
  215. return parent::redirectAfterAdd($aContentInfo, $sRedirectUrl);
  216. }
  217. }
  218. /** @} */