| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
- * CC-BY License - http://creativecommons.org/licenses/by/3.0/
- *
- * @defgroup DolphinCore Dolphin Core
- * @{
- */
- /**
- * System service for profiles handling functionality.
- */
- class BxBaseServiceProfiles extends BxDol
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function serviceProfileStats ($iProfileId = 0)
- {
- if (!$iProfileId)
- $iProfileId = bx_get_logged_profile_id();
- bx_import('BxDolMenu');
- $oMenu = BxDolMenu::getObjectInstance('sys_profile_stats');
- bx_import('BxDolProfile');
- $oProfile = BxDolProfile::getInstance($iProfileId);
-
- $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(),
- 'menu' => $oMenu->getCode(),
- );
- $oTemplate = BxDolTemplate::getInstance();
- return $oTemplate->parseHtmlByName('profile_stats.html', $aVars);
- }
- public function serviceProfileNotifications ($iProfileId = 0)
- {
- if (!$iProfileId)
- $iProfileId = bx_get_logged_profile_id();
- bx_import('BxDolMenu');
- $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
- $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;
- }
- public function serviceGetProfilesModules ()
- {
- if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
- $oCache = $this->getDbCacheObject ();
- $sKey = $this->genDbCacheKey('profiles_modules_array');
- $sKeyTs = $this->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 ();
- $oCache->setData($sKey, $aModulesArray);
- $oCache->setData($sKeyTs, $iNewestModuleTs);
- }
- } else {
- $aModulesArray = $this->_getProfilesModules ();
- }
- return $aModulesArray;
- }
- public function serviceProfilesSearch ($sTerm, $iLimit = 20)
- {
- // get list of "profiles" modules
- $aModules = $this->serviceGetProfilesModules();
- // search in each module
- $a = array();
- foreach ($aModules as $aModule)
- $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, 10)));
- // sort result
- usort($a, function($r1, $r2) {
- return strcmp($r1['label'], $r2['label']);
- });
- // return as array
- return array_slice($a, 0, $iLimit);
- }
- public function serviceProfilesList ($iAccountId = 0)
- {
- bx_import('BxDolProfileQuery');
- $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)
- {
- bx_import('BxDolProfileQuery');
- $oProfilesQuery = BxDolProfileQuery::getInstance();
- $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
- if (!$aProfiles)
- return false;
-
- $iLoggedPofileId = bx_get_logged_profile_id();
- $aVars = array (
- 'bx_repeat:row' => array(),
- );
- foreach ($aProfiles as $aProfile) {
- if ($aProfile['type'] == 'system') // skip system account profile
- continue;
- $aVars['bx_repeat:row'][] = array (
- 'bx_if:active' => array (
- 'condition' => $iLoggedPofileId == $aProfile['id'],
- 'content' => array('id' => $aProfile['id']),
- ),
- 'bx_if:inactive' => array (
- 'condition' => $iLoggedPofileId != $aProfile['id'],
- 'content' => array('id' => $aProfile['id'], 'url_switch' => BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher', array('switch_to_profile' => $aProfile['id']))),
- ),
- 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'])),
- );
- }
- if (!$aVars['bx_repeat:row'])
- return array(
- 'content' => MsgBox(_t('_sys_txt_empty')),
- 'menu' => 'sys_add_profile',
- );
- bx_import('BxDolTemplate');
- $oTemplate = BxDolTemplate::getInstance();
- $oTemplate->addCss('account.css');
- return array(
- 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
- 'menu' => 'sys_add_profile',
- );
- }
- protected function _getLatestModuleTimestamp ()
- {
- bx_import('BxDolModuleQuery');
- $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 ()
- {
- $aRet = array();
- bx_import('BxDolModuleQuery');
- $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
- foreach ($aModules as $aModule) {
- $oModule = BxDolModule::getInstance($aModule['name']);
- if ($oModule instanceof iBxDolProfileService)
- $aRet[] = $aModule;
- }
- return $aRet;
- }
- }
- /** @} */
|