1
0

BxBaseServiceProfiles.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * System service for profiles handling functionality.
  11. */
  12. class BxBaseServiceProfiles extends BxDol
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. public function serviceGetProfileTypes()
  19. {
  20. $aTypes = array(
  21. '' => _t('_Select_one')
  22. );
  23. $aModules = $this->_getProfilesModules();
  24. foreach($aModules as $aModule) {
  25. if(empty($aModule) || !is_array($aModule))
  26. continue;
  27. $sTitleKey = '_' . $aModule['name'];
  28. $sTitle = _t($sTitleKey);
  29. $aTypes[$aModule['name']] = !empty($sTitle) && $sTitle != $sTitleKey ? $sTitle : $aModule['title'];
  30. }
  31. return $aTypes;
  32. }
  33. public function serviceProfileStats ($iProfileId = 0)
  34. {
  35. if (!$iProfileId && !($iProfileId = bx_get_logged_profile_id()))
  36. return '';
  37. $oProfile = BxDolProfile::getInstance($iProfileId);
  38. if(!$oProfile)
  39. return '';
  40. $oAcl = BxDolAcl::getInstance();
  41. $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
  42. $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
  43. $aVars = array(
  44. 'profile_id' => $oProfile->id(),
  45. 'profile_url' => $oProfile->getUrl(),
  46. 'profile_edit_url' => $oProfile->getEditUrl(),
  47. 'profile_title' => $oProfile->getDisplayName(),
  48. 'profile_title_attr' => bx_html_attribute($oProfile->getDisplayName()),
  49. 'profile_ava_url' => $oProfile->getAvatar(),
  50. 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
  51. 'name' => 'unit_wo_info',
  52. 'size' => 'ava'
  53. ))),
  54. 'profile_acl_title' => _t($aAclInfo['name']),
  55. 'profile_acl_icon' => $aAclInfo['icon'],
  56. 'menu' => BxDolMenu::getObjectInstance('sys_profile_stats')->getCode(),
  57. );
  58. return BxDolTemplate::getInstance()->parseHtmlByName('profile_stats.html', $aVars);
  59. }
  60. public function serviceGetMenuAddonProfileEdit($iProfileId = 0, $sCaption = '')
  61. {
  62. if(empty($sCaption))
  63. $sCaption = _t('_Edit');
  64. $oProfile = BxDolProfile::getInstance($iProfileId);
  65. if(!$oProfile)
  66. return '';
  67. $sModule = $oProfile->getModule();
  68. $sMethod = 'profile_edit_url';
  69. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  70. return '';
  71. return BxDolTemplate::getInstance()->parseLink(BxDolService::call($sModule, $sMethod, array($oProfile->getContentId())), $sCaption);
  72. }
  73. /**
  74. * @page service Service Calls
  75. * @section bx_system_general System Services
  76. * @subsection bx_system_general-profiles Profiles
  77. * @subsubsection bx_system_general-profile_membership profile_membership
  78. *
  79. * @code bx_srv('system', 'profile_membership', [2], 'TemplServiceProfiles'); @endcode
  80. * @code {{~system:profile_membership:TemplServiceProfiles[2]~}} @endcode
  81. *
  82. * Get membership level for specified profile.
  83. * @param $iProfileId profile ID
  84. *
  85. * @see BxBaseServiceProfiles::serviceProfileMembership
  86. */
  87. /**
  88. * @ref bx_system_general-profile_membership "profile_membership"
  89. */
  90. public function serviceProfileMembership ($iProfileId = 0)
  91. {
  92. if (!$iProfileId)
  93. $iProfileId = bx_get_logged_profile_id();
  94. return BxDolAcl::getInstance()->getProfileMembership($iProfileId);
  95. }
  96. /**
  97. * @page service Service Calls
  98. * @section bx_system_general System Services
  99. * @subsection bx_system_general-profiles Profiles
  100. * @subsubsection bx_system_general-profile_notifications profile_notifications
  101. *
  102. * @code bx_srv('system', 'profile_notifications', [], 'TemplServiceProfiles'); @endcode
  103. * @code {{~system:profile_notifications:TemplServiceProfiles~}} @endcode
  104. *
  105. * Get number of unread notifications for logged in profile
  106. *
  107. * @see BxBaseServiceProfiles::serviceProfileNotifications
  108. */
  109. /**
  110. * @ref bx_system_general-profile_notifications "profile_notifications"
  111. */
  112. public function serviceProfileNotifications ($iProfileId = 0)
  113. {
  114. if (!$iProfileId)
  115. $iProfileId = bx_get_logged_profile_id();
  116. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  117. if(!$oMenu)
  118. return 0;
  119. $iNum = 0;
  120. $aMenuItems = $oMenu->getMenuItems ();
  121. foreach ($aMenuItems as $r) {
  122. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  123. $iNum += $r['bx_if:addon']['content']['addon'];
  124. }
  125. return $iNum;
  126. }
  127. /**
  128. * @page service Service Calls
  129. * @section bx_system_general System Services
  130. * @subsection bx_system_general-profiles Profiles
  131. * @subsubsection bx_system_general-get_count_online_profiles get_count_online_profiles
  132. *
  133. * @code bx_srv('system', 'get_count_online_profiles', [], 'TemplServiceProfiles'); @endcode
  134. * @code {{~system:get_count_online_profiles:TemplServiceProfiles~}} @endcode
  135. *
  136. * Get number of online profiles
  137. *
  138. * @see BxBaseServiceProfiles::serviceGetCountOnlineProfiles
  139. */
  140. /**
  141. * @ref bx_system_general-get_count_online_profiles "get_count_online_profiles"
  142. */
  143. public function serviceGetCountOnlineProfiles ()
  144. {
  145. $oProfilesQuery = BxDolProfileQuery::getInstance();
  146. return $oProfilesQuery->getOnlineCount();
  147. }
  148. public function serviceGetProfilesModules ()
  149. {
  150. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  151. $oDb = BxDolDb::getInstance();
  152. $oCache = $oDb->getDbCacheObject ();
  153. $sKey = $oDb->genDbCacheKey('profiles_modules_array');
  154. $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
  155. $mixedRetTs = $oCache->getData($sKeyTs);
  156. $mixedRet = $oCache->getData($sKey);
  157. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  158. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  159. $aModulesArray = $mixedRet;
  160. } else {
  161. $aModulesArray = $this->_getProfilesModules ();
  162. $oCache->setData($sKey, $aModulesArray);
  163. $oCache->setData($sKeyTs, $iNewestModuleTs);
  164. }
  165. } else {
  166. $aModulesArray = $this->_getProfilesModules ();
  167. }
  168. return $aModulesArray;
  169. }
  170. public function serviceProfilesFriends ($iLimit = 20)
  171. {
  172. if (!($iProfileId = bx_get_logged_profile_id()))
  173. return array();
  174. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  175. if (!$oConnection)
  176. return array();
  177. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  178. return array();
  179. $aRet = array();
  180. foreach ($a as $iId) {
  181. $oProfile = BxDolProfile::getInstance($iId);
  182. $aRet[] = array (
  183. 'label' => $oProfile->getDisplayName(),
  184. 'value' => $iId,
  185. 'url' => $oProfile->getUrl(),
  186. 'thumb' => $oProfile->getThumb(),
  187. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  188. );
  189. }
  190. return $aRet;
  191. }
  192. public function serviceProfilesSearch ($sTerm, $iLimit = 20)
  193. {
  194. // display friends by default
  195. if (!$sTerm)
  196. return $this->serviceProfilesFriends($iLimit);
  197. // get list of "profiles" modules
  198. $aModules = $this->serviceGetProfilesModules();
  199. // search in each module
  200. $a = array();
  201. foreach ($aModules as $aModule) {
  202. if (!BxDolService::call($aModule['name'], 'act_as_profile'))
  203. continue;
  204. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, getParam('sys_per_page_search_keyword_single'))));
  205. }
  206. // sort result
  207. usort($a, function($r1, $r2) {
  208. return strcmp($r1['label'], $r2['label']);
  209. });
  210. // return as array
  211. return array_slice($a, 0, $iLimit);
  212. }
  213. public function serviceProfilesList ($iAccountId = 0)
  214. {
  215. $oProfilesQuery = BxDolProfileQuery::getInstance();
  216. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  217. if (!$aProfiles)
  218. return false;
  219. $s = '';
  220. foreach ($aProfiles as $aProfile)
  221. if ($aProfile['type'] != 'system')
  222. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  223. if (!$s)
  224. $s = MsgBox(_t('_sys_txt_empty'));
  225. return $s . '<div class="bx-clear"></div>';
  226. }
  227. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  228. {
  229. $oTemplate = BxDolTemplate::getInstance();
  230. BxDolInformer::getInstance($oTemplate)->setEnabled(false);
  231. $oProfilesQuery = BxDolProfileQuery::getInstance();
  232. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  233. if (!$aProfiles)
  234. return false;
  235. if (null === $iActiveProfileId)
  236. $iActiveProfileId = bx_get_logged_profile_id();
  237. $oModuleDb = BxDolModuleQuery::getInstance();
  238. $aVars = array (
  239. 'bx_repeat:row' => array(),
  240. );
  241. foreach ($aProfiles as $aProfile) {
  242. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  243. continue;
  244. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  245. continue;
  246. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  247. continue;
  248. $aVars['bx_repeat:row'][] = array (
  249. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  250. 'bx_if:active' => array (
  251. 'condition' => $iActiveProfileId == $aProfile['id'],
  252. 'content' => array('id' => $aProfile['id']),
  253. ),
  254. 'bx_if:inactive' => array (
  255. 'condition' => $iActiveProfileId != $aProfile['id'],
  256. 'content' => array(
  257. 'id' => $aProfile['id'],
  258. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  259. 'url_switch' => $sUrlProfileAction ? str_replace('{profile_id}', $aProfile['id'], $sUrlProfileAction) : BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher', array('switch_to_profile' => $aProfile['id'], 'redirect_back' => 1))
  260. ),
  261. ),
  262. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  263. );
  264. }
  265. $oTemplate->addCss('account.css');
  266. return array(
  267. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  268. );
  269. }
  270. protected function _getLatestModuleTimestamp ()
  271. {
  272. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  273. if (empty($aModules))
  274. return 0;
  275. $aModuleNewest = array_pop($aModules);
  276. return $aModuleNewest['date'];
  277. }
  278. protected function _getProfilesModules ()
  279. {
  280. $aRet = array();
  281. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  282. foreach ($aModules as $aModule) {
  283. $oModule = BxDolModule::getInstance($aModule['name']);
  284. if ($oModule instanceof iBxDolProfileService && $oModule->serviceActAsProfile())
  285. $aRet[] = $aModule;
  286. }
  287. return $aRet;
  288. }
  289. }
  290. /** @} */