BxBaseServiceProfiles.php 18 KB

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