BxBaseServiceProfiles.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 serviceProfileStats ($iProfileId = 0)
  19. {
  20. if (!$iProfileId)
  21. $iProfileId = bx_get_logged_profile_id();
  22. bx_import('BxDolMenu');
  23. $oMenu = BxDolMenu::getObjectInstance('sys_profile_stats');
  24. bx_import('BxDolProfile');
  25. $oProfile = BxDolProfile::getInstance($iProfileId);
  26. $aVars = array(
  27. 'profile_id' => $oProfile->id(),
  28. 'profile_url' => $oProfile->getUrl(),
  29. 'profile_edit_url' => $oProfile->getEditUrl(),
  30. 'profile_title' => $oProfile->getDisplayName(),
  31. 'profile_title_attr' => bx_html_attribute($oProfile->getDisplayName()),
  32. 'profile_ava_url' => $oProfile->getAvatar(),
  33. 'menu' => $oMenu->getCode(),
  34. );
  35. $oTemplate = BxDolTemplate::getInstance();
  36. return $oTemplate->parseHtmlByName('profile_stats.html', $aVars);
  37. }
  38. public function serviceProfileNotifications ($iProfileId = 0)
  39. {
  40. if (!$iProfileId)
  41. $iProfileId = bx_get_logged_profile_id();
  42. bx_import('BxDolMenu');
  43. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  44. $iNum = 0;
  45. $aMenuItems = $oMenu->getMenuItems ();
  46. foreach ($aMenuItems as $r) {
  47. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  48. $iNum += $r['bx_if:addon']['content']['addon'];
  49. }
  50. return $iNum;
  51. }
  52. public function serviceGetProfilesModules ()
  53. {
  54. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  55. $oCache = $this->getDbCacheObject ();
  56. $sKey = $this->genDbCacheKey('profiles_modules_array');
  57. $sKeyTs = $this->genDbCacheKey('profiles_modules_ts');
  58. $mixedRetTs = $oCache->getData($sKeyTs);
  59. $mixedRet = $oCache->getData($sKey);
  60. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  61. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  62. $aModulesArray = $mixedRet;
  63. } else {
  64. $aModulesArray = $this->_getProfilesModules ();
  65. $oCache->setData($sKey, $aModulesArray);
  66. $oCache->setData($sKeyTs, $iNewestModuleTs);
  67. }
  68. } else {
  69. $aModulesArray = $this->_getProfilesModules ();
  70. }
  71. return $aModulesArray;
  72. }
  73. public function serviceProfilesSearch ($sTerm, $iLimit = 20)
  74. {
  75. // get list of "profiles" modules
  76. $aModules = $this->serviceGetProfilesModules();
  77. // search in each module
  78. $a = array();
  79. foreach ($aModules as $aModule) {
  80. if ('system' == $aModule['name']) // don't search in account profiles
  81. continue;
  82. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, 10)));
  83. }
  84. // sort result
  85. usort($a, function($r1, $r2) {
  86. return strcmp($r1['label'], $r2['label']);
  87. });
  88. // return as array
  89. return array_slice($a, 0, $iLimit);
  90. }
  91. public function serviceProfilesList ($iAccountId = 0)
  92. {
  93. bx_import('BxDolProfileQuery');
  94. $oProfilesQuery = BxDolProfileQuery::getInstance();
  95. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  96. if (!$aProfiles)
  97. return false;
  98. $s = '';
  99. foreach ($aProfiles as $aProfile)
  100. if ($aProfile['type'] != 'system')
  101. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  102. if (!$s)
  103. $s = MsgBox(_t('_sys_txt_empty'));
  104. return $s . '<div class="bx-clear"></div>';
  105. }
  106. public function serviceAccountProfileSwitcher ($iAccountId = false)
  107. {
  108. bx_import('BxDolProfileQuery');
  109. $oProfilesQuery = BxDolProfileQuery::getInstance();
  110. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  111. if (!$aProfiles)
  112. return false;
  113. $iLoggedPofileId = bx_get_logged_profile_id();
  114. $aVars = array (
  115. 'bx_repeat:row' => array(),
  116. );
  117. foreach ($aProfiles as $aProfile) {
  118. if ($aProfile['type'] == 'system') // skip system account profile
  119. continue;
  120. $aVars['bx_repeat:row'][] = array (
  121. 'class' => $iLoggedPofileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  122. 'bx_if:active' => array (
  123. 'condition' => $iLoggedPofileId == $aProfile['id'],
  124. 'content' => array('id' => $aProfile['id']),
  125. ),
  126. 'bx_if:inactive' => array (
  127. 'condition' => $iLoggedPofileId != $aProfile['id'],
  128. 'content' => array('id' => $aProfile['id'], 'url_switch' => BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher', array('switch_to_profile' => $aProfile['id']))),
  129. ),
  130. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'])),
  131. );
  132. }
  133. if (!$aVars['bx_repeat:row'])
  134. return array(
  135. 'content' => MsgBox(_t('_sys_txt_empty')),
  136. 'menu' => 'sys_add_profile',
  137. );
  138. bx_import('BxDolTemplate');
  139. $oTemplate = BxDolTemplate::getInstance();
  140. $oTemplate->addCss('account.css');
  141. return array(
  142. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  143. 'menu' => 'sys_add_profile',
  144. );
  145. }
  146. protected function _getLatestModuleTimestamp ()
  147. {
  148. bx_import('BxDolModuleQuery');
  149. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  150. if (empty($aModules))
  151. return 0;
  152. $aModuleNewest = array_pop($aModules);
  153. return $aModuleNewest['date'];
  154. }
  155. protected function _getProfilesModules ()
  156. {
  157. $aRet = array();
  158. bx_import('BxDolModuleQuery');
  159. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  160. foreach ($aModules as $aModule) {
  161. $oModule = BxDolModule::getInstance($aModule['name']);
  162. if ($oModule instanceof iBxDolProfileService)
  163. $aRet[] = $aModule;
  164. }
  165. return $aRet;
  166. }
  167. }
  168. /** @} */