BxDolProfile.php 30 KB

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