BxDolProfile.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. define('BX_PROFILE_ACTION_AUTO', 0); ///< automatic action without any checking
  10. define('BX_PROFILE_ACTION_MANUAL', 1); ///< manual action performed by human
  11. define('BX_PROFILE_ACTION_ROBOT', 2); ///< action peformed by some robot based on some conditions
  12. define('BX_PROFILE_ACTION_EXTERNAL', 2); ///< action peformed by external service, like join using OAuth
  13. class BxDolProfile extends BxDolFactory implements iBxDolProfile
  14. {
  15. protected $_iProfileID;
  16. protected $_aProfile;
  17. protected $_oQuery;
  18. /**
  19. * Constructor
  20. */
  21. protected function __construct ($iProfileId)
  22. {
  23. $iProfileId = (int)$iProfileId;
  24. $sClass = get_class($this) . '_' . $iProfileId;
  25. if (isset($GLOBALS['bxDolClasses'][$sClass]))
  26. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  27. parent::__construct();
  28. $this->_iProfileID = $iProfileId; // since constructor is protected $iProfileId is always valid
  29. $this->_oQuery = BxDolProfileQuery::getInstance();
  30. }
  31. /**
  32. * Prevent cloning the instance
  33. */
  34. public function __clone()
  35. {
  36. $sClass = get_class($this) . '_' . $this->_iProfileID;
  37. if (isset($GLOBALS['bxDolClasses'][$sClass]))
  38. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  39. }
  40. /**
  41. * Get singleton instance of Account Profile by account id
  42. */
  43. public static function getInstanceAccountProfile($iAccountId = false, $bClearCache = false)
  44. {
  45. if (!$iAccountId)
  46. $iAccountId = getLoggedId();
  47. $oQuery = BxDolProfileQuery::getInstance();
  48. $aProfile = $oQuery->getProfileByContentTypeAccount($iAccountId, 'system', $iAccountId);
  49. if (!$aProfile)
  50. return false;
  51. return self::getInstance($aProfile['id'], $bClearCache);
  52. }
  53. /**
  54. * Get singleton instance of Profile by account id, content id and type
  55. */
  56. public static function getInstanceByContentTypeAccount($iContent, $sType, $iAccountId = false)
  57. {
  58. if (!$iAccountId)
  59. $iAccountId = getLoggedId();
  60. $oQuery = BxDolProfileQuery::getInstance();
  61. $aProfile = $oQuery->getProfileByContentTypeAccount($iContent, $sType, $iAccountId);
  62. if (!$aProfile)
  63. return false;
  64. return self::getInstance($aProfile['id']);
  65. }
  66. /**
  67. * Get singleton instance of Profile by content id and type
  68. */
  69. public static function getInstanceByContentAndType($iContent, $sType, $bClearCache = false)
  70. {
  71. $oQuery = BxDolProfileQuery::getInstance();
  72. $aProfile = $oQuery->getProfileByContentAndType($iContent, $sType, $bClearCache);
  73. if (!$aProfile)
  74. return false;
  75. return self::getInstance($aProfile['id']);
  76. }
  77. /**
  78. * Get singleton instance of Profile by Account id (currently active profile is returned)
  79. */
  80. public static function getInstanceByAccount($iAccountId = false, $bClearCache = false)
  81. {
  82. $oQuery = BxDolProfileQuery::getInstance();
  83. $mixedProfileId = $oQuery->getCurrentProfileByAccount($iAccountId, $bClearCache);
  84. return self::getInstance($mixedProfileId);
  85. }
  86. /**
  87. * Get singleton instance of Profile by profile id, if profile isn't found it returns instance of BxDolProfileAnonymous or BxDolProfileUndefined
  88. */
  89. public static function getInstanceMagic($mixedProfileId = false, $bClearCache = false)
  90. {
  91. if ($mixedProfileId < 0)
  92. return BxDolProfileAnonymous::getInstance($mixedProfileId);
  93. if (0 === $mixedProfileId || !($oProfile = self::getInstance($mixedProfileId, $bClearCache)))
  94. return BxDolProfileUndefined::getInstance();
  95. return $oProfile;
  96. }
  97. /**
  98. * Get singleton instance of Profile by profile id
  99. */
  100. public static function getInstance($mixedProfileId = false, $bClearCache = false)
  101. {
  102. $oQuery = BxDolProfileQuery::getInstance();
  103. if (!$mixedProfileId) {
  104. if (isset($GLOBALS['glForceCurrentProfileId']) && $GLOBALS['glForceCurrentProfileId'] > 0)
  105. $mixedProfileId = $GLOBALS['glForceCurrentProfileId'];
  106. else
  107. $mixedProfileId = $oQuery->getCurrentProfileByAccount(getLoggedId(), $bClearCache);
  108. }
  109. $aProfileInfo = $oQuery->getInfoById($mixedProfileId);
  110. if (empty($aProfileInfo['id']) || !BxDolModuleDb::getInstance()->isEnabledByName($aProfileInfo['type']))
  111. return false;
  112. $sClass = __CLASS__ . '_' . $aProfileInfo['id'];
  113. if (!isset($GLOBALS['bxDolClasses'][$sClass]))
  114. $GLOBALS['bxDolClasses'][$sClass] = new BxDolProfile($aProfileInfo['id']);
  115. return $GLOBALS['bxDolClasses'][$sClass];
  116. }
  117. public static function getData($mixedProfileId = false, $aParams = [])
  118. {
  119. $sDisplayType = 'unit';
  120. if(isset($aParams['display_type']))
  121. $sDisplayType = $aParams['display_type'];
  122. if(!($mixedProfileId instanceof BxDolProfile))
  123. $oProfile = BxDolProfile::getInstanceMagic($mixedProfileId);
  124. else
  125. $oProfile = $mixedProfileId;
  126. $aRv = [
  127. 'id' => $oProfile->id(),
  128. 'display_name' => $oProfile->getDisplayName(),
  129. 'url' => ($sUrl = $oProfile->getUrl()) !== false ? bx_api_get_relative_url($sUrl) : false,
  130. 'url_avatar' => $oProfile->{isset($aParams['get_avatar']) && method_exists($oProfile, $aParams['get_avatar']) ? $aParams['get_avatar'] : 'getAvatar'}(),
  131. 'module' => $oProfile->getModule(),
  132. ];
  133. // Online status (the online dot on the site), only on request: it costs a query per profile.
  134. if(isset($aParams['with_online']) && (bool)$aParams['with_online']) {
  135. $aRv['online'] = $oProfile->isOnline() ? 1 : 0;
  136. }
  137. if(isset($aParams['with_info']) && (bool)$aParams['with_info'])
  138. $aRv['info'] = bx_srv($oProfile->getModule(), 'get_info', [$oProfile->getContentId(), false]);
  139. return $aRv;
  140. }
  141. public static function getDataForPage($mixedProfileId = false, $aParams = [])
  142. {
  143. $oAcl = BxDolAcl::getInstance();
  144. $oTemplate = BxDolTemplate::getInstance();
  145. if(!($mixedProfileId instanceof BxDolProfile))
  146. $oProfile = BxDolProfile::getInstanceMagic($mixedProfileId);
  147. else
  148. $oProfile = $mixedProfileId;
  149. $oAccount = $oProfile->getAccountObject();
  150. $iId = $oProfile->id();
  151. $sUrl = bx_api_get_relative_url($oProfile->getUrl());
  152. $aMembershipInfo = $oAcl->getMemberMembershipInfo($iId);
  153. $aMembershipLevelInfo = $oAcl->getMembershipInfo($aMembershipInfo['id']);
  154. $sIconS = $aMembershipLevelInfo['icon'];
  155. $sIconD = $oTemplate->getImage($sIconS, ['wrap_in_tag' => false]);
  156. $aRv = [
  157. 'id' => $iId,
  158. 'account_id' => $oAccount->id(),
  159. 'email' => $oAccount->getEmail(),
  160. 'display_name' => $oProfile->getDisplayName(),
  161. 'url' => $sUrl,
  162. 'url_postfix' => substr($sUrl, strrpos($sUrl, '/') + 1),
  163. 'avatar' => $oProfile->getAvatar(),
  164. 'settings' => $oProfile->getSettings(),
  165. 'membership' => $aMembershipInfo['id'],
  166. 'membership_name' => _t($aMembershipInfo['name']),
  167. 'membership_icon' => strcmp($sIconS, $sIconD) != 0 ? $sIconD : BxDolIconset::getObjectInstance()->getIcon($sIconD),
  168. 'moderator' => (bool)BxDolAcl::getInstance()->isMemberLevelInSet([MEMBERSHIP_ID_ADMINISTRATOR, MEMBERSHIP_ID_MODERATOR], $iId),
  169. 'operator' => isAdmin(),
  170. //'info' => $oProfile->getInfo(),
  171. 'confirmed' => $oAccount->isConfirmed(),
  172. 'profiles_count' => $oAccount->getProfilesNumber(true),
  173. 'hash' => encryptUserId($iId),
  174. 'profiles_limit' => (int)getParam('sys_account_limit_profiles_number'),
  175. 'active' => $oProfile->isActive(),
  176. 'status' => $oProfile->getStatus(),
  177. ];
  178. if ($iId == bx_get_logged_profile_id()){
  179. $aRv['counters'] = bx_srv('system', 'profile_counters', [defined('BX_API_PAGE')], 'TemplServiceProfiles');
  180. $oInformer = BxDolInformer::getInstance($oTemplate);
  181. $sRet = $oInformer ? $oInformer->display() : '';
  182. if ($sRet){
  183. $aRv['informer'] = $sRet;
  184. }
  185. /*
  186. $oPayments = BxDolPayments::getInstance();
  187. if($oPayments->isActive())
  188. $aRv['cart'] = $oPayments->getCartItemsCount();
  189. $sModuleNotifications = 'bx_notifications';
  190. if(BxDolRequest::serviceExists($sModuleNotifications, 'get_unread_notifications_num'))
  191. $aRv['notifications'] = bx_srv($sModuleNotifications, 'get_unread_notifications_num', [$iId]);
  192. */
  193. if($oProfile !== false && BxDolAccount::isAllowedCreateMultiple($iId)) {
  194. $oAccount = BxDolAccount::getInstance();
  195. if($oAccount != false && !$oAccount->isProfilesLimitReached()) {
  196. $oMenuProfileAdd = BxDolMenu::getObjectInstance('sys_add_profile');
  197. if($oMenuProfileAdd !== false)
  198. $aRv['menu'] = $oMenuProfileAdd->getCodeAPI();
  199. }
  200. }
  201. }
  202. bx_alert('system', 'get_profile_for_page_api', 0, 0, ['override_result' => &$aRv]);
  203. return $aRv;
  204. }
  205. /**
  206. * Get profile id
  207. */
  208. public function id()
  209. {
  210. $aProfile = $this->getInfo($this->_iProfileID);
  211. return isset($aProfile['id']) ? $aProfile['id'] : false;
  212. }
  213. /**
  214. * Get account id associated with the profile
  215. */
  216. public function getAccountId($iProfileId = false)
  217. {
  218. $aInfo = $this->getInfo($iProfileId);
  219. return $aInfo['account_id'];
  220. }
  221. /**
  222. * Get account object associated with the profile
  223. */
  224. public function getAccountObject($iProfileId = false)
  225. {
  226. return BxDolAccount::getInstance($this->getAccountId($iProfileId));
  227. }
  228. /**
  229. * Get content id associated with the profile
  230. */
  231. public function getContentId()
  232. {
  233. $aInfo = $this->getInfo();
  234. return $aInfo['content_id'];
  235. }
  236. /**
  237. * Check if profile status is active
  238. */
  239. public function isActive($iProfileId = false)
  240. {
  241. if($this->getStatus($iProfileId) != BX_PROFILE_STATUS_ACTIVE)
  242. return false;
  243. return (($oAccount = $this->getAccountObject($iProfileId)) !== false && $oAccount->isConfirmed()) || !getParam('sys_account_hide_unconfirmed_accounts');
  244. }
  245. /**
  246. * Is profile online
  247. */
  248. public function isOnline($iProfileId = false)
  249. {
  250. $iProfileId = (int)$iProfileId ? $iProfileId : $this->_iProfileID;
  251. return $this->_oQuery->isOnline($iProfileId);
  252. }
  253. /**
  254. * Is profile can 'Act as Profile'
  255. */
  256. public function isActAsProfile($iProfileId = false)
  257. {
  258. $aInfo = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  259. return BxDolService::call($aInfo['type'], 'act_as_profile');
  260. }
  261. /**
  262. * Get profile status
  263. */
  264. public function getStatus($iProfileId = false)
  265. {
  266. $aInfo = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  267. if(empty($aInfo) || !is_array($aInfo))
  268. return false;
  269. return $aInfo['status'];
  270. }
  271. /**
  272. * Get profile module name
  273. */
  274. public function getModule($iProfileId = false)
  275. {
  276. $aInfo = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  277. return $aInfo['type'];
  278. }
  279. /**
  280. * Get profile info
  281. */
  282. public function getInfo($iProfileId = 0)
  283. {
  284. if ($iProfileId && $iProfileId != $this->_iProfileID)
  285. return $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  286. if ($this->_aProfile)
  287. return $this->_aProfile;
  288. $this->_aProfile = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  289. return $this->_aProfile;
  290. }
  291. /**
  292. * Validate profile id.
  293. * @param $s - profile id
  294. * @return profile id or false if profile was not found
  295. */
  296. static public function getID($s)
  297. {
  298. $iId = BxDolProfileQuery::getInstance()->getIdById((int)$s);
  299. return $iId ? $iId : false;
  300. }
  301. /**
  302. * Get name to display in thumbnail
  303. */
  304. public function getDisplayName($iProfileId = 0)
  305. {
  306. $aInfo = $this->getInfo($iProfileId);
  307. $sDisplayName = BxDolService::call($aInfo['type'], 'profile_name', array($aInfo['content_id']));
  308. /**
  309. * @hooks
  310. * @hookdef hook-profile-profile_name 'profile', 'profile_name' - hook on before profile deletion
  311. * - $unit_name - equals `profile`
  312. * - $action - equals `profile_name`
  313. * - $object_id - profile_id
  314. * - $sender_id - not used
  315. * - $extra_params - array of additional params with the following array keys:
  316. * - `info` - [array] profile info
  317. * - `display_name` - [bool] by ref, display profile name, can be overridden in hook processing
  318. * @hook @ref hook-profile-profile_name
  319. */
  320. bx_alert('profile', 'profile_name', $iProfileId, 0, array(
  321. 'info' => $aInfo,
  322. 'display_name' => &$sDisplayName,
  323. 'display_name_ref' => &$sDisplayName
  324. ));
  325. return $sDisplayName;
  326. }
  327. public function getSettings($iProfileId = 0)
  328. {
  329. $aInfo = $this->getInfo($iProfileId);
  330. return BxDolService::call($aInfo['type'], 'profile_settings', array($aInfo['content_id']));
  331. }
  332. /**
  333. * Get profile url
  334. */
  335. public function getUrl($iProfileId = 0)
  336. {
  337. $aInfo = $this->getInfo($iProfileId);
  338. return BxDolService::call($aInfo['type'], 'profile_url', array($aInfo['content_id']));
  339. }
  340. /**
  341. * Get profile unit
  342. */
  343. public function getUnit($iProfileId = 0, $aParams = array())
  344. {
  345. $aInfo = $this->getInfo($iProfileId);
  346. return BxDolService::call($aInfo['type'], 'profile_unit', array($aInfo['content_id'], $aParams));
  347. }
  348. /**
  349. * Get profile unit for API calls
  350. */
  351. public function getUnitAPI($iProfileId = 0, $aParams = array())
  352. {
  353. $aInfo = $this->getInfo($iProfileId);
  354. return BxDolService::call($aInfo['type'], 'profile_unit_api', array($aInfo['content_id'], $aParams));
  355. }
  356. /**
  357. * Get badges
  358. */
  359. public function getBadges($iProfileId = 0)
  360. {
  361. $aInfo = $this->getInfo($iProfileId);
  362. return BxDolService::call($aInfo['type'], 'get_badges', array($aInfo['content_id'], false, true));
  363. }
  364. /**
  365. * Check whether a profile has real image uploaded by user.
  366. */
  367. public function hasImage($iProfileId = 0)
  368. {
  369. $aInfo = $this->getInfo($iProfileId);
  370. return BxDolService::call($aInfo['type'], 'has_image', array($aInfo['content_id']));
  371. }
  372. /**
  373. * Get picture url
  374. */
  375. public function getPicture($iProfileId = 0)
  376. {
  377. $aInfo = $this->getInfo($iProfileId);
  378. return BxDolService::call($aInfo['type'], 'profile_picture', array($aInfo['content_id']));
  379. }
  380. /**
  381. * Get avatar url
  382. */
  383. public function getAvatar($iProfileId = 0)
  384. {
  385. $aInfo = $this->getInfo($iProfileId);
  386. return BxDolService::call($aInfo['type'], 'profile_avatar', array($aInfo['content_id']));
  387. }
  388. /**
  389. * Get big (2x) avatar url
  390. */
  391. public function getAvatarBig($iProfileId = 0)
  392. {
  393. $aInfo = $this->getInfo($iProfileId);
  394. return BxDolService::call($aInfo['type'], 'profile_avatar_big', array($aInfo['content_id']));
  395. }
  396. /**
  397. * Get cover url
  398. */
  399. public function getCover($iProfileId = 0)
  400. {
  401. $aInfo = $this->getInfo($iProfileId);
  402. return BxDolService::call($aInfo['type'], 'profile_cover', array($aInfo['content_id']));
  403. }
  404. /**
  405. * Get unit cover url
  406. */
  407. public function getUnitCover($iProfileId = 0)
  408. {
  409. $aInfo = $this->getInfo($iProfileId);
  410. return BxDolService::call($aInfo['type'], 'profile_unit_cover', array($aInfo['content_id']));
  411. }
  412. /**
  413. * Get thumbnail url
  414. */
  415. public function getThumb($iProfileId = 0)
  416. {
  417. $aInfo = $this->getInfo($iProfileId);
  418. return BxDolService::call($aInfo['type'], 'profile_thumb', array($aInfo['content_id']));
  419. }
  420. /**
  421. * Get icon url
  422. */
  423. public function getIcon($iProfileId = 0)
  424. {
  425. $aInfo = $this->getInfo($iProfileId);
  426. return BxDolService::call($aInfo['type'], 'profile_icon', array($aInfo['content_id']));
  427. }
  428. /**
  429. * Get module icon
  430. */
  431. public function getIconModule($iProfileId = 0)
  432. {
  433. $aInfo = $this->getInfo($iProfileId);
  434. return BxDolService::call($aInfo['type'], 'module_icon');
  435. }
  436. /**
  437. * get profile edit page url
  438. */
  439. public function getEditUrl($iProfileId = 0)
  440. {
  441. $aInfo = $this->getInfo($iProfileId);
  442. return BxDolService::call($aInfo['type'], 'profile_edit_url', array($aInfo['content_id']));
  443. }
  444. /**
  445. * @see iBxDolProfile::checkAllowedProfileView
  446. */
  447. public function checkAllowedProfileView($iProfileId = 0)
  448. {
  449. $aInfo = $this->getInfo($iProfileId);
  450. return BxDolService::call($aInfo['type'], 'check_allowed_profile_view', array($aInfo['content_id']));
  451. }
  452. /**
  453. * @see iBxDolProfile::checkAllowedProfileContact
  454. */
  455. public function checkAllowedProfileContact($iProfileId = 0)
  456. {
  457. $aInfo = $this->getInfo($iProfileId);
  458. return BxDolService::call($aInfo['type'], 'check_allowed_profile_contact', array($aInfo['content_id']));
  459. }
  460. /**
  461. * @see iBxDolProfile::checkAllowedPostInProfile
  462. */
  463. public function checkAllowedPostInProfile($iProfileId = 0, $sPostModule = '')
  464. {
  465. $aInfo = $this->getInfo($iProfileId);
  466. return BxDolService::call($aInfo['type'], 'check_allowed_post_in_profile', array($aInfo['content_id'], $sPostModule));
  467. }
  468. /**
  469. * Delete profile.
  470. * @param $ID - optional profile id to delete
  471. * @param $bDeleteWithContent - delete profile with all its contents
  472. * @param $bForceDelete - force deletetion is case of account profile deletion
  473. * @return false on error, or true on success
  474. */
  475. function delete($ID = false, $bDeleteWithContent = false, $bForceDelete = false)
  476. {
  477. $ID = (int)$ID;
  478. if (!$ID)
  479. $ID = $this->_iProfileID;
  480. $aProfileInfo = $this->_oQuery->getInfoById($ID);
  481. if (!$aProfileInfo)
  482. return false;
  483. // delete system profiles (accounts) is not allowed, instead - delete whole account
  484. if (!$bForceDelete && 'system' == $aProfileInfo['type'])
  485. return false;
  486. // delete actual profile
  487. if ($sErrorMsg = BxDolService::call($aProfileInfo['type'] , 'delete_entity_service', array($aProfileInfo['content_id'], $bDeleteWithContent)))
  488. return false;
  489. // switch profile context if deleted profile is active profile context
  490. $oAccount = BxDolAccount::getInstance ($aProfileInfo['account_id']);
  491. $aAccountInfo = $oAccount->getInfo();
  492. if (!$bForceDelete && $ID == $aAccountInfo['profile_id'])
  493. $oAccount->updateProfileContextAuto($ID);
  494. $isStopDeletion = false;
  495. /**
  496. * @hooks
  497. * @hookdef hook-profile-before_delete 'profile', 'before_delete' - hook on before profile deletion
  498. * - $unit_name - equals `profile`
  499. * - $action - equals `before_delete`
  500. * - $object_id - profile_id
  501. * - $sender_id - not used
  502. * - $extra_params - array of additional params with the following array keys:
  503. * - `delete_with_content` - [bool] also delete content for profile or not
  504. * - `stop_deletion` - [bool] by ref, if true then stop profile deletion, can be overridden in hook processing
  505. * - `type` - [string] module name
  506. * @hook @ref hook-profile-before_delete
  507. */
  508. bx_alert('profile', 'before_delete', $ID, 0, array('delete_with_content' => $bDeleteWithContent, 'stop_deletion' => &$isStopDeletion, 'stop_deletion_ref' => &$isStopDeletion, 'type' => $aProfileInfo['type']));
  509. if ($isStopDeletion)
  510. return false;
  511. // delete associated content
  512. if($bDeleteWithContent) {
  513. BxDolCmts::onAuthorDelete($ID);
  514. BxDolReport::onAuthorDelete($ID);
  515. BxDolVote::onAuthorDelete($ID);
  516. BxDolFavorite::onAuthorDelete($ID);
  517. BxDolView::onAuthorDelete($ID);
  518. }
  519. // delete connections
  520. $oConn = BxDolConnection::getObjectInstance('sys_profiles_friends');
  521. $oConn->onDeleteInitiatorAndContent($ID);
  522. $oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  523. $oConn->onDeleteInitiatorAndContent($ID);
  524. // delete profile's acl levels
  525. BxDolAcl::getInstance()->onProfileDelete($ID);
  526. // delete SEO links
  527. BxDolPage::deleteSeoLinkByParam ('profile_id', $ID);
  528. // delete profile
  529. if (!$this->_oQuery->delete($ID))
  530. return false;
  531. /**
  532. * @hooks
  533. * @hookdef hook-profile-delete 'profile', 'delete' - hook on profile deleted
  534. * - $unit_name - equals `profile`
  535. * - $action - equals `delete`
  536. * - $object_id - profile_id
  537. * - $sender_id - not used
  538. * - $extra_params - array of additional params with the following array keys:
  539. * - `delete_with_content` - [bool] also delete content for profile or not
  540. * - `type` - [string] module name
  541. * @hook @ref hook-profile-delete
  542. */
  543. bx_alert('profile', 'delete', $ID, 0, array('delete_with_content' => $bDeleteWithContent, 'type' => $aProfileInfo['type']));
  544. // unset class instance to prevent creating the instance again
  545. $this->_iProfileID = 0;
  546. $sClass = get_class($this) . '_' . $ID;
  547. unset($GLOBALS['bxDolClasses'][$sClass]);
  548. return true;
  549. }
  550. /**
  551. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  552. * @param $iAccountId account id
  553. * @param $iContentId content id
  554. * @param $sStatus profile status
  555. * @param $sType profile content type
  556. * @return inserted profile's id
  557. */
  558. static public function add ($iAction, $iAccountId, $iContentId, $sStatus, $sType = 'system')
  559. {
  560. $oQuery = BxDolProfileQuery::getInstance();
  561. if (!($iProfileId = $oQuery->insertProfile ($iAccountId, $iContentId, $sStatus, $sType)))
  562. return false;
  563. /**
  564. * @hooks
  565. * @hookdef hook-profile-add 'profile', 'add' - hook on profile added
  566. * - $unit_name - equals `profile`
  567. * - $action - equals `add`
  568. * - $object_id - profile_id
  569. * - $sender_id - not used
  570. * - $extra_params - array of additional params with the following array keys:
  571. * - `module` - [string] action id
  572. * - `content` - [int] content_id in module
  573. * - `account` - [int] account_id
  574. * - `status` - [string] status
  575. * - `action` - [int] action id
  576. * - `profile_id` - [int] by ref, iprofile_id, can be overridden in hook processing
  577. * @hook @ref hook-profile-add
  578. */
  579. bx_alert('profile', 'add', $iProfileId, 0, array('module' => $sType, 'content' => $iContentId, 'account' => $iAccountId, 'status' => $sStatus, 'action' => $iAction, 'profile_id' => &$iProfileId, 'profile_id_ref' => &$iProfileId));
  580. return $iProfileId;
  581. }
  582. /**
  583. * Change profile status to 'Active'
  584. */
  585. public function activate($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  586. {
  587. $sStatus = $this->getStatus($iProfileId);
  588. return $this->changeStatus(BX_PROFILE_STATUS_ACTIVE, BX_PROFILE_STATUS_PENDING == $sStatus ? 'approve' : 'activate', $iAction, $iProfileId, $bSendEmailNotification);
  589. }
  590. /**
  591. * Change profile status from 'Pending' to the next level - 'Active'
  592. */
  593. public function approve($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  594. {
  595. return $this->changeStatus(BX_PROFILE_STATUS_ACTIVE, 'approve', $iAction, $iProfileId, $bSendEmailNotification);
  596. }
  597. /**
  598. * Change profile status to 'Pending'
  599. */
  600. public function disapprove($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  601. {
  602. return $this->changeStatus(BX_PROFILE_STATUS_PENDING, 'disapprove', $iAction, $iProfileId, $bSendEmailNotification);
  603. }
  604. /**
  605. * Move profile to another account
  606. */
  607. public function move($iAccountId, $iProfileId = 0)
  608. {
  609. if (!$iProfileId)
  610. $iProfileId = $this->_iProfileID;
  611. if($this->getAccountId($iProfileId) == $iAccountId)
  612. return true;
  613. return $this->_oQuery->changeAccountId($iProfileId, $iAccountId) !== false;
  614. }
  615. public function doAudit($sAction, $aData = array())
  616. {
  617. bx_audit(
  618. $this->getContentId(),
  619. $this->getModule(),
  620. $sAction,
  621. array('content_title' => $this->getDisplayName(), 'data' => $aData)
  622. );
  623. }
  624. /**
  625. * Change profile status to 'Suspended'
  626. */
  627. public function suspend($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  628. {
  629. if (!$iProfileId)
  630. $iProfileId = $this->_iProfileID;
  631. //moderators shouldn't be able to suspend other moderators and admins
  632. if (BxDolAcl::getInstance()->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR), bx_get_logged_profile_id()) && BxDolAcl::getInstance()->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR), $iProfileId))
  633. return false;
  634. return $this->changeStatus(BX_PROFILE_STATUS_SUSPENDED, 'suspend', $iAction, $iProfileId, $bSendEmailNotification);
  635. }
  636. protected function changeStatus($sStatus, $sAlertActionName, $iAction, $iProfileId = 0, $bSendEmailNotification = true)
  637. {
  638. if (!$iProfileId)
  639. $iProfileId = $this->_iProfileID;
  640. // get account and profile objects
  641. $oProfile = BxDolProfile::getInstance($iProfileId);
  642. $oAccount = $oProfile->getAccountObject();
  643. if (!$oProfile || !$oAccount)
  644. return false;
  645. // change status
  646. if (!$this->_oQuery->changeStatus($iProfileId, $sStatus))
  647. return false;
  648. $this->_aProfile = array();
  649. /**
  650. * @hooks
  651. * @hookdef hook-profile-approve 'profile', 'approve' - hook on switch profile
  652. * - $unit_name - equals `profile`
  653. * - $action - equals `approve`
  654. * - $object_id - profile_id for current user
  655. * - $sender_id - not used
  656. * - $extra_params - array of additional params with the following array keys:
  657. * - `action` - [int] action id
  658. * - `status` - [string] by ref, status, can be overridden in hook processing
  659. * - `send_email_notification` - [bool] by ref, if need to send notification about changed status = true, otherwise false, can be overridden in hook processing
  660. * @hook @ref hook-profile-approve
  661. */
  662. bx_alert('profile', $sAlertActionName, $iProfileId, false, array(
  663. 'action' => $iAction,
  664. 'status' => &$sStatus,
  665. 'status_ref' => &$sStatus,
  666. 'send_email_notification' => &$bSendEmailNotification,
  667. 'send_email_notification_ref' => &$bSendEmailNotification
  668. ));
  669. $this->doAudit('_sys_audit_action_set_status_' . $sStatus);
  670. // send email to member about status change
  671. if ($bSendEmailNotification)
  672. sendMailTemplate('t_ChangeStatus' . ucfirst($sStatus), $oAccount->id(), $iProfileId, array('status' => $sStatus), BX_EMAIL_SYSTEM);
  673. return true;
  674. }
  675. public static function getSwitchToProfileRedirectUrl($iProfileId)
  676. {
  677. return BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher', [
  678. 'switch_to_profile' => $iProfileId,
  679. 'redirect' => getParam('sys_account_switch_to_profile_redirect')
  680. ]);
  681. }
  682. /**
  683. * Display informer message if it is possible to switch to this profile
  684. */
  685. public function checkSwitchToProfile($oTemplate = null, $iViewerAccountId = false, $iViewerProfileId = false)
  686. {
  687. if (false === $iViewerAccountId)
  688. $iViewerAccountId = getLoggedId();
  689. if (false === $iViewerProfileId)
  690. $iViewerProfileId = bx_get_logged_profile_id();
  691. if (!$iViewerAccountId || !$iViewerProfileId)
  692. return;
  693. $aCheck = checkActionModule($iViewerProfileId, 'switch to any profile', 'system', false);
  694. $bAllowSwitchToAnyProfile = $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  695. $iSwitchToAccountId = $this->getAccountId();
  696. $iSwitchToProfileId = $this->id();
  697. $bCanSwitch = ($iSwitchToAccountId == $iViewerAccountId || $bAllowSwitchToAnyProfile);
  698. /**
  699. * @hooks
  700. * @hookdef hook-account-check_switch_context 'account', 'check_switch_context' - hook on switch profile
  701. * - $unit_name - equals `account`
  702. * - $action - equals `check_switch_context`
  703. * - $object_id - account id
  704. * - $sender_id - not used
  705. * - $extra_params - array of additional params with the following array keys:
  706. * - `switch_to_profile` - [int] profile_id for switched profile
  707. * - `viewer_account` - [int] profile_id for viewer profile
  708. * - `override_result` - [bool] by ref, if allow to switch to profile = true, otherwise false, can be overridden in hook processing
  709. * @hook @ref hook-account-check_switch_context
  710. */
  711. bx_alert('account', 'check_switch_context', $iSwitchToAccountId, $iViewerProfileId, array('switch_to_profile' => $iSwitchToProfileId, 'viewer_account' => $iViewerAccountId, 'override_result' => &$bCanSwitch));
  712. if(!$bCanSwitch || $iViewerProfileId == $iSwitchToProfileId)
  713. return;
  714. $oInformer = BxDolInformer::getInstance($oTemplate);
  715. if($oInformer)
  716. $oInformer->add('sys-switch-profile-context', _t('_sys_txt_account_profile_context_change' . ($iSwitchToAccountId != $iViewerAccountId ? '_to_another' : '') . '_suggestion', self::getSwitchToProfileRedirectUrl($this->id())), BX_INFORMER_INFO);
  717. }
  718. /**
  719. * Add permament messages.
  720. */
  721. public function addInformerPermanentMessages ($oInformer)
  722. {
  723. $aInfo = $this->getInfo();
  724. $aProfiles = $this->_oQuery->getProfilesByAccount($aInfo['account_id']);
  725. if ($aInfo['type'] == 'system' && count($aProfiles) == 1) {
  726. $oInformer->add('sys-account-profile-system', _t('_sys_txt_account_profile_system'), BX_INFORMER_INFO, [
  727. 'icon' => 'user-plus',
  728. 'action_url' => BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher'),
  729. 'action_title' => _t('_sys_txt_account_profile_system_action'),
  730. ]);
  731. }
  732. }
  733. }
  734. /** @} */