BxBaseServiceProfiles.php 7.7 KB

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