BxBaseServiceProfiles.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. public function serviceProfileMembershipStats ($iProfileId = 0)
  101. {
  102. if(!$iProfileId)
  103. $iProfileId = bx_get_logged_profile_id();
  104. $sTxtUnlimit = _t('_unlimit');
  105. $sTxtFiles = _t('_sys_storage_files');
  106. $aQuota = BxDolProfileQuery::getInstance()->getProfileQuota($iProfileId);
  107. $aTmplVarsStats = array();
  108. if(!empty($aQuota['quota_size'])) {
  109. $iWidth = (int)round(100 * $aQuota['current_size']/$aQuota['quota_size']);
  110. $sPercent = $iWidth . '%';
  111. }
  112. else {
  113. $iWidth = 0;
  114. $sPercent = $sTxtUnlimit;
  115. }
  116. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_size'), 'width' => $iWidth, 'value' => _t_format_size($aQuota['current_size']), 'percent' => $sPercent);
  117. if(!empty($aQuota['quota_number'])) {
  118. $iWidth = (int)round(100 * $aQuota['current_number']/$aQuota['quota_number']);
  119. $sPercent = $iWidth . '%';
  120. }
  121. else {
  122. $iWidth = 0;
  123. $sPercent = $sTxtUnlimit;
  124. }
  125. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_number'), 'width' => $iWidth, 'value' => $aQuota['current_number'] . ' ' . $sTxtFiles, 'percent' => $sPercent);
  126. return BxDolTemplate::getInstance()->parseHtmlByName('profile_membership_stats.html', array(
  127. 'membership' => BxDolAcl::getInstance()->getProfileMembership($iProfileId),
  128. 'bx_repeat:stat_items' => $aTmplVarsStats
  129. ));
  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-profile_notifications profile_notifications
  136. *
  137. * @code bx_srv('system', 'profile_notifications', [], 'TemplServiceProfiles'); @endcode
  138. * @code {{~system:profile_notifications:TemplServiceProfiles~}} @endcode
  139. *
  140. * Get number of unread notifications for logged in profile
  141. *
  142. * @see BxBaseServiceProfiles::serviceProfileNotifications
  143. */
  144. /**
  145. * @ref bx_system_general-profile_notifications "profile_notifications"
  146. */
  147. public function serviceProfileNotifications ($iProfileId = 0)
  148. {
  149. if (!$iProfileId)
  150. $iProfileId = bx_get_logged_profile_id();
  151. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  152. if(!$oMenu)
  153. return 0;
  154. $iNum = 0;
  155. $aMenuItems = $oMenu->getMenuItems ();
  156. foreach ($aMenuItems as $r) {
  157. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  158. $iNum += $r['bx_if:addon']['content']['addon'];
  159. }
  160. return $iNum;
  161. }
  162. /**
  163. * @page service Service Calls
  164. * @section bx_system_general System Services
  165. * @subsection bx_system_general-profiles Profiles
  166. * @subsubsection bx_system_general-get_count_online_profiles get_count_online_profiles
  167. *
  168. * @code bx_srv('system', 'get_count_online_profiles', [], 'TemplServiceProfiles'); @endcode
  169. * @code {{~system:get_count_online_profiles:TemplServiceProfiles~}} @endcode
  170. *
  171. * Get number of online profiles
  172. *
  173. * @see BxBaseServiceProfiles::serviceGetCountOnlineProfiles
  174. */
  175. /**
  176. * @ref bx_system_general-get_count_online_profiles "get_count_online_profiles"
  177. */
  178. public function serviceGetCountOnlineProfiles ()
  179. {
  180. $oProfilesQuery = BxDolProfileQuery::getInstance();
  181. return $oProfilesQuery->getOnlineCount();
  182. }
  183. public function serviceGetProfilesModules ()
  184. {
  185. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  186. $oDb = BxDolDb::getInstance();
  187. $oCache = $oDb->getDbCacheObject ();
  188. $sKey = $oDb->genDbCacheKey('profiles_modules_array');
  189. $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
  190. $mixedRetTs = $oCache->getData($sKeyTs);
  191. $mixedRet = $oCache->getData($sKey);
  192. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  193. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  194. $aModulesArray = $mixedRet;
  195. } else {
  196. $aModulesArray = $this->_getProfilesModules ();
  197. $oCache->setData($sKey, $aModulesArray);
  198. $oCache->setData($sKeyTs, $iNewestModuleTs);
  199. }
  200. } else {
  201. $aModulesArray = $this->_getProfilesModules ();
  202. }
  203. return $aModulesArray;
  204. }
  205. public function serviceProfilesFriends ($iLimit = 20)
  206. {
  207. if (!($iProfileId = bx_get_logged_profile_id()))
  208. return array();
  209. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  210. if (!$oConnection)
  211. return array();
  212. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  213. return array();
  214. $aRet = array();
  215. foreach ($a as $iId) {
  216. $oProfile = BxDolProfile::getInstance($iId);
  217. $aRet[] = array (
  218. 'label' => $oProfile->getDisplayName(),
  219. 'value' => $iId,
  220. 'url' => $oProfile->getUrl(),
  221. 'thumb' => $oProfile->getThumb(),
  222. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  223. );
  224. }
  225. return $aRet;
  226. }
  227. public function serviceProfilesSearch ($sTerm, $iLimit = 20)
  228. {
  229. // display friends by default
  230. if (!$sTerm)
  231. return $this->serviceProfilesFriends($iLimit);
  232. // get list of "profiles" modules
  233. $aModules = $this->serviceGetProfilesModules();
  234. // search in each module
  235. $a = array();
  236. foreach ($aModules as $aModule) {
  237. if (!BxDolService::call($aModule['name'], 'act_as_profile'))
  238. continue;
  239. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, getParam('sys_per_page_search_keyword_single'))));
  240. }
  241. // sort result
  242. usort($a, function($r1, $r2) {
  243. return strcmp($r1['label'], $r2['label']);
  244. });
  245. bx_alert('system', 'profiles_search', 0, 0, array(
  246. 'term' => $sTerm,
  247. 'result' => &$a
  248. ));
  249. // return as array
  250. return array_slice($a, 0, $iLimit);
  251. }
  252. public function serviceProfilesList ($iAccountId = 0)
  253. {
  254. $oProfilesQuery = BxDolProfileQuery::getInstance();
  255. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  256. if (!$aProfiles)
  257. return false;
  258. $s = '';
  259. foreach ($aProfiles as $aProfile)
  260. if ($aProfile['type'] != 'system')
  261. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  262. if (!$s)
  263. $s = MsgBox(_t('_sys_txt_empty'));
  264. return $s . '<div class="bx-clear"></div>';
  265. }
  266. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  267. {
  268. $oTemplate = BxDolTemplate::getInstance();
  269. BxDolInformer::getInstance($oTemplate)->setEnabled(false);
  270. $oProfilesQuery = BxDolProfileQuery::getInstance();
  271. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  272. if (!$aProfiles)
  273. return false;
  274. if (null === $iActiveProfileId)
  275. $iActiveProfileId = bx_get_logged_profile_id();
  276. $oModuleDb = BxDolModuleQuery::getInstance();
  277. $aVars = array (
  278. 'bx_repeat:row' => array(),
  279. );
  280. foreach ($aProfiles as $aProfile) {
  281. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  282. continue;
  283. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  284. continue;
  285. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  286. continue;
  287. $aVars['bx_repeat:row'][] = array (
  288. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  289. 'bx_if:active' => array (
  290. 'condition' => $iActiveProfileId == $aProfile['id'],
  291. 'content' => array('id' => $aProfile['id']),
  292. ),
  293. 'bx_if:inactive' => array (
  294. 'condition' => $iActiveProfileId != $aProfile['id'],
  295. 'content' => array(
  296. 'id' => $aProfile['id'],
  297. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  298. '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))
  299. ),
  300. ),
  301. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  302. );
  303. }
  304. $oTemplate->addCss('account.css');
  305. return array(
  306. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  307. );
  308. }
  309. protected function _getLatestModuleTimestamp ()
  310. {
  311. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  312. if (empty($aModules))
  313. return 0;
  314. $aModuleNewest = array_pop($aModules);
  315. return $aModuleNewest['date'];
  316. }
  317. protected function _getProfilesModules ()
  318. {
  319. $aRet = array();
  320. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  321. foreach ($aModules as $aModule) {
  322. $oModule = BxDolModule::getInstance($aModule['name']);
  323. if ($oModule instanceof iBxDolProfileService && $oModule->serviceActAsProfile())
  324. $aRet[] = $aModule;
  325. }
  326. return $aRet;
  327. }
  328. }
  329. /** @} */