1
0

BxBaseModProfileModule.php 14 KB

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