BxPersonsModule.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 Persons Persons
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import ('BxDolModule');
  12. bx_import ('BxDolAcl');
  13. /**
  14. * Persons module
  15. *
  16. * Basic person profiles.
  17. */
  18. class BxPersonsModule extends BxDolModule implements iBxDolProfileService {
  19. protected $_iProfileId;
  20. protected $_iAccountId;
  21. function __construct(&$aModule) {
  22. parent::__construct($aModule);
  23. $this->_iProfileId = bx_get_logged_profile_id();
  24. $this->_iAccountId = getLoggedId();
  25. }
  26. // ====== SERVICE METHODS
  27. public function serviceProfileUnit ($iContentId) {
  28. return $this->_serviceTemplateFunc('unit', $iContentId);
  29. }
  30. public function serviceProfileAvatar ($iContentId) {
  31. return $this->_serviceTemplateFunc('urlAvatar', $iContentId);
  32. }
  33. public function serviceProfileThumb ($iContentId) {
  34. return $this->_serviceTemplateFunc('thumb', $iContentId);
  35. }
  36. public function serviceProfileIcon ($iContentId) {
  37. return $this->_serviceTemplateFunc('icon', $iContentId);
  38. }
  39. protected function _serviceTemplateFunc ($sFunc, $iContentId) {
  40. if (!$iContentId)
  41. return false;
  42. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  43. if (!$aContentInfo)
  44. return false;
  45. return $this->_oTemplate->$sFunc($aContentInfo);
  46. }
  47. public function serviceProfileName ($iContentId) {
  48. if (!$iContentId)
  49. return false;
  50. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  51. if (!$aContentInfo)
  52. return false;
  53. return $aContentInfo[BxPersonsConfig::$FIELD_NAME];
  54. }
  55. public function serviceProfileUrl ($iContentId) {
  56. if (!$iContentId)
  57. return false;
  58. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  59. if (!$aContentInfo)
  60. return false;
  61. bx_import('BxDolPermalinks');
  62. return BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=view-persons-profile&id=' . $aContentInfo['id']);
  63. }
  64. public function serviceBrowseRecentPersons () {
  65. return $this->_serviceBrowse ('recent');
  66. }
  67. public function serviceBrowseConnections ($iProfileId, $sObjectConnections = 'sys_profiles_friends', $sConnectionsType = 'content', $iMutual = false, $iDesignBox = BX_DB_PADDING_DEF) {
  68. return $this->_serviceBrowse (
  69. 'connections',
  70. array(
  71. 'object' => $sObjectConnections,
  72. 'type' => $sConnectionsType,
  73. 'mutual' => $iMutual,
  74. 'profile' => (int)$iProfileId),
  75. $iDesignBox
  76. );
  77. }
  78. protected function _serviceBrowse ($sMode, $aParams = false, $iDesignBox = BX_DB_PADDING_DEF) {
  79. bx_import('SearchResult', $this->_aModule);
  80. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  81. $o = new $sClass($sMode, $aParams);
  82. $o->setDesignBoxTemplateId($iDesignBox);
  83. if ($o->isError)
  84. return false;
  85. if ($s = $o->processing())
  86. return $s;
  87. else
  88. return false;
  89. }
  90. public function serviceCreateProfile () {
  91. bx_import('ProfileForms', $this->_aModule);
  92. $oProfileForms = new BxPersonsProfileForms($this);
  93. return $oProfileForms->addDataForm();
  94. }
  95. public function serviceEditProfile ($iContentId = 0) {
  96. if (!$iContentId)
  97. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  98. if (!$iContentId)
  99. return false;
  100. bx_import('ProfileForms', $this->_aModule);
  101. $oProfileForms = new BxPersonsProfileForms($this);
  102. return $oProfileForms->editDataForm((int)$iContentId);
  103. }
  104. public function serviceEditCover ($iContentId = 0) {
  105. if (!$iContentId)
  106. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  107. if (!$iContentId)
  108. return false;
  109. bx_import('ProfileForms', $this->_aModule);
  110. $oProfileForms = new BxPersonsProfileForms($this);
  111. return $oProfileForms->editDataForm((int)$iContentId, 'bx_person_edit_cover');
  112. }
  113. public function serviceDeleteProfile ($iContentId = 0) {
  114. if (!$iContentId)
  115. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  116. if (!$iContentId)
  117. return false;
  118. bx_import('ProfileForms', $this->_aModule);
  119. $oProfileForms = new BxPersonsProfileForms($this);
  120. return $oProfileForms->deleteDataForm((int)$iContentId);
  121. }
  122. public function serviceProfileInfo ($iContentId = 0) {
  123. if (!$iContentId)
  124. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  125. if (!$iContentId)
  126. return false;
  127. bx_import('ProfileForms', $this->_aModule);
  128. $oProfileForms = new BxPersonsProfileForms($this);
  129. return $oProfileForms->viewDataForm((int)$iContentId);
  130. }
  131. public function serviceProfileCover ($iContentId = 0) {
  132. if (!$iContentId)
  133. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  134. if (!$iContentId)
  135. return false;
  136. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  137. if (!$aContentInfo)
  138. return false;
  139. return $this->_oTemplate->cover($aContentInfo);
  140. }
  141. public function serviceProfileActions ($iContentId = 0) {
  142. if (!$iContentId)
  143. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  144. if (!$iContentId)
  145. return false;
  146. bx_import('BxTemplMenu');
  147. $oMenu = BxTemplMenu::getObjectInstance('bx_persons_view_actions');
  148. return $oMenu ? $oMenu->getCode() : false;
  149. }
  150. public function serviceProfileFriends ($iContentId = 0) {
  151. if (!$iContentId)
  152. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  153. if (!$iContentId)
  154. return false;
  155. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  156. if (!$aContentInfo)
  157. return false;
  158. return 'TODO: friends here';
  159. }
  160. // ====== ACTION METHODS
  161. function actionBrowse ($sMode = '') {
  162. $sMode = bx_process_input($sMode);
  163. /*
  164. if ('user' == $sMode || 'my' == $sMode) {
  165. $aProfile = getProfileInfo ($this->_iProfileId);
  166. if (0 == strcasecmp($sValue, $aProfile['NickName']) || 'my' == $sMode) {
  167. $this->_browseMy ($aProfile);
  168. return;
  169. }
  170. }
  171. if ('tag' == $sMode || 'category' == $sMode)
  172. $sValue = uri2title($sValue);
  173. */
  174. if (CHECK_ACTION_RESULT_ALLOWED !== $this->isAllowedBrowse()) {
  175. $this->_oTemplate->displayAccessDenied ();
  176. return;
  177. }
  178. bx_import ('SearchResult', $this->_aModule);
  179. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  180. $o = new $sClass($sMode);
  181. if ($o->isError) {
  182. $this->_oTemplate->displayPageNotFound ();
  183. return;
  184. }
  185. if (bx_get('rss')) {
  186. echo $o->rss();
  187. exit;
  188. }
  189. if (!($s = $o->processing())) {
  190. $this->_oTemplate->displayNoData ();
  191. return;
  192. }
  193. // TODO: remale to use "pages"
  194. $this->_oTemplate->addCss ('main.css');
  195. $oTemplate = BxDolTemplate::getInstance();
  196. $oTemplate->setPageHeader ($o->aCurrent['title']);
  197. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  198. $oTemplate->setPageContent ('page_main_code', $s);
  199. $oTemplate->getPageCode();
  200. }
  201. // ====== PERMISSION METHODS
  202. function _checkModeratorAccess ($isPerformAction = false) {
  203. // check moderator ACL
  204. $aCheck = checkActionModule($this->_iProfileId, 'edit any person profile', $this->getName(), $isPerformAction);
  205. return $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  206. }
  207. /**
  208. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make "true === " checking.
  209. */
  210. function isAllowedView ($aDataEntry, $isPerformAction = false) {
  211. // moderator and owner always have access
  212. if ($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR] == $this->_iProfileId || $this->_checkModeratorAccess($isPerformAction))
  213. return CHECK_ACTION_RESULT_ALLOWED;
  214. // check ACL
  215. $aCheck = checkActionModule($this->_iProfileId, 'view person profile', $this->getName(), $isPerformAction);
  216. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  217. return $aCheck[CHECK_ACTION_MESSAGE];
  218. // TODO: check privacy
  219. return CHECK_ACTION_RESULT_ALLOWED;
  220. }
  221. /**
  222. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make "true === " checking.
  223. */
  224. function isAllowedBrowse () {
  225. return CHECK_ACTION_RESULT_ALLOWED;
  226. }
  227. /**
  228. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  229. */
  230. function isAllowedAdd ($isPerformAction = false) {
  231. // check ACL
  232. $aCheck = checkActionModule($this->_iProfileId, 'create person profile', $this->getName(), $isPerformAction);
  233. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  234. return $aCheck[CHECK_ACTION_MESSAGE];
  235. return CHECK_ACTION_RESULT_ALLOWED;
  236. }
  237. /**
  238. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  239. */
  240. function isAllowedEdit ($aDataEntry, $isPerformAction = false) {
  241. // moderator always has access
  242. if ($this->_checkModeratorAccess($isPerformAction))
  243. return CHECK_ACTION_RESULT_ALLOWED;
  244. // owner (checked by account! not as profile as ususal) always have access
  245. bx_import('BxDolProfile');
  246. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  247. if ($oProfileAurhor->getAccountId() == $this->_iAccountId)
  248. return CHECK_ACTION_RESULT_ALLOWED;
  249. return _t('_sys_txt_access_denied');
  250. }
  251. /**
  252. * Check if user can change cover image
  253. */
  254. function isAllowedChangeCover ($aDataEntry, $isPerformAction = false) {
  255. // moderator always has access
  256. if ($this->_checkModeratorAccess($isPerformAction))
  257. return CHECK_ACTION_RESULT_ALLOWED;
  258. // owner (checked by account! not as profile as ususal) always have access
  259. bx_import('BxDolProfile');
  260. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  261. if ($oProfileAurhor->getAccountId() == $this->_iAccountId)
  262. return CHECK_ACTION_RESULT_ALLOWED;
  263. return _t('_sys_txt_access_denied');
  264. }
  265. /**
  266. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  267. */
  268. function isAllowedDelete (&$aDataEntry, $isPerformAction = false) {
  269. // moderator always has access
  270. if ($this->_checkModeratorAccess($isPerformAction))
  271. return CHECK_ACTION_RESULT_ALLOWED;
  272. // check ACL and owner (checked by account! not as profile as ususal)
  273. bx_import('BxDolProfile');
  274. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  275. $aCheck = checkActionModule($this->_iProfileId, 'delete person profile', $this->getName(), $isPerformAction);
  276. if ($oProfileAurhor->getAccountId() == $this->_iAccountId && $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  277. return CHECK_ACTION_RESULT_ALLOWED;
  278. return _t('_sys_txt_access_denied');
  279. }
  280. /**
  281. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  282. */
  283. function isAllowedViewMoreMenu (&$aDataEntry, $isPerformAction = false) {
  284. bx_import('BxTemplMenu');
  285. $oMenu = BxTemplMenu::getObjectInstance('bx_persons_view_actions_more');
  286. if (!$oMenu || !$oMenu->getCode())
  287. return _t('_sys_txt_access_denied');
  288. return CHECK_ACTION_RESULT_ALLOWED;
  289. }
  290. /**
  291. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  292. */
  293. function isAllowedFriendAdd (&$aDataEntry, $isPerformAction = false) {
  294. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', true, false);
  295. }
  296. /**
  297. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  298. */
  299. function isAllowedFriendRemove (&$aDataEntry, $isPerformAction = false) {
  300. if (CHECK_ACTION_RESULT_ALLOWED === $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, true))
  301. return CHECK_ACTION_RESULT_ALLOWED;
  302. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, false);
  303. }
  304. /**
  305. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  306. */
  307. function isAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false) {
  308. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, false);
  309. }
  310. /**
  311. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  312. */
  313. function isAllowedSubscribeRemove (&$aDataEntry, $isPerformAction = false) {
  314. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, true);
  315. }
  316. function _isAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false) {
  317. if (!$this->_iProfileId)
  318. return _t('_sys_txt_access_denied');
  319. $oProfileAuthor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  320. $oProfile = $oProfileAuthor ? BxDolProfile::getInstanceByContentTypeAccount($aDataEntry[BxPersonsConfig::$FIELD_ID], 'bx_persons', $oProfileAuthor->getAccountId()) : false;
  321. if (!$oProfile || $oProfile->id() == $this->_iProfileId)
  322. return _t('_sys_txt_access_denied');
  323. bx_import('BxDolConnection');
  324. $oConn = BxDolConnection::getObjectInstance($sObjConnection);
  325. if ($isSwap)
  326. $isConnected = $oConn->isConnected($oProfile->id(), $this->_iProfileId, $isMutual);
  327. else
  328. $isConnected = $oConn->isConnected($this->_iProfileId, $oProfile->id(), $isMutual);
  329. if ($isInvertResult)
  330. $isConnected = !$isConnected;
  331. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  332. }
  333. }
  334. /** @} */