BxDolProfile.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. var $_iProfileID;
  15. var $_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 content id associated with the profile
  101. */
  102. public function getContentId() {
  103. $aInfo = $this->getInfo();
  104. return $aInfo['content_id'];
  105. }
  106. /**
  107. * Check if profile status is active
  108. */
  109. public function isActive($iProfileId = false) {
  110. return BX_PROFILE_STATUS_ACTIVE == $this->getStatus($iProfileId);
  111. }
  112. /**
  113. * Get profile status
  114. */
  115. public function getStatus($iProfileId = false) {
  116. $aInfo = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  117. return $aInfo['status'];
  118. }
  119. /**
  120. * Get profile info
  121. */
  122. public function getInfo($iProfileId = 0) {
  123. return $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  124. }
  125. /**
  126. * Validate profile id.
  127. * @param $s - profile id
  128. * @return profile id or false if profile was not found
  129. */
  130. static public function getID($s) {
  131. $iId = BxDolProfileQuery::getInstance()->getIdById((int)$s);
  132. return $iId ? $iId : false;
  133. }
  134. /**
  135. * Get name to display in thumbnail
  136. */
  137. public function getDisplayName($iProfileId = 0) {
  138. $aInfo = $this->getInfo($iProfileId);
  139. return BxDolService::call($aInfo['type'], 'profile_name', array($aInfo['content_id']));
  140. }
  141. /**
  142. * Get profile url
  143. */
  144. public function getUrl($iProfileId = 0) {
  145. $aInfo = $this->getInfo($iProfileId);
  146. return BxDolService::call($aInfo['type'], 'profile_url', array($aInfo['content_id']));
  147. }
  148. /**
  149. * Get profile unit
  150. */
  151. public function getUnit($iProfileId = 0) {
  152. $aInfo = $this->getInfo($iProfileId);
  153. return BxDolService::call($aInfo['type'], 'profile_unit', array($aInfo['content_id']));
  154. }
  155. /**
  156. * Get thumbnail url
  157. */
  158. public function getAvatar($iProfileId = 0) {
  159. $aInfo = $this->getInfo($iProfileId);
  160. return BxDolService::call($aInfo['type'], 'profile_avatar', array($aInfo['content_id']));
  161. }
  162. /**
  163. * Get thumbnail url
  164. */
  165. public function getThumb($iProfileId = 0) {
  166. $aInfo = $this->getInfo($iProfileId);
  167. return BxDolService::call($aInfo['type'], 'profile_thumb', array($aInfo['content_id']));
  168. }
  169. /**
  170. * Get icon url
  171. */
  172. public function getIcon($iProfileId = 0) {
  173. $aInfo = $this->getInfo($iProfileId);
  174. return BxDolService::call($aInfo['type'], 'profile_icon', array($aInfo['content_id']));
  175. }
  176. /**
  177. * get profile edit page url
  178. */
  179. public function getEditUrl($iProfileId = 0) {
  180. $aInfo = $this->getInfo($iProfileId);
  181. return BxDolService::call($aInfo['type'], 'profile_edit_url', array($aInfo['content_id']));
  182. }
  183. /**
  184. * Delete profile.
  185. * @param $ID - optional profile id to delete
  186. * @param $bForceDelete - force deletetion is case of account profile deletion
  187. * @return false on error, or true on success
  188. */
  189. function delete($ID = false, $bForceDelete = false) {
  190. $ID = (int)$ID;
  191. if (!$ID)
  192. $ID = $this->_iProfileID;
  193. $aProfileInfo = $this->_oQuery->getInfoById($ID);
  194. if (!$aProfileInfo)
  195. return false;
  196. // delete system profiles (accounts) is not allowed, instead - delete whole account
  197. if (!$bForceDelete && 'system' == $aProfileInfo['type'])
  198. return false;
  199. // switch profile context if deleted profile is active profile context
  200. bx_import('BxDolAccount');
  201. $oAccount = BxDolAccount::getInstance ($aProfileInfo['account_id']);
  202. $aAccountInfo = $oAccount->getInfo();
  203. if (!$bForceDelete && $ID == $aAccountInfo['profile_id']) {
  204. $oProfileAccount = BxDolProfile::getInstanceAccountProfile($aProfileInfo['account_id']);
  205. $oAccount->updateProfileContext($oProfileAccount->id());
  206. }
  207. // create system event before deletion
  208. $isStopDeletion = false;
  209. bx_alert('profile', 'before_delete', $ID, 0, array('stop_deletion' => &$isStopDeletion));
  210. if ($isStopDeletion)
  211. return false;
  212. // delete associated content
  213. // TODO: remake deletion of associated content
  214. $this->_oQuery->res("DELETE FROM `sys_acl_levels_members` WHERE `IDMember` = {$ID}");
  215. $this->_oQuery->res("DELETE FROM `sys_tags` WHERE `ObjID` = {$ID} AND `Type` = 'profile'");
  216. // delete profile
  217. if (!$this->_oQuery->delete($ID))
  218. return false;
  219. // create system event
  220. bx_alert('profile', 'delete', $ID);
  221. // unset class instance to prevent creating the instance again
  222. $this->_iProfileID = 0;
  223. $sClass = get_class($this) . '_' . $ID;
  224. unset($GLOBALS['bxDolClasses'][$sClass]);
  225. return true;
  226. }
  227. /**
  228. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  229. * @param $iAccountId account id
  230. * @param $iContentId content id
  231. * @param $sStatus profile status
  232. * @param $sType profile content type
  233. * @return inserted profile's id
  234. */
  235. static public function add ($iAction, $iAccountId, $iContentId, $sStatus, $sType = 'system') {
  236. $oQuery = BxDolProfileQuery::getInstance();
  237. if (!($iProfileId = $oQuery->insertProfile ($iAccountId, $iContentId, $sStatus, $sType)))
  238. return false;
  239. bx_alert('profile', 'add', $iProfileId, 0, array('module' => $sType, 'content' => $iContentId, 'account' => $iAccountId, 'status' => $sStatus, 'action' => $iAction));
  240. return $iProfileId;
  241. }
  242. /**
  243. * Change profile status from 'Pending' to the next level - 'Active'
  244. */
  245. public function approve($iAction, $iProfileId = 0) {
  246. if (!$iProfileId)
  247. $iProfileId = $this->_iProfileID;
  248. if (!$this->_oQuery->changeStatus($iProfileId, BX_PROFILE_STATUS_ACTIVE))
  249. return false;
  250. bx_alert('profile', 'approve', $iProfileId, false, array('action' => $iAction));
  251. // TODO: send email that profile is approved
  252. return true;
  253. }
  254. /**
  255. * Change profile status to 'Pending'
  256. */
  257. public function disapprove($iAction, $iProfileId = 0) {
  258. if (!$this->_oQuery->changeStatus((int)$iProfileId ? $iProfileId : $this->_iProfileID, BX_PROFILE_STATUS_PENDING))
  259. return false;
  260. bx_alert('profile', 'disapprove', $iProfileId ? $iProfileId : $this->_iProfileID, false, array('action' => $iAction));
  261. // TODO: send email that profile is disapproved ?
  262. return true;
  263. }
  264. /**
  265. * Change profile status to 'Suspended'
  266. */
  267. public function suspend($iAction, $iProfileId = 0) {
  268. if (!$this->_oQuery->changeStatus((int)$iProfileId ? $iProfileId : $this->_iProfileID, BX_PROFILE_STATUS_SUSPENDED))
  269. return false;
  270. bx_alert('profile', 'suspend', $iProfileId, false, array('action' => $iAction));
  271. // TODO: send email that profile is suspended
  272. return true;
  273. }
  274. /**
  275. * Display informer message if it is possible to switch to this profile
  276. */
  277. public function checkSwitchToProfile($oTemplate = null, $iViewerAccountId = false, $iViewerProfileId = false) {
  278. if (false === $iViewerAccountId)
  279. $iViewerAccountId = getLoggedId();
  280. if (false === $iViewerProfileId)
  281. $iViewerProfileId = bx_get_logged_profile_id();
  282. if (!$iViewerAccountId || !$iViewerProfileId)
  283. return;
  284. if ($iViewerAccountId != $this->getAccountId() || $iViewerProfileId == $this->id())
  285. return;
  286. bx_import('BxDolInformer');
  287. bx_import('BxDolPermalinks');
  288. $oInformer = BxDolInformer::getInstance($oTemplate);
  289. if ($oInformer)
  290. $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);
  291. }
  292. }
  293. /** @} */