BxDolProfile.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident 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 BxDol 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. * Get profile status
  141. */
  142. public function getStatus($iProfileId = false)
  143. {
  144. $aInfo = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  145. return $aInfo['status'];
  146. }
  147. /**
  148. * Get profile module name
  149. */
  150. public function getModule($iProfileId = false)
  151. {
  152. $aInfo = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  153. return $aInfo['type'];
  154. }
  155. /**
  156. * Get profile info
  157. */
  158. public function getInfo($iProfileId = 0)
  159. {
  160. return $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  161. }
  162. /**
  163. * Validate profile id.
  164. * @param $s - profile id
  165. * @return profile id or false if profile was not found
  166. */
  167. static public function getID($s)
  168. {
  169. $iId = BxDolProfileQuery::getInstance()->getIdById((int)$s);
  170. return $iId ? $iId : false;
  171. }
  172. /**
  173. * Get name to display in thumbnail
  174. */
  175. public function getDisplayName($iProfileId = 0)
  176. {
  177. $aInfo = $this->getInfo($iProfileId);
  178. return BxDolService::call($aInfo['type'], 'profile_name', array($aInfo['content_id']));
  179. }
  180. /**
  181. * Get profile url
  182. */
  183. public function getUrl($iProfileId = 0)
  184. {
  185. $aInfo = $this->getInfo($iProfileId);
  186. return BxDolService::call($aInfo['type'], 'profile_url', array($aInfo['content_id']));
  187. }
  188. /**
  189. * Get profile unit
  190. */
  191. public function getUnit($iProfileId = 0)
  192. {
  193. $aInfo = $this->getInfo($iProfileId);
  194. return BxDolService::call($aInfo['type'], 'profile_unit', array($aInfo['content_id']));
  195. }
  196. /**
  197. * Get picture url
  198. */
  199. public function getPicture($iProfileId = 0)
  200. {
  201. $aInfo = $this->getInfo($iProfileId);
  202. return BxDolService::call($aInfo['type'], 'profile_picture', array($aInfo['content_id']));
  203. }
  204. /**
  205. * Get avatar url
  206. */
  207. public function getAvatar($iProfileId = 0)
  208. {
  209. $aInfo = $this->getInfo($iProfileId);
  210. return BxDolService::call($aInfo['type'], 'profile_avatar', array($aInfo['content_id']));
  211. }
  212. /**
  213. * Get thumbnail url
  214. */
  215. public function getThumb($iProfileId = 0)
  216. {
  217. $aInfo = $this->getInfo($iProfileId);
  218. return BxDolService::call($aInfo['type'], 'profile_thumb', array($aInfo['content_id']));
  219. }
  220. /**
  221. * Get icon url
  222. */
  223. public function getIcon($iProfileId = 0)
  224. {
  225. $aInfo = $this->getInfo($iProfileId);
  226. return BxDolService::call($aInfo['type'], 'profile_icon', array($aInfo['content_id']));
  227. }
  228. /**
  229. * Get module icon
  230. */
  231. public function getIconModule($iProfileId = 0)
  232. {
  233. $aInfo = $this->getInfo($iProfileId);
  234. return BxDolService::call($aInfo['type'], 'module_icon');
  235. }
  236. /**
  237. * get profile edit page url
  238. */
  239. public function getEditUrl($iProfileId = 0)
  240. {
  241. $aInfo = $this->getInfo($iProfileId);
  242. return BxDolService::call($aInfo['type'], 'profile_edit_url', array($aInfo['content_id']));
  243. }
  244. /**
  245. * Delete profile.
  246. * @param $ID - optional profile id to delete
  247. * @param $bDeleteWithContent - delete profile with all its contents
  248. * @param $bForceDelete - force deletetion is case of account profile deletion
  249. * @return false on error, or true on success
  250. */
  251. function delete($ID = false, $bDeleteWithContent = false, $bForceDelete = false)
  252. {
  253. $ID = (int)$ID;
  254. if (!$ID)
  255. $ID = $this->_iProfileID;
  256. $aProfileInfo = $this->_oQuery->getInfoById($ID);
  257. if (!$aProfileInfo)
  258. return false;
  259. // delete system profiles (accounts) is not allowed, instead - delete whole account
  260. if (!$bForceDelete && 'system' == $aProfileInfo['type'])
  261. return false;
  262. // delete actual profile
  263. if ($sErrorMsg = BxDolService::call($aProfileInfo['type'] , 'delete_entity_service', array($aProfileInfo['content_id'], $bDeleteWithContent)))
  264. return false;
  265. // switch profile context if deleted profile is active profile context
  266. $oAccount = BxDolAccount::getInstance ($aProfileInfo['account_id']);
  267. $aAccountInfo = $oAccount->getInfo();
  268. if (!$bForceDelete && $ID == $aAccountInfo['profile_id']) {
  269. $aProfiles = $oAccount->getProfiles();
  270. $oProfileAccount = BxDolProfile::getInstanceAccountProfile($aProfileInfo['account_id']);
  271. // unset deleted profile and account profile
  272. unset($aProfiles[$ID]);
  273. unset($aProfiles[$oProfileAccount->id()]);
  274. if ($aProfiles) {
  275. // try to use another profile
  276. reset($aProfiles);
  277. $iProfileId = key($aProfiles);
  278. }
  279. else {
  280. // if no profiles exist, use account profile
  281. $iProfileId = $oProfileAccount->id();
  282. }
  283. $oAccount->updateProfileContext($iProfileId);
  284. }
  285. // create system event before deletion
  286. $isStopDeletion = false;
  287. bx_alert('profile', 'before_delete', $ID, 0, array('delete_with_content' => $bDeleteWithContent, 'stop_deletion' => &$isStopDeletion));
  288. if ($isStopDeletion)
  289. return false;
  290. // delete associated comments
  291. if($bDeleteWithContent)
  292. BxDolCmts::onAuthorDelete($ID);
  293. // delete connections
  294. $oConn = BxDolConnection::getObjectInstance('sys_profiles_friends');
  295. $oConn->onDeleteInitiatorAndContent($ID);
  296. $oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  297. $oConn->onDeleteInitiatorAndContent($ID);
  298. // delete profile's acl levels
  299. BxDolAcl::getInstance()->onProfileDelete($ID);
  300. // delete profile
  301. if (!$this->_oQuery->delete($ID))
  302. return false;
  303. // create system event
  304. bx_alert('profile', 'delete', $ID, 0, array('delete_with_content' => $bDeleteWithContent));
  305. // unset class instance to prevent creating the instance again
  306. $this->_iProfileID = 0;
  307. $sClass = get_class($this) . '_' . $ID;
  308. unset($GLOBALS['bxDolClasses'][$sClass]);
  309. return true;
  310. }
  311. /**
  312. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  313. * @param $iAccountId account id
  314. * @param $iContentId content id
  315. * @param $sStatus profile status
  316. * @param $sType profile content type
  317. * @return inserted profile's id
  318. */
  319. static public function add ($iAction, $iAccountId, $iContentId, $sStatus, $sType = 'system')
  320. {
  321. $oQuery = BxDolProfileQuery::getInstance();
  322. if (!($iProfileId = $oQuery->insertProfile ($iAccountId, $iContentId, $sStatus, $sType)))
  323. return false;
  324. bx_alert('profile', 'add', $iProfileId, 0, array('module' => $sType, 'content' => $iContentId, 'account' => $iAccountId, 'status' => $sStatus, 'action' => $iAction));
  325. return $iProfileId;
  326. }
  327. /**
  328. * Change profile status to 'Active'
  329. */
  330. public function activate($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  331. {
  332. $sStatus = $this->getStatus($iProfileId);
  333. return $this->changeStatus(BX_PROFILE_STATUS_ACTIVE, BX_PROFILE_STATUS_PENDING == $sStatus ? 'approve' : 'activate', $iAction, $iProfileId, $bSendEmailNotification);
  334. }
  335. /**
  336. * Change profile status from 'Pending' to the next level - 'Active'
  337. */
  338. public function approve($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  339. {
  340. return $this->changeStatus(BX_PROFILE_STATUS_ACTIVE, 'approve', $iAction, $iProfileId, $bSendEmailNotification);
  341. }
  342. /**
  343. * Change profile status to 'Pending'
  344. */
  345. public function disapprove($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  346. {
  347. return $this->changeStatus(BX_PROFILE_STATUS_PENDING, 'disapprove', $iAction, $iProfileId, $bSendEmailNotification);
  348. }
  349. /**
  350. * Change profile status to 'Suspended'
  351. */
  352. public function suspend($iAction, $iProfileId = 0, $bSendEmailNotification = true)
  353. {
  354. return $this->changeStatus(BX_PROFILE_STATUS_SUSPENDED, 'suspend', $iAction, $iProfileId, $bSendEmailNotification);
  355. }
  356. protected function changeStatus($sStatus, $sAlertActionName, $iAction, $iProfileId = 0, $bSendEmailNotification = true)
  357. {
  358. if (!$iProfileId)
  359. $iProfileId = $this->_iProfileID;
  360. // get account and profile objects
  361. $oProfile = BxDolProfile::getInstance($iProfileId);
  362. $oAccount = $oProfile->getAccountObject();
  363. if (!$oProfile || !$oAccount)
  364. return false;
  365. // change status
  366. if (!$this->_oQuery->changeStatus($iProfileId, $sStatus))
  367. return false;
  368. // alert about status changing
  369. bx_alert('profile', $sAlertActionName, $iProfileId, false, array('action' => $iAction));
  370. // send email to member about status change
  371. if ($bSendEmailNotification)
  372. sendMailTemplate('t_ChangeStatus' . ucfirst($sStatus), $oAccount->id(), $iProfileId, array('status' => $sStatus), BX_EMAIL_SYSTEM);
  373. return true;
  374. }
  375. /**
  376. * Display informer message if it is possible to switch to this profile
  377. */
  378. public function checkSwitchToProfile($oTemplate = null, $iViewerAccountId = false, $iViewerProfileId = false)
  379. {
  380. if (false === $iViewerAccountId)
  381. $iViewerAccountId = getLoggedId();
  382. if (false === $iViewerProfileId)
  383. $iViewerProfileId = bx_get_logged_profile_id();
  384. if (!$iViewerAccountId || !$iViewerProfileId)
  385. return;
  386. if ($iViewerAccountId != $this->getAccountId() || $iViewerProfileId == $this->id())
  387. return;
  388. $oInformer = BxDolInformer::getInstance($oTemplate);
  389. if ($oInformer)
  390. $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);
  391. }
  392. /**
  393. * Add permament messages.
  394. */
  395. public function addInformerPermanentMessages ($oInformer)
  396. {
  397. $aInfo = $this->getInfo();
  398. $aProfiles = $this->_oQuery->getProfilesByAccount($aInfo['account_id']);
  399. if ($aInfo['type'] == 'system' && count($aProfiles) == 1) {
  400. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher');
  401. $oInformer->add('sys-account-profile-system', _t('_sys_txt_account_profile_system', $sUrl), BX_INFORMER_ALERT);
  402. }
  403. }
  404. }
  405. /** @} */