BxBaseServiceProfiles.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 serviceProfileAvatar ($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. $oTemplate = BxDolTemplate::getInstance();
  44. $aSwitcher = bx_srv('system', 'account_profile_switcher', array(false, null, '', true), 'TemplServiceProfiles');
  45. $sSwitcher = $aSwitcher !== false ? BxTemplFunctions::getInstance()->transBox('bx-profile-switcher', $oTemplate->parseHtmlByName('profile_avatar_switcher.html', array(
  46. 'profile_switcher' => $aSwitcher['content'],
  47. 'bx_if:multiple_profiles_mode' => array(
  48. 'condition' => (int)getParam('sys_account_limit_profiles_number') != 1,
  49. 'content' => array(
  50. 'url_switch_profile' => BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher')
  51. )
  52. )
  53. )), true) : '';
  54. $sDisplayName = $oProfile->getDisplayName();
  55. $aVars = array(
  56. 'profile_id' => $oProfile->id(),
  57. 'profile_url' => $oProfile->getUrl(),
  58. 'profile_edit_url' => $oProfile->getEditUrl(),
  59. 'profile_title' => $sDisplayName,
  60. 'profile_title_attr' => bx_html_attribute($sDisplayName),
  61. 'profile_ava_url' => $oProfile->getAvatar(),
  62. 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
  63. 'name' => 'unit_wo_info_links',
  64. 'size' => 'ava'
  65. ))),
  66. 'profile_acl_title' => _t($aAclInfo['name']),
  67. 'profile_acl_icon' => $aAclInfo['icon'],
  68. 'profile_switcher' => $sSwitcher
  69. );
  70. return $oTemplate->parseHtmlByName('profile_avatar.html', $aVars);
  71. }
  72. public function serviceProfileMenu ($iProfileId = 0)
  73. {
  74. return BxDolMenu::getObjectInstance('sys_profile_stats')->getCode();
  75. }
  76. public function serviceProfileFollowings ($iProfileId = 0)
  77. {
  78. return BxDolMenu::getObjectInstance('sys_profile_followings')->getCode();
  79. }
  80. public function serviceProfileStats ($iProfileId = 0)
  81. {
  82. if (!$iProfileId && !($iProfileId = bx_get_logged_profile_id()))
  83. return '';
  84. $oProfile = BxDolProfile::getInstance($iProfileId);
  85. if(!$oProfile)
  86. return '';
  87. $oAcl = BxDolAcl::getInstance();
  88. $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
  89. $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
  90. $aVars = array(
  91. 'profile_id' => $oProfile->id(),
  92. 'profile_url' => $oProfile->getUrl(),
  93. 'profile_edit_url' => $oProfile->getEditUrl(),
  94. 'profile_title' => $oProfile->getDisplayName(),
  95. 'profile_title_attr' => bx_html_attribute($oProfile->getDisplayName()),
  96. 'profile_ava_url' => $oProfile->getAvatar(),
  97. 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
  98. 'name' => 'unit_wo_info',
  99. 'size' => 'ava'
  100. ))),
  101. 'profile_acl_title' => _t($aAclInfo['name']),
  102. 'profile_acl_icon' => $aAclInfo['icon'],
  103. 'menu' => BxDolMenu::getObjectInstance('sys_profile_stats')->getCode(),
  104. );
  105. return BxDolTemplate::getInstance()->parseHtmlByName('profile_stats.html', $aVars);
  106. }
  107. public function serviceGetMenuAddonProfileEdit($iProfileId = 0, $sCaption = '')
  108. {
  109. /**
  110. * Disabled.
  111. */
  112. return '';
  113. if(empty($sCaption))
  114. $sCaption = _t('_Edit');
  115. $oProfile = BxDolProfile::getInstance($iProfileId);
  116. if(!$oProfile)
  117. return '';
  118. $sModule = $oProfile->getModule();
  119. $sMethod = 'profile_edit_url';
  120. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  121. return '';
  122. return BxDolTemplate::getInstance()->parseLink(BxDolService::call($sModule, $sMethod, array($oProfile->getContentId())), $sCaption);
  123. }
  124. /**
  125. * @page service Service Calls
  126. * @section bx_system_general System Services
  127. * @subsection bx_system_general-profiles Profiles
  128. * @subsubsection bx_system_general-profile_membership profile_membership
  129. *
  130. * @code bx_srv('system', 'profile_membership', [2], 'TemplServiceProfiles'); @endcode
  131. * @code {{~system:profile_membership:TemplServiceProfiles[2]~}} @endcode
  132. *
  133. * Get membership level for specified profile.
  134. * @param $iProfileId profile ID
  135. *
  136. * @see BxBaseServiceProfiles::serviceProfileMembership
  137. */
  138. /**
  139. * @ref bx_system_general-profile_membership "profile_membership"
  140. */
  141. public function serviceProfileMembership ($iProfileId = 0)
  142. {
  143. if (!$iProfileId)
  144. $iProfileId = bx_get_logged_profile_id();
  145. return BxDolAcl::getInstance()->getProfileMembership($iProfileId);
  146. }
  147. public function serviceProfileMembershipStats ($iProfileId = 0)
  148. {
  149. if(!$iProfileId)
  150. $iProfileId = bx_get_logged_profile_id();
  151. $sTxtUnlimited = _t('_unlimited');
  152. $sTxtFiles = _t('_sys_storage_files');
  153. $aQuota = BxDolProfileQuery::getInstance()->getProfileQuota($iProfileId);
  154. $aTmplVarsStats = array();
  155. if(!empty($aQuota['quota_size'])) {
  156. $iWidth = (int)round(100 * $aQuota['current_size']/$aQuota['quota_size']);
  157. $sPercent = $iWidth . '%';
  158. }
  159. else {
  160. $iWidth = 0;
  161. $sPercent = $sTxtUnlimited;
  162. }
  163. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_size'), 'width' => $iWidth, 'value' => _t_format_size($aQuota['current_size']), 'percent' => $sPercent);
  164. if(!empty($aQuota['quota_number'])) {
  165. $iWidth = (int)round(100 * $aQuota['current_number']/$aQuota['quota_number']);
  166. $sPercent = $iWidth . '%';
  167. }
  168. else {
  169. $iWidth = 0;
  170. $sPercent = $sTxtUnlimited;
  171. }
  172. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_number'), 'width' => $iWidth, 'value' => $aQuota['current_number'] . ' ' . $sTxtFiles, 'percent' => $sPercent);
  173. return BxDolTemplate::getInstance()->parseHtmlByName('profile_membership_stats.html', array(
  174. 'membership' => BxDolAcl::getInstance()->getProfileMembership($iProfileId),
  175. 'bx_repeat:stat_items' => $aTmplVarsStats
  176. ));
  177. }
  178. /**
  179. * @page service Service Calls
  180. * @section bx_system_general System Services
  181. * @subsection bx_system_general-profiles Profiles
  182. * @subsubsection bx_system_general-profile_notifications profile_notifications
  183. *
  184. * @code bx_srv('system', 'profile_notifications', [], 'TemplServiceProfiles'); @endcode
  185. * @code {{~system:profile_notifications:TemplServiceProfiles~}} @endcode
  186. *
  187. * Get number of unread notifications for logged in profile
  188. *
  189. * @see BxBaseServiceProfiles::serviceProfileNotifications
  190. */
  191. /**
  192. * @ref bx_system_general-profile_notifications "profile_notifications"
  193. */
  194. public function serviceProfileNotifications ($iProfileId = 0)
  195. {
  196. if (!$iProfileId)
  197. $iProfileId = bx_get_logged_profile_id();
  198. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  199. if(!$oMenu)
  200. return 0;
  201. $iNum = 0;
  202. $aMenuItems = $oMenu->getMenuItems ();
  203. foreach ($aMenuItems as $r) {
  204. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  205. $iNum += $r['bx_if:addon']['content']['addon'];
  206. }
  207. return $iNum;
  208. }
  209. /**
  210. * @page service Service Calls
  211. * @section bx_system_general System Services
  212. * @subsection bx_system_general-profiles Profiles
  213. * @subsubsection bx_system_general-get_count_online_profiles get_count_online_profiles
  214. *
  215. * @code bx_srv('system', 'get_count_online_profiles', [], 'TemplServiceProfiles'); @endcode
  216. * @code {{~system:get_count_online_profiles:TemplServiceProfiles~}} @endcode
  217. *
  218. * Get number of online profiles
  219. *
  220. * @see BxBaseServiceProfiles::serviceGetCountOnlineProfiles
  221. */
  222. /**
  223. * @ref bx_system_general-get_count_online_profiles "get_count_online_profiles"
  224. */
  225. public function serviceGetCountOnlineProfiles ()
  226. {
  227. $oProfilesQuery = BxDolProfileQuery::getInstance();
  228. return $oProfilesQuery->getOnlineCount();
  229. }
  230. public function serviceGetProfilesModules ()
  231. {
  232. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  233. $oDb = BxDolDb::getInstance();
  234. $oCache = $oDb->getDbCacheObject ();
  235. $sKey = $oDb->genDbCacheKey('profiles_modules_array');
  236. $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
  237. $mixedRetTs = $oCache->getData($sKeyTs);
  238. $mixedRet = $oCache->getData($sKey);
  239. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  240. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  241. $aModulesArray = $mixedRet;
  242. } else {
  243. $aModulesArray = $this->_getProfilesModules ();
  244. $oCache->setData($sKey, $aModulesArray);
  245. $oCache->setData($sKeyTs, $iNewestModuleTs);
  246. }
  247. } else {
  248. $aModulesArray = $this->_getProfilesModules ();
  249. }
  250. return $aModulesArray;
  251. }
  252. public function serviceProfilesFriends ($iLimit = 20)
  253. {
  254. if (!($iProfileId = bx_get_logged_profile_id()))
  255. return array();
  256. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  257. if (!$oConnection)
  258. return array();
  259. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  260. return array();
  261. $aRet = array();
  262. foreach ($a as $iId) {
  263. $oProfile = BxDolProfile::getInstance($iId);
  264. $aRet[] = array (
  265. 'label' => $oProfile->getDisplayName(),
  266. 'value' => $iId,
  267. 'url' => $oProfile->getUrl(),
  268. 'thumb' => $oProfile->getThumb(),
  269. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  270. );
  271. }
  272. return $aRet;
  273. }
  274. public function serviceProfilesSearch ($sTerm, $iLimit = 20)
  275. {
  276. // display friends by default
  277. if (!$sTerm)
  278. return $this->serviceProfilesFriends($iLimit);
  279. // get list of "profiles" modules
  280. $aModules = $this->serviceGetProfilesModules();
  281. // search in each module
  282. $a = array();
  283. foreach ($aModules as $aModule) {
  284. if (!BxDolService::call($aModule['name'], 'act_as_profile'))
  285. continue;
  286. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, getParam('sys_per_page_search_keyword_single'))));
  287. }
  288. // sort result
  289. usort($a, function($r1, $r2) {
  290. return strcmp($r1['label'], $r2['label']);
  291. });
  292. bx_alert('system', 'profiles_search', 0, 0, array(
  293. 'term' => $sTerm,
  294. 'result' => &$a
  295. ));
  296. // return as array
  297. return array_slice($a, 0, $iLimit);
  298. }
  299. public function serviceProfilesList ($iAccountId = 0)
  300. {
  301. $oProfilesQuery = BxDolProfileQuery::getInstance();
  302. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  303. if (!$aProfiles)
  304. return false;
  305. $s = '';
  306. foreach ($aProfiles as $aProfile)
  307. if ($aProfile['type'] != 'system')
  308. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  309. if (!$s)
  310. $s = MsgBox(_t('_sys_txt_empty'));
  311. return $s . '<div class="bx-clear"></div>';
  312. }
  313. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  314. {
  315. $oTemplate = BxDolTemplate::getInstance();
  316. BxDolInformer::getInstance($oTemplate)->setEnabled(false);
  317. $oProfilesQuery = BxDolProfileQuery::getInstance();
  318. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  319. if (!$aProfiles)
  320. return false;
  321. if (null === $iActiveProfileId)
  322. $iActiveProfileId = bx_get_logged_profile_id();
  323. $oModuleDb = BxDolModuleQuery::getInstance();
  324. $aVars = array (
  325. 'bx_repeat:row' => array(),
  326. );
  327. foreach ($aProfiles as $aProfile) {
  328. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  329. continue;
  330. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  331. continue;
  332. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  333. continue;
  334. $aVars['bx_repeat:row'][] = array (
  335. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  336. 'bx_if:active' => array (
  337. 'condition' => $iActiveProfileId == $aProfile['id'],
  338. 'content' => array('id' => $aProfile['id']),
  339. ),
  340. 'bx_if:inactive' => array (
  341. 'condition' => $iActiveProfileId != $aProfile['id'],
  342. 'content' => array(
  343. 'id' => $aProfile['id'],
  344. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  345. '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))
  346. ),
  347. ),
  348. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  349. );
  350. }
  351. $oTemplate->addCss('account.css');
  352. return array(
  353. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  354. );
  355. }
  356. protected function _getLatestModuleTimestamp ()
  357. {
  358. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  359. if (empty($aModules))
  360. return 0;
  361. $aModuleNewest = array_pop($aModules);
  362. return $aModuleNewest['date'];
  363. }
  364. protected function _getProfilesModules ()
  365. {
  366. $aRet = array();
  367. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  368. foreach ($aModules as $aModule) {
  369. $oModule = BxDolModule::getInstance($aModule['name']);
  370. if ($oModule instanceof iBxDolProfileService && $oModule->serviceActAsProfile())
  371. $aRet[] = $aModule;
  372. }
  373. return $aRet;
  374. }
  375. }
  376. /** @} */