BxBaseModProfileFormsEntryHelper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. public function getObjectFormEdit ($sDisplay = false)
  40. {
  41. $CNF = &$this->_oModule->_oConfig->CNF;
  42. $oForm = parent::getObjectFormEdit ($sDisplay);
  43. if(!$oForm)
  44. return $oForm;
  45. switch($sDisplay) {
  46. case $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT_SETTINGS']:
  47. //--- Fill in tabs list
  48. if(($sField = 'FIELD_STG_TABS') && !empty($CNF[$sField]) && !empty($oForm->aInputs[$CNF[$sField]]) && is_array($oForm->aInputs[$CNF[$sField]]))
  49. if(($sMenu = 'OBJECT_MENU_SUBMENU_VIEW_ENTRY') && !empty($CNF[$sMenu]) && ($oMenu = BxDolMenu::getObjectInstance($CNF[$sMenu])) !== false && ($aMenuItems = $oMenu->getQueryObject()->getMenuItems()))
  50. foreach($aMenuItems as $aMenuItem) {
  51. if($aMenuItem['name'] == 'more-auto' || (int)$aMenuItem['active'] == 0)
  52. continue;
  53. $oForm->aInputs[$CNF[$sField]]['values'][] = [
  54. 'key' => $aMenuItem['name'],
  55. 'value' => _t($aMenuItem['title'])
  56. ];
  57. }
  58. break;
  59. }
  60. return $oForm;
  61. }
  62. protected function _getProfileAndContentData ($iContentId)
  63. {
  64. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  65. if(!$aContentInfo)
  66. return array (false, false);
  67. $oProfile = BxDolProfile::getInstanceMagic($aContentInfo['profile_id']);
  68. return array($oProfile, $aContentInfo);
  69. }
  70. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  71. {
  72. if (!$aContentInfo)
  73. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  74. // delete profile with content
  75. $oProfile = BxDolProfile::getInstance($aContentInfo['profile_id']);
  76. if (!$oProfile->delete(false, true))
  77. return _t('_sys_txt_error_entry_delete');
  78. return '';
  79. }
  80. public function deleteDataService ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  81. {
  82. return parent::deleteData ($iContentId, $aContentInfo, $oProfile, $oForm);
  83. }
  84. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  85. {
  86. if($s = parent::onDataDeleteAfter($iContentId, $aContentInfo, $oProfile))
  87. return $s;
  88. $CNF = &$this->_oModule->_oConfig->CNF;
  89. $bActAsProfile = BxDolService::call($oProfile->getModule(), 'act_as_profile');
  90. if(($oPrivacyView = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false && $bActAsProfile)
  91. $oPrivacyView->deleteGroupCustomByProfileId($oProfile->id());
  92. if(($oPrivacyPost = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false) {
  93. $oPrivacyPost->deleteGroupCustomByContentId($iContentId);
  94. if($bActAsProfile)
  95. $oPrivacyPost->deleteGroupCustomByProfileId($oProfile->id());
  96. }
  97. return '';
  98. }
  99. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges, &$oProfile, &$oForm)
  100. {
  101. $CNF = &$this->_oModule->_oConfig->CNF;
  102. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  103. $oEditedProfile = BxDolProfile::getInstanceMagic($aContentInfo['profile_id']);
  104. $sStatus = $oEditedProfile->getStatus();
  105. if (!$this->isAutoApproval(BX_DOL_PROFILE_ACTIVATE_EDIT) && BX_PROFILE_STATUS_ACTIVE == $sStatus){
  106. $aTrackTextFieldsChanges = array ();
  107. }
  108. if(isset($CNF['FIELD_BIRTHDAY']) && isset($aContentInfo[$CNF['FIELD_BIRTHDAY']]))
  109. $oForm->addTrackFields($CNF['FIELD_BIRTHDAY'], $aContentInfo);
  110. }
  111. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  112. {
  113. if ($s = parent::onDataEditAfter($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)){
  114. return $s;
  115. }
  116. $CNF = &$this->_oModule->_oConfig->CNF;
  117. /*
  118. * Load updated data.
  119. */
  120. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  121. // change profile to 'pending' only if profile is 'active'
  122. $oEditedProfile = BxDolProfile::getInstanceMagic($aContentInfo['profile_id']);
  123. $sStatus = $oEditedProfile->getStatus();
  124. if (!$this->isAutoApproval(BX_DOL_PROFILE_ACTIVATE_EDIT) && BX_PROFILE_STATUS_ACTIVE == $sStatus && !empty($aTrackTextFieldsChanges['changed_fields']))
  125. $oEditedProfile->disapprove(BX_PROFILE_ACTION_AUTO, 0, $this->_oModule->serviceActAsProfile());
  126. // process uploaded files
  127. if (isset($CNF['FIELD_PICTURE']))
  128. $oForm->processFiles($CNF['FIELD_PICTURE'], $iContentId, false);
  129. if (isset($CNF['FIELD_COVER']))
  130. $oForm->processFiles($CNF['FIELD_COVER'], $iContentId, false);
  131. if(isset($CNF['FIELD_ALLOW_POST_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false)
  132. $oPrivacy->reassociateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]);
  133. // update content filters
  134. if(isset($CNF['FIELD_BIRTHDAY']) && $oForm->isTrackFieldChanged($CNF['FIELD_BIRTHDAY']))
  135. BxDolContentFilter::getInstance()->updateValuesByProfile($oProfile->getInfo());
  136. return '';
  137. }
  138. public function onDataAddAfter ($iAccountId, $iContentId)
  139. {
  140. /*
  141. * Add account and content association.
  142. * Note. It should be done first to correctly get and use author's profile later.
  143. */
  144. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iContentId, BX_PROFILE_STATUS_PENDING, $this->_oModule->getName());
  145. $oProfile = BxDolProfile::getInstance($iProfileId);
  146. if($s = parent::onDataAddAfter($iAccountId, $iContentId))
  147. return $s;
  148. $CNF = &$this->_oModule->_oConfig->CNF;
  149. /*
  150. * Load updated data.
  151. */
  152. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  153. // approve profile if auto-approval is enabled and profile status is 'pending'
  154. $sStatus = $oProfile->getStatus();
  155. if ($sStatus == BX_PROFILE_STATUS_PENDING && $this->isAutoApproval(BX_DOL_PROFILE_ACTIVATE_ADD))
  156. $oProfile->approve(BX_PROFILE_ACTION_AUTO, 0, $this->_oModule->serviceActAsProfile() && $this->_oModule->serviceIsEnableProfileActivationLetter());
  157. // set created profile some default membership
  158. if ((int)bx_get('level_id') && bx_srv('bx_acl', 'get_prices', [(int)bx_get('level_id'), true]))
  159. $iAclLevel = (int)bx_get('level_id');
  160. else if(isset($CNF['PARAM_DEFAULT_ACL_LEVEL']))
  161. $iAclLevel = isAdmin() && getLoggedId() == $iAccountId ? MEMBERSHIP_ID_ADMINISTRATOR : getParam($CNF['PARAM_DEFAULT_ACL_LEVEL']);
  162. else
  163. $iAclLevel = MEMBERSHIP_ID_STANDARD;
  164. BxDolAcl::getInstance()->setMembership($iProfileId, $iAclLevel, 0, true);
  165. // process uploaded files
  166. $oForm = $this->getObjectFormAdd();
  167. if (isset($CNF['FIELD_PICTURE']))
  168. $oForm->processFiles($CNF['FIELD_PICTURE'], $iContentId, true);
  169. if (isset($CNF['FIELD_COVER']))
  170. $oForm->processFiles($CNF['FIELD_COVER'], $iContentId, true);
  171. if(isset($CNF['FIELD_ALLOW_POST_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false)
  172. $oPrivacy->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_POST_TO']]);
  173. // switch context to the created profile
  174. if ($this->_oModule->serviceActAsProfile()) {
  175. $oAccount = BxDolAccount::getInstance($iAccountId);
  176. $oAccount->updateProfileContext($iProfileId);
  177. //update recomedations
  178. BxDolRecommendation::updateData($iProfileId);
  179. // update content filters
  180. BxDolContentFilter::getInstance()->updateValuesByProfile($oProfile->getInfo());
  181. }
  182. return '';
  183. }
  184. public function redirectAfterAdd($aContentInfo, $sUrl = '')
  185. {
  186. $CNF = &$this->_oModule->_oConfig->CNF;
  187. $oSession = BxDolSession::getInstance();
  188. $sRedirectType = empty($CNF['PARAM_REDIRECT_AADD']) ? BX_DOL_PROFILE_REDIRECT_PROFILE : getParam($CNF['PARAM_REDIRECT_AADD']);
  189. $sRedirectDefault = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  190. $sRedirectUrl = $sRedirectDefault;
  191. switch ($sRedirectType) {
  192. case BX_DOL_PROFILE_REDIRECT_PROFILE:
  193. break;
  194. // if user just joined the redirect to the page where user comes from.
  195. case BX_DOL_PROFILE_REDIRECT_LAST:
  196. $sJoinReferrer = $oSession->getValue('join-referrer');
  197. if($sJoinReferrer) {
  198. $sRedirectUrl = $sJoinReferrer;
  199. $oSession->unsetValue('join-referrer');
  200. }
  201. break;
  202. case BX_DOL_PROFILE_REDIRECT_CUSTOM:
  203. $sRedirectCustom = getParam($CNF['PARAM_REDIRECT_AADD_CUSTOM_URL']);
  204. if($sRedirectCustom) {
  205. $sRedirectUrl = $this->prepareCustomRedirectUrl($sRedirectCustom, $aContentInfo);
  206. }
  207. break;
  208. case BX_DOL_PROFILE_REDIRECT_HOMEPAGE:
  209. $sRedirectUrl = BX_DOL_URL_ROOT;
  210. break;
  211. }
  212. $sCustomReferrer = $oSession->getValue('custom-referrer');
  213. if($sCustomReferrer) {
  214. $sRedirectUrl = $sCustomReferrer;
  215. $oSession->unsetValue('custom-referrer');
  216. }
  217. return parent::redirectAfterAdd($aContentInfo, $sRedirectUrl);
  218. }
  219. }
  220. /** @} */