1
0

BxBaseServiceProfiles.php 6.8 KB

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