BxBaseServiceProfiles.php 16 KB

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