BxDolProfile.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 $_oQuery;
  17. /**
  18. * Constructor
  19. */
  20. protected function __construct ($iProfileId)
  21. {
  22. $iProfileId = (int)$iProfileId;
  23. $sClass = get_class($this) . '_' . $iProfileId;
  24. if (isset($GLOBALS['bxDolClasses'][$sClass]))
  25. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  26. parent::__construct();
  27. $this->_iProfileID = $iProfileId; // since constructor is protected $iProfileId is always valid
  28. $this->_oQuery = BxDolProfileQuery::getInstance();
  29. }
  30. /**
  31. * Prevent cloning the instance
  32. */
  33. public function __clone()
  34. {
  35. $sClass = get_class($this) . '_' . $this->_iProfileID;
  36. if (isset($GLOBALS['bxDolClasses'][$sClass]))
  37. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  38. }
  39. /**
  40. * Get singleton instance of Account Profile by account id
  41. */
  42. public static function getInstanceAccountProfile($iAccountId = false)
  43. {
  44. if (!$iAccountId)
  45. $iAccountId = getLoggedId();
  46. $oQuery = BxDolProfileQuery::getInstance();
  47. $aProfile = $oQuery->getProfileByContentTypeAccount($iAccountId, 'system', $iAccountId);
  48. if (!$aProfile)
  49. return false;
  50. return self::getInstance($aProfile['id']);
  51. }
  52. /**
  53. * Get singleton instance of Profile by account id, content id and type
  54. */
  55. public static function getInstanceByContentTypeAccount($iContent, $sType, $iAccountId = false)
  56. {
  57. if (!$iAccountId)
  58. $iAccountId = getLoggedId();
  59. $oQuery = BxDolProfileQuery::getInstance();
  60. $aProfile = $oQuery->getProfileByContentTypeAccount($iContent, $sType, $iAccountId);
  61. if (!$aProfile)
  62. return false;
  63. return self::getInstance($aProfile['id']);
  64. }
  65. /**
  66. * Get singleton instance of Profile by content id and type
  67. */
  68. public static function getInstanceByContentAndType($iContent, $sType)
  69. {
  70. $oQuery = BxDolProfileQuery::getInstance();
  71. $aProfile = $oQuery->getProfileByContentAndType($iContent, $sType);
  72. if (!$aProfile)
  73. return false;
  74. return self::getInstance($aProfile['id']);
  75. }
  76. /**
  77. * Get singleton instance of Profile by Account id (currently active profile is returned)
  78. */
  79. public static function getInstanceByAccount($iAccountId = false)
  80. {
  81. $oQuery = BxDolProfileQuery::getInstance();
  82. $mixedProfileId = $oQuery->getCurrentProfileByAccount($iAccountId);
  83. return self::getInstance($mixedProfileId);
  84. }
  85. /**
  86. * Get singleton instance of Profile by profile id
  87. */
  88. public static function getInstance($mixedProfileId = false)
  89. {
  90. if (!$mixedProfileId) {
  91. $oQuery = BxDolProfileQuery::getInstance();
  92. $mixedProfileId = $oQuery->getCurrentProfileByAccount(getLoggedId());
  93. }
  94. $iProfileId = self::getID($mixedProfileId);
  95. if (!$iProfileId)
  96. return false;
  97. $sClass = __CLASS__ . '_' . $iProfileId;
  98. if (!isset($GLOBALS['bxDolClasses'][$sClass]))
  99. $GLOBALS['bxDolClasses'][$sClass] = new BxDolProfile($iProfileId);
  100. return $GLOBALS['bxDolClasses'][$sClass];
  101. }
  102. /**
  103. * Get profile id
  104. */
  105. public function id()
  106. {
  107. return $this->_oQuery->getIdById($this->_iProfileID);
  108. }
  109. /**
  110. * Get account id associated with the profile
  111. */
  112. public function getAccountId()
  113. {
  114. $aInfo = $this->getInfo();
  115. return $aInfo['account_id'];
  116. }
  117. /**
  118. * Get account object associated with the profile
  119. */
  120. public function getAccountObject()
  121. {
  122. return BxDolAccount::getInstance($this->getAccountId());
  123. }
  124. /**
  125. * Get content id associated with the profile
  126. */
  127. public function getContentId()
  128. {
  129. $aInfo = $this->getInfo();
  130. return $aInfo['content_id'];
  131. }
  132. /**
  133. * Check if profile status is active
  134. */
  135. public function isActive($iProfileId = false)
  136. {
  137. return BX_PROFILE_STATUS_ACTIVE == $this->getStatus($iProfileId);
  138. }
  139. /**
  140. * Is profile online
  141. */
  142. public function isOnline($iProfileId = false)
  143. {
  144. $iProfileId = (int)$iProfileId ? $iProfileId : $this->_iProfileID;
  145. return $this->_oQuery->isOnline($iProfileId);
  146. }
  147. /**
  148. * Get profile status
  149. */
  150. public function getStatus($iProfileId = false)
  151. {
  152. $aInfo = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  153. return $aInfo['status'];
  154. }
  155. /**
  156. * Get profile module name
  157. */
  158. public function getModule($iProfileId = false)
  159. {
  160. $aInfo = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  161. return $aInfo['type'];
  162. }
  163. /**
  164. * Get profile info
  165. */
  166. public function getInfo($iProfileId = 0)
  167. {
  168. return $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  169. }
  170. /**
  171. * Validate profile id.
  172. * @param $s - profile id
  173. * @return profile id or false if profile was not found
  174. */
  175. static public function getID($s)
  176. {
  177. $iId = BxDolProfileQuery::getInstance()->getIdById((int)$s);
  178. return $iId ? $iId : false;
  179. }
  180. /**
  181. * Get name to display in thumbnail
  182. */
  183. public function getDisplayName($iProfileId = 0)
  184. {
  185. $aInfo = $this->getInfo($iProfileId);
  186. return BxDolService::call($aInfo['type'], 'profile_name', array($aInfo['content_id']));
  187. }
  188. /**
  189. * Get profile url
  190. */
  191. public function getUrl($iProfileId = 0)
  192. {
  193. $aInfo = $this->getInfo($iProfileId);
  194. return BxDolService::call($aInfo['type'], 'profile_url', array($aInfo['content_id']));
  195. }
  196. /**
  197. * Get profile unit
  198. */
  199. public function getUnit($iProfileId = 0)
  200. {
  201. $aInfo = $this->getInfo($iProfileId);
  202. return BxDolService::call($aInfo['type'], 'profile_unit', array($aInfo['content_id']));
  203. }
  204. /**
  205. * Get picture url
  206. */
  207. public function getPicture($iProfileId = 0)
  208. {
  209. $aInfo = $this->getInfo($iProfileId);
  210. return BxDolService::call($aInfo['type'], 'profile_picture', array($aInfo['content_id']));
  211. }
  212. /**
  213. * Get avatar url
  214. */
  215. public function getAvatar($iProfileId = 0)
  216. {
  217. $aInfo = $this->getInfo($iProfileId);
  218. return BxDolService::call($aInfo['type'], 'profile_avatar', array($aInfo['content_id']));
  219. }
  220. /**
  221. * Get thumbnail url
  222. */
  223. public function getThumb($iProfileId = 0)
  224. {
  225. $aInfo = $this->getInfo($iProfileId);
  226. return BxDolService::call($aInfo['type'], 'profile_thumb', array($aInfo['content_id']));
  227. }
  228. /**
  229. * Get icon url
  230. */
  231. public function getIcon($iProfileId = 0)
  232. {
  233. $aInfo = $this->getInfo($iProfileId);
  234. return BxDolService::call($aInfo['type'], 'profile_icon', array($aInfo['content_id']));
  235. }
  236. /**
  237. * Get module icon
  238. */
  239. public function getIconModule($iProfileId = 0)
  240. {
  241. $aInfo = $this->getInfo($iProfileId);
  242. return BxDolService::call($aInfo['type'], 'module_icon');
  243. }
  244. /**
  245. * get profile edit page url
  246. */
  247. public function getEditUrl($iProfileId = 0)
  248. {
  249. $aInfo = $this->getInfo($iProfileId);
  250. return BxDolService::call($aInfo['type'], 'profile_edit_url', array($aInfo['content_id']));
  251. }
  252. /**
  253. * Delete profile.
  254. * @param $ID - optional profile id to delete
  255. * @param $bDeleteWithContent - delete profile with all its contents
  256. * @param $bForceDelete - force deletetion is case of account profile deletion
  257. * @return false on error, or true on success
  258. */
  259. function delete($ID = false, $bDeleteWithContent = false, $bForceDelete = false)
  260. {
  261. $ID = (int)$ID;
  262. if (!$ID)
  263. $ID = $this->_iProfileID;
  264. $aProfileInfo = $this->_oQuery->getInfoById($ID);
  265. if (!$aProfileInfo)
  266. return false;
  267. // delete system profiles (accounts) is not allowed, instead - delete whole account
  268. if (!$bForceDelete && 'system' == $aProfileInfo['type'])
  269. return false;
  270. // delete actual profile
  271. if ($sErrorMsg = BxDolService::call($aProfileInfo['type'] , 'delete_entity_service', array($aProfileInfo['content_id'], $bDeleteWithContent)))
  272. return false;
  273. // switch profile context if deleted profile is active profile context
  274. $oAccount = BxDolAccount::getInstance ($aProfileInfo['account_id']);
  275. $aAccountInfo = $oAccount->getInfo();
  276. if (!$bForceDelete && $ID == $aAccountInfo['profile_id']) {
  277. $aProfiles = $oAccount->getProfiles();
  278. $oProfileAccount = BxDolProfile::getInstanceAccountProfile($aProfileInfo['account_id']);
  279. // unset deleted profile and account profile
  280. unset($aProfiles[$ID]);
  281. unset($aProfiles[$oProfileAccount->id()]);
  282. if ($aProfiles) {
  283. // try to use another profile
  284. reset($aProfiles);
  285. $iProfileId = key($aProfiles);
  286. }
  287. else {
  288. // if no profiles exist, use account profile
  289. $iProfileId = $oProfileAccount->id();
  290. }
  291. $oAccount->updateProfileContext($iProfileId);
  292. }
  293. // create system event before deletion
  294. $isStopDeletion = false;
  295. bx_alert('profile', 'before_delete', $ID, 0, array('delete_with_content' => $bDeleteWithContent, 'stop_deletion' => &$isStopDeletion));
  296. if ($isStopDeletion)
  297. return false;
  298. // delete associated comments
  299. if($bDeleteWithContent)
  300. BxDolCmts::onAuthorDelete($ID);
  301. // delete connections
  302. $oConn = BxDolConnection::getObjectInstance('sys_profiles_friends');
  303. $oConn->onDeleteInitiatorAndContent($ID);
  304. $oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  305. $oConn->onDeleteInitiatorAndContent($ID);
  306. // delete profile's acl levels
  307. BxDolAcl::getInstance()->onProfileDelete($ID);
  308. // delete profile
  309. if (!$this->_oQuery->delete($ID))
  310. return false;
  311. // create system event
  312. bx_alert('profile', 'delete', $ID, 0, array('delete_with_content' => $bDeleteWithContent));
  313. // unset class instance to prevent creating the instance again
  314. $this->_iProfileID = 0;
  315. $sClass = get_class($this) . '_' . $ID;
  316. unset($GLOBALS['bxDolClasses'][$sClass]);
  317. return true;
  318. }
  319. /**
  320. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  321. * @param $iAccountId account id
  322. * @param $iContentId content id
  323. * @param $sStatus profile status
  324. * @param $sType profile content type
  325. * @return inserted profile's id
  326. */
  327. static public function add ($iAction, $iAccountId, $iContentId, $sStatus, $sType = 'system')
  328. {
  329. $oQuery = BxDolProfileQuery::getInstance();
  330. if (!($iProfileId = $oQuery->insertProfile ($iAccountId, $iContentId, $sStatus, $sType)))
  331. return false;
  332. bx_alert('profile', 'add', $iProfileId, 0, array('module' => $sType, 'content' => $iContentId, 'account' => $iAccountId, 'status' => $sStatus, 'action' => $iAction, 'profile_id' => &$iProfileId));
  333. return $iProfileId;
  334. }
  335. /**
  336. * Change profile status to 'Active'
  337. */
  338. public function activate($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  339. {
  340. $sStatus = $this->getStatus($iProfileId);
  341. return $this->changeStatus(BX_PROFILE_STATUS_ACTIVE, BX_PROFILE_STATUS_PENDING == $sStatus ? 'approve' : 'activate', $iAction, $iProfileId, $bSendEmailNotification);
  342. }
  343. /**
  344. * Change profile status from 'Pending' to the next level - 'Active'
  345. */
  346. public function approve($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  347. {
  348. return $this->changeStatus(BX_PROFILE_STATUS_ACTIVE, 'approve', $iAction, $iProfileId, $bSendEmailNotification);
  349. }
  350. /**
  351. * Change profile status to 'Pending'
  352. */
  353. public function disapprove($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  354. {
  355. return $this->changeStatus(BX_PROFILE_STATUS_PENDING, 'disapprove', $iAction, $iProfileId, $bSendEmailNotification);
  356. }
  357. /**
  358. * Change profile status to 'Suspended'
  359. */
  360. public function suspend($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  361. {
  362. return $this->changeStatus(BX_PROFILE_STATUS_SUSPENDED, 'suspend', $iAction, $iProfileId, $bSendEmailNotification);
  363. }
  364. protected function changeStatus($sStatus, $sAlertActionName, $iAction, $iProfileId = 0, $bSendEmailNotification = true)
  365. {
  366. if (!$iProfileId)
  367. $iProfileId = $this->_iProfileID;
  368. // get account and profile objects
  369. $oProfile = BxDolProfile::getInstance($iProfileId);
  370. $oAccount = $oProfile->getAccountObject();
  371. if (!$oProfile || !$oAccount)
  372. return false;
  373. // change status
  374. if (!$this->_oQuery->changeStatus($iProfileId, $sStatus))
  375. return false;
  376. // alert about status changing
  377. bx_alert('profile', $sAlertActionName, $iProfileId, false, array('action' => $iAction));
  378. // send email to member about status change
  379. if ($bSendEmailNotification)
  380. sendMailTemplate('t_ChangeStatus' . ucfirst($sStatus), $oAccount->id(), $iProfileId, array('status' => $sStatus), BX_EMAIL_SYSTEM);
  381. return true;
  382. }
  383. /**
  384. * Display informer message if it is possible to switch to this profile
  385. */
  386. public function checkSwitchToProfile($oTemplate = null, $iViewerAccountId = false, $iViewerProfileId = false)
  387. {
  388. if (false === $iViewerAccountId)
  389. $iViewerAccountId = getLoggedId();
  390. if (false === $iViewerProfileId)
  391. $iViewerProfileId = bx_get_logged_profile_id();
  392. if (!$iViewerAccountId || !$iViewerProfileId)
  393. return;
  394. if ($iViewerAccountId != $this->getAccountId() || $iViewerProfileId == $this->id())
  395. return;
  396. $oInformer = BxDolInformer::getInstance($oTemplate);
  397. if ($oInformer)
  398. $oInformer->add('sys-switch-profile-context', _t('_sys_txt_account_profile_context_change_suggestion', BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher', array('switch_to_profile' => $this->id()))), BX_INFORMER_INFO);
  399. }
  400. /**
  401. * Add permament messages.
  402. */
  403. public function addInformerPermanentMessages ($oInformer)
  404. {
  405. $aInfo = $this->getInfo();
  406. $aProfiles = $this->_oQuery->getProfilesByAccount($aInfo['account_id']);
  407. if ($aInfo['type'] == 'system' && count($aProfiles) == 1) {
  408. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher');
  409. $oInformer->add('sys-account-profile-system', _t('_sys_txt_account_profile_system', $sUrl), BX_INFORMER_ALERT);
  410. }
  411. }
  412. }
  413. /** @} */