1
0

BxBaseServiceProfiles.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  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 serviceGetProfilesModules ()
  19. {
  20. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  21. $oCache = $this->getDbCacheObject ();
  22. $sKey = $this->genDbCacheKey('profiles_modules_array');
  23. $sKeyTs = $this->genDbCacheKey('profiles_modules_ts');
  24. $mixedRetTs = $oCache->getData($sKeyTs);
  25. $mixedRet = $oCache->getData($sKey);
  26. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  27. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  28. $aModulesArray = $mixedRet;
  29. } else {
  30. $aModulesArray = $this->_getProfilesModules ();
  31. $oCache->setData($sKey, $aModulesArray);
  32. $oCache->setData($sKeyTs, $iNewestModuleTs);
  33. }
  34. } else {
  35. $aModulesArray = $this->_getProfilesModules ();
  36. }
  37. return $aModulesArray;
  38. }
  39. public function serviceProfilesSearch ($sTerm, $iLimit = 20)
  40. {
  41. // get list of "profiles" modules
  42. $aModules = $this->serviceGetProfilesModules();
  43. // search in each module
  44. $a = array();
  45. foreach ($aModules as $aModule)
  46. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, 10)));
  47. // sort result
  48. usort($a, function($r1, $r2) {
  49. return strcmp($r1['label'], $r2['label']);
  50. });
  51. // return as array
  52. return array_slice($a, 0, $iLimit);
  53. }
  54. public function serviceProfilesList ($iAccountId = 0)
  55. {
  56. bx_import('BxDolProfileQuery');
  57. $oProfilesQuery = BxDolProfileQuery::getInstance();
  58. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  59. if (!$aProfiles)
  60. return false;
  61. $s = '';
  62. foreach ($aProfiles as $aProfile)
  63. if ($aProfile['type'] != 'system')
  64. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  65. if (!$s)
  66. $s = MsgBox(_t('_sys_txt_empty'));
  67. return $s . '<div class="bx-clear"></div>';
  68. }
  69. public function serviceAccountProfileSwitcher ($iAccountId = false)
  70. {
  71. bx_import('BxDolProfileQuery');
  72. $oProfilesQuery = BxDolProfileQuery::getInstance();
  73. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  74. if (!$aProfiles)
  75. return false;
  76. $iLoggedPofileId = bx_get_logged_profile_id();
  77. $aVars = array (
  78. 'bx_repeat:row' => array(),
  79. );
  80. foreach ($aProfiles as $aProfile) {
  81. //if ($aProfile['type'] == 'system')
  82. // continue;
  83. $aVars['bx_repeat:row'][] = array (
  84. 'bx_if:active' => array (
  85. 'condition' => $iLoggedPofileId == $aProfile['id'],
  86. 'content' => array('id' => $aProfile['id']),
  87. ),
  88. 'bx_if:inactive' => array (
  89. 'condition' => $iLoggedPofileId != $aProfile['id'],
  90. 'content' => array('id' => $aProfile['id'], 'url_switch' => BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher', array('switch_to_profile' => $aProfile['id']))),
  91. ),
  92. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'])),
  93. );
  94. }
  95. if (!$aVars['bx_repeat:row'])
  96. return MsgBox(_t('_sys_txt_empty'));
  97. bx_import('BxDolTemplate');
  98. $oTemplate = BxDolTemplate::getInstance();
  99. $oTemplate->addCss('account.css');
  100. return $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars);
  101. }
  102. protected function _getLatestModuleTimestamp ()
  103. {
  104. $aRet = array();
  105. bx_import('BxDolModuleQuery');
  106. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  107. if (empty($aModules))
  108. return 0;
  109. $aModuleNewest = array_pop($aModules);
  110. return $aModuleNewest['date'];
  111. }
  112. protected function _getProfilesModules ()
  113. {
  114. $aRet = array();
  115. bx_import('BxDolModuleQuery');
  116. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  117. foreach ($aModules as $aModule) {
  118. $oModule = BxDolModule::getInstance($aModule['name']);
  119. if ($oModule instanceof iBxDolProfileService)
  120. $aRet[] = $aModule;
  121. }
  122. return $aRet;
  123. }
  124. }
  125. /** @} */