BxBaseServiceProfiles.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 serviceProfilesFriends ($iLimit = 20)
  93. {
  94. if (!($iProfileId = bx_get_logged_profile_id()))
  95. return array();
  96. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  97. if (!$oConnection)
  98. return array();
  99. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  100. return array();
  101. $aRet = array();
  102. foreach ($a as $iId) {
  103. $oProfile = BxDolProfile::getInstance($iId);
  104. $aRet[] = array (
  105. 'label' => $oProfile->getDisplayName(),
  106. 'value' => $iId,
  107. 'url' => $oProfile->getUrl(),
  108. 'thumb' => $oProfile->getThumb(),
  109. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  110. );
  111. }
  112. return $aRet;
  113. }
  114. public function serviceProfilesSearch ($sTerm, $iLimit = 20)
  115. {
  116. // display friends by default
  117. if (!$sTerm)
  118. return $this->serviceProfilesFriends($iLimit);
  119. // get list of "profiles" modules
  120. $aModules = $this->serviceGetProfilesModules();
  121. // search in each module
  122. $a = array();
  123. foreach ($aModules as $aModule) {
  124. if (!BxDolService::call($aModule['name'], 'act_as_profile'))
  125. continue;
  126. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, 10)));
  127. }
  128. // sort result
  129. usort($a, function($r1, $r2) {
  130. return strcmp($r1['label'], $r2['label']);
  131. });
  132. // return as array
  133. return array_slice($a, 0, $iLimit);
  134. }
  135. public function serviceProfilesList ($iAccountId = 0)
  136. {
  137. $oProfilesQuery = BxDolProfileQuery::getInstance();
  138. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  139. if (!$aProfiles)
  140. return false;
  141. $s = '';
  142. foreach ($aProfiles as $aProfile)
  143. if ($aProfile['type'] != 'system')
  144. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  145. if (!$s)
  146. $s = MsgBox(_t('_sys_txt_empty'));
  147. return $s . '<div class="bx-clear"></div>';
  148. }
  149. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  150. {
  151. $oTemplate = BxDolTemplate::getInstance();
  152. BxDolInformer::getInstance($oTemplate)->setEnabled(false);
  153. $oProfilesQuery = BxDolProfileQuery::getInstance();
  154. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  155. if (!$aProfiles)
  156. return false;
  157. if (null === $iActiveProfileId)
  158. $iActiveProfileId = bx_get_logged_profile_id();
  159. $oModuleDb = BxDolModuleQuery::getInstance();
  160. $aVars = array (
  161. 'bx_repeat:row' => array(),
  162. );
  163. foreach ($aProfiles as $aProfile) {
  164. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  165. continue;
  166. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  167. continue;
  168. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  169. continue;
  170. $aVars['bx_repeat:row'][] = array (
  171. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  172. 'bx_if:active' => array (
  173. 'condition' => $iActiveProfileId == $aProfile['id'],
  174. 'content' => array('id' => $aProfile['id']),
  175. ),
  176. 'bx_if:inactive' => array (
  177. 'condition' => $iActiveProfileId != $aProfile['id'],
  178. 'content' => array(
  179. 'id' => $aProfile['id'],
  180. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  181. '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))
  182. ),
  183. ),
  184. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  185. );
  186. }
  187. $oTemplate->addCss('account.css');
  188. return array(
  189. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  190. );
  191. }
  192. protected function _getLatestModuleTimestamp ()
  193. {
  194. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  195. if (empty($aModules))
  196. return 0;
  197. $aModuleNewest = array_pop($aModules);
  198. return $aModuleNewest['date'];
  199. }
  200. protected function _getProfilesModules ()
  201. {
  202. $aRet = array();
  203. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  204. foreach ($aModules as $aModule) {
  205. $oModule = BxDolModule::getInstance($aModule['name']);
  206. if ($oModule instanceof iBxDolProfileService && $oModule->serviceActAsProfile())
  207. $aRet[] = $aModule;
  208. }
  209. return $aRet;
  210. }
  211. }
  212. /** @} */