BxDolProfile.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 {
  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 the class by account id
  38. */
  39. public static function getInstanceAccountProfile($iAccountId = false) {
  40. $oQuery = BxDolProfileQuery::getInstance();
  41. $aProfile = $oQuery->getAccountProfile($iAccountId ? $iAccountId : getLoggedId());
  42. if (!$aProfile)
  43. return false;
  44. return self::getInstance($aProfile['id']);
  45. }
  46. /**
  47. * Get singleton instance of the class by account id, content id and type
  48. */
  49. public static function getInstanceByContentTypeAccount($iContent, $sType, $iAccountId = false) {
  50. $oQuery = BxDolProfileQuery::getInstance();
  51. $aProfile = $oQuery->getProfileByContentTypeAccount($iContent, $sType, $iAccountId ? $iAccountId : getLoggedId());
  52. if (!$aProfile)
  53. return false;
  54. return self::getInstance($aProfile['id']);
  55. }
  56. /**
  57. * Get singleton instance of the class by profile id
  58. */
  59. public static function getInstance($mixedProfileId = false) {
  60. if (!$mixedProfileId) {
  61. $oQuery = BxDolProfileQuery::getInstance();
  62. $mixedProfileId = $oQuery->getCurrentProfileByAccount(getLoggedId());
  63. }
  64. $iProfileId = self::getID($mixedProfileId);
  65. if (!$iProfileId)
  66. return false;
  67. $sClass = __CLASS__ . '_' . $iProfileId;
  68. if (!isset($GLOBALS['bxDolClasses'][$sClass]))
  69. $GLOBALS['bxDolClasses'][$sClass] = new BxDolProfile($iProfileId);
  70. return $GLOBALS['bxDolClasses'][$sClass];
  71. }
  72. /**
  73. * Get profile id
  74. */
  75. public function id() {
  76. return $this->_oQuery->getIdById($this->_iProfileID);
  77. }
  78. /**
  79. * Get account id associated with the profile
  80. */
  81. public function getAccountId() {
  82. $aInfo = $this->getInfo();
  83. return $aInfo['account_id'];
  84. }
  85. /**
  86. * Check if profile status is active
  87. */
  88. public function isActive($iProfileId = false) {
  89. $aInfo = $this->getStatus((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  90. return 'active' == $aInfo['status'];
  91. }
  92. /**
  93. * Get profile status
  94. */
  95. public function getStatus($iProfileId = false) {
  96. $aInfo = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  97. return $aInfo['status'];
  98. }
  99. /**
  100. * Get profile info
  101. */
  102. public function getInfo($iProfileId = 0) {
  103. return $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
  104. }
  105. /**
  106. * Validate profile id.
  107. * @param $s - profile id
  108. * @return profile id or false if profile was not found
  109. */
  110. static public function getID($s) {
  111. $iId = BxDolProfileQuery::getInstance()->getIdById((int)$s);
  112. return $iId ? $iId : false;
  113. }
  114. /**
  115. * Get name to display in thumbnail
  116. */
  117. public function getDisplayName($iProfileId = 0) {
  118. $aInfo = $this->getInfo($iProfileId);
  119. return BxDolService::call($aInfo['type'], 'profile_name', array($aInfo['content_id']));
  120. }
  121. /**
  122. * Get profile url
  123. */
  124. public function getUrl($iProfileId = 0) {
  125. $aInfo = $this->getInfo($iProfileId);
  126. return BxDolService::call($aInfo['type'], 'profile_url', array($aInfo['content_id']));
  127. }
  128. /**
  129. * Get profile unit
  130. */
  131. public function getUnit($iProfileId = 0) {
  132. $aInfo = $this->getInfo($iProfileId);
  133. return BxDolService::call($aInfo['type'], 'profile_thumb', array($aInfo['content_id']));
  134. }
  135. /**
  136. * Get thumbnail
  137. */
  138. public function getThumb($iProfileId = 0) {
  139. $aInfo = $this->getInfo($iProfileId);
  140. return BxDolService::call($aInfo['type'], 'profile_thumb', array($aInfo['content_id']));
  141. }
  142. /**
  143. * Delete profile.
  144. */
  145. function delete($ID = false) {
  146. $ID = (int)$ID;
  147. if (!$ID)
  148. $ID = $this->_iProfileID;
  149. $aProfileInfo = $this->_oQuery->getInfoById($ID);
  150. if (!$aProfileInfo)
  151. return false;
  152. // create system event before deletion
  153. $isStopDeletion = false;
  154. bx_alert('profile', 'before_delete', $ID, 0, array('stop_deletion' => &$isStopDeletion));
  155. if ($isStopDeletion)
  156. return false;
  157. // delete associated content
  158. // TODO: remake deletion of associated content
  159. $this->_oQuery->res("DELETE FROM `sys_friend_list` WHERE ID = {$ID} OR Profile = {$ID}");
  160. $this->_oQuery->res("DELETE FROM `sys_acl_levels_members` WHERE `IDMember` = {$ID}");
  161. $this->_oQuery->res("DELETE FROM `sys_tags` WHERE `ObjID` = {$ID} AND `Type` = 'profile'");
  162. // delete profile
  163. if (!$this->_oQuery->delete($ID))
  164. return false;
  165. // create system event
  166. bx_alert('profile', 'delete', $ID);
  167. // unset class instance to prevent creating the instance again
  168. $this->_iProfileID = 0;
  169. $sClass = get_class($this) . '_' . $ID;
  170. unset($GLOBALS['bxDolClasses'][$sClass]);
  171. return true;
  172. }
  173. /**
  174. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  175. * @param $iAccountId account id
  176. * @param $iContentId content id
  177. * @param $sStatus profile status
  178. * @param $sType profile content type
  179. * @return inserted profile's id
  180. */
  181. static public function add ($iAction, $iAccountId, $iContentId, $sStatus, $sType = 'system') {
  182. $oQuery = BxDolProfileQuery::getInstance();
  183. if (!($iProfileId = $oQuery->insertProfile ($iAccountId, $iContentId, $sStatus, $sType)))
  184. return false;
  185. bx_alert('profile', 'add', $iProfileId, 0, array('module' => $sType, 'content' => $iContentId, 'account' => $iAccountId, 'status' => $sStatus, 'action' => $iAction));
  186. return $iProfileId;
  187. }
  188. /**
  189. * Change profile status from 'Pending' to the next level - 'Active'
  190. */
  191. public function approve($iAction, $iProfileId = 0) {
  192. if (!$iProfileId)
  193. $iProfileId = $this->_iProfileID;
  194. if (!$this->_oQuery->changeStatus($iProfileId, BX_PROFILE_STATUS_ACTIVE))
  195. return false;
  196. bx_alert('profile', 'approve', $iProfileId, false, array('action' => $iAction));
  197. // TODO: send email that profile is approved
  198. return true;
  199. }
  200. /**
  201. * Change profile status to 'Pending'
  202. */
  203. public function disapprove($iAction, $iProfileId = 0) {
  204. if (!$this->_oQuery->changeStatus((int)$iProfileId ? $iProfileId : $this->_iProfileID, BX_PROFILE_STATUS_PENDING))
  205. return false;
  206. bx_alert('profile', 'disapprove', $iProfileId ? $iProfileId : $this->_iProfileID, false, array('action' => $iAction));
  207. // TODO: send email that profile is disapproved ?
  208. return true;
  209. }
  210. /**
  211. * Change profile status to 'Suspended'
  212. */
  213. public function suspend($iAction, $iProfileId = 0) {
  214. if (!$this->_oQuery->changeStatus((int)$iProfileId ? $iProfileId : $this->_iProfileID, BX_PROFILE_STATUS_SUSPENDED))
  215. return false;
  216. bx_alert('profile', 'suspend', $iProfileId, false, array('action' => $iAction));
  217. // TODO: send email that profile is suspended
  218. return true;
  219. }
  220. /**
  221. * Display informer message if it is possible to switch to this profile
  222. */
  223. public function checkSwitchToProfile($oTemplate = null, $iViewerAccountId = false, $iViewerProfileId = false) {
  224. if (false === $iViewerAccountId)
  225. $iViewerAccountId = getLoggedId();
  226. if (false === $iViewerProfileId)
  227. $iViewerProfileId = bx_get_logged_profile_id();
  228. if (!$iViewerAccountId || !$iViewerProfileId)
  229. return;
  230. if ($iViewerAccountId != $this->getAccountId() || $iViewerProfileId == $this->id())
  231. return;
  232. bx_import('BxDolInformer');
  233. bx_import('BxDolPermalinks');
  234. $oInformer = BxDolInformer::getInstance($oTemplate);
  235. if ($oInformer)
  236. $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);
  237. }
  238. }
  239. /** @} */