BxPersonsModule.php 15 KB

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