BxPersonsModule.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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('BxDolView');
  128. BxDolView::getObjectInstance(BxPersonsConfig::$OBJECT_VIEWS, $iContentId)->doView();
  129. bx_import('ProfileForms', $this->_aModule);
  130. $oProfileForms = new BxPersonsProfileForms($this);
  131. return $oProfileForms->viewDataForm((int)$iContentId);
  132. }
  133. public function serviceProfileCover ($iContentId = 0) {
  134. if (!$iContentId)
  135. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  136. if (!$iContentId)
  137. return false;
  138. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  139. if (!$aContentInfo)
  140. return false;
  141. return $this->_oTemplate->cover($aContentInfo);
  142. }
  143. public function serviceProfileActions ($iContentId = 0) {
  144. if (!$iContentId)
  145. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  146. if (!$iContentId)
  147. return false;
  148. bx_import('BxTemplMenu');
  149. $oMenu = BxTemplMenu::getObjectInstance('bx_persons_view_actions');
  150. return $oMenu ? $oMenu->getCode() : false;
  151. }
  152. public function serviceProfileFriends ($iContentId = 0) {
  153. if (!$iContentId)
  154. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  155. if (!$iContentId)
  156. return false;
  157. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  158. if (!$aContentInfo)
  159. return false;
  160. return 'TODO: friends here';
  161. }
  162. // ====== ACTION METHODS
  163. function actionBrowse ($sMode = '') {
  164. $sMode = bx_process_input($sMode);
  165. /*
  166. if ('user' == $sMode || 'my' == $sMode) {
  167. $aProfile = getProfileInfo ($this->_iProfileId);
  168. if (0 == strcasecmp($sValue, $aProfile['NickName']) || 'my' == $sMode) {
  169. $this->_browseMy ($aProfile);
  170. return;
  171. }
  172. }
  173. if ('tag' == $sMode || 'category' == $sMode)
  174. $sValue = uri2title($sValue);
  175. */
  176. if (CHECK_ACTION_RESULT_ALLOWED !== $this->isAllowedBrowse()) {
  177. $this->_oTemplate->displayAccessDenied ();
  178. return;
  179. }
  180. bx_import ('SearchResult', $this->_aModule);
  181. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  182. $o = new $sClass($sMode);
  183. if ($o->isError) {
  184. $this->_oTemplate->displayPageNotFound ();
  185. return;
  186. }
  187. if (bx_get('rss')) {
  188. echo $o->rss();
  189. exit;
  190. }
  191. if (!($s = $o->processing())) {
  192. $this->_oTemplate->displayNoData ();
  193. return;
  194. }
  195. // TODO: remale to use "pages"
  196. $this->_oTemplate->addCss ('main.css');
  197. $oTemplate = BxDolTemplate::getInstance();
  198. $oTemplate->setPageHeader ($o->aCurrent['title']);
  199. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  200. $oTemplate->setPageContent ('page_main_code', $s);
  201. $oTemplate->getPageCode();
  202. }
  203. // ====== PERMISSION METHODS
  204. function _checkModeratorAccess ($isPerformAction = false) {
  205. // check moderator ACL
  206. $aCheck = checkActionModule($this->_iProfileId, 'edit any person profile', $this->getName(), $isPerformAction);
  207. return $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  208. }
  209. /**
  210. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make "true === " checking.
  211. */
  212. function isAllowedView ($aDataEntry, $isPerformAction = false) {
  213. // moderator and owner always have access
  214. if ($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR] == $this->_iProfileId || $this->_checkModeratorAccess($isPerformAction))
  215. return CHECK_ACTION_RESULT_ALLOWED;
  216. // check ACL
  217. $aCheck = checkActionModule($this->_iProfileId, 'view person profile', $this->getName(), $isPerformAction);
  218. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  219. return $aCheck[CHECK_ACTION_MESSAGE];
  220. // TODO: check privacy
  221. return CHECK_ACTION_RESULT_ALLOWED;
  222. }
  223. /**
  224. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make "true === " checking.
  225. */
  226. function isAllowedBrowse () {
  227. return CHECK_ACTION_RESULT_ALLOWED;
  228. }
  229. /**
  230. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  231. */
  232. function isAllowedAdd ($isPerformAction = false) {
  233. // check ACL
  234. $aCheck = checkActionModule($this->_iProfileId, 'create person profile', $this->getName(), $isPerformAction);
  235. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  236. return $aCheck[CHECK_ACTION_MESSAGE];
  237. return CHECK_ACTION_RESULT_ALLOWED;
  238. }
  239. /**
  240. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  241. */
  242. function isAllowedEdit ($aDataEntry, $isPerformAction = false) {
  243. // moderator always has access
  244. if ($this->_checkModeratorAccess($isPerformAction))
  245. return CHECK_ACTION_RESULT_ALLOWED;
  246. // owner (checked by account! not as profile as ususal) always have access
  247. bx_import('BxDolProfile');
  248. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  249. if ($oProfileAurhor->getAccountId() == $this->_iAccountId)
  250. return CHECK_ACTION_RESULT_ALLOWED;
  251. return _t('_sys_txt_access_denied');
  252. }
  253. /**
  254. * Check if user can change cover image
  255. */
  256. function isAllowedChangeCover ($aDataEntry, $isPerformAction = false) {
  257. // moderator always has access
  258. if ($this->_checkModeratorAccess($isPerformAction))
  259. return CHECK_ACTION_RESULT_ALLOWED;
  260. // owner (checked by account! not as profile as ususal) always have access
  261. bx_import('BxDolProfile');
  262. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  263. if ($oProfileAurhor->getAccountId() == $this->_iAccountId)
  264. return CHECK_ACTION_RESULT_ALLOWED;
  265. return _t('_sys_txt_access_denied');
  266. }
  267. /**
  268. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  269. */
  270. function isAllowedDelete (&$aDataEntry, $isPerformAction = false) {
  271. // moderator always has access
  272. if ($this->_checkModeratorAccess($isPerformAction))
  273. return CHECK_ACTION_RESULT_ALLOWED;
  274. // check ACL and owner (checked by account! not as profile as ususal)
  275. bx_import('BxDolProfile');
  276. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  277. $aCheck = checkActionModule($this->_iProfileId, 'delete person profile', $this->getName(), $isPerformAction);
  278. if ($oProfileAurhor->getAccountId() == $this->_iAccountId && $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  279. return CHECK_ACTION_RESULT_ALLOWED;
  280. return _t('_sys_txt_access_denied');
  281. }
  282. /**
  283. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  284. */
  285. function isAllowedViewMoreMenu (&$aDataEntry, $isPerformAction = false) {
  286. bx_import('BxTemplMenu');
  287. $oMenu = BxTemplMenu::getObjectInstance('bx_persons_view_actions_more');
  288. if (!$oMenu || !$oMenu->getCode())
  289. return _t('_sys_txt_access_denied');
  290. return CHECK_ACTION_RESULT_ALLOWED;
  291. }
  292. /**
  293. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  294. */
  295. function isAllowedFriendAdd (&$aDataEntry, $isPerformAction = false) {
  296. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', true, false);
  297. }
  298. /**
  299. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  300. */
  301. function isAllowedFriendRemove (&$aDataEntry, $isPerformAction = false) {
  302. if (CHECK_ACTION_RESULT_ALLOWED === $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, true))
  303. return CHECK_ACTION_RESULT_ALLOWED;
  304. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, false);
  305. }
  306. /**
  307. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  308. */
  309. function isAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false) {
  310. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, false);
  311. }
  312. /**
  313. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  314. */
  315. function isAllowedSubscribeRemove (&$aDataEntry, $isPerformAction = false) {
  316. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, true);
  317. }
  318. function _isAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false) {
  319. if (!$this->_iProfileId)
  320. return _t('_sys_txt_access_denied');
  321. $oProfileAuthor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  322. $oProfile = $oProfileAuthor ? BxDolProfile::getInstanceByContentTypeAccount($aDataEntry[BxPersonsConfig::$FIELD_ID], 'bx_persons', $oProfileAuthor->getAccountId()) : false;
  323. if (!$oProfile || $oProfile->id() == $this->_iProfileId)
  324. return _t('_sys_txt_access_denied');
  325. bx_import('BxDolConnection');
  326. $oConn = BxDolConnection::getObjectInstance($sObjConnection);
  327. if ($isSwap)
  328. $isConnected = $oConn->isConnected($oProfile->id(), $this->_iProfileId, $isMutual);
  329. else
  330. $isConnected = $oConn->isConnected($this->_iProfileId, $oProfile->id(), $isMutual);
  331. if ($isInvertResult)
  332. $isConnected = !$isConnected;
  333. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  334. }
  335. }
  336. /** @} */