1
0

BxBaseServiceProfiles.php 19 KB

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