1
0

BxBaseModProfileModule.php 12 KB

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