BxPersonsModule.php 15 KB

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