1
0

BxBaseModProfileFormsEntryHelper.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 = array();
  36. $oProfile = false;
  37. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  38. if (!$aContentInfo)
  39. return array (false, false);
  40. $oProfile = BxDolProfile::getInstance($aContentInfo['profile_id']);
  41. if (!$oProfile)
  42. $oProfile = BxDolProfileUndefined::getInstance();
  43. return array ($oProfile, $aContentInfo);
  44. }
  45. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  46. {
  47. if (!$aContentInfo)
  48. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  49. // delete profile
  50. $oProfile = BxDolProfile::getInstance($aContentInfo['profile_id']);
  51. if (!$oProfile->delete())
  52. return _t('_sys_txt_error_entry_delete');
  53. return '';
  54. }
  55. public function deleteDataService ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  56. {
  57. return parent::deleteData ($iContentId, $aContentInfo, $oProfile, $oForm);
  58. }
  59. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges)
  60. {
  61. $CNF = &$this->_oModule->_oConfig->CNF;
  62. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  63. $aProfileInfo = $oProfile->getInfo();
  64. if (!$this->isAutoApproval() && BX_PROFILE_STATUS_ACTIVE == $aProfileInfo['status'])
  65. $aTrackTextFieldsChanges = array ();
  66. }
  67. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  68. {
  69. if ($s = parent::onDataEditAfter($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm))
  70. return $s;
  71. $CNF = &$this->_oModule->_oConfig->CNF;
  72. $aProfileInfo = $oProfile->getInfo();
  73. // change profile to 'pending' only if profile is 'active'
  74. if (!$this->isAutoApproval() && BX_PROFILE_STATUS_ACTIVE == $aProfileInfo['status'] && !empty($aTrackTextFieldsChanges['changed_fields']))
  75. $oProfile->disapprove(BX_PROFILE_ACTION_AUTO, 0, $this->_oModule->serviceActAsProfile());
  76. // process uploaded files
  77. if (isset($CNF['FIELD_PICTURE']))
  78. $oForm->processFiles($CNF['FIELD_PICTURE'], $iContentId, false);
  79. if (isset($CNF['FIELD_COVER']))
  80. $oForm->processFiles($CNF['FIELD_COVER'], $iContentId, false);
  81. // create an alert
  82. bx_alert($this->_oModule->getName(), 'edited', $aContentInfo[$CNF['FIELD_ID']]);
  83. bx_alert('profile', 'edit', $oProfile->id(), 0, array('content' => $iContentId, 'module' => $this->_oModule->getName()));
  84. return '';
  85. }
  86. public function onDataAddAfter ($iAccountId, $iContentId)
  87. {
  88. // parent method isn't processing metas, so we are processing it below
  89. if ($s = parent::onDataAddAfter($iAccountId, $iContentId))
  90. return $s;
  91. $CNF = &$this->_oModule->_oConfig->CNF;
  92. // add account and content association
  93. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iContentId, BX_PROFILE_STATUS_PENDING, $this->_oModule->getName());
  94. $oProfile = BxDolProfile::getInstance($iProfileId);
  95. // approve profile if auto-approval is enabled and profile status is 'pending'
  96. $sStatus = $oProfile->getStatus();
  97. if ($sStatus == BX_PROFILE_STATUS_PENDING && $this->isAutoApproval())
  98. $oProfile->approve(BX_PROFILE_ACTION_AUTO, 0, $this->_oModule->serviceActAsProfile() & $this->_oModule->serviceIsEnableProfileActivationLetter());
  99. // set created profile some default membership
  100. if ((int)bx_get('level_id') && bx_srv('bx_acl', 'get_prices', [(int)bx_get('level_id'), true]))
  101. $iAclLevel = (int)bx_get('level_id');
  102. else
  103. $iAclLevel = !isset($CNF['PARAM_DEFAULT_ACL_LEVEL']) ? MEMBERSHIP_ID_STANDARD :
  104. (isAdmin() ? MEMBERSHIP_ID_ADMINISTRATOR : getParam($CNF['PARAM_DEFAULT_ACL_LEVEL']));
  105. BxDolAcl::getInstance()->setMembership($iProfileId, $iAclLevel, 0, true);
  106. // process metas
  107. $this->_processMetas($iAccountId, $iContentId);
  108. // process uploaded files
  109. $oForm = $this->getObjectFormAdd();
  110. if (isset($CNF['FIELD_PICTURE']))
  111. $oForm->processFiles($CNF['FIELD_PICTURE'], $iContentId, true);
  112. if (isset($CNF['FIELD_COVER']))
  113. $oForm->processFiles($CNF['FIELD_COVER'], $iContentId, true);
  114. // alert
  115. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  116. $aParams = array();
  117. if(isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]))
  118. $aParams['privacy_view'] = $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  119. bx_alert($this->_oModule->getName(), 'added', $iContentId, false, $aParams);
  120. // switch context to the created profile
  121. if ($this->_oModule->serviceActAsProfile()) {
  122. $oAccount = BxDolAccount::getInstance($iAccountId);
  123. $oAccount->updateProfileContext($iProfileId);
  124. }
  125. return '';
  126. }
  127. public function redirectAfterAdd($aContentInfo)
  128. {
  129. $CNF = &$this->_oModule->_oConfig->CNF;
  130. $oSession = BxDolSession::getInstance();
  131. $sRedirectType = empty($CNF['PARAM_REDIRECT_AADD']) ? BX_DOL_PROFILE_REDIRECT_PROFILE : getParam($CNF['PARAM_REDIRECT_AADD']);
  132. $sRedirectDefault = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  133. $sRedirectUrl = $sRedirectDefault;
  134. switch ($sRedirectType) {
  135. case BX_DOL_PROFILE_REDIRECT_PROFILE:
  136. break;
  137. // if user just joined the redirect to the page where user comes from.
  138. case BX_DOL_PROFILE_REDIRECT_LAST:
  139. $sJoinReferrer = $oSession->getValue('join-referrer');
  140. if($sJoinReferrer) {
  141. $sRedirectUrl = $sJoinReferrer;
  142. $oSession->unsetValue('join-referrer');
  143. }
  144. break;
  145. case BX_DOL_PROFILE_REDIRECT_CUSTOM:
  146. $sRedirectCustom = getParam($CNF['PARAM_REDIRECT_AADD_CUSTOM_URL']);
  147. if($sRedirectCustom) {
  148. $sRedirectUrl = $this->prepareCustomRedirectUrl($sRedirectCustom, $aContentInfo);
  149. }
  150. }
  151. $sCustomReferrer = $oSession->getValue('custom-referrer');
  152. if($sCustomReferrer) {
  153. $sRedirectUrl = $sCustomReferrer;
  154. $oSession->unsetValue('custom-referrer');
  155. }
  156. if($sRedirectUrl) {
  157. if(!bx_has_proto($sRedirectUrl))
  158. $sRedirectUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sRedirectUrl);
  159. header('Location: ' . $sRedirectUrl);
  160. exit;
  161. }
  162. parent::redirectAfterAdd($aContentInfo);
  163. }
  164. }
  165. /** @} */