BxDolProfile.php 31 KB

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