BxBaseModProfileModule.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. $o = BxDolProfile::getInstance($iProfileId);
  123. $s .= $o->getUnit();
  124. }
  125. // return profiles + paginate
  126. return $s . (!$iStart && $oPaginate->getNum() <= $iLimit ? '' : $oPaginate->getSimplePaginate());
  127. }
  128. public function serviceEntityEditCover ($iContentId = 0)
  129. {
  130. return $this->_serviceEntityForm ('editDataForm', $iContentId, $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT_COVER']);
  131. }
  132. public function serviceProfileCover ($iContentId = 0)
  133. {
  134. if (!$iContentId)
  135. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  136. if (!$iContentId)
  137. return false;
  138. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  139. if (!$aContentInfo)
  140. return false;
  141. return $this->_oTemplate->cover($aContentInfo);
  142. }
  143. public function serviceProfileFriends ($iContentId = 0)
  144. {
  145. if (!$iContentId)
  146. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  147. if (!$iContentId)
  148. return false;
  149. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  150. if (!$aContentInfo)
  151. return false;
  152. bx_import('BxDolConnection');
  153. $s = $this->serviceBrowseConnectionsQuick ($aContentInfo['profile_id'], 'sys_profiles_friends', BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  154. if (!$s)
  155. return MsgBox(_t('_sys_txt_empty'));
  156. return $s;
  157. }
  158. // ====== PERMISSION METHODS
  159. /**
  160. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  161. */
  162. public function checkAllowedEdit ($aDataEntry, $isPerformAction = false)
  163. {
  164. // moderator always has access
  165. if ($this->_isModerator($isPerformAction))
  166. return CHECK_ACTION_RESULT_ALLOWED;
  167. // owner (checked by account! not as profile as ususal) always have access
  168. bx_import('BxDolProfile');
  169. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']]);
  170. if ($oProfileAurhor->getAccountId() == $this->_iAccountId)
  171. return CHECK_ACTION_RESULT_ALLOWED;
  172. return _t('_sys_txt_access_denied');
  173. }
  174. /**
  175. * Check if user can change cover image
  176. */
  177. public function checkAllowedChangeCover ($aDataEntry, $isPerformAction = false)
  178. {
  179. // moderator always has access
  180. if ($this->_isModerator($isPerformAction))
  181. return CHECK_ACTION_RESULT_ALLOWED;
  182. // owner (checked by account! not as profile as ususal) always have access
  183. bx_import('BxDolProfile');
  184. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']]);
  185. if ($oProfileAurhor->getAccountId() == $this->_iAccountId)
  186. return CHECK_ACTION_RESULT_ALLOWED;
  187. return _t('_sys_txt_access_denied');
  188. }
  189. /**
  190. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  191. */
  192. public function checkAllowedDelete (&$aDataEntry, $isPerformAction = false)
  193. {
  194. // moderator always has access
  195. if ($this->_isModerator($isPerformAction))
  196. return CHECK_ACTION_RESULT_ALLOWED;
  197. // check ACL and owner (checked by account! not as profile as ususal)
  198. bx_import('BxDolProfile');
  199. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']]);
  200. $aCheck = checkActionModule($this->_iProfileId, 'delete entry', $this->getName(), $isPerformAction);
  201. if ($oProfileAurhor->getAccountId() == $this->_iAccountId && $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  202. return CHECK_ACTION_RESULT_ALLOWED;
  203. return _t('_sys_txt_access_denied');
  204. }
  205. /**
  206. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  207. */
  208. public function checkAllowedViewMoreMenu (&$aDataEntry, $isPerformAction = false)
  209. {
  210. bx_import('BxTemplMenu');
  211. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']);
  212. if (!$oMenu || !$oMenu->getCode())
  213. return _t('_sys_txt_access_denied');
  214. return CHECK_ACTION_RESULT_ALLOWED;
  215. }
  216. /**
  217. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  218. */
  219. public function checkAllowedFriendAdd (&$aDataEntry, $isPerformAction = false)
  220. {
  221. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', true, false);
  222. }
  223. /**
  224. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  225. */
  226. public function checkAllowedFriendRemove (&$aDataEntry, $isPerformAction = false)
  227. {
  228. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, true))
  229. return CHECK_ACTION_RESULT_ALLOWED;
  230. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, false);
  231. }
  232. /**
  233. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  234. */
  235. public function checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  236. {
  237. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, false);
  238. }
  239. /**
  240. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  241. */
  242. public function checkAllowedSubscribeRemove (&$aDataEntry, $isPerformAction = false)
  243. {
  244. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, true);
  245. }
  246. // ====== PROTECTED METHODS
  247. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  248. {
  249. if (!$this->_iProfileId)
  250. return _t('_sys_txt_access_denied');
  251. $CNF = &$this->_oConfig->CNF;
  252. $oProfileAuthor = BxDolProfile::getInstance($aDataEntry[$CNF['FIELD_AUTHOR']]);
  253. $oProfile = $oProfileAuthor ? BxDolProfile::getInstanceByContentTypeAccount($aDataEntry[$CNF['FIELD_ID']], $this->_aModule['name'], $oProfileAuthor->getAccountId()) : false;
  254. if (!$oProfile || $oProfile->id() == $this->_iProfileId)
  255. return _t('_sys_txt_access_denied');
  256. bx_import('BxDolConnection');
  257. $oConn = BxDolConnection::getObjectInstance($sObjConnection);
  258. if ($isSwap)
  259. $isConnected = $oConn->isConnected($oProfile->id(), $this->_iProfileId, $isMutual);
  260. else
  261. $isConnected = $oConn->isConnected($this->_iProfileId, $oProfile->id(), $isMutual);
  262. if ($isInvertResult)
  263. $isConnected = !$isConnected;
  264. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  265. }
  266. protected function _buildRssParams($sMode, $aArgs)
  267. {
  268. $aParams = array ();
  269. $sMode = bx_process_input($sMode);
  270. switch ($sMode) {
  271. case 'connections':
  272. $aParams = array(
  273. 'object' => isset($aArgs[0]) ? $aArgs[0] : '',
  274. 'type' => isset($aArgs[1]) ? $aArgs[1] : '',
  275. 'profile' => isset($aArgs[2]) ? (int)$aArgs[2] : 0,
  276. 'mutual' => isset($aArgs[3]) ? (int)$aArgs[3] : 0,
  277. 'profile2' => isset($aArgs[4]) ? (int)$aArgs[4] : 0,
  278. );
  279. break;
  280. }
  281. return $aParams;
  282. }
  283. }
  284. /** @} */