1
0

BxBaseServiceProfiles.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. /**
  63. * Disabled.
  64. */
  65. return '';
  66. if(empty($sCaption))
  67. $sCaption = _t('_Edit');
  68. $oProfile = BxDolProfile::getInstance($iProfileId);
  69. if(!$oProfile)
  70. return '';
  71. $sModule = $oProfile->getModule();
  72. $sMethod = 'profile_edit_url';
  73. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  74. return '';
  75. return BxDolTemplate::getInstance()->parseLink(BxDolService::call($sModule, $sMethod, array($oProfile->getContentId())), $sCaption);
  76. }
  77. /**
  78. * @page service Service Calls
  79. * @section bx_system_general System Services
  80. * @subsection bx_system_general-profiles Profiles
  81. * @subsubsection bx_system_general-profile_membership profile_membership
  82. *
  83. * @code bx_srv('system', 'profile_membership', [2], 'TemplServiceProfiles'); @endcode
  84. * @code {{~system:profile_membership:TemplServiceProfiles[2]~}} @endcode
  85. *
  86. * Get membership level for specified profile.
  87. * @param $iProfileId profile ID
  88. *
  89. * @see BxBaseServiceProfiles::serviceProfileMembership
  90. */
  91. /**
  92. * @ref bx_system_general-profile_membership "profile_membership"
  93. */
  94. public function serviceProfileMembership ($iProfileId = 0)
  95. {
  96. if (!$iProfileId)
  97. $iProfileId = bx_get_logged_profile_id();
  98. return BxDolAcl::getInstance()->getProfileMembership($iProfileId);
  99. }
  100. /**
  101. * @page service Service Calls
  102. * @section bx_system_general System Services
  103. * @subsection bx_system_general-profiles Profiles
  104. * @subsubsection bx_system_general-profile_notifications profile_notifications
  105. *
  106. * @code bx_srv('system', 'profile_notifications', [], 'TemplServiceProfiles'); @endcode
  107. * @code {{~system:profile_notifications:TemplServiceProfiles~}} @endcode
  108. *
  109. * Get number of unread notifications for logged in profile
  110. *
  111. * @see BxBaseServiceProfiles::serviceProfileNotifications
  112. */
  113. /**
  114. * @ref bx_system_general-profile_notifications "profile_notifications"
  115. */
  116. public function serviceProfileNotifications ($iProfileId = 0)
  117. {
  118. if (!$iProfileId)
  119. $iProfileId = bx_get_logged_profile_id();
  120. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  121. if(!$oMenu)
  122. return 0;
  123. $iNum = 0;
  124. $aMenuItems = $oMenu->getMenuItems ();
  125. foreach ($aMenuItems as $r) {
  126. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  127. $iNum += $r['bx_if:addon']['content']['addon'];
  128. }
  129. return $iNum;
  130. }
  131. /**
  132. * @page service Service Calls
  133. * @section bx_system_general System Services
  134. * @subsection bx_system_general-profiles Profiles
  135. * @subsubsection bx_system_general-get_count_online_profiles get_count_online_profiles
  136. *
  137. * @code bx_srv('system', 'get_count_online_profiles', [], 'TemplServiceProfiles'); @endcode
  138. * @code {{~system:get_count_online_profiles:TemplServiceProfiles~}} @endcode
  139. *
  140. * Get number of online profiles
  141. *
  142. * @see BxBaseServiceProfiles::serviceGetCountOnlineProfiles
  143. */
  144. /**
  145. * @ref bx_system_general-get_count_online_profiles "get_count_online_profiles"
  146. */
  147. public function serviceGetCountOnlineProfiles ()
  148. {
  149. $oProfilesQuery = BxDolProfileQuery::getInstance();
  150. return $oProfilesQuery->getOnlineCount();
  151. }
  152. public function serviceGetProfilesModules ()
  153. {
  154. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  155. $oDb = BxDolDb::getInstance();
  156. $oCache = $oDb->getDbCacheObject ();
  157. $sKey = $oDb->genDbCacheKey('profiles_modules_array');
  158. $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
  159. $mixedRetTs = $oCache->getData($sKeyTs);
  160. $mixedRet = $oCache->getData($sKey);
  161. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  162. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  163. $aModulesArray = $mixedRet;
  164. } else {
  165. $aModulesArray = $this->_getProfilesModules ();
  166. $oCache->setData($sKey, $aModulesArray);
  167. $oCache->setData($sKeyTs, $iNewestModuleTs);
  168. }
  169. } else {
  170. $aModulesArray = $this->_getProfilesModules ();
  171. }
  172. return $aModulesArray;
  173. }
  174. public function serviceProfilesFriends ($iLimit = 20)
  175. {
  176. if (!($iProfileId = bx_get_logged_profile_id()))
  177. return array();
  178. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  179. if (!$oConnection)
  180. return array();
  181. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  182. return array();
  183. $aRet = array();
  184. foreach ($a as $iId) {
  185. $oProfile = BxDolProfile::getInstance($iId);
  186. $aRet[] = array (
  187. 'label' => $oProfile->getDisplayName(),
  188. 'value' => $iId,
  189. 'url' => $oProfile->getUrl(),
  190. 'thumb' => $oProfile->getThumb(),
  191. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  192. );
  193. }
  194. return $aRet;
  195. }
  196. public function serviceProfilesSearch ($sTerm, $iLimit = 20)
  197. {
  198. // display friends by default
  199. if (!$sTerm)
  200. return $this->serviceProfilesFriends($iLimit);
  201. // get list of "profiles" modules
  202. $aModules = $this->serviceGetProfilesModules();
  203. // search in each module
  204. $a = array();
  205. foreach ($aModules as $aModule) {
  206. if (!BxDolService::call($aModule['name'], 'act_as_profile'))
  207. continue;
  208. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, getParam('sys_per_page_search_keyword_single'))));
  209. }
  210. // sort result
  211. usort($a, function($r1, $r2) {
  212. return strcmp($r1['label'], $r2['label']);
  213. });
  214. // return as array
  215. return array_slice($a, 0, $iLimit);
  216. }
  217. public function serviceProfilesList ($iAccountId = 0)
  218. {
  219. $oProfilesQuery = BxDolProfileQuery::getInstance();
  220. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  221. if (!$aProfiles)
  222. return false;
  223. $s = '';
  224. foreach ($aProfiles as $aProfile)
  225. if ($aProfile['type'] != 'system')
  226. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  227. if (!$s)
  228. $s = MsgBox(_t('_sys_txt_empty'));
  229. return $s . '<div class="bx-clear"></div>';
  230. }
  231. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  232. {
  233. $oTemplate = BxDolTemplate::getInstance();
  234. BxDolInformer::getInstance($oTemplate)->setEnabled(false);
  235. $oProfilesQuery = BxDolProfileQuery::getInstance();
  236. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  237. if (!$aProfiles)
  238. return false;
  239. if (null === $iActiveProfileId)
  240. $iActiveProfileId = bx_get_logged_profile_id();
  241. $oModuleDb = BxDolModuleQuery::getInstance();
  242. $aVars = array (
  243. 'bx_repeat:row' => array(),
  244. );
  245. foreach ($aProfiles as $aProfile) {
  246. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  247. continue;
  248. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  249. continue;
  250. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  251. continue;
  252. $aVars['bx_repeat:row'][] = array (
  253. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  254. 'bx_if:active' => array (
  255. 'condition' => $iActiveProfileId == $aProfile['id'],
  256. 'content' => array('id' => $aProfile['id']),
  257. ),
  258. 'bx_if:inactive' => array (
  259. 'condition' => $iActiveProfileId != $aProfile['id'],
  260. 'content' => array(
  261. 'id' => $aProfile['id'],
  262. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  263. '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))
  264. ),
  265. ),
  266. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  267. );
  268. }
  269. $oTemplate->addCss('account.css');
  270. return array(
  271. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  272. );
  273. }
  274. protected function _getLatestModuleTimestamp ()
  275. {
  276. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  277. if (empty($aModules))
  278. return 0;
  279. $aModuleNewest = array_pop($aModules);
  280. return $aModuleNewest['date'];
  281. }
  282. protected function _getProfilesModules ()
  283. {
  284. $aRet = array();
  285. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  286. foreach ($aModules as $aModule) {
  287. $oModule = BxDolModule::getInstance($aModule['name']);
  288. if ($oModule instanceof iBxDolProfileService && $oModule->serviceActAsProfile())
  289. $aRet[] = $aModule;
  290. }
  291. return $aRet;
  292. }
  293. }
  294. /** @} */