1
0

BxBaseModProfileModule.php 13 KB

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