BxBaseServiceProfiles.php 19 KB

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