BxPersonsModule.php 14 KB

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