BxBaseServiceProfiles.php 7.2 KB

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