BxDolProfile.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolProfileQuery');
  10. define('BX_PROFILE_ACTION_AUTO', 0); ///< automatic action without any checking
  11. define('BX_PROFILE_ACTION_MANUAL', 1); ///< manual action performed by human
  12. define('BX_PROFILE_ACTION_ROBOT', 2); ///< action peformed by some robot based on some conditions
  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 profile id
  78. */
  79. public static function getInstance($mixedProfileId = false)
  80. {
  81. if (!$mixedProfileId) {
  82. $oQuery = BxDolProfileQuery::getInstance();
  83. $mixedProfileId = $oQuery->getCurrentProfileByAccount(getLoggedId());
  84. }
  85. $iProfileId = self::getID($mixedProfileId);
  86. if (!$iProfileId)
  87. return false;
  88. $sClass = __CLASS__ . '_' . $iProfileId;
  89. if (!isset($GLOBALS['bxDolClasses'][$sClass]))
  90. $GLOBALS['bxDolClasses'][$sClass] = new BxDolProfile($iProfileId);
  91. return $GLOBALS['bxDolClasses'][$sClass];
  92. }
  93. /**
  94. * Get profile id
  95. */
  96. public function id()
  97. {
  98. return $this->_oQuery->getIdById($this->_iProfileID);
  99. }
  100. /**
  101. * Get account id associated with the profile
  102. */
  103. public function getAccountId()
  104. {
  105. $aInfo = $this->getInfo();
  106. return $aInfo['account_id'];
  107. }
  108. /**
  109. * Get account object associated with the profile
  110. */
  111. public function getAccountObject()
  112. {
  113. bx_import('BxDolAccount');
  114. return BxDolAccount::getInstance($this->getAccountId());
  115. }
  116. /**
  117. * Get content id associated with the profile
  118. */
  119. public function getContentId()
  120. {
  121. $aInfo = $this->getInfo();
  122. return $aInfo['content_id'];
  123. }
  124. /**
  125. * Check if profile status is active
  126. */
  127. public function isActive($iProfileId = false)
  128. {
  129. return BX_PROFILE_STATUS_ACTIVE == $this->getStatus($iProfileId);
  130. }
  131. /**
  132. * Get profile status
  133. */
  134. public function getStatus($iProfileId = false)
  135. {
  136. $aInfo = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  137. return $aInfo['status'];
  138. }
  139. /**
  140. * Get profile info
  141. */
  142. public function getInfo($iProfileId = 0)
  143. {
  144. return $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  145. }
  146. /**
  147. * Validate profile id.
  148. * @param $s - profile id
  149. * @return profile id or false if profile was not found
  150. */
  151. static public function getID($s)
  152. {
  153. $iId = BxDolProfileQuery::getInstance()->getIdById((int)$s);
  154. return $iId ? $iId : false;
  155. }
  156. /**
  157. * Get name to display in thumbnail
  158. */
  159. public function getDisplayName($iProfileId = 0)
  160. {
  161. $aInfo = $this->getInfo($iProfileId);
  162. return BxDolService::call($aInfo['type'], 'profile_name', array($aInfo['content_id']));
  163. }
  164. /**
  165. * Get profile url
  166. */
  167. public function getUrl($iProfileId = 0)
  168. {
  169. $aInfo = $this->getInfo($iProfileId);
  170. return BxDolService::call($aInfo['type'], 'profile_url', array($aInfo['content_id']));
  171. }
  172. /**
  173. * Get profile unit
  174. */
  175. public function getUnit($iProfileId = 0)
  176. {
  177. $aInfo = $this->getInfo($iProfileId);
  178. return BxDolService::call($aInfo['type'], 'profile_unit', array($aInfo['content_id']));
  179. }
  180. /**
  181. * Get thumbnail url
  182. */
  183. public function getAvatar($iProfileId = 0)
  184. {
  185. $aInfo = $this->getInfo($iProfileId);
  186. return BxDolService::call($aInfo['type'], 'profile_avatar', array($aInfo['content_id']));
  187. }
  188. /**
  189. * Get thumbnail url
  190. */
  191. public function getThumb($iProfileId = 0)
  192. {
  193. $aInfo = $this->getInfo($iProfileId);
  194. return BxDolService::call($aInfo['type'], 'profile_thumb', array($aInfo['content_id']));
  195. }
  196. /**
  197. * Get icon url
  198. */
  199. public function getIcon($iProfileId = 0)
  200. {
  201. $aInfo = $this->getInfo($iProfileId);
  202. return BxDolService::call($aInfo['type'], 'profile_icon', array($aInfo['content_id']));
  203. }
  204. /**
  205. * get profile edit page url
  206. */
  207. public function getEditUrl($iProfileId = 0)
  208. {
  209. $aInfo = $this->getInfo($iProfileId);
  210. return BxDolService::call($aInfo['type'], 'profile_edit_url', array($aInfo['content_id']));
  211. }
  212. /**
  213. * Delete profile.
  214. * @param $ID - optional profile id to delete
  215. * @param $bForceDelete - force deletetion is case of account profile deletion
  216. * @return false on error, or true on success
  217. */
  218. function delete($ID = false, $bForceDelete = false)
  219. {
  220. $ID = (int)$ID;
  221. if (!$ID)
  222. $ID = $this->_iProfileID;
  223. $aProfileInfo = $this->_oQuery->getInfoById($ID);
  224. if (!$aProfileInfo)
  225. return false;
  226. // delete system profiles (accounts) is not allowed, instead - delete whole account
  227. if (!$bForceDelete && 'system' == $aProfileInfo['type'])
  228. return false;
  229. // switch profile context if deleted profile is active profile context
  230. bx_import('BxDolAccount');
  231. $oAccount = BxDolAccount::getInstance ($aProfileInfo['account_id']);
  232. $aAccountInfo = $oAccount->getInfo();
  233. if (!$bForceDelete && $ID == $aAccountInfo['profile_id']) {
  234. $oProfileAccount = BxDolProfile::getInstanceAccountProfile($aProfileInfo['account_id']);
  235. $oAccount->updateProfileContext($oProfileAccount->id());
  236. }
  237. // create system event before deletion
  238. $isStopDeletion = false;
  239. bx_alert('profile', 'before_delete', $ID, 0, array('stop_deletion' => &$isStopDeletion));
  240. if ($isStopDeletion)
  241. return false;
  242. // delete associated comments
  243. bx_import('BxDolCmts');
  244. BxDolCmts::onAuthorDelete($ID);
  245. // delete connections
  246. bx_import('BxDolConnection');
  247. $oConn = BxDolConnection::getObjectInstance('sys_profiles_friends');
  248. $oConn->onDeleteInitiatorAndContent($ID);
  249. $oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  250. $oConn->onDeleteInitiatorAndContent($ID);
  251. // delete profile's acl levels
  252. bx_import('BxDolAcl');
  253. BxDolAcl::getInstance()->onProfileDelete($ID);
  254. // delete profile
  255. if (!$this->_oQuery->delete($ID))
  256. return false;
  257. // create system event
  258. bx_alert('profile', 'delete', $ID);
  259. // unset class instance to prevent creating the instance again
  260. $this->_iProfileID = 0;
  261. $sClass = get_class($this) . '_' . $ID;
  262. unset($GLOBALS['bxDolClasses'][$sClass]);
  263. return true;
  264. }
  265. /**
  266. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  267. * @param $iAccountId account id
  268. * @param $iContentId content id
  269. * @param $sStatus profile status
  270. * @param $sType profile content type
  271. * @return inserted profile's id
  272. */
  273. static public function add ($iAction, $iAccountId, $iContentId, $sStatus, $sType = 'system')
  274. {
  275. $oQuery = BxDolProfileQuery::getInstance();
  276. if (!($iProfileId = $oQuery->insertProfile ($iAccountId, $iContentId, $sStatus, $sType)))
  277. return false;
  278. bx_alert('profile', 'add', $iProfileId, 0, array('module' => $sType, 'content' => $iContentId, 'account' => $iAccountId, 'status' => $sStatus, 'action' => $iAction));
  279. return $iProfileId;
  280. }
  281. /**
  282. * Change profile status to 'Active'
  283. */
  284. public function activate($iAction, $iProfileId = 0)
  285. {
  286. $sStatus = $this->getStatus($iProfileId);
  287. return $this->changeStatus(BX_PROFILE_STATUS_ACTIVE, BX_PROFILE_STATUS_PENDING == $sStatus ? 'approve' : 'activate', $iAction, $iProfileId);
  288. }
  289. /**
  290. * Change profile status from 'Pending' to the next level - 'Active'
  291. */
  292. public function approve($iAction, $iProfileId = 0)
  293. {
  294. return $this->changeStatus(BX_PROFILE_STATUS_ACTIVE, 'approve', $iAction, $iProfileId);
  295. }
  296. /**
  297. * Change profile status to 'Pending'
  298. */
  299. public function disapprove($iAction, $iProfileId = 0)
  300. {
  301. return $this->changeStatus(BX_PROFILE_STATUS_PENDING, 'disapprove', $iAction, $iProfileId);
  302. }
  303. /**
  304. * Change profile status to 'Suspended'
  305. */
  306. public function suspend($iAction, $iProfileId = 0)
  307. {
  308. return $this->changeStatus(BX_PROFILE_STATUS_SUSPENDED, 'suspend', $iAction, $iProfileId);
  309. }
  310. protected function changeStatus($sStatus, $sAlertActionName, $iAction, $iProfileId = 0)
  311. {
  312. if (!$iProfileId)
  313. $iProfileId = $this->_iProfileID;
  314. // get account and profile objects
  315. $oProfile = BxDolProfile::getInstance($iProfileId);
  316. $oAccount = $oProfile->getAccountObject();
  317. if (!$oProfile || !$oAccount)
  318. return false;
  319. // change status
  320. if (!$this->_oQuery->changeStatus($iProfileId, $sStatus))
  321. return false;
  322. // alert about status changing
  323. bx_alert('profile', $sAlertActionName, $iProfileId, false, array('action' => $iAction));
  324. // send email to member about status change
  325. sendMailTemplate('t_ChangeStatus' . ucfirst($sStatus), $oAccount->id(), $iProfileId, array('status' => $sStatus), BX_EMAIL_SYSTEM);
  326. return true;
  327. }
  328. /**
  329. * Display informer message if it is possible to switch to this profile
  330. */
  331. public function checkSwitchToProfile($oTemplate = null, $iViewerAccountId = false, $iViewerProfileId = false)
  332. {
  333. if (false === $iViewerAccountId)
  334. $iViewerAccountId = getLoggedId();
  335. if (false === $iViewerProfileId)
  336. $iViewerProfileId = bx_get_logged_profile_id();
  337. if (!$iViewerAccountId || !$iViewerProfileId)
  338. return;
  339. if ($iViewerAccountId != $this->getAccountId() || $iViewerProfileId == $this->id())
  340. return;
  341. bx_import('BxDolInformer');
  342. bx_import('BxDolPermalinks');
  343. $oInformer = BxDolInformer::getInstance($oTemplate);
  344. if ($oInformer)
  345. $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);
  346. }
  347. /**
  348. * Add permament messages.
  349. */
  350. public function addInformerPermanentMessages ($oInformer)
  351. {
  352. $aInfo = $this->getInfo();
  353. $aProfiles = $this->_oQuery->getProfilesByAccount($aInfo['account_id']);
  354. if ($aInfo['type'] == 'system' && count($aProfiles) == 1) {
  355. bx_import('BxDolPermalinks');
  356. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher');
  357. $oInformer->add('sys-account-profile-system', _t('_sys_txt_account_profile_system', $sUrl), BX_INFORMER_ALERT);
  358. }
  359. }
  360. }
  361. /** @} */