BxDolProfile.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. protected $_iProfileID;
  15. protected $_oQuery;
  16. /**
  17. * Constructor
  18. */
  19. protected function __construct ($iProfileId) {
  20. $iProfileId = (int)$iProfileId;
  21. $sClass = get_class($this) . '_' . $iProfileId;
  22. if (isset($GLOBALS['bxDolClasses'][$sClass]))
  23. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  24. parent::__construct();
  25. $this->_iProfileID = $iProfileId; // since constructor is protected $iProfileId is always valid
  26. $this->_oQuery = BxDolProfileQuery::getInstance();
  27. }
  28. /**
  29. * Prevent cloning the instance
  30. */
  31. public function __clone() {
  32. $sClass = get_class($this) . '_' . $this->_iProfileID;
  33. if (isset($GLOBALS['bxDolClasses'][$sClass]))
  34. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  35. }
  36. /**
  37. * Get singleton instance of Account Profile by account id
  38. */
  39. public static function getInstanceAccountProfile($iAccountId = false) {
  40. if (!$iAccountId)
  41. $iAccountId = getLoggedId();
  42. $oQuery = BxDolProfileQuery::getInstance();
  43. $aProfile = $oQuery->getProfileByContentTypeAccount($iAccountId, 'system', $iAccountId);
  44. if (!$aProfile)
  45. return false;
  46. return self::getInstance($aProfile['id']);
  47. }
  48. /**
  49. * Get singleton instance of Profile by account id, content id and type
  50. */
  51. public static function getInstanceByContentTypeAccount($iContent, $sType, $iAccountId = false) {
  52. if (!$iAccountId)
  53. $iAccountId = getLoggedId();
  54. $oQuery = BxDolProfileQuery::getInstance();
  55. $aProfile = $oQuery->getProfileByContentTypeAccount($iContent, $sType, $iAccountId);
  56. if (!$aProfile)
  57. return false;
  58. return self::getInstance($aProfile['id']);
  59. }
  60. /**
  61. * Get singleton instance of Profile by content id and type
  62. */
  63. public static function getInstanceByContentAndType($iContent, $sType) {
  64. $oQuery = BxDolProfileQuery::getInstance();
  65. $aProfile = $oQuery->getProfileByContentAndType($iContent, $sType);
  66. if (!$aProfile)
  67. return false;
  68. return self::getInstance($aProfile['id']);
  69. }
  70. /**
  71. * Get singleton instance of Profile by profile id
  72. */
  73. public static function getInstance($mixedProfileId = false) {
  74. if (!$mixedProfileId) {
  75. $oQuery = BxDolProfileQuery::getInstance();
  76. $mixedProfileId = $oQuery->getCurrentProfileByAccount(getLoggedId());
  77. }
  78. $iProfileId = self::getID($mixedProfileId);
  79. if (!$iProfileId)
  80. return false;
  81. $sClass = __CLASS__ . '_' . $iProfileId;
  82. if (!isset($GLOBALS['bxDolClasses'][$sClass]))
  83. $GLOBALS['bxDolClasses'][$sClass] = new BxDolProfile($iProfileId);
  84. return $GLOBALS['bxDolClasses'][$sClass];
  85. }
  86. /**
  87. * Get profile id
  88. */
  89. public function id() {
  90. return $this->_oQuery->getIdById($this->_iProfileID);
  91. }
  92. /**
  93. * Get account id associated with the profile
  94. */
  95. public function getAccountId() {
  96. $aInfo = $this->getInfo();
  97. return $aInfo['account_id'];
  98. }
  99. /**
  100. * Get account object associated with the profile
  101. */
  102. public function getAccountObject() {
  103. bx_import('BxDolAccount');
  104. return BxDolAccount::getInstance($this->getAccountId());
  105. }
  106. /**
  107. * Get content id associated with the profile
  108. */
  109. public function getContentId() {
  110. $aInfo = $this->getInfo();
  111. return $aInfo['content_id'];
  112. }
  113. /**
  114. * Check if profile status is active
  115. */
  116. public function isActive($iProfileId = false) {
  117. return BX_PROFILE_STATUS_ACTIVE == $this->getStatus($iProfileId);
  118. }
  119. /**
  120. * Get profile status
  121. */
  122. public function getStatus($iProfileId = false) {
  123. $aInfo = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  124. return $aInfo['status'];
  125. }
  126. /**
  127. * Get profile info
  128. */
  129. public function getInfo($iProfileId = 0) {
  130. return $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  131. }
  132. /**
  133. * Validate profile id.
  134. * @param $s - profile id
  135. * @return profile id or false if profile was not found
  136. */
  137. static public function getID($s) {
  138. $iId = BxDolProfileQuery::getInstance()->getIdById((int)$s);
  139. return $iId ? $iId : false;
  140. }
  141. /**
  142. * Get name to display in thumbnail
  143. */
  144. public function getDisplayName($iProfileId = 0) {
  145. $aInfo = $this->getInfo($iProfileId);
  146. return BxDolService::call($aInfo['type'], 'profile_name', array($aInfo['content_id']));
  147. }
  148. /**
  149. * Get profile url
  150. */
  151. public function getUrl($iProfileId = 0) {
  152. $aInfo = $this->getInfo($iProfileId);
  153. return BxDolService::call($aInfo['type'], 'profile_url', array($aInfo['content_id']));
  154. }
  155. /**
  156. * Get profile unit
  157. */
  158. public function getUnit($iProfileId = 0) {
  159. $aInfo = $this->getInfo($iProfileId);
  160. return BxDolService::call($aInfo['type'], 'profile_unit', array($aInfo['content_id']));
  161. }
  162. /**
  163. * Get thumbnail url
  164. */
  165. public function getAvatar($iProfileId = 0) {
  166. $aInfo = $this->getInfo($iProfileId);
  167. return BxDolService::call($aInfo['type'], 'profile_avatar', array($aInfo['content_id']));
  168. }
  169. /**
  170. * Get thumbnail url
  171. */
  172. public function getThumb($iProfileId = 0) {
  173. $aInfo = $this->getInfo($iProfileId);
  174. return BxDolService::call($aInfo['type'], 'profile_thumb', array($aInfo['content_id']));
  175. }
  176. /**
  177. * Get icon url
  178. */
  179. public function getIcon($iProfileId = 0) {
  180. $aInfo = $this->getInfo($iProfileId);
  181. return BxDolService::call($aInfo['type'], 'profile_icon', array($aInfo['content_id']));
  182. }
  183. /**
  184. * get profile edit page url
  185. */
  186. public function getEditUrl($iProfileId = 0) {
  187. $aInfo = $this->getInfo($iProfileId);
  188. return BxDolService::call($aInfo['type'], 'profile_edit_url', array($aInfo['content_id']));
  189. }
  190. /**
  191. * Delete profile.
  192. * @param $ID - optional profile id to delete
  193. * @param $bForceDelete - force deletetion is case of account profile deletion
  194. * @return false on error, or true on success
  195. */
  196. function delete($ID = false, $bForceDelete = false) {
  197. $ID = (int)$ID;
  198. if (!$ID)
  199. $ID = $this->_iProfileID;
  200. $aProfileInfo = $this->_oQuery->getInfoById($ID);
  201. if (!$aProfileInfo)
  202. return false;
  203. // delete system profiles (accounts) is not allowed, instead - delete whole account
  204. if (!$bForceDelete && 'system' == $aProfileInfo['type'])
  205. return false;
  206. // switch profile context if deleted profile is active profile context
  207. bx_import('BxDolAccount');
  208. $oAccount = BxDolAccount::getInstance ($aProfileInfo['account_id']);
  209. $aAccountInfo = $oAccount->getInfo();
  210. if (!$bForceDelete && $ID == $aAccountInfo['profile_id']) {
  211. $oProfileAccount = BxDolProfile::getInstanceAccountProfile($aProfileInfo['account_id']);
  212. $oAccount->updateProfileContext($oProfileAccount->id());
  213. }
  214. // create system event before deletion
  215. $isStopDeletion = false;
  216. bx_alert('profile', 'before_delete', $ID, 0, array('stop_deletion' => &$isStopDeletion));
  217. if ($isStopDeletion)
  218. return false;
  219. // delete associated comments
  220. bx_import('BxDolCmts');
  221. BxDolCmts::onAuthorDelete($ID);
  222. // delete connections
  223. bx_import('BxDolConnection');
  224. $oConn = BxDolConnection::getObjectInstance('sys_profiles_friends');
  225. $oConn->onDeleteInitiatorAndContent($ID);
  226. $oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  227. $oConn->onDeleteInitiatorAndContent($ID);
  228. // delete profile's acl levels
  229. bx_import('BxDolAcl');
  230. BxDolAcl::getInstance()->onProfileDelete($ID);
  231. // delete profile
  232. if (!$this->_oQuery->delete($ID))
  233. return false;
  234. // create system event
  235. bx_alert('profile', 'delete', $ID);
  236. // unset class instance to prevent creating the instance again
  237. $this->_iProfileID = 0;
  238. $sClass = get_class($this) . '_' . $ID;
  239. unset($GLOBALS['bxDolClasses'][$sClass]);
  240. return true;
  241. }
  242. /**
  243. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  244. * @param $iAccountId account id
  245. * @param $iContentId content id
  246. * @param $sStatus profile status
  247. * @param $sType profile content type
  248. * @return inserted profile's id
  249. */
  250. static public function add ($iAction, $iAccountId, $iContentId, $sStatus, $sType = 'system') {
  251. $oQuery = BxDolProfileQuery::getInstance();
  252. if (!($iProfileId = $oQuery->insertProfile ($iAccountId, $iContentId, $sStatus, $sType)))
  253. return false;
  254. bx_alert('profile', 'add', $iProfileId, 0, array('module' => $sType, 'content' => $iContentId, 'account' => $iAccountId, 'status' => $sStatus, 'action' => $iAction));
  255. return $iProfileId;
  256. }
  257. /**
  258. * Change profile status from 'Pending' to the next level - 'Active'
  259. */
  260. public function approve($iAction, $iProfileId = 0) {
  261. return $this->changeStatus(BX_PROFILE_STATUS_ACTIVE, 'approve', $iAction, $iProfileId);
  262. }
  263. /**
  264. * Change profile status to 'Pending'
  265. */
  266. public function disapprove($iAction, $iProfileId = 0) {
  267. return $this->changeStatus(BX_PROFILE_STATUS_PENDING, 'disapprove', $iAction, $iProfileId);
  268. }
  269. /**
  270. * Change profile status to 'Suspended'
  271. */
  272. public function suspend($iAction, $iProfileId = 0) {
  273. return $this->changeStatus(BX_PROFILE_STATUS_SUSPENDED, 'suspend', $iAction, $iProfileId);
  274. }
  275. protected function changeStatus($sStatus, $sAlertActionName, $iAction, $iProfileId = 0) {
  276. if (!$iProfileId)
  277. $iProfileId = $this->_iProfileID;
  278. // get account and profile objects
  279. $oProfile = BxDolProfile::getInstance($iProfileId);
  280. $oAccount = $oProfile->getAccountObject();
  281. if (!$oProfile || !$oAccount)
  282. return false;
  283. // change status
  284. if (!$this->_oQuery->changeStatus($iProfileId, $sStatus))
  285. return false;
  286. // alert about status changing
  287. bx_alert('profile', $sAlertActionName, $iProfileId, false, array('action' => $iAction));
  288. // send email to member about status change
  289. sendMailTemplate('t_ChangeStatus' . ucfirst($sStatus), $oAccount->id(), $iProfileId, array('status' => $sStatus), BX_EMAIL_SYSTEM);
  290. return true;
  291. }
  292. /**
  293. * Display informer message if it is possible to switch to this profile
  294. */
  295. public function checkSwitchToProfile($oTemplate = null, $iViewerAccountId = false, $iViewerProfileId = false) {
  296. if (false === $iViewerAccountId)
  297. $iViewerAccountId = getLoggedId();
  298. if (false === $iViewerProfileId)
  299. $iViewerProfileId = bx_get_logged_profile_id();
  300. if (!$iViewerAccountId || !$iViewerProfileId)
  301. return;
  302. if ($iViewerAccountId != $this->getAccountId() || $iViewerProfileId == $this->id())
  303. return;
  304. bx_import('BxDolInformer');
  305. bx_import('BxDolPermalinks');
  306. $oInformer = BxDolInformer::getInstance($oTemplate);
  307. if ($oInformer)
  308. $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);
  309. }
  310. }
  311. /** @} */