BxDolProfile.php 30 KB

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