| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaBaseView UNA Base Representation Classes
- * @{
- */
- /**
- * System service for profiles handling functionality.
- */
- class BxBaseServiceProfiles extends BxDol
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function serviceGetProfileTypes()
- {
- $aTypes = array(
- '' => _t('_Select_one')
- );
- $aModules = $this->_getProfilesModules();
- foreach($aModules as $aModule) {
- if(empty($aModule) || !is_array($aModule))
- continue;
- $sTitleKey = '_' . $aModule['name'];
- $sTitle = _t($sTitleKey);
- $aTypes[$aModule['name']] = !empty($sTitle) && $sTitle != $sTitleKey ? $sTitle : $aModule['title'];
- }
- return $aTypes;
- }
- public function serviceProfileAvatar ($iProfileId = 0)
- {
- if (!$iProfileId && !($iProfileId = bx_get_logged_profile_id()))
- return '';
- $oProfile = BxDolProfile::getInstance($iProfileId);
- if(!$oProfile)
- return '';
- $oAcl = BxDolAcl::getInstance();
- $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
- $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
- $oTemplate = BxDolTemplate::getInstance();
- $aSwitcher = bx_srv('system', 'account_profile_switcher', array(false, null, '', true), 'TemplServiceProfiles');
- $sSwitcher = $aSwitcher !== false ? BxTemplFunctions::getInstance()->transBox('bx-profile-switcher', $oTemplate->parseHtmlByName('profile_avatar_switcher.html', array(
- 'profile_switcher' => $aSwitcher['content'],
- 'bx_if:multiple_profiles_mode' => array(
- 'condition' => (int)getParam('sys_account_limit_profiles_number') != 1,
- 'content' => array(
- 'url_switch_profile' => BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher')
- )
- )
- )), true) : '';
- $sDisplayName = $oProfile->getDisplayName();
- $aVars = array(
- 'profile_id' => $oProfile->id(),
- 'profile_url' => $oProfile->getUrl(),
- 'profile_edit_url' => $oProfile->getEditUrl(),
- 'profile_title' => $sDisplayName,
- 'profile_title_attr' => bx_html_attribute($sDisplayName),
- 'profile_ava_url' => $oProfile->getAvatar(),
- 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
- 'name' => 'unit_wo_info_links',
- 'size' => 'thumb'
- ))),
- 'profile_acl_title' => _t($aAclInfo['name']),
- 'profile_acl_icon' => $aAclInfo['icon'],
- 'profile_switcher' => $sSwitcher
- );
- return $oTemplate->parseHtmlByName('profile_avatar.html', $aVars);
- }
- public function serviceProfileMenu ($iProfileId = 0)
- {
- return BxDolMenu::getObjectInstance('sys_profile_stats')->getCode();
- }
- public function serviceProfileFollowings ($iProfileId = 0)
- {
- return BxDolMenu::getObjectInstance('sys_profile_followings')->getCode();
- }
- public function serviceProfileStats ($iProfileId = 0)
- {
- if (!$iProfileId && !($iProfileId = bx_get_logged_profile_id()))
- return '';
- $oProfile = BxDolProfile::getInstance($iProfileId);
- if(!$oProfile)
- return '';
- $oAcl = BxDolAcl::getInstance();
- $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
- $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
- list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getIcon($aAclInfo['icon']);
-
- $aVars = array(
- 'profile_id' => $oProfile->id(),
- 'profile_url' => $oProfile->getUrl(),
- 'profile_edit_url' => $oProfile->getEditUrl(),
- 'profile_title' => $oProfile->getDisplayName(),
- 'profile_title_attr' => bx_html_attribute($oProfile->getDisplayName()),
- 'profile_ava_url' => $oProfile->getAvatar(),
- 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
- 'name' => 'unit_wo_info',
- 'size' => 'ava'
- ))),
- 'profile_acl_title' => _t($aAclInfo['name']),
- 'menu' => BxDolMenu::getObjectInstance('sys_profile_stats')->getCode(),
- );
-
- $aVars['bx_if:image'] = array (
- 'condition' => (bool)$sIconUrl,
- 'content' => array('icon_url' => $sIconUrl),
- );
- $aVars['bx_if:image_inline'] = array (
- 'condition' => false,
- 'content' => array('image' => ''),
- );
- $aVars['bx_if:icon'] = array (
- 'condition' => (bool)$sIcon,
- 'content' => array('icon' => $sIcon),
- );
- $a['bx_if:icon-html'] = array (
- 'condition' => (bool)$sIconHtml,
- 'content' => array('icon' => $sIconHtml),
- );
- $aVars['bx_if:icon-a'] = array (
- 'condition' => (bool)$sIconA,
- 'content' => array('icon-a' => $sIconA),
- );
- return BxDolTemplate::getInstance()->parseHtmlByName('profile_stats.html', $aVars);
- }
- public function serviceGetMenuAddonProfileEdit($iProfileId = 0, $sCaption = '')
- {
- /**
- * Disabled.
- */
- return '';
- if(empty($sCaption))
- $sCaption = _t('_Edit');
- $oProfile = BxDolProfile::getInstance($iProfileId);
- if(!$oProfile)
- return '';
- $sModule = $oProfile->getModule();
- $sMethod = 'profile_edit_url';
- if(!BxDolRequest::serviceExists($sModule, $sMethod))
- return '';
- return BxDolTemplate::getInstance()->parseLink(BxDolService::call($sModule, $sMethod, array($oProfile->getContentId())), $sCaption);
- }
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-profiles Profiles
- * @subsubsection bx_system_general-profile_membership profile_membership
- *
- * @code bx_srv('system', 'profile_membership', [2], 'TemplServiceProfiles'); @endcode
- * @code {{~system:profile_membership:TemplServiceProfiles[2]~}} @endcode
- *
- * Get membership level for specified profile.
- * @param $iProfileId profile ID
- *
- * @see BxBaseServiceProfiles::serviceProfileMembership
- */
- /**
- * @ref bx_system_general-profile_membership "profile_membership"
- */
- public function serviceProfileMembership ($iProfileId = 0)
- {
- if (!$iProfileId)
- $iProfileId = bx_get_logged_profile_id();
- return BxDolAcl::getInstance()->getProfileMembership($iProfileId);
- }
- public function serviceProfileMembershipStats ($iProfileId = 0)
- {
- if(!$iProfileId)
- $iProfileId = bx_get_logged_profile_id();
- $sTxtUnlimited = _t('_unlimited');
- $sTxtFiles = _t('_sys_storage_files');
- $aQuota = BxDolProfileQuery::getInstance()->getProfileQuota($iProfileId);
- $aTmplVarsStats = array();
- if(!empty($aQuota['quota_size'])) {
- $iWidth = (int)round(100 * $aQuota['current_size']/$aQuota['quota_size']);
- $sPercent = $iWidth . '%';
- }
- else {
- $iWidth = 0;
- $sPercent = $sTxtUnlimited;
- }
- $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_size'), 'width' => $iWidth, 'value' => _t_format_size($aQuota['current_size']), 'percent' => $sPercent);
- if(!empty($aQuota['quota_number'])) {
- $iWidth = (int)round(100 * $aQuota['current_number']/$aQuota['quota_number']);
- $sPercent = $iWidth . '%';
- }
- else {
- $iWidth = 0;
- $sPercent = $sTxtUnlimited;
- }
- $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_number'), 'width' => $iWidth, 'value' => $aQuota['current_number'] . ' ' . $sTxtFiles, 'percent' => $sPercent);
- return BxDolTemplate::getInstance()->parseHtmlByName('profile_membership_stats.html', array(
- 'membership' => BxDolAcl::getInstance()->getProfileMembership($iProfileId),
- 'bx_repeat:stat_items' => $aTmplVarsStats
- ));
- }
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-profiles Profiles
- * @subsubsection bx_system_general-profile_notifications profile_notifications
- *
- * @code bx_srv('system', 'profile_notifications', [], 'TemplServiceProfiles'); @endcode
- * @code {{~system:profile_notifications:TemplServiceProfiles~}} @endcode
- *
- * Get number of unread notifications for logged in profile
- *
- * @see BxBaseServiceProfiles::serviceProfileNotifications
- */
- /**
- * @ref bx_system_general-profile_notifications "profile_notifications"
- */
- public function serviceProfileNotifications ($iProfileId = 0)
- {
- if (!$iProfileId)
- $iProfileId = bx_get_logged_profile_id();
- $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
- if(!$oMenu)
- return 0;
- $iNum = 0;
- $aMenuItems = $oMenu->getMenuItems ();
- foreach ($aMenuItems as $r) {
- if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
- $iNum += $r['bx_if:addon']['content']['addon'];
- }
- return $iNum;
- }
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-profiles Profiles
- * @subsubsection bx_system_general-get_count_online_profiles get_count_online_profiles
- *
- * @code bx_srv('system', 'get_count_online_profiles', [], 'TemplServiceProfiles'); @endcode
- * @code {{~system:get_count_online_profiles:TemplServiceProfiles~}} @endcode
- *
- * Get number of online profiles
- *
- * @see BxBaseServiceProfiles::serviceGetCountOnlineProfiles
- */
- /**
- * @ref bx_system_general-get_count_online_profiles "get_count_online_profiles"
- */
- public function serviceGetCountOnlineProfiles ()
- {
- $oProfilesQuery = BxDolProfileQuery::getInstance();
- return $oProfilesQuery->getOnlineCount();
- }
-
- public function serviceGetProfilesModules ($bForceActAsProfile = true)
- {
- if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
- $oDb = BxDolDb::getInstance();
- $oCache = $oDb->getDbCacheObject ();
- $sKey = $oDb->genDbCacheKey('profiles_' . (!$bForceActAsProfile ? 'all_' : '') . 'modules_array');
- $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
- $mixedRetTs = $oCache->getData($sKeyTs);
- $mixedRet = $oCache->getData($sKey);
- $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
- if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
- $aModulesArray = $mixedRet;
- } else {
- $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
- $oCache->setData($sKey, $aModulesArray);
- $oCache->setData($sKeyTs, $iNewestModuleTs);
- }
- } else {
- $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
- }
- return $aModulesArray;
- }
- public function serviceProfilesFriends ($iLimit = 20)
- {
- if (!($iProfileId = bx_get_logged_profile_id()))
- return array();
-
- $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
- if (!$oConnection)
- return array();
- if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
- return array();
- $aRet = array();
- foreach ($a as $iId) {
- $oProfile = BxDolProfile::getInstance($iId);
- $aRet[] = array (
- 'label' => $oProfile->getDisplayName(),
- 'value' => $iId,
- 'url' => $oProfile->getUrl(),
- 'thumb' => $oProfile->getThumb(),
- 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
- );
- }
- return $aRet;
- }
- public function serviceProfilesSearch ($sTerm, $iLimit = 20)
- {
- // display friends by default
- if (!$sTerm)
- return $this->serviceProfilesFriends($iLimit);
- // get list of "profiles" modules
- $aModules = $this->serviceGetProfilesModules();
- // search in each module
- $a = array();
- foreach ($aModules as $aModule) {
- if (!BxDolService::call($aModule['name'], 'act_as_profile'))
- continue;
- $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, getParam('sys_per_page_search_keyword_single'))));
- }
- // sort result
- usort($a, function($r1, $r2) {
- return strcmp($r1['label'], $r2['label']);
- });
- bx_alert('system', 'profiles_search', 0, 0, array(
- 'term' => $sTerm,
- 'result' => &$a
- ));
-
- // return as array
- return array_slice($a, 0, $iLimit);
- }
- public function serviceProfilesList ($iAccountId = 0)
- {
- $oProfilesQuery = BxDolProfileQuery::getInstance();
- $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
- if (!$aProfiles)
- return false;
- $s = '';
- foreach ($aProfiles as $aProfile)
- if ($aProfile['type'] != 'system')
- $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
- if (!$s)
- $s = MsgBox(_t('_sys_txt_empty'));
- return $s . '<div class="bx-clear"></div>';
- }
- public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
- {
- $oTemplate = BxDolTemplate::getInstance();
- BxDolInformer::getInstance($oTemplate)->setEnabled(false);
- $oProfilesQuery = BxDolProfileQuery::getInstance();
- $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
- if (!$aProfiles)
- return false;
- if (null === $iActiveProfileId)
- $iActiveProfileId = bx_get_logged_profile_id();
- $oModuleDb = BxDolModuleQuery::getInstance();
- $aVars = array (
- 'bx_repeat:row' => array(),
- );
-
- bx_alert('system', 'account_profile_switcher', 0, false, array(
- 'account_id' => $iAccountId,
- 'active_profile_id' => &$iActiveProfileId,
- 'url_profile_action' => &$sUrlProfileAction,
- 'show_all' => &$bShowAll,
- 'button_title' => &$sButtonTitle,
- 'profile_template' => &$sProfileTemplate,
- 'profiles' => &$aProfiles
- ));
-
- foreach ($aProfiles as $aProfile) {
- if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
- continue;
- if(!$oModuleDb->isEnabledByName($aProfile['type']))
- continue;
- if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
- continue;
- $aVars['bx_repeat:row'][] = array (
- 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
- 'bx_if:active' => array (
- 'condition' => $iActiveProfileId == $aProfile['id'],
- 'content' => array('id' => $aProfile['id']),
- ),
- 'bx_if:inactive' => array (
- 'condition' => $iActiveProfileId != $aProfile['id'],
- 'content' => array(
- 'id' => $aProfile['id'],
- 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
- '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))
- ),
- ),
- 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
- );
- }
- $oTemplate->addCss('account.css');
- return array(
- 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
- );
- }
- protected function _getIcon ($sIcon)
- {
- $sIconFont = false;
- $sIconA = false;
- $sIconUrl = false;
- $sIconHtml = false;
- if ($sIcon != '') {
- if ((int)$sIcon > 0 ) {
- $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
- $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$sIcon) : false;
- } else {
- //svg
- if (strpos($sIcon, '<svg') !== false){
- $sIconHtml = $sIcon;
- $sClass = 'sys-icon sys-icon-svg-inline ';
- if ($sClass != '' && strpos($sIconHtml, 'class="') !== false)
- $sIconHtml = str_replace('class="', 'class="' . $sClass, $sIconHtml);
- else
- $sIconHtml = str_replace('<svg', '<svg class="' . $sClass . '" ', $sIconHtml);
- }
- else{
- //emoji
- 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)){
- $sIconHtml = $this->_oTemplate->parseHtmlByName('icon_emoji.html', array('icon' => $sIcon));
- }
- else{
- if (false === strpos($sIcon, '.')) {
- if (0 === strncmp($sIcon, 'a:', 2))
- $sIconA = substr($sIcon, 2); // animated icon
- else
- $sIconFont = $sIcon; // font icons
- } else {
- $sIconUrl = $this->_oTemplate->getIconUrl($sIcon);
- }
- }
- }
- }
- }
- return array ($sIconFont, $sIconUrl, $sIconA, $sIconHtml);
- }
-
- protected function _getLatestModuleTimestamp ()
- {
- $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
- if (empty($aModules))
- return 0;
- $aModuleNewest = array_pop($aModules);
- return $aModuleNewest['date'];
- }
- protected function _getProfilesModules($bForceActAsProfile = true)
- {
- $aRet = array();
- $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
- foreach($aModules as $aModule) {
- $oModule = BxDolModule::getInstance($aModule['name']);
- if(!($oModule instanceof iBxDolProfileService))
- continue;
- if($bForceActAsProfile && !$oModule->serviceActAsProfile())
- continue;
- $aRet[] = $aModule;
- }
- return $aRet;
- }
- }
- /** @} */
|