BxBaseModProfileModule.php 14 KB

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