1
0

BxBaseModProfileModule.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 BaseProfile Base classes for profile modules
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import ('BxBaseModGeneralModule');
  12. bx_import ('BxDolAcl');
  13. /**
  14. * Base class for profile modules.
  15. */
  16. class BxBaseModProfileModule extends BxBaseModGeneralModule implements iBxDolProfileService
  17. {
  18. protected $_iAccountId;
  19. function __construct(&$aModule)
  20. {
  21. parent::__construct($aModule);
  22. $this->_iAccountId = getLoggedId();
  23. }
  24. // ====== SERVICE METHODS
  25. public function serviceGetMenuSetNameForMenuTrigger ($sMenuTriggerName)
  26. {
  27. if ('trigger_profile_view_submenu' == $sMenuTriggerName)
  28. return $this->_oConfig->CNF['OBJECT_MENU_SUBMENU_VIEW_ENTRY'];
  29. return '';
  30. }
  31. public function serviceProfilesSearch ($sTerm, $iLimit)
  32. {
  33. $aRet = array();
  34. $a = $this->_oDb->searchByTerm($sTerm, $iLimit);
  35. foreach ($a as $r)
  36. $aRet[] = array ('label' => $this->serviceProfileName($r['content_id']), 'value' => $r['profile_id']);
  37. return $aRet;
  38. }
  39. public function serviceProfileUnit ($iContentId)
  40. {
  41. return $this->_serviceTemplateFunc('unit', $iContentId);
  42. }
  43. public function serviceProfileAvatar ($iContentId)
  44. {
  45. return $this->_serviceTemplateFunc('urlAvatar', $iContentId);
  46. }
  47. public function serviceProfileEditUrl ($iContentId)
  48. {
  49. bx_import('BxDolPermalinks');
  50. return BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oConfig->CNF['URI_EDIT_ENTRY'] . '&id=' . $iContentId);
  51. }
  52. public function serviceProfileThumb ($iContentId)
  53. {
  54. return $this->_serviceTemplateFunc('thumb', $iContentId);
  55. }
  56. public function serviceProfileIcon ($iContentId)
  57. {
  58. return $this->_serviceTemplateFunc('icon', $iContentId);
  59. }
  60. public function serviceProfileName ($iContentId)
  61. {
  62. if (!$iContentId)
  63. return false;
  64. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  65. if (!$aContentInfo)
  66. return false;
  67. return $aContentInfo[$this->_oConfig->CNF['FIELD_NAME']];
  68. }
  69. public function serviceProfileUrl ($iContentId)
  70. {
  71. if (!$iContentId)
  72. return false;
  73. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  74. if (!$aContentInfo)
  75. return false;
  76. $CNF = $this->_oConfig->CNF;
  77. bx_import('BxDolPermalinks');
  78. return BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  79. }
  80. public function serviceBrowseRecentProfiles ()
  81. {
  82. return $this->_serviceBrowse ('recent');
  83. }
  84. public function serviceBrowseConnections ($iProfileId, $sObjectConnections = 'sys_profiles_friends', $sConnectionsType = 'content', $iMutual = false, $iDesignBox = BX_DB_PADDING_DEF, $iProfileId2 = 0)
  85. {
  86. return $this->_serviceBrowse (
  87. 'connections',
  88. array(
  89. 'object' => $sObjectConnections,
  90. 'type' => $sConnectionsType,
  91. 'mutual' => $iMutual,
  92. 'profile' => (int)$iProfileId,
  93. 'profile2' => (int)$iProfileId2),
  94. $iDesignBox
  95. );
  96. }
  97. public function serviceBrowseConnectionsQuick ($iProfileId, $sObjectConnections = 'sys_profiles_friends', $sConnectionsType = 'content', $iMutual = false, $iProfileId2 = 0)
  98. {
  99. // get connections object
  100. bx_import('BxDolConnection');
  101. $oConnection = BxDolConnection::getObjectInstance($sObjectConnections);
  102. if (!$oConnection)
  103. return '';
  104. // set some vars
  105. $iLimit = empty($this->_oConfig->CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? 4 : getParam($this->_oConfig->CNF['PARAM_NUM_CONNECTIONS_QUICK']);
  106. if (!$iLimit)
  107. $iLimit = 4;
  108. $iStart = (int)bx_get('start');
  109. // get connections array
  110. $a = $oConnection->getConnectionsAsArray ($sConnectionsType, $iProfileId, $iProfileId2, $iMutual, (int)bx_get('start'), $iLimit + 1, BX_CONNECTIONS_ORDER_ADDED_DESC);
  111. if (!$a)
  112. return '';
  113. // get paginate object
  114. bx_import('BxTemplPaginate');
  115. $oPaginate = new BxTemplPaginate(array(
  116. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}');",
  117. 'num' => count($a),
  118. 'per_page' => $iLimit,
  119. 'start' => $iStart,
  120. ));
  121. // remove last item from connection array, because we've got one more item for pagination calculations only
  122. if (count($a) > $iLimit)
  123. array_pop($a);
  124. // get profiles HTML
  125. bx_import('BxDolProfile');
  126. $s = '';
  127. foreach ($a as $iProfileId) {
  128. if (!($o = BxDolProfile::getInstance($iProfileId))) {
  129. continue;
  130. }
  131. $s .= $o->getUnit();
  132. }
  133. // return profiles + paginate
  134. return $s . (!$iStart && $oPaginate->getNum() <= $iLimit ? '' : $oPaginate->getSimplePaginate());
  135. }
  136. public function serviceEntityEditCover ($iContentId = 0)
  137. {
  138. return $this->_serviceEntityForm ('editDataForm', $iContentId, $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT_COVER']);
  139. }
  140. public function serviceProfileCover ($iContentId = 0)
  141. {
  142. if (!$iContentId)
  143. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  144. if (!$iContentId)
  145. return false;
  146. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  147. if (!$aContentInfo)
  148. return false;
  149. return $this->_oTemplate->cover($aContentInfo);
  150. }
  151. public function serviceProfileFriends ($iContentId = 0)
  152. {
  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. bx_import('BxDolConnection');
  161. $s = $this->serviceBrowseConnectionsQuick ($aContentInfo['profile_id'], 'sys_profiles_friends', BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  162. if (!$s)
  163. return MsgBox(_t('_sys_txt_empty'));
  164. return $s;
  165. }
  166. // ====== PERMISSION METHODS
  167. /**
  168. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  169. */
  170. public function checkAllowedEdit ($aDataEntry, $isPerformAction = false)
  171. {
  172. // moderator always has access
  173. if ($this->_isModerator($isPerformAction))
  174. return CHECK_ACTION_RESULT_ALLOWED;
  175. // owner (checked by account! not as profile as ususal) always have access
  176. bx_import('BxDolProfile');
  177. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']]);
  178. if ($oProfileAurhor->getAccountId() == $this->_iAccountId)
  179. return CHECK_ACTION_RESULT_ALLOWED;
  180. return _t('_sys_txt_access_denied');
  181. }
  182. /**
  183. * Check if user can change cover image
  184. */
  185. public function checkAllowedChangeCover ($aDataEntry, $isPerformAction = false)
  186. {
  187. // moderator always has access
  188. if ($this->_isModerator($isPerformAction))
  189. return CHECK_ACTION_RESULT_ALLOWED;
  190. // owner (checked by account! not as profile as ususal) always have access
  191. bx_import('BxDolProfile');
  192. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']]);
  193. if ($oProfileAurhor->getAccountId() == $this->_iAccountId)
  194. return CHECK_ACTION_RESULT_ALLOWED;
  195. return _t('_sys_txt_access_denied');
  196. }
  197. /**
  198. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  199. */
  200. public function checkAllowedDelete (&$aDataEntry, $isPerformAction = false)
  201. {
  202. // moderator always has access
  203. if ($this->_isModerator($isPerformAction))
  204. return CHECK_ACTION_RESULT_ALLOWED;
  205. // check ACL and owner (checked by account! not as profile as ususal)
  206. bx_import('BxDolProfile');
  207. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']]);
  208. $aCheck = checkActionModule($this->_iProfileId, 'delete entry', $this->getName(), $isPerformAction);
  209. if ($oProfileAurhor->getAccountId() == $this->_iAccountId && $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  210. return CHECK_ACTION_RESULT_ALLOWED;
  211. return _t('_sys_txt_access_denied');
  212. }
  213. /**
  214. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  215. */
  216. public function checkAllowedViewMoreMenu (&$aDataEntry, $isPerformAction = false)
  217. {
  218. bx_import('BxTemplMenu');
  219. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']);
  220. if (!$oMenu || !$oMenu->getCode())
  221. return _t('_sys_txt_access_denied');
  222. return CHECK_ACTION_RESULT_ALLOWED;
  223. }
  224. /**
  225. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  226. */
  227. public function checkAllowedFriendAdd (&$aDataEntry, $isPerformAction = false)
  228. {
  229. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', true, false);
  230. }
  231. /**
  232. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  233. */
  234. public function checkAllowedFriendRemove (&$aDataEntry, $isPerformAction = false)
  235. {
  236. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, true))
  237. return CHECK_ACTION_RESULT_ALLOWED;
  238. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, false);
  239. }
  240. /**
  241. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  242. */
  243. public function checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  244. {
  245. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, false);
  246. }
  247. /**
  248. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  249. */
  250. public function checkAllowedSubscribeRemove (&$aDataEntry, $isPerformAction = false)
  251. {
  252. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, true);
  253. }
  254. // ====== PROTECTED METHODS
  255. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  256. {
  257. if (!$this->_iProfileId)
  258. return _t('_sys_txt_access_denied');
  259. $CNF = &$this->_oConfig->CNF;
  260. $oProfileAuthor = BxDolProfile::getInstance($aDataEntry[$CNF['FIELD_AUTHOR']]);
  261. $oProfile = $oProfileAuthor ? BxDolProfile::getInstanceByContentTypeAccount($aDataEntry[$CNF['FIELD_ID']], $this->_aModule['name'], $oProfileAuthor->getAccountId()) : false;
  262. if (!$oProfile || $oProfile->id() == $this->_iProfileId)
  263. return _t('_sys_txt_access_denied');
  264. bx_import('BxDolConnection');
  265. $oConn = BxDolConnection::getObjectInstance($sObjConnection);
  266. if ($isSwap)
  267. $isConnected = $oConn->isConnected($oProfile->id(), $this->_iProfileId, $isMutual);
  268. else
  269. $isConnected = $oConn->isConnected($this->_iProfileId, $oProfile->id(), $isMutual);
  270. if ($isInvertResult)
  271. $isConnected = !$isConnected;
  272. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  273. }
  274. protected function _buildRssParams($sMode, $aArgs)
  275. {
  276. $aParams = array ();
  277. $sMode = bx_process_input($sMode);
  278. switch ($sMode) {
  279. case 'connections':
  280. $aParams = array(
  281. 'object' => isset($aArgs[0]) ? $aArgs[0] : '',
  282. 'type' => isset($aArgs[1]) ? $aArgs[1] : '',
  283. 'profile' => isset($aArgs[2]) ? (int)$aArgs[2] : 0,
  284. 'mutual' => isset($aArgs[3]) ? (int)$aArgs[3] : 0,
  285. 'profile2' => isset($aArgs[4]) ? (int)$aArgs[4] : 0,
  286. );
  287. break;
  288. }
  289. return $aParams;
  290. }
  291. }
  292. /** @} */