BxBaseModProfileFormsEntryHelper.php 12 KB

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