BxBaseModProfileFormsEntryHelper.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. $oForm->processFiles($CNF['FIELD_PICTURE'], $iContentId, false);
  78. $oForm->processFiles($CNF['FIELD_COVER'], $iContentId, false);
  79. // create an alert
  80. bx_alert($this->_oModule->getName(), 'edited', $aContentInfo[$CNF['FIELD_ID']]);
  81. bx_alert('profile', 'edit', $oProfile->id(), 0, array('content' => $iContentId, 'module' => $this->_oModule->getName()));
  82. return '';
  83. }
  84. public function onDataAddAfter ($iAccountId, $iContentId)
  85. {
  86. // parent method isn't processing metas, so we are processing it below
  87. if ($s = parent::onDataAddAfter($iAccountId, $iContentId))
  88. return $s;
  89. $CNF = &$this->_oModule->_oConfig->CNF;
  90. // add account and content association
  91. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iContentId, BX_PROFILE_STATUS_PENDING, $this->_oModule->getName());
  92. $oProfile = BxDolProfile::getInstance($iProfileId);
  93. // approve profile if auto-approval is enabled and profile status is 'pending'
  94. $sStatus = $oProfile->getStatus();
  95. if ($sStatus == BX_PROFILE_STATUS_PENDING && $this->isAutoApproval())
  96. $oProfile->approve(BX_PROFILE_ACTION_AUTO, 0, $this->_oModule->serviceActAsProfile());
  97. // set created profile some default membership
  98. $iAclLevel = !isset($CNF['PARAM_DEFAULT_ACL_LEVEL']) ? MEMBERSHIP_ID_STANDARD :
  99. (isAdmin() ? MEMBERSHIP_ID_ADMINISTRATOR : getParam($CNF['PARAM_DEFAULT_ACL_LEVEL']));
  100. BxDolAcl::getInstance()->setMembership($iProfileId, $iAclLevel, 0, true);
  101. // process metas
  102. $this->_processMetas($iAccountId, $iContentId);
  103. // process uploaded files
  104. $oForm = $this->getObjectFormAdd();
  105. $oForm->processFiles($CNF['FIELD_PICTURE'], $iContentId, true);
  106. $oForm->processFiles($CNF['FIELD_COVER'], $iContentId, true);
  107. // alert
  108. bx_alert($this->_oModule->getName(), 'added', $iContentId);
  109. // switch context to the created profile
  110. if ($this->_oModule->serviceActAsProfile()) {
  111. $oAccount = BxDolAccount::getInstance($iAccountId);
  112. $oAccount->updateProfileContext($iProfileId);
  113. }
  114. return '';
  115. }
  116. protected function redirectAfterAdd($aContentInfo)
  117. {
  118. $CNF = &$this->_oModule->_oConfig->CNF;
  119. $oSession = BxDolSession::getInstance();
  120. $sRedirectType = empty($CNF['PARAM_REDIRECT_AADD']) ? BX_DOL_PROFILE_REDIRECT_PROFILE : getParam($CNF['PARAM_REDIRECT_AADD']);
  121. $sRedirectDefault = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  122. $sRedirectUrl = $sRedirectDefault;
  123. switch ($sRedirectType) {
  124. case BX_DOL_PROFILE_REDIRECT_PROFILE:
  125. break;
  126. // if user just joined the redirect to the page where user comes from.
  127. case BX_DOL_PROFILE_REDIRECT_LAST:
  128. $sJoinReferrer = $oSession->getValue('join-referrer');
  129. if($sJoinReferrer) {
  130. $sRedirectUrl = $sJoinReferrer;
  131. $oSession->unsetValue('join-referrer');
  132. }
  133. break;
  134. case BX_DOL_PROFILE_REDIRECT_CUSTOM:
  135. $sRedirectCustom = getParam($CNF['PARAM_REDIRECT_AADD_CUSTOM_URL']);
  136. if($sRedirectCustom)
  137. $sRedirectUrl = $sRedirectCustom;
  138. }
  139. $sCustomReferrer = $oSession->getValue('custom-referrer');
  140. if($sCustomReferrer) {
  141. $sRedirectUrl = $sCustomReferrer;
  142. $oSession->unsetValue('custom-referrer');
  143. }
  144. if($sRedirectUrl) {
  145. if(!bx_has_proto($sRedirectUrl))
  146. $sRedirectUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sRedirectUrl);
  147. header('Location: ' . $sRedirectUrl);
  148. exit;
  149. }
  150. parent::redirectAfterAdd($aContentInfo);
  151. }
  152. }
  153. /** @} */