BxBaseModProfileModule.php 13 KB

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