BxBaseServiceProfiles.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. $oProfile = BxDolProfile::getInstance($iProfileId);
  38. if(!$oProfile)
  39. return '';
  40. $oAcl = BxDolAcl::getInstance();
  41. $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
  42. $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
  43. $aVars = array(
  44. 'profile_id' => $oProfile->id(),
  45. 'profile_url' => $oProfile->getUrl(),
  46. 'profile_edit_url' => $oProfile->getEditUrl(),
  47. 'profile_title' => $oProfile->getDisplayName(),
  48. 'profile_title_attr' => bx_html_attribute($oProfile->getDisplayName()),
  49. 'profile_ava_url' => $oProfile->getAvatar(),
  50. 'profile_acl_title' => _t($aAclInfo['name']),
  51. 'profile_acl_icon' => $aAclInfo['icon'],
  52. 'menu' => BxDolMenu::getObjectInstance('sys_profile_stats')->getCode(),
  53. );
  54. $oTemplate = BxDolTemplate::getInstance();
  55. return $oTemplate->parseHtmlByName('profile_stats.html', $aVars);
  56. }
  57. public function serviceGetMenuAddonProfileEdit($iProfileId = 0, $sCaption = '')
  58. {
  59. if(empty($sCaption))
  60. $sCaption = _t('_Edit');
  61. $oProfile = BxDolProfile::getInstance($iProfileId);
  62. if(!$oProfile)
  63. return '';
  64. $sModule = $oProfile->getModule();
  65. $sMethod = 'profile_edit_url';
  66. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  67. return '';
  68. return BxDolTemplate::getInstance()->parseLink(BxDolService::call($sModule, $sMethod, array($oProfile->getContentId())), $sCaption);
  69. }
  70. public function serviceProfileMembership ($iProfileId = 0)
  71. {
  72. if (!$iProfileId)
  73. $iProfileId = bx_get_logged_profile_id();
  74. return BxDolAcl::getInstance()->getProfileMembership($iProfileId);
  75. }
  76. public function serviceProfileNotifications ($iProfileId = 0)
  77. {
  78. if (!$iProfileId)
  79. $iProfileId = bx_get_logged_profile_id();
  80. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  81. if(!$oMenu)
  82. return 0;
  83. $iNum = 0;
  84. $aMenuItems = $oMenu->getMenuItems ();
  85. foreach ($aMenuItems as $r) {
  86. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  87. $iNum += $r['bx_if:addon']['content']['addon'];
  88. }
  89. return $iNum;
  90. }
  91. public function serviceGetCountOnlineProfiles ()
  92. {
  93. $oProfilesQuery = BxDolProfileQuery::getInstance();
  94. return $oProfilesQuery->getOnlineCount();
  95. }
  96. public function serviceGetProfilesModules ()
  97. {
  98. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  99. $oDb = BxDolDb::getInstance();
  100. $oCache = $oDb->getDbCacheObject ();
  101. $sKey = $oDb->genDbCacheKey('profiles_modules_array');
  102. $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
  103. $mixedRetTs = $oCache->getData($sKeyTs);
  104. $mixedRet = $oCache->getData($sKey);
  105. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  106. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  107. $aModulesArray = $mixedRet;
  108. } else {
  109. $aModulesArray = $this->_getProfilesModules ();
  110. $oCache->setData($sKey, $aModulesArray);
  111. $oCache->setData($sKeyTs, $iNewestModuleTs);
  112. }
  113. } else {
  114. $aModulesArray = $this->_getProfilesModules ();
  115. }
  116. return $aModulesArray;
  117. }
  118. public function serviceProfilesFriends ($iLimit = 20)
  119. {
  120. if (!($iProfileId = bx_get_logged_profile_id()))
  121. return array();
  122. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  123. if (!$oConnection)
  124. return array();
  125. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  126. return array();
  127. $aRet = array();
  128. foreach ($a as $iId) {
  129. $oProfile = BxDolProfile::getInstance($iId);
  130. $aRet[] = array (
  131. 'label' => $oProfile->getDisplayName(),
  132. 'value' => $iId,
  133. 'url' => $oProfile->getUrl(),
  134. 'thumb' => $oProfile->getThumb(),
  135. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  136. );
  137. }
  138. return $aRet;
  139. }
  140. public function serviceProfilesSearch ($sTerm, $iLimit = 20)
  141. {
  142. // display friends by default
  143. if (!$sTerm)
  144. return $this->serviceProfilesFriends($iLimit);
  145. // get list of "profiles" modules
  146. $aModules = $this->serviceGetProfilesModules();
  147. // search in each module
  148. $a = array();
  149. foreach ($aModules as $aModule) {
  150. if (!BxDolService::call($aModule['name'], 'act_as_profile'))
  151. continue;
  152. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, 10)));
  153. }
  154. // sort result
  155. usort($a, function($r1, $r2) {
  156. return strcmp($r1['label'], $r2['label']);
  157. });
  158. // return as array
  159. return array_slice($a, 0, $iLimit);
  160. }
  161. public function serviceProfilesList ($iAccountId = 0)
  162. {
  163. $oProfilesQuery = BxDolProfileQuery::getInstance();
  164. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  165. if (!$aProfiles)
  166. return false;
  167. $s = '';
  168. foreach ($aProfiles as $aProfile)
  169. if ($aProfile['type'] != 'system')
  170. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  171. if (!$s)
  172. $s = MsgBox(_t('_sys_txt_empty'));
  173. return $s . '<div class="bx-clear"></div>';
  174. }
  175. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  176. {
  177. $oTemplate = BxDolTemplate::getInstance();
  178. BxDolInformer::getInstance($oTemplate)->setEnabled(false);
  179. $oProfilesQuery = BxDolProfileQuery::getInstance();
  180. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  181. if (!$aProfiles)
  182. return false;
  183. if (null === $iActiveProfileId)
  184. $iActiveProfileId = bx_get_logged_profile_id();
  185. $oModuleDb = BxDolModuleQuery::getInstance();
  186. $aVars = array (
  187. 'bx_repeat:row' => array(),
  188. );
  189. foreach ($aProfiles as $aProfile) {
  190. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  191. continue;
  192. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  193. continue;
  194. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  195. continue;
  196. $aVars['bx_repeat:row'][] = array (
  197. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  198. 'bx_if:active' => array (
  199. 'condition' => $iActiveProfileId == $aProfile['id'],
  200. 'content' => array('id' => $aProfile['id']),
  201. ),
  202. 'bx_if:inactive' => array (
  203. 'condition' => $iActiveProfileId != $aProfile['id'],
  204. 'content' => array(
  205. 'id' => $aProfile['id'],
  206. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  207. '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))
  208. ),
  209. ),
  210. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  211. );
  212. }
  213. $oTemplate->addCss('account.css');
  214. return array(
  215. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  216. );
  217. }
  218. protected function _getLatestModuleTimestamp ()
  219. {
  220. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  221. if (empty($aModules))
  222. return 0;
  223. $aModuleNewest = array_pop($aModules);
  224. return $aModuleNewest['date'];
  225. }
  226. protected function _getProfilesModules ()
  227. {
  228. $aRet = array();
  229. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  230. foreach ($aModules as $aModule) {
  231. $oModule = BxDolModule::getInstance($aModule['name']);
  232. if ($oModule instanceof iBxDolProfileService && $oModule->serviceActAsProfile())
  233. $aRet[] = $aModule;
  234. }
  235. return $aRet;
  236. }
  237. }
  238. /** @} */